comparison code/gameState.lm @ 62:c17380c8bac3

ghost movement compiles. fixed code that relied on if being an expression.
author William Morgan <billjunk@mrgn.org>
date Mon, 28 Jul 2014 01:47:07 -0700
parents 476cd9eba2be
children 8f6ade456edf
comparison
equal deleted inserted replaced
61:0eff4790249f 62:c17380c8bac3
27 grid:set:to 27 grid:set:to
28 gridMaxX 28 gridMaxX
29 gridMaxY 29 gridMaxY
30 gridArea 30 gridArea
31 grid:inBounds? 31 grid:inBounds?
32 grid:get:withDefault
32 calcPos 33 calcPos
33 ] from: (module: "grid.lm") 34 ] from: (module: "grid.lm")
34 35
35 myAnd <- :a b { 36 myAnd <- :a b {
36 // only ones and zeros 37 // only ones and zeros
37 (a + b) = 2 38 (a + b) = 2
38 } 39 }
39 40
40 makeTicker <- :mapGrid { 41 poorMod <- :input mod {
42 ret <- input
43 if: input >= mod {
44 ret <- input - mod
45 } else: {
46 ret input
47 }
48 ret
49 }
50
51 tupGet <- :cell idx size {
52 ret <- 0
53 if: idx = 0 {
54 ret <- cell value
55 } else: {
56 if: size = 1 {
57 ret <- cell tail
58 } else: { // warning, out-of-bounds idx will return a value!
59 ret <- tupGet: (cell tail) (idx - 1) (size - 1)
60 }
61 }
62 ret
63 }
64
65 tileWall <- 0
66 tileEmpty <- 1
67 tilePill <- 2
68 tilePower <- 3
69 tileFruit <- 4
70 tileLm <- 5
71 tileGhost <- 6
72
73 dirUp <- 0
74 dirRight <- 1
75 dirDown <- 2
76 dirLeft <- 3
77 dirFlipped <- :dir {(dir + 2) poorMod: 4}
78
79 makeTicker <- :mapGrid :ghosts{
41 lives <- 3 80 lives <- 3
42 lambdamanPos <- #[5 5] 81 lambdamanStartPos <- #[5 5] // TODO grab during parse or write {grid: mapGrid find: tileLm}
82 lambdamanStartDir <- dirDown
83 lambdamanPos <- lambdamanStartPos
84 lambdamanDir <- lambdamanStartDir
43 win <- 0 85 win <- 0
44 pillCount <- 50 86 pillCount <- 50 // TODO count pills during parse
45 87
46 isFood <- :tile { (2 <= tile) myAnd: (tile <= 4) } 88 isFood <- :tile { (2 <= tile) myAnd: (tile <= 4) }
47 getLambdamanChoice <- {0} 89 getLambdamanChoice <- {0} // TODO ai hookups
90
91 ghostPopulation <- ghosts value
92 ghostChoosers <- #[{0} {0} {0} {0}] // TODO ai hookups
93 ghostChoice <- :ghostIdx{tupGet: ghostChoosers ghostIdx 4}
94 ghostModeStandard <- 0
95 ghostModeFright <- 1
96 ghostModeinvisble <- 2
97 ghostVitality <- :ghost {ghost value}
98 ghostPos <- :ghost {(ghost tail) value}
99 ghostSetPos <- :ghost pos { #[(ghost ghostVitality) pos (ghost ghostDir)] }
100 ghostDir <- :ghost {(ghost tail) tail}
101 ghostSetDir <- :ghost dir { #[(ghost ghostVitality) (ghost ghostPos) dir] }
102 ghostGetLag <- :ghostType vitality{
103 ticksPerMove <- 130 + 2 * ghostType
104 frightTicksPerMove <- ticksPerMove + 65 + ghostType
105 ret <- 0
106 if: vitality = ghostModeFright {
107 ret <- ticksPerMove
108 } else: {
109 ret <- frightTicksPerMove
110 }
111 ret
112 }
113
48 114
49 makeEventType <- :lagTick isMovement behavior{ 115 makeEventType <- :lagTick isMovement behavior{
50 print: 12 116 print: 12
51 :curTick { 117 :curTick {
52 print: 11 118 print: 11
54 #[expireTick isMovement :events {behavior: expireTick events}] 120 #[expireTick isMovement :events {behavior: expireTick events}]
55 } 121 }
56 } 122 }
57 123
58 endOfLives <- makeEventType: (127 * (mapGrid gridArea) * 16) 0 :tick events { 124 endOfLives <- makeEventType: (127 * (mapGrid gridArea) * 16) 0 :tick events {
125 777 print
59 lives <- 0 126 lives <- 0
60 777 print
61 events 127 events
62 } 128 }
129
130 /*
131 TODO the rest of the easy action events
132 frightModeDeactivate <- (127 * 20) 0
133
134 // fruit must be a lambda that can be given a time
135 fruit <- :tick { }
136 fruit1Appears <- makeEventType: (127 * 200) 0 :events {
137 fruit <- :tick{80}
138 fruit1Expires | events
139 }
140 fruit2Appears <- (127 * 400) 0
141 fruit1Expires <- (127 * 280) 0
142 fruit2Expires <- (127 * 480) 0
143 */
63 144
64 moveLambdaman <- makeEventType: 127 1 :tick events { 145 moveLambdaman <- makeEventType: 127 1 :tick events {
65 print: 20 146 print: 20
66 move <- getLambdamanChoice: 147 move <- getLambdamanChoice:
67 newPos <- (calcPos: move lambdamanPos) 148 newPos <- (calcPos: move lambdamanPos)
68 impending <- grid: mapGrid get: newPos 149 newTile <- grid: mapGrid get: newPos withDefault: tileWall
150 if: (not: (newTile = tileWall)) {
151 lambdamanPos <- newPos
152 } else: { }
153
154 ret <- 0
155 if: (newTile isFood) {
156 ret <- (moveLambdaman: (tick + 10)) | events
157 } else: {
158 ret <-(moveLambdaman: tick) | events
159 }
160 ret
161 }
162
163 moveGhost <- :lagTick ghostIdx ghostType curTick {
69 print: 21 164 print: 21
70 if: (not: (impending = 0)) { 165 expireTick <- curTick + lagTick
71 lambdamanPos <- impending 166 #[expireTick 1 :events {
72 } else: { } 167 nextExpire <- 0
73 168 tree: ghosts update: ghostIdx with: :ghost {
74 print: 22 169 dir <- ghostDir: ghost
75 if: (impending isFood) { 170 legalChoice <- dir
76 (moveLambdaman: (tick + 10)) | events 171 choice <- ghostChoice: ghostIdx
77 } else: { 172 if: (not: (choice = (dir dirFlipped))) {
78 (moveLambdaman: tick) | events 173 legalChoice <- choice
79 } 174 } else: { }
80 } 175
81 176 attempts <- [legalChoice dirUp dirDown dirLeft dirRight]
82 /* 177 newDir <- 0
83 fruit <- 0 178 newPos <- ghost ghostPos
84 fruit1Appears <- makeEventType: (127 * 200) 0 :events { 179 newTile <- 0
85 fruit <- 80 180 while: {not: (attempts empty?:)} do:{
86 fruit1Expires | events 181 newDir <- attempts value
87 } 182 newPos <- (calcPos: newDir (ghost ghostPos))
88 fruit2Appears <- (127 * 400) 0 183 newTile <- grid: mapGrid get: newPos withDefault: tileWall
89 fruit1Expires <- (127 * 280) 0 184 if: (not: (newTile = tileWall)) {
90 fruit2Expires <- (127 * 480) 0 185 ghost ghostSetPos: newPos
91 moveGhost0 <- 1 //(ghostType, ghostId)!? 186 ghost ghostSetDir: newDir
92 moveGhost1 <- 1 //(ghostType, ghostId)!? 187 attempts <- []
93 moveGhost2 <- 1 //(ghostType, ghostId)!? 188 } else: {
94 moveGhost3 <- 1 //(ghostType, ghostId)!? 189 attempts <- attempts tail
95 frightModeDeactivate <- (127 * 20) 0 190 }
96 */ 191 }
192
193 nextExpire <- ghostGetLag: ghostType (ghost ghostVitality)
194 ghost
195 }
196 (moveGhost: nextExpire ghostIdx ghostType expireTick) | events
197 }]
198 }
97 199
98 executeEvents <- :tick isMove events { 200 executeEvents <- :tick isMove events {
99 print: #[23 tick isMove events] 201 print: #[23 tick isMove events]
100 event <- 0 202 event <- 0
101 eventTick <- 0 203 eventTick <- 0
119 print: 18 221 print: 18
120 unexpired 222 unexpired
121 } 223 }
122 224
123 executeTick <- :tick events { 225 executeTick <- :tick events {
124 // TODO: update fruit countdown for AI input
125
126 print: 14 226 print: 14
127 // 1.) movement 227 // 1.) movement
128 events <- executeEvents: tick 1 events 228 events <- executeEvents: tick 1 events
129 229
130 print: 15 230 print: 15
131 // 2.) actions 231 // 2.) actions
132 events <- executeEvents: tick 0 events 232 events <- executeEvents: tick 0 events
133 233
134 print: 16 234 print: 16
135 // 3.) collide pills powerpills fruit 235 // 3.) TODO collide pills powerpills fruit
136 236
237
238 // 4.) TODO ghost collision
239
240 ret <- 0
137 if: pillCount = 0 { 241 if: pillCount = 0 {
138 win <- 1 242 win <- 1
139 [] 243 ret <- []
140 } else: { 244 } else: {
141 if: lives = 0 { 245 if: lives = 0 {
142 [] 246 ret <- []
143 } else: { 247 } else: {
144 events 248 ret <- events
145 } 249 }
146 } 250 }
147 251 ret
148 } 252 }
149 253
150 nextTick <- :events { 254 nextTick <- :events {
151 print: 8 255 print: 8
152 fold: events 0x7FFFFFFF with: :curMin event { 256 fold: events 0x7FFFFFFF with: :curMin event {
153 print: #[9 curMin event] 257 print: #[9 curMin event]
154 eventTick <- event value 258 eventTick <- event value
155 print: 10 259 print: 10
156 if: eventTick < curMin { 260 if: eventTick < curMin {
157 curMin <- eventTick 261 curMin <- eventTick
158 } else: {} 262 } else: { }
159 curMin 263 curMin
160 } 264 }
161 } 265 }
162 266
163 print: 4 267 print: 4
165 tick <- 1 269 tick <- 1
166 events <- [ 270 events <- [
167 endOfLives: 0 271 endOfLives: 0
168 moveLambdaman: 0 272 moveLambdaman: 0
169 ] 273 ]
274 ghostIdx <- 0
275 ghostType <- 0
276 ghostTick <- 0
277 while: {ghostIdx < ghostPopulation} do: {
278 ghostTick <- ghostGetLag: ghostType ghostModeStandard
279 events <- (moveGhost: 0 ghostIdx ghostType ghostTick) | events
280 ghostIdx <- ghostIdx + 1
281 ghostType <- (ghostType + 1) poorMod: 4
282 }
170 print: 5 283 print: 5
171 while: {(tick < runUntil) myAnd: (not: (events empty?))} do: { 284 while: {(tick < runUntil) myAnd: (not: (events empty?))} do: {
172 print: 6 285 print: 6
173 events <- executeTick: tick events 286 events <- executeTick: tick events
174 print: 7 287 print: 7
181 print: 1 294 print: 1
182 grid <- makeTree: (map: (world value) :row { 295 grid <- makeTree: (map: (world value) :row {
183 makeTree: row 296 makeTree: row
184 }) 297 })
185 298
299 world
300
186 ticker <- makeTicker: grid 301 ticker <- makeTicker: grid
187 print: 2 302 print: 2
188 ticker: 1000 303 ticker: 1000
189 print: 3 304 print: 3
190 305
191 #[0 0] 306 #[0 0]
192 } 307 }
308
193 /* 309 /*
194 main <- :initWorld ghostCode{ 310 main <- :initWorld ghostCode{
195 #[0 step] 311 #[0 step]
196 } 312 }
197 */ 313 */