Algorithms and Networking for Computer Games phần 3 pps

29 290 0
Algorithms and Networking for Computer Games phần 3 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

34 RANDOM NUMBERS (a) (b) (c) (d) (e) (f) Figure 2.8 Randomly generated terrains where h max = 256. (a) Simple random terrain. (b) Limited random terrain where d max = 64. (c) Particle deposition terrain where m = 10 7 , i = 1andb = 4. (d) Fault line terrain where f = 1000 and c = 2. (e) Circle hill terrain where c = 400, r = 32 and s = 16. (f) Midpoint displacement terrain using diamond square where d max = 128 and s = 1. RANDOM NUMBERS 35 Algorithm 2.8 Generating limited random terrain. Limited-Random-Terrain() out: height map H (H is rectangular) local: average height of northern and western neighbours a; height h constant: maximum height h max ; maximum height difference d max 1: for x ← 0 (columns(H ) − 1) do 2: for y ← 0 (rows (H) − 1) do 3: if x = 0 and y = 0 then 4: a ←  H (x−1),y + H x,(y−1)  /2 5: else if x = 0 and y = 0 then 6: a ← H (x−1),y 7: else 8: a ← Random-Unit() · h max 9: end if 10: h ← a + d max · (Random-Unit() − 1/2) 11: H x,y ← max{0, min{h, h max }}  H x,y ∈ [0,h max ]. 12: end for 13: end for 14: return H noisy to resemble any landscape in the real world, as we can see in Figure 2.8(a). To smoothen the terrain, we can set a range within which the random value can vary (see Algorithm 2.8). Since the range depends on the heights already assigned (i.e. the neighbours to the west and the north), the generated terrain has diagonal ridges going to the south-east, as illustrated in Figure 2.8(b). Instead of generating random height values, we can randomize the process of formation. In particle deposition method, ‘grains’ are dropped randomly on the terrain and they are allowed to pile up (see Algorithm 2.9). The height difference between neighbouring points is limited. If the grain dropped causes the height difference to exceed this limit, the grain falls down to a neighbouring point until it reaches an equilibrium (see Figure 2.9). The grains are dropped following Brownian movement, where the next drop point is selected randomly from the neighbourhood of the current drop point. The resulting terrain is illustrated in Figure 2.8(c). Random numbers can also be used to select fault lines in the terrain. The height differ- ence between the sides of a fault line is increased as shown in Figure 2.10. Algorithm 2.10 gives an implementation where we first randomly select two points (x 0 ,y 0 ) and (x 1 ,y 1 ). To calculate the fault line going through these points, we form a vector ¯v with components ¯v x = x 1 − x 0 and ¯v y = y 1 − y 0 . Thereafter, for each point (x, y) in the terrain, we can form a vector ¯w for which ¯w x = x − x 0 and ¯w y = y − y 0 . When we calculate the cross product ¯u =¯v ׯw, depending on the sign of ¯u z we know whether the terrain at the point (x, y) has to be lowered or lifted: ¯u z =¯v x ¯w y −¯v y ¯w x . An example of the fault line terrain can be seen in Figure 2.8(d). 36 RANDOM NUMBERS Algorithm 2.9 Generating particle deposition terrain. Particle-Deposition-Terrain(m) in: number of movements m out: height map H (H is rectangular) 1: p ←Random-Integer(0, columns(H )), Random-Integer(0, rows(H )) 2: for i ← 1 m do 3: p  ,i  ←Increase(H,p)  Increase i  that fits to H p  . 4: H p  ← H p  + i  5: p ← Brownian-Movement(H,p) 6: end for 7: return H Brownian-Movement(H,p) in: height map H ; position p out: neighbouring position of p 1: case Random-Integer(0, 4) of 2: 0:return East-Neighbour(H,p) 3: 1:return West-Neighbour(H,p) 4: 2:return South-Neighbour(H, p) 5: 3:return North-Neighbour(H, p) 6: end case Increase(H,p) in: height map H ; position p out: pair position, increase constant: increase i; maximum height h max 1: i  ← min{h max − H p ,i}Proper amount for increase. 2: n ← Unbalanced-Neighbour(H, p,i  ) 3: if n = nil then return p, i   4: else return Increase(H,n) 5: end if Unbalanced-Neighbour(H,p,i  ) in: height map H ; position p; increase i  that fits to H p out: neighbour of p which exceeds b or otherwise nil constant: height difference threshold b 1: e ← East-Neighbour(H,p); w ← West-Neighbour(H,p) 2: s ← South-Neighbour(H,p); n ← North-Neighbour(H,p) 3: if H p + i  − H e >bthen return e 4: if H p + i  − H w >bthen return w 5: if H p + i  − H s >bthen return s 6: if H p + i  − H n >bthen return n 7: return nil RANDOM NUMBERS 37 (c)(a) (b) Figure 2.9 In particle deposition, each grain dropped falls down until it reaches an equilib- rium. If the threshold b = 1, the grey grain moves downwards until the difference compared to the height of neighbourhood is at most b. (a) (b) (c) Figure 2.10 Fault lines are selected randomly. The terrain is raised on one side of the fault line and lowered on the other. Instead of fault lines, we can use hills to simulate real-world terrain formation. Random numbers can be used to select the place for the hills. Algorithm 2.11 gives a simple method, where every hill is in a circle with the same diameter and the height increase is based on the cosine function. The resulting terrain is shown in Figure 2.8(e). Random midpoint displacement method, introduced by Fournier et al. (1982), starts by first setting the heights for the corner points of the terrain. After that, it subdivides the region inside iteratively using two steps (see Figure 2.11): (i) The diamond step: Taking a square of four corner points, generate a random value at the diamond point (i.e. the centre of the square), where the two diagonals meet. The value is calculated by averaging the four corner values and by adding a random displacement value. (ii) The square step: Taking each diamond of four corner points, generate a random value at the square point (i.e. the centre of the diamond). The value is calculated by averaging the corner values and by adding a random displacement value. Variations on these steps are presented by Miller (1986) and Lewis (1987). 38 RANDOM NUMBERS (b) (d) (a) (c) Figure 2.11 Midpoint displacement method consists of the diamond step, shown in subfig- ures (a) and (c), and the square step, shown in subfigures (b) and (d). The circles represent the calculated values. To make the implementation easier, we limit the size of the height map to n × n,where n = 2 k + 1 when the integer k ≥ 0. Algorithm 2.12 gives an implementation, where the subroutine Displacement(H, x, y, S, d) returns the height value for position (x, y) in height map H d + 1 4 · 3  i=0 H (x+S 2i ),(y+S 2i+1 ) , where S defines the point offsets from (x, y) in the square or diamond, and d is the current height displacement. In addition to the methods described here, there are approaches such as fractal noise (Perlin 1985) or stream erosion (Kelley et al. 1988) for terrain generation. Moreover, ex- isting height maps can be modified using image processing methods (e.g. sharpening and smoothing). 2.5 Summary If we try to generate random numbers using a deterministic method, we end up gener- ating pseudo-random numbers. The linear congruential method – which is basically just RANDOM NUMBERS 39 Algorithm 2.10 Generating fault line terrain. Fault-Line-Terrain() out: height map H (H is rectangular) constant: maximum height h max ; number of fault lines f ; fault change c 1: H ← Level-Terrain(h max /2)  Initialize the terrain to flat. 2: for i ← 1 f do 3: x 0 ← Random-Integer(0, columns(H )) 4: y 0 ← Random-Integer(0, rows(H )) 5: x 1 ← Random-Integer(0, columns(H )) 6: y 1 ← Random-Integer(0, rows(H )) 7: for x ← 0 (columns(H ) − 1) do 8: for y ← 0 (rows (H ) − 1) do 9: if (x 1 − x 0 ) · (y −y 0 ) − (y 1 − y 0 ) · (x −x 0 )>0 then 10: H x,y ← min{H x,y + c, h max } 11: else 12: H x,y ← max{H x,y − c, 0} 13: end if 14: end for 15: end for 16: end for 17: return H Algorithm 2.11 Generating circle hill terrain. Circle-Hill-Terrain() out: height map H (H is rectangular) constant: maximum height h max ; number of circles c; circle radius r; circle height increase s local: centre of the circle (x  ,y  ) 1: for i ← 1 c do 2: x  ← Random-Integer(0, columns(H )) 3: y  ← Random-Integer(0, rows(H )) 4: for x ← 0 (columns(H ) − 1) do 5: for y ← 0 (rows (H ) − 1) do 6: d ← (x  − x) 2 + (y  − y) 2 7: if d <r 2 then 8: a ← (s/2) ·(1 +cos(πd/r 2 )) 9: H x,y ← min{H x,y + a, h max } 10: end if 11: end for 12: end for 13: end for 14: return H 40 RANDOM NUMBERS Algorithm 2.12 Generating midpoint displacement terrain. Midpoint-Displacement-Terrain() out: height map H (columns(H ) = rows(H ) = n = 2 k + 1 when k ≥ 0) constant: maximum displacement d max ; smoothness s 1: initialize H 0,0 , H column(H )−1,0 , H 0,row (H )−1 and H column(H )−1,row (H )−1 2: m ← (n − 1); c ← 1; d ← d max 3: while m ≥ 2 do 4: w ← m/2; x ← w 5: for i ← 0 (c− 1) do  Centres. 6: y ← w 7: for j ← 0 (c− 1) do 8: H x,y ← Displacement(H, x, y, −w, −w, −w, +w, +w, −w, +w, +w,d) 9: y ← y + m 10: end for 11: x ← x + m 12: end for 13: x ← x − w; t ← w 14: for p ← 0 (c− 1) do  Borders. 15: H 0,t ← Displacement(H,0,t,0, −w, 0, +w, +w, 0, +w, 0,d) 16: H t,0 ← Displacement(H,t,0, −w, 0, +w, 0, 0, +w, 0, +w,d) 17: H t,x ← Displacement(H,t,x,−w, 0, +w, 0, 0, −w, 0, −w,d) 18: H x,t ← Displacement(H,x,t,0, −w, 0, +w, −w, 0, −w, 0,d) 19: t ← t + m 20: end for 21: x ← m 22: for i ← 0 (c− 2) do  Middle horizontal. 23: y ← w 24: for j ← 0 (c− 1) do 25: H x,y ← Displacement(H, x, y, −w, 0, +w, 0, 0, −w, 0, +w,d) 26: y ← y + m 27: end for 28: x ← x + m 29: end for 30: x ← w 31: for i ← 0 (c− 1) do  Middle vertical. 32: y ← m 33: for j ← 0 (c− 2) do 34: H x,y ← Displacement(H, x, y, −w, 0, +w, 0, 0, −w, 0, +w,d) 35: y ← y + m 36: end for 37: x ← x + m 38: end for 39: m ← m/2; c ← c ·2; d ← d · 2 −s 40: end while 41: return H RANDOM NUMBERS 41 a recursive multiplication equation – is one of the simplest, oldest, and the most studied of such methods. Pseudo-randomness differs in many respects from true randomness, and common sense does not always apply when we are generating pseudo-random numbers. For example, a pseudo-random sequence cannot usually be modified and operated as freely as a true random sequence. Therefore, the design of a pseudo-random number generator must be done with great care – and this implies that the user also has to understand the underlying limitations. We can introduce randomness into a deterministic algorithm to have a controlled vari- ation of its output. This enables us, for example, to create game worlds that resemble the real world but still include randomly varying attributes. Moreover, we can choose a deter- ministic algorithm randomly, which can be a good decision-making policy when we do not have any guiding information on what the next step should be. A random decision is the safest choice in the long run, since it reduces the likelihood of making bad decisions (as well as good ones). Exercises 2-1 A friend gives you the following random number generator: My-Random() out: random integer r constant: modulus m; starting value X 0 local: previously generated random number x (initially x = X 0 ) 1: if x mod 2 = 0 then 2: r ← (x + 3) ·5 3: else if x mod 3 = 0 then 4: r ← (x + 5)  314159265  Bitwise exclusive-or. 5: else if x mod 5 = 0 then 6: r ← x 2 7: else 8: r ← x + 7 9: end if 10: r ← r mod m; x ← r 11: return r How can you verify how well (or poorly) it works? 2-2 In the discussion on the design of random number generators (p. 24–25), paralleliza- tion and portability across platforms have been mentioned. Why are they considered as important issues? 2-3 The Las Vegas approach is not guaranteed to terminate. What is the probability that the repeat loop of Algorithm 2.2 continues after 100 rounds when m = 100 and w = 9? 2-4 An obvious variant to the linear congruential method is to choose its parameters randomly. Is the result of this new algorithm more random than the original? 42 RANDOM NUMBERS 2-5 Random number generators are as good as they perform on the tests. What would hap- pen if someone comes up with a test where the linear congruential method performs poorly? 2-6 Does the following algorithm produce a unit vector (i.e. with length one) starting from the origin towards a random direction? Verify your answer by writing a program that visualizes the angle distributions with respect to the x-axis. My-Vector() out: unit vector (x  ,y  ) towards a random direction 1: x ← 2 · Random-Unit() − 1 2: y ← 2 · Random-Unit() − 1 3:  ←  x 2 + y 2  Distance from (0, 0) to (x, y). 4: return (x/, y/)  Scale to the unit circle. 2-7 Let us define functions c and s from domain (0, 1) ×(0, 1) to codomain R: c(x,y) = √ −2lnx cos(2πy), s(x,y) = √ −2lnx sin(2πy). If we have two independent uniform random numbers U 0 ,U 1 ∈ (0, 1), then c(U 0 ,U 1 ) and s(U 0 ,U 1 ) are independent random numbers from the standard normal distribution N(0, 1) (i.e. with a mean of zero and a standard deviation of one) (Box and Muller 1958). In other words, if we aggregate combinations of independent uniform values, we have a normally distributed two-dimensional ‘cloud’ C of points around the origin: C =(c(U 2i ,U 2i+1 ), s(U 2i ,U 2i+1 )) i≥0 . However, if we use any linear congruential method for generating these uniform values (i.e. U 2i = X 2i /m and U 2i+1 = X 2i+1 /m), the independence requirement is not met. Curiously, in this case all the points in C fall on a single two-dimensional spiral and, therefore, cannot be considered normally distributed (Bratley et al. 1983). The effect can be analysed mathematically. To demonstrate how hard it is to recognize this defect experimentally, implement a program that draws the points of C using the linear congruential generator a = 7 5 , c = 0, and m = 2 31 − 1(Lewiset al. 1969). Observe the effect using the following example generators: • a = 799, c = 0, and m = 2 11 − 9 (L’Ecuyer 1999) • a = 137, c = 187, and m = 2 8 (Knuth 1998b, p. 94) • a = 78, c = 0, and m = 2 7 − 1 (Entacher 1999). What can be learned from this exercise? 2-8 Suppose we are satisfied with the linear congruential method with parameter values a = 799, c = 0, and m = 2039 = 2 11 − 9 (L’Ecuyer 1999). If we change the multi- plier a to value 393, what happens to the generated sequence? Can you explain why? What does this mean when we test randomness of these two generators? RANDOM NUMBERS 43 10 8 6 6 4 2 |x − y|+1123456 Figure 2.12 Probability distribution of a phantom die |x − y|+1 when x and y are integers from [1, 6]. 2-9 Explain why parallel pseudo-random number generators such as given in Equation (2.3) should not overlap? 2-10 Assume that we have a pseudo-random number sequence R =X i  i≥0 with a period of length p.Wedefinek parallel generators S j (j = 0, ,k− 1) of length  =p/k from R: S j =X i+j  −1 i=0 . Does S j also have pseudo-random properties? 2-11 Let us call a die phantom if it produces the same elementary events – possibly with different probabilities – as an ordinary die. For example, if an ordinary hexahedron die gives integer values from [1, 6], equation |x − y|+1 defines its phantom variant for integers x,y ∈ [1, 6]. The probability distribution of these phantom outcomes is depicted in Figure 2.12. In the game Phantom Cube Die, a player can freely stack 6 ·6 = 36 tokens to six piles labelled with integers [1, 6]. The player casts two ordinary dice to determine the outcome e of the phantom die and removes one token from the pile labelled with e. The game ends when the phantom die gives the label of an empty pile. The score is the number of phantom die throws. The challenge is to place the tokens so that the player can continue casting the die as long as possible. It is worth noting that although Figure 2.12 represents the probability distribution of the phantom die, it does not give the optimal token placement. Find out a better way to stack the tokens and explain this poltergeist phenomenon. 2-12 Interestingly, in the first edition of The Art of Computer Programming (1969) Knuth presents – albeit with some concern – Ulam’s method, which simulates how a human shuffles cards. In the subsequent editions it has been removed and Knuth dismisses such methods as being ‘miserably inadequate’ (Knuth 1998b, p. 145). Ulam’s method for shuffling works as follows: Ulam-Shuffle(S) in: ordered set S out: shuffled ordered set R [...]... tournaments in a season In the real-world sports games, a fair assessment of ranks for all players before the tournament can be too demanding a task To compensate for and to reduce the effect of seeding, we can introduce a random element into the pairing For example, if we are able to TOURNAMENTS 59 Algorithm 3. 8 Standard seeding for an elimination bracket Standard-Seeding(n) in: number of players n (2... determine a partial order between the participants and, therefore, we can apply them to a much wider range of problems The (possibly incomplete) ranking information can be used, for instance, in game balancing (e.g adjusting point rewarding schemes, or testing synthetic players by making them engage in Algorithms and Networking for Computer Games Jouni Smed and Harri Hakonen  2006 John Wiley & Sons, Ltd... Ladder-Match(R, ranked(R, r), c) 7: c ← ranked(R, r) 8: end for 9: return R Algorithm 3. 3 assumes that the players are unranked and the initial order is generated using Algorithm 3. 1 However, there are other ways to arrange the players into the match sequence For example, we can produce a uniformly distributed random permutation Shuffle( 0, 1, , n − 1 ) and use it for the initial ranks Alternatively, the initial... interleaved on top (1 234 5678 → 5162 738 4) and in an out-shuffle the top half is interleaved on top (1 234 5678 → 152 637 48) Take an ordinary deck of 52 cards and sort it into a recognizable order Do consecutive out-shuffles for the deck and observe how the order changes (alternatively, if you feel more agile, write a computer program that simulates the shuffling) What happens eventually? RANDOM NUMBERS 45 2-17... Shuffling is done by selecting and applying randomly one of these permutations and by repeating this r times What is the fundamental problem of this method? 2- 13 In Section 2.2, a discrete finite distribution is defined by listing the weight values Wr for each elementary event r Obviously, W is not a unique representation for the distribution, because, for example, W = {1, 2, 3} and W = {2, 4, 6} define the... player weights (|W | = n−1 n ∧ Wi ∈ N for i = 0, , n − 1 ∧ 1 ≤ k=0 Wk ) out: set R of ranked players which has a champion ranked(R, 0) and the rest of the players have rank 1 1: R ← copy P 2: k ← Random-From-Weights(W ) 3: c ← Rk 4: rank(c) ← 0 5: for all p ∈ (R \ {c}) do 6: rank(p) ← 1 7: end for 8: return R 56 TOURNAMENTS Algorithm 3. 7 Random pairing tournament Random-Pairing-Tournament(P ) in: set... Algorithm 3. 1 lets the players compete for the initial ranking, it is one of the simplest fair initialization methods If fairness is unnecessary, the body of the algorithm becomes even simpler For example, we can assign each player a random rank from structure S: 1: 2: 3: 4: 5: 6: 7: 8: 9: R ← Shuffle(P ) c←0 for r ← 0 (m − 1) do for i ← 0 (Sr − 1) do rank(Rc+i ) ← r end for c ← c + Sr end for return... ← n/2r+1 Reserve space for the winners 9: for i ← 0 (n/2r+1 − 1) do For each match 10: Mi ← match(M2i , M2i+1 ) 11: end for 12: for all p ∈ (M \ M ) do The runners-up 13: rank(p) ← 1 14: wins(p) ← r 15: end for 16: M ← M 17: end for 18: p ← the single value in M 19: rank(p) ← 0 The champion 20: wins(p) ← lg n 21: return R Scoring can be included in any type of tournament, and it provides an easy... false 10: R ← Pyramid-Match(R, M2i , M2i+1 ) 11: end for 12: M ← all peer winner players in rankeds(R, r) 13: M ← enumeration(rankeds(R, r − 1)) Arrange the set into an order 14: for i ← 0 ( /2 − 1) do Determine the rank exchanges 15: R ← Pyramid-Match(R, Mi , Mi ) 16: end for 17: end for 18: return R Algorithm 3. 6 Random selection tournament Random-Selection-Tournament(P , W ) in: sequence P of... variant to your friends and let them ponder what it does 3 Tournaments The seven brothers of Jukola – Juhani, Tuomas, Aapo, Simeoni, Timo, Lauri, and Eero – have decided to find out which one is the best at the game of Kyykk¨ To do this, the brothers a need a series of matches, a tournament, and have to set down the rules for the form of the tournament (see Figure 3. 1) They can form a scoring tournament, . end for 30 : x ← w 31 : for i ← 0 (c− 1) do  Middle vertical. 32 : y ← m 33 : for j ← 0 (c− 2) do 34 : H x,y ← Displacement(H, x, y, −w, 0, +w, 0, 0, −w, 0, +w,d) 35 : y ← y + m 36 : end for 37 :. information can be used, for instance, in game balancing (e.g. adjusting point rewarding schemes, or testing synthetic players by making them engage in Algorithms and Networking for Computer Games. uniform random numbers U 0 ,U 1 ∈ (0, 1), then c(U 0 ,U 1 ) and s(U 0 ,U 1 ) are independent random numbers from the standard normal distribution N(0, 1) (i.e. with a mean of zero and a standard

Ngày đăng: 14/08/2014, 11:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan