-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost.lua
More file actions
745 lines (728 loc) · 19.6 KB
/
ghost.lua
File metadata and controls
745 lines (728 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
function generatepathgraph()
-- Create the first node to begin mapping from
pathgraph = {}
for y=1, LEVELHEIGHT do
pathgraph[y] = {}
end
local getfirst = function ()
for x=1, LEVELWIDTH do
for y=1, LEVELHEIGHT do
if isnode(x,y) then
return x, y
end
end
end
end
local firstx, firsty = getfirst()
pathgraph[firsty][firstx] = {}
-- This will recurse until the entire graph is mapped
createnode(firstx, firsty)
end
function printgraph()
for x=1, LEVELWIDTH do
for y=1, LEVELHEIGHT do
if pathgraph[y][x] ~= nil then
print(x .. "," .. y .. ":")
for i=1, 4 do
if pathgraph[y][x][i] ~= nil then
print("[" .. i .. "]: " .. pathgraph[y][x][i])
end
end
end
end
end
end
-- Nodes occur at intersections and dead ends
-- Intersections are where two lines meet,
-- Dead ends are where there is only one way: back
function isnode(x, y)
local horizontaljoint = false
local verticaljoint = false
local endcount = 0
if level[y][x] == 0 then
if x > 1 then
if level[y][x-1] == 0 then
horizontaljoint = true
endcount = endcount + 1
end
else
endcount = 2
end
if x < LEVELWIDTH then
if level[y][x+1] == 0 then
horizontaljoint = true
endcount = endcount + 1
end
else
endcount = 2
end
if y > 1 then
if level[y-1][x] == 0 then
verticaljoint = true
endcount = endcount + 1
end
else
endcount = 3
end
if y < LEVELHEIGHT then
if level[y+1][x] == 0 then
verticaljoint = true
endcount = endcount + 1
end
else
endcount = 4
end
if horizontaljoint and verticaljoint then
return true
end
end
return endcount == 1
end
-- End edges at corners, dead ends or intersections
-- Create a new node at these spots if needed
-- Factor in loops
function createedge(x, y, direction)
local edgelength = 0
local startx = x
local starty = y
if direction == 1 then
repeat
y = y - 1
edgelength = edgelength + 1
if y < 1 then
y = LEVELHEIGHT
end
until isnode(x, y)
pathgraph[starty][startx][1] = edgelength
if pathgraph[y][x] == nil then
pathgraph[y][x] = {}
pathgraph[y][x][3] = edgelength
createnode(x, y)
else
pathgraph[y][x][3] = edgelength
end
elseif direction == 2 then
repeat
x = x + 1
edgelength = edgelength + 1
if x > LEVELWIDTH then
x = 1
end
until isnode(x, y)
pathgraph[starty][startx][2] = edgelength
if pathgraph[y][x] == nil then
pathgraph[y][x] = {}
pathgraph[y][x][4] = edgelength
createnode(x, y)
else
pathgraph[y][x][4] = edgelength
end
elseif direction == 3 then
repeat
y = y + 1
edgelength = edgelength + 1
if y > LEVELHEIGHT then
y = 1
end
until isnode(x, y)
pathgraph[starty][startx][3] = edgelength
if pathgraph[y][x] == nil then
pathgraph[y][x] = {}
pathgraph[y][x][1] = edgelength
createnode(x, y)
else
pathgraph[y][x][1] = edgelength
end
elseif direction == 4 then
repeat
x = x - 1
edgelength = edgelength + 1
if x < 1 then
x = LEVELWIDTH
end
until isnode(x, y)
pathgraph[starty][startx][4] = edgelength
if pathgraph[y][x] == nil then
pathgraph[y][x] = {}
pathgraph[y][x][2] = edgelength
createnode(x, y)
else
pathgraph[y][x][2] = edgelength
end
end
end
function createnode(x, y)
if y > 1 and level[y - 1][x] == 0 and pathgraph[y][x][1] == nil then
createedge(x, y, 1)
end
if x < LEVELWIDTH and level[y][x + 1] == 0 and pathgraph[y][x][2] == nil then
createedge(x, y, 2)
end
if y < LEVELHEIGHT and level[y + 1][x] == 0 and pathgraph[y][x][3] == nil then
createedge(x, y, 3)
end
if x > 1 and level[y][x - 1] == 0 and pathgraph[y][x][4] == nil then
createedge(x, y, 4)
end
end
-- Allows the pathfinding algorithm to treat the
-- player or ghost's positions as nodes even when
-- they are on edges
function createtempnode(x, y)
local topy = y - 1
local topdist = 1
local verticalalligned = true
while true do
if topy < 1 then
topy = LEVELHEIGHT
end
if pathgraph[topy][x] ~= nil then
break
elseif level[topy][x] == 1 then
verticalalligned = false
break
end
topy = topy - 1
topdist = topdist + 1
end
local boty = y + 1
local botdist = 1
if verticalalligned then
while true do
if boty > LEVELHEIGHT then
boty = 1
end
if pathgraph[boty][x] ~= nil then
break
elseif level[boty][x] == 1 then
verticalalligned = false
break
end
boty = boty + 1
botdist = botdist + 1
end
end
if verticalalligned then
pathgraph[y][x] = {
[1] = topdist,
[3] = botdist
}
pathgraph[topy][x][3] = topdist
pathgraph[boty][x][1] = botdist
return
end
local rightx = x + 1
local rightdist = 1
while true do
if rightx > LEVELWIDTH then
rightx = 1
end
if pathgraph[y][rightx] ~= nil then
break
end
rightx = rightx + 1
rightdist = rightdist + 1
end
local leftx = x - 1
local leftdist = 1
while true do
if leftx < 1 then
leftx = LEVELWIDTH
end
if pathgraph[y][leftx] ~= nil then
break
end
leftx = leftx - 1
leftdist = leftdist + 1
end
pathgraph[y][x] = {
[2] = rightdist,
[4] = leftdist
}
pathgraph[y][rightx][4] = rightdist
pathgraph[y][leftx][2] = leftdist
end
function removetempnode(x, y)
if pathgraph[y][x][1] ~= nil then
-- Vertical node
local verticallength = pathgraph[y][x][1] + pathgraph[y][x][3]
local topy = y - pathgraph[y][x][1]
if topy < 1 then
topy = LEVELHEIGHT + topy
end
pathgraph[topy][x][3] = verticallength
local boty = y + pathgraph[y][x][3]
if boty > LEVELHEIGHT then
boty = boty - LEVELHEIGHT
end
pathgraph[boty][x][1] = verticallength
pathgraph[y][x] = nil
else
-- Horizontal node
local horizontallength = pathgraph[y][x][2] + pathgraph[y][x][4]
local rightx = x + pathgraph[y][x][2]
if rightx > LEVELWIDTH then
rightx = rightx - LEVELWIDTH
end
pathgraph[y][rightx][4] = horizontallength
local leftx = x - pathgraph[y][x][4]
if leftx < 1 then
leftx = LEVELWIDTH + leftx
end
pathgraph[y][leftx][2] = horizontallength
pathgraph[y][x] = nil
end
end
-- Calculating paths based on the graph
function calculateghostdestination(ghost, targetpoint)
-- Follow the path set for the ghost already
if calculatedpaths[ghost.name] ~= nil and calculatedpaths[ghost.name].targetpoint.x == targetpoint.x
and calculatedpaths[ghost.name].targetpoint.y == targetpoint.y then
table.remove(calculatedpaths[ghost.name], 1)
if #calculatedpaths[ghost.name] == 0 then
-- Ghost has reached the destination
calculatedpaths[ghost.name] = nil
end
ghost.destx = calculatedpaths[ghost.name][1].x
ghost.desty = calculatedpaths[ghost.name][1].y
else
-- The path is not set, calculate it
calculateghostpath(ghost, targetpoint)
ghost.destx = calculatedpaths[ghost.name][1].x
ghost.desty = calculatedpaths[ghost.name][1].y
end
end
function calculateghostpath(ghost, targetpoint)
-- If the ghost or target are not on nodes create temporary ones
-- Ghost must be on a node or between two nodes
-- Same for the target
local ghostx = (math.floor(ghost.x / TILEWIDTH) + 1)
local ghosty = (math.floor(ghost.y / TILEHEIGHT) + 1)
local tempghostnodecreated = pathgraph[ghosty][ghostx] == nil
local temptargetnodecreated = pathgraph[targetpoint.y][targetpoint.x] == nil
if tempghostnodecreated then
createtempnode(ghostx, ghosty)
end
if temptargetnodecreated then
createtempnode(targetpoint.x, targetpoint.y)
end
-- Initialize global state shared between pathfinding algorithm components
visitednodes = {}
for y=1, LEVELHEIGHT do
visitednodes[y] = {}
end
visitednodes[ghosty][ghostx] = 0
endpoint = targetpoint
pathstack = {
steps = 0,
nodes = {
{
x = ghostx,
y = ghosty
}
}
}
calculatebestgraphpath(ghostx, ghosty)
calculategridgraph(ghost.name, targetpoint)
if tempghostnodecreated then
removetempnode(ghostx, ghosty)
end
if temptargetnodecreated then
removetempnode(targetpoint.x, targetpoint.y)
end
end
-- Convert graphpath into step by step path
function calculategridgraph(name, targetpoint)
calculatedpaths[name] = {}
calculatedpaths[name].targetpoint = {
x = targetpoint.x,
y = targetpoint.y
}
for i=2, #bestpath do
if bestpath[i].dir == 1 then
-- Top edge
local topy = bestpath[i-1].y - 1
for j=1, pathgraph[bestpath[i-1].y][bestpath[i-1].x][1] do
if topy < 1 then
topy = LEVELHEIGHT
end
calculatedpaths[name][#calculatedpaths[name] + 1] = {
x = bestpath[i-1].x,
y = topy
}
topy = topy - 1
end
elseif bestpath[i].dir == 2 then
-- Right edge
local rightx = bestpath[i-1].x + 1
for j=1, pathgraph[bestpath[i-1].y][bestpath[i-1].x][2] do
if rightx > LEVELWIDTH then
rightx = 1
end
calculatedpaths[name][#calculatedpaths[name] + 1] = {
x = rightx,
y = bestpath[i-1].y
}
rightx = rightx + 1
end
elseif bestpath[i].dir == 3 then
-- Bottom edge
local boty = bestpath[i-1].y + 1
for j=1, pathgraph[bestpath[i-1].y][bestpath[i-1].x][3] do
if boty > LEVELHEIGHT then
boty = 1
end
calculatedpaths[name][#calculatedpaths[name] + 1] = {
x = bestpath[i-1].x,
y = boty
}
boty = boty + 1
end
elseif bestpath[i].dir == 4 then
-- Left edge
local leftx = bestpath[i-1].x - 1
for j=1, pathgraph[bestpath[i-1].y][bestpath[i-1].x][4] do
if leftx < 1 then
leftx = LEVELWIDTH
end
calculatedpaths[name][#calculatedpaths[name] + 1] = {
x = leftx,
y = bestpath[i-1].y
}
leftx = leftx - 1
end
end
end
end
-- Get the shortest list of graph nodes recursively.
function calculatebestgraphpath(x, y)
-- If you have been here before and it is not the best, go back.
if visitednodes[y][x] ~= nil then
if pathstack.steps > visitednodes[y][x] then
return
else
visitednodes[y][x] = pathstack.steps
end
end
-- If at the end then save the new best path
if x == endpoint.x and y == endpoint.y then
bestpath = {}
for i=1, #pathstack.nodes do
bestpath[i] = {
x = pathstack.nodes[i].x,
y = pathstack.nodes[i].y,
dir = pathstack.nodes[i].dir
}
end
return
end
-- Set the visitednodes in these directions
-- Continue trying all combinations
if pathgraph[y][x][1] ~= nil then
pathstack.steps = pathstack.steps + pathgraph[y][x][1]
local topy = y - pathgraph[y][x][1]
if topy < 1 then
topy = topy + LEVELHEIGHT
end
pathstack.nodes[#pathstack.nodes + 1] = {
x = x,
y = topy,
dir = 1
}
if visitednodes[topy][x] == nil or pathstack.steps < visitednodes[topy][x] then
visitednodes[topy][x] = pathstack.steps
end
calculatebestgraphpath(x, topy)
pathstack.nodes[#pathstack.nodes] = nil
pathstack.steps = pathstack.steps - pathgraph[y][x][1]
end
if pathgraph[y][x][2] ~= nil then
pathstack.steps = pathstack.steps + pathgraph[y][x][2]
local rightx = x + pathgraph[y][x][2]
if rightx > LEVELWIDTH then
rightx = rightx - LEVELWIDTH
end
pathstack.nodes[#pathstack.nodes + 1] = {
x = rightx,
y = y,
dir = 2
}
if visitednodes[y][rightx] == nil or pathstack.steps < visitednodes[y][rightx] then
visitednodes[y][rightx] = pathstack.steps
end
calculatebestgraphpath(rightx, y)
pathstack.nodes[#pathstack.nodes] = nil
pathstack.steps = pathstack.steps - pathgraph[y][x][2]
end
if pathgraph[y][x][3] ~= nil then
pathstack.steps = pathstack.steps + pathgraph[y][x][3]
local boty = y + pathgraph[y][x][3]
if boty > LEVELHEIGHT then
boty = boty - LEVELHEIGHT
end
pathstack.nodes[#pathstack.nodes + 1] = {
x = x,
y = boty,
dir = 3
}
if visitednodes[boty][x] == nil or pathstack.steps < visitednodes[boty][x] then
visitednodes[boty][x] = pathstack.steps
end
calculatebestgraphpath(x, boty)
pathstack.nodes[#pathstack.nodes] = nil
pathstack.steps = pathstack.steps - pathgraph[y][x][3]
end
if pathgraph[y][x][4] ~= nil then
pathstack.steps = pathstack.steps + pathgraph[y][x][4]
local leftx = x - pathgraph[y][x][4]
if leftx < 1 then
leftx = leftx + LEVELWIDTH
end
pathstack.nodes[#pathstack.nodes + 1] = {
x = leftx,
y = y,
dir = 4
}
if visitednodes[y][leftx] == nil or pathstack.steps < visitednodes[y][leftx] then
visitednodes[y][leftx] = pathstack.steps
end
calculatebestgraphpath(leftx, y)
pathstack.nodes[#pathstack.nodes] = nil
pathstack.steps = pathstack.steps - pathgraph[y][x][4]
end
end
function moveghosttopoint(ghost, targetpoint)
if ghost.x == (targetpoint.x - 1) * TILEWIDTH
and ghost.y == (targetpoint.y - 1) * TILEHEIGHT then
-- No need to move if the ghost is already there
ghost.resting = true
return
elseif ghost.resting then
-- Get the next destination if the ghost is at the previous one
calculateghostdestination(ghost, targetpoint)
-- Wrapping destinations
if ghost.y == 0 and ghost.desty == LEVELHEIGHT then
ghost.desty = 0
elseif ghost.x == (LEVELWIDTH * TILEWIDTH) - TILEWIDTH and ghost.destx == 1 then
ghost.destx = LEVELWIDTH + 1
elseif ghost.y == (LEVELHEIGHT * TILEHEIGHT) - TILEHEIGHT and ghost.desty == 1 then
ghost.desty = LEVELHEIGHT + 1
elseif ghost.x == 0 and ghost.destx == LEVELWIDTH then
ghost.destx = 0
end
ghost.resting = false
end
-- Animate and move the ghost
-- Right is the default
local ghostdirection = "right"
local prevspeed = ghost.speed
if ghost.state == "dead" then
ghost.speed = DEADSPEED
end
if (ghost.destx - 1) * TILEWIDTH > ghost.x then
ghostdirection = "right"
ghost.x = ghost.x + (globaldt * ghost.speed)
if ghost.x > (ghost.destx - 1) * TILEWIDTH then
ghost.x = (ghost.destx - 1) * TILEWIDTH
ghost.resting = true
ghost.crossing = false
ghost.sprite.paused = true
end
elseif (ghost.destx - 1) * TILEWIDTH < ghost.x then
ghostdirection = "left"
ghost.x = ghost.x - (globaldt * ghost.speed)
if ghost.x < (ghost.destx - 1) * TILEWIDTH then
ghost.x = (ghost.destx - 1) * TILEWIDTH
ghost.resting = true
ghost.crossing = false
ghost.sprite.paused = true
end
elseif (ghost.desty - 1) * TILEHEIGHT > ghost.y then
ghostdirection = "down"
ghost.y = ghost.y + (globaldt * ghost.speed)
if ghost.y > (ghost.desty - 1) * TILEHEIGHT then
ghost.y = (ghost.desty - 1) * TILEHEIGHT
ghost.resting = true
ghost.crossing = false
ghost.sprite.paused = true
end
elseif (ghost.desty - 1) * TILEHEIGHT < ghost.y then
ghostdirection = "up"
ghost.y = ghost.y - (globaldt * ghost.speed)
if ghost.y < (ghost.desty - 1) * TILEHEIGHT then
ghost.y = (ghost.desty - 1) * TILEHEIGHT
ghost.resting = true
ghost.crossing = false
ghost.sprite.paused = true
end
end
if not ghost.resting then
if player.state == "energized" then
if ghost.state == "dead" then
setanimation(ghost, "dead-move-" .. ghostdirection)
else
if energizertimer < (ENERGIZERDURATION / 1.5) then
setanimation(ghost, "ghost-scared")
else
setanimation(ghost, "ghost-flash")
end
end
else
setanimation(ghost, ghost.name .. "-move-" .. ghostdirection)
end
end
ghost.speed = prevspeed
-- Wrapping around the map
if not ghost.crossing then
if ghost.x < 0 then
ghost.destx = LEVELWIDTH
ghost.x = (TILEWIDTH * LEVELWIDTH) + TILEWIDTH
ghost.crossing = true
ghost.resting = false
elseif ghost.x > (TILEWIDTH * LEVELWIDTH) - TILEWIDTH then
ghost.destx = 1
ghost.x = -TILEWIDTH
ghost.crossing = true
ghost.resting = false
elseif ghost.y < 0 then
ghost.desty = LEVELHEIGHT
ghost.y = (TILEHEIGHT * LEVELHEIGHT) + TILEHEIGHT
ghost.crossing = true
ghost.resting = false
elseif ghost.y > (TILEHEIGHT * LEVELHEIGHT) - TILEHEIGHT then
ghost.desty = 1
ghost.y = -TILEHEIGHT
ghost.crossing = true
ghost.resting = false
end
end
end
function controlghosts()
if player.state == "energized" then
ghostenergizedai()
else
if ghostaicycle == "chase" then
ghostchaseai()
else
ghostscatterai()
end
end
end
function moveghostrandom(ghost)
if ghostrandomtargets[ghost.name] == nil
or (ghost.x == (ghostrandomtargets[ghost.name].x - 1) * TILEWIDTH
and ghost.y == (ghostrandomtargets[ghost.name].y - 1) * TILEHEIGHT) then
repeat
ghostrandomtargets[ghost.name] = {
x = math.random(LEVELWIDTH),
y = math.random(LEVELHEIGHT)
}
until level[ghostrandomtargets[ghost.name].y][ghostrandomtargets[ghost.name].x] == 0
and not isghostwall(ghostrandomtargets[ghost.name].x, ghostrandomtargets[ghost.name].y)
and (ghost.x ~= (ghostrandomtargets[ghost.name].x - 1) * TILEWIDTH
or ghost.y ~= (ghostrandomtargets[ghost.name].y - 1) * TILEHEIGHT)
end
moveghosttopoint(ghost, ghostrandomtargets[ghost.name])
end
function ghostenergizedai()
if blinky.state == "dead" then
moveghosttopoint(blinky, blinky.startpoint)
else
moveghostrandom(blinky)
end
if pinky.state == "dead" then
moveghosttopoint(pinky, pinky.startpoint)
else
moveghostrandom(pinky)
end
if inky.state == "dead" then
moveghosttopoint(inky, inky.startpoint)
else
moveghostrandom(inky)
end
if clyde.state == "dead" then
moveghosttopoint(clyde, clyde.startpoint)
else
moveghostrandom(clyde)
end
end
function ghostscatterai()
moveghosttopoint(blinky, blinky.corner)
moveghosttopoint(pinky, pinky.corner)
moveghosttopoint(inky, inky.corner)
moveghosttopoint(clyde, clyde.corner)
end
function ghostchaseai()
-- Blinky just moves toward the player
local playerx = player.destx
if playerx > LEVELWIDTH then
playerx = 1
elseif playerx < 1 then
playerx = LEVELWIDTH
end
local playery = player.desty
if playery > LEVELHEIGHT then
playery = 1
elseif playery < 1 then
playery = LEVELHEIGHT
end
moveghosttopoint(blinky, {x=playerx,y=playery})
-- Pinky tries to cut off the player
if #calculatedpaths["blinky"] >= 2 then
local blinkydirection = calculatedpaths["blinky"][#calculatedpaths["blinky"] - 1]
local pinkytarget = nil
if playery > 1 and not iswall(playerx, playery - 1)
and (playery - 1 ~= blinkydirection.y or playerx ~= blinkydirection.x) then
pinkytarget = {
x = playerx,
y = playery - 1
}
elseif playerx < LEVELWIDTH and not iswall(playerx + 1, playery)
and (playery ~= blinkydirection.y or playerx + 1 ~= blinkydirection.x) then
pinkytarget = {
x = playerx + 1,
y = playery
}
elseif playery < LEVELHEIGHT and not iswall(playerx, playery + 1)
and (playery + 1 ~= blinkydirection.y or playerx ~= blinkydirection.x) then
pinkytarget = {
x = playerx,
y = playery + 1
}
elseif playerx > 1 and not iswall(playerx - 1, playery)
and (playery ~= blinkydirection.y or playerx - 1 ~= blinkydirection.x) then
pinkytarget = {
x = playerx - 1,
y = playery
}
else
pinkytarget = {
x = playerx,
y = playery
}
end
moveghosttopoint(pinky, pinkytarget)
else
moveghosttopoint(pinky, {x=playerx,y=playery})
end
-- Inky moves at random
moveghostrandom(inky)
-- Clyde runs from the player (mostly)
if clydetarget == nil then
clydetarget = {
x = clyde.destx,
y = clyde.desty
}
end
-- Move clyde if the player is in his(?) corner of the map
while level[clydetarget.y][clydetarget.x] == 1
or (math.abs(clydetarget.x - playerx) < (LEVELWIDTH / 2) and
math.abs(clydetarget.y - playery) < (LEVELHEIGHT / 2))
or isghostwall(clydetarget.x, clydetarget.y) do
clydetarget = {
x = math.random(LEVELWIDTH),
y = math.random(LEVELHEIGHT)
}
end
moveghosttopoint(clyde, clydetarget)
end