-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldController.cpp
More file actions
1593 lines (1296 loc) · 51.7 KB
/
WorldController.cpp
File metadata and controls
1593 lines (1296 loc) · 51.7 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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* WorldController.cpp
Robert Kirk DeLisle
23 August 2006
Purpose: Defines an interface between the WorldModel and the WorldView_Win32Resourced
Contains the primary game logic and is the central controller
Modificaton History:
*/
#include "WorldController.h"
#include "WorldView.h"
#include "Brain_Keyboard.h"
#include "Brain_RandomGhost.h"
WorldController::WorldController(const string ParamFile)
{
/* Purpose: Build the necessary pieces for the World, make appropriate connections, start
the process rolling.
Parameters: hWnd - Handle to the window that will become the target for drawing
hInstance - Handle to the application instance - required for some bitmap operations
Return: none
Exceptions:
*/
m_MapDrawn = false; //the map has yet to be drawn, so flag it
//get the parameters for this world
// m_Param.SetParamFile(ParamFile);
//these are already set elsewhere. I need to make sure things are properly ordered.
//connect the world view to the world model
m_View.Connect(&m_Model);
//execute various initialization routines
InitTimers();
InitMaze();
InitDots();
//tell the view how big it needs to be
m_View.SetViewDimensions(m_MazeController.GetPixelHeight(), m_MazeController.GetPixelWidth());
InitPacMan();
m_NumGhosts = m_Param.GetLongParam("NumberOfGhosts");
InitGhosts();
InitPowerUps();
m_UpdateCount=0;
return;
}
void WorldController::Draw(HDC TargetDC)
{
/* Purpose: Draw all the world to the TargetDC
Parameters: TargetDC - where the drawing will occur
Return: none
Exceptions:
*/
long state; //used to check state of powerups
//draw the map, if necessary
if ( !m_MapDrawn )
{
//clear out everything first
m_View.Clear();
//draw the maze and the dots
m_MazeController.Draw();
//m_DotsController.Draw();
m_MapDrawn = true;
}
//erase the sprites
m_PacManController.Erase();
for (int x=0; x<4; x++)
{
m_vecPowerUpControllers.at(x)->GetState(state);
if ( state == PowerUp_Active )
m_vecPowerUpControllers.at(x)->Erase();
}
for (int x=0; x<m_NumGhosts; x++)
{
m_vecGhostControllers.at(x)->Erase();
}
//draw the dots - necessary to redraw due to changes
m_DotsController.Draw();
//capture the section being drawn to for later erasure
for (int x=0; x<4; x++)
{
m_vecPowerUpControllers.at(x)->GetState(state);
if ( state == PowerUp_Active )
m_vecPowerUpControllers.at(x)->StoreEraseBuffer();
}
for (int x=0; x<m_NumGhosts; x++)
{
m_vecGhostControllers.at(x)->StoreEraseBuffer();
}
m_PacManController.StoreEraseBuffer();
//draw the sprites
for (int x=0; x<4; x++)
{
//check state of Powerup and erase if needed
m_vecPowerUpControllers.at(x)->GetState(state);
if ( state == PowerUp_Active )
m_vecPowerUpControllers.at(x)->Draw();
}
for (int x=0; x<m_NumGhosts; x++)
{
m_vecGhostControllers.at(x)->Draw();
}
m_PacManController.Draw();
//everybody is done, so draw the world
m_View.Draw(TargetDC);
return;
}
void WorldController::InitTimers()
{
/* Purpose: Initialize various timers related to game events
Parameters: none
Return: none
Exceptions:
*/
//set the timers for the world model
m_Model.SetFruitTimer(m_Param.GetLongParam("FruitTimer"));
m_Model.SetGhostTimer(m_Param.GetLongParam("GhostTimer"));
m_Model.SetPacManTimer(m_Param.GetLongParam("PacManTimer"));
m_Model.SetPowerUpTimer(m_Param.GetLongParam("PowerUpTimer"));
return;
}
void WorldController::InitMaze()
{
/* Purpose: Initialize details related to the maze.
Parameters: none
Return: none
Exceptions:
*/
//create the maze
m_MazeController.LoadMap(m_Param.GetStringParam("MazeMap"));
m_MazeController.SetBitmap(m_Param.GetStringParam("MazeBitmap"),
m_Param.GetLongParam("MazeBitmapTiles"));
m_MazeController.SetTargetDC(m_View.GetBackBuffer());
return;
}
void WorldController::InitDots()
{
/* Purpose: Initialize details related to the dots.
Parameters: none
Return: none
Exceptions:
*/
m_DotsController.LoadMap(m_Param.GetStringParam("DotMap"));
m_DotsController.SetBitmap(m_Param.GetStringParam("DotBitmap"),
m_Param.GetLongParam("DotBitmapTiles"));
m_DotsController.SetTargetDC(m_View.GetBackBuffer());
return;
}
void WorldController::InitPacMan()
{
/* Purpose: Initialize details related to the PacMan.
Parameters: none
Return: none
Exceptions:
*/
long index;
long state;
vector<long> AnimSeq;
m_PacManController.SetBitmap(m_Param.GetStringParam("PacManBitmap"),
m_Param.GetLongParam("PacManBitmapTiles"));
m_PacManController.SetBoundaryWrap(true);
m_PacManController.SetMaxMinBounds(0,m_MazeController.GetPixelWidth(),
0,m_MazeController.GetPixelHeight());
m_PacManController.SetPosition(m_Param.GetLongParam("PacManPositionX"),
m_Param.GetLongParam("PacManPositionY"));
m_PacManController.SetTargetDC(m_View.GetBackBuffer());
if (m_Param.GetStringParam("PacManBoundaryWrap") == "true")
m_PacManController.SetBoundaryWrap(true);
else
m_PacManController.SetBoundaryWrap(false);
state = PacMan_ActiveRight;
m_PacManController.SetState(state);
//I'm doing this the hard way right now - someday I'll get back to doing it properly
//set the animation sequences
for (index=0; index<15; index++)
{
if ( (index % 3 == 0) && (index != 0) )
{
//we've hit a break in the animation seq, so push it into the view (via the controller)
switch(index)
{
case 3:
AnimSeq.push_back(index-2); //get that intermediate frame
m_PacManController.SetAnimationSequence(PacMan_ActiveRight,AnimSeq);
AnimSeq.clear();
break;
case 6:
AnimSeq.push_back(index-2); //get that intermediate frame
m_PacManController.SetAnimationSequence(PacMan_ActiveUp,AnimSeq);
AnimSeq.clear();
break;
case 9:
AnimSeq.push_back(index-2); //get that intermediate frame
m_PacManController.SetAnimationSequence(PacMan_ActiveLeft,AnimSeq);
AnimSeq.clear();
break;
case 12:
AnimSeq.push_back(index-2); //get that intermediate frame
m_PacManController.SetAnimationSequence(PacMan_ActiveDown,AnimSeq);
AnimSeq.clear();
break;
case 15:
AnimSeq.push_back(index-2); //get that intermediate frame
m_PacManController.SetAnimationSequence(PacMan_Dead,AnimSeq);
AnimSeq.clear();
}
}
//store the current frame number
AnimSeq.push_back(index);
//create a new AI and tell the sprite
//in this version the AI is created elsewhere and pushed in
}
return;
}
void WorldController::InitGhosts()
{
/* Purpose: Initialize details related to the Ghosts.
Parameters: none
Return: none
Exceptions:
*/
long index;
long state;
long XSpeed = 2, YSpeed = 0;
vector<long> AnimSeq;
for (index=0; index<m_NumGhosts; index++)
{
m_vecGhostControllers.push_back(new SpriteController());
m_vecGhostControllers.at(index)->SetBitmap(m_Param.GetStringParam("GhostBitmap"),
m_Param.GetLongParam("GhostBitmapTiles"));
if (m_Param.GetStringParam("GhostBoundaryWrap") == "true")
m_vecGhostControllers.at(index)->SetBoundaryWrap(true);
else
m_vecGhostControllers.at(index)->SetBoundaryWrap(false);
m_vecGhostControllers.at(index)->SetMaxMinBounds(0, m_MazeController.GetPixelWidth(),
0, m_MazeController.GetPixelHeight());
m_vecGhostControllers.at(index)->SetPosition(m_Param.GetLongParam("GhostPositionX")+(index*16),
m_Param.GetLongParam("GhostPositionY"));
m_vecGhostControllers.at(index)->SetTargetDC(m_View.GetBackBuffer());
state = Ghost_CagedRight;
m_vecGhostControllers.at(index)->SetState(state);
m_vecGhostControllers.at(index)->SetSpeed(XSpeed, YSpeed);
XSpeed=2; YSpeed=0;
state = Ghost_CagedRight;
m_vecGhostControllers.at(index)->SetState(state); //done a second time to store the state for reversion
m_vecGhostControllers.at(index)->SetSpeed(XSpeed, YSpeed);
//create a new AI and tell the sprite
IBrain *pBrain = new Brain_RandomGhost(m_vecGhostControllers.at(index), this, 0.5+(0.1*((double)(index))));
m_vecGhostControllers.at(index)->SetSpriteBrain( pBrain );
//set the animation sequences
//yes, this is ugly - one day I'll do it properly...maybe...
switch(index)
{
case 0: //red
AnimSeq.clear();
AnimSeq.push_back(2);
AnimSeq.push_back(6);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(0);
AnimSeq.push_back(4);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntRight,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(1);
AnimSeq.push_back(5);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveUp,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(3);
AnimSeq.push_back(7);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntDown,AnimSeq);
break;
case 1: //yellow
AnimSeq.clear();
AnimSeq.push_back(10);
AnimSeq.push_back(14);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(8);
AnimSeq.push_back(12);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntRight,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(9);
AnimSeq.push_back(13);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveUp,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(11);
AnimSeq.push_back(15);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntDown,AnimSeq);
break;
case 2: //green
AnimSeq.clear();
AnimSeq.push_back(18);
AnimSeq.push_back(22);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(16);
AnimSeq.push_back(20);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntRight,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(17);
AnimSeq.push_back(21);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveUp,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(19);
AnimSeq.push_back(23);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntDown,AnimSeq);
break;
case 3: //purple
AnimSeq.clear();
AnimSeq.push_back(26);
AnimSeq.push_back(30);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveLeft,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(24);
AnimSeq.push_back(28);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_CagedRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveRight,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntRight,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(25);
AnimSeq.push_back(29);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_ActiveUp,AnimSeq);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(27);
AnimSeq.push_back(31);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_HuntDown,AnimSeq);
}
//blue
AnimSeq.clear();
AnimSeq.push_back(32);
AnimSeq.push_back(33);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_BlueUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(32);
AnimSeq.push_back(33);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_BlueDown,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(32);
AnimSeq.push_back(33);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_BlueLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(32);
AnimSeq.push_back(33);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_BlueRight,AnimSeq);
//dead
AnimSeq.clear();
AnimSeq.push_back(36);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_DeadLeft,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(34);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_DeadRight,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(35);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_DeadUp,AnimSeq);
AnimSeq.clear();
AnimSeq.push_back(37);
m_vecGhostControllers.at(index)->SetAnimationSequence(Ghost_DeadDown,AnimSeq);
}
m_Model.StartGhostTimer();
return;
}
void WorldController::InitPowerUps()
{
/* Purpose: Initialize details related to the PowerUps.
Parameters: none
Return: none
Exceptions:
*/
long state;
long index;
vector<long> AnimSeq;
for (index=0; index<4; index++)
{
m_vecPowerUpControllers.push_back(new SpriteController());
m_vecPowerUpControllers.at(index)->SetBitmap("Resources\\PowerPill.bmp",4);
m_vecPowerUpControllers.at(index)->SetBoundaryWrap(true);
m_vecPowerUpControllers.at(index)->SetMaxMinBounds(0, m_MazeController.GetPixelWidth(),
0, m_MazeController.GetPixelHeight());
state = PowerUp_Active;
m_vecPowerUpControllers.at(index)->SetState(state);
switch(index)
{
case 0:
m_vecPowerUpControllers.at(index)->SetPosition(16,48);
break;
case 1:
m_vecPowerUpControllers.at(index)->SetPosition(416,48);
break;
case 2:
m_vecPowerUpControllers.at(index)->SetPosition(16,352);
break;
case 3:
m_vecPowerUpControllers.at(index)->SetPosition(416,352);
break;
}
m_vecPowerUpControllers.at(index)->SetTargetDC(m_View.GetBackBuffer());
}
//PowerUps
for (index=0; index<4; index++)
AnimSeq.push_back(index);
for (index=0; index<4; index++)
m_vecPowerUpControllers.at(index)->SetAnimationSequence(PowerUp_Active,AnimSeq);
return;
}
bool WorldController::Update(bool loggit)
{
/* Purpose: Update all parts of the world for one time step
Parameters: none
Return: none
Exceptions:
*/
long PacState;
static long DeadSeq=0;
long XSpeed=0, YSpeed=0;
static long OldX, OldY, MaximumUpdates;
long CurrX, CurrY;
static bool firstRun = true;
static long stallCount=0;
static long MaximumStall;
static long OldScore;
static long ScoreStall;
static long ScoreStallCount=0;
ParameterMap params;
static long index = 0;
fstream logfile;
//logfile.open("update.log", ios::out|ios::app);
//logfile << "Update: " << index++ << endl;
//logfile.flush();
if ( firstRun )
{
m_PacManController.GetPosition(OldX, OldY);
MaximumUpdates = m_Param.GetLongParam("MaximumUpdates");
MaximumStall = m_Param.GetLongParam("MaximumStall");
OldScore = m_Model.GetScore();
ScoreStall = m_Param.GetLongParam("ScoreStall");
firstRun = false;
}
//logfile << "Checktimers" << endl;
//logfile.flush();
//check everyone's timers
CheckTimers();
//logfile << "MovePacMan" << endl;
//logfile.flush();
//check for fruit on/off
//move the PacMan
MovePacMan(loggit);
//move ghosts
//logfile << "MoveGhosts" << endl;
//logfile.flush();
MoveGhosts();
//logfile << "CheckDots" << endl;
//logfile.flush();
//check for dot collisions
CheckDotCollisions();
//logfile << "CheckPowerups" << endl;
//logfile.flush();
//check for power up collisions
CheckPowerUpCollisions();
//logfile << "Checkghosts" << endl;
//logfile.flush();
//check for ghost collisions
CheckGhostCollisions();
//logfile << "prevent stall" << endl;
//logfile.flush();
//keep PacMan from stalling out - no sense in wasting time if he's stuck
m_PacManController.GetPosition(CurrX, CurrY);
if ( CurrX == OldX && CurrY == OldY )
{
stallCount++;
}
else
{
OldX = CurrX;
OldY = CurrY;
stallCount = 0;
}
if ( stallCount > MaximumStall )
{
stallCount = 0;
return false;
}
//logfile << "Prevent stuck" << endl;
//logfile.flush();
//keep PacMan from getting stuck in non-scoring cycles
if ( m_Model.GetScore() - OldScore == 0 )
ScoreStallCount++;
else
{
OldScore = m_Model.GetScore();
ScoreStallCount=0;
}
if ( ScoreStallCount > ScoreStall )
{
ScoreStallCount = 0;
return false;
}
//logfile << "PacDead" << endl;
//logfile.flush();
//if PacMan dies, end early
m_PacManController.GetState(PacState);
if ( PacState == PacMan_Dead )
{
return false;
}
//logfile << "ClearMaze" << endl;
//logfile.flush();
//if PacMan clears the maze, end
if ( m_Model.GetScore() == params.GetLongParam("MaxScore") )
return false;
//keep a tally of the number of updates and only allow a specific amount
m_UpdateCount++;
if ( m_UpdateCount < MaximumUpdates )
{
return true;
}
//logfile << "Done" << endl << endl << endl;
//logfile.close();
return false;
}
void WorldController::UpdateGhostSpeed(long i)
{
/* Purpose: Make changes to the ghost speed based on state
Parameters: i - index of the ghost to adjust
Return: none
Exceptions:
*/
long state;
// long i;
long CurrentX, CurrentY;
long XSpeed, YSpeed;
bool speedUpdated = false;
long minX, minY, maxX, maxY;
long ret;
//step through the ghosts and update their speed
// for (i=0; i<4; i++)
// {
m_vecGhostControllers.at(i)->GetState(state);
m_vecGhostControllers.at(i)->GetPosition(CurrentX, CurrentY);
m_vecGhostControllers.at(i)->GetSpeed(XSpeed, YSpeed);
if ( state == Ghost_ActiveUp || state == Ghost_ActiveLeft || state == Ghost_ActiveRight )
{
//we're trying to get out of the cage
//get the X-value right first, then move out of the cage
if ( CurrentX < 216 )
{
state = Ghost_ActiveRight;
XSpeed = 2; YSpeed = 0;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated=true;
}
else if ( CurrentX > 216 )
{
state = Ghost_ActiveLeft;
XSpeed = -2; YSpeed = 0;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated=true;
}
else
{
if ( CurrentY > 176 )
{
//open the cage - make sure it is open to avoid errors later
m_MazeController.ModifyTile(13,13,0);
m_MazeController.ModifyTile(14,13,0);
m_MazeController.ModifyTile(15,13,0);
state = Ghost_ActiveUp;
XSpeed = 0; YSpeed = -2;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated=true;
}
else
{
//pick a starting direction at random
if ( (rand() % 2) == 0 )
{
XSpeed = -8;
state = Ghost_HuntLeft;
}
else
{
XSpeed = 8;
state = Ghost_HuntRight;
}
YSpeed = 0;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated=true;
//close the cage
CloseCage();
// m_MazeController.ModifyTile(13,13,16);
// m_MazeController.ModifyTile(14,13,17);
// m_MazeController.ModifyTile(15,13,18);
m_MapDrawn = false; //force a maze redraw
}
}
}
else if ( state == Ghost_HuntUp || state == Ghost_HuntDown ||
state == Ghost_HuntLeft || state == Ghost_HuntRight ||
state == Ghost_BlueUp || state == Ghost_BlueDown ||
state == Ghost_BlueLeft || state == Ghost_BlueRight ||
state == Ghost_DeadUp || state == Ghost_DeadDown ||
state == Ghost_DeadLeft || state == Ghost_DeadRight )
{
//we're on the hunt, so look to the AI
//check to see if we're at an intersection
if ( state == Ghost_HuntUp || state == Ghost_HuntDown ||
state == Ghost_BlueUp || state == Ghost_BlueDown ||
state == Ghost_DeadUp || state == Ghost_DeadDown )
{
if ( state == Ghost_DeadDown )
{
if ( CurrentX == 216 && ( CurrentY > 176 && CurrentY < 224 ))
{
//we're dead and trying to move into the cage
XSpeed = 0; YSpeed = 2;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated = true;
}
else if ( CurrentX == 216 && CurrentY == 224 )
{
//we've made it back into the cage - revert to Caged state
state = Ghost_CagedRight;
XSpeed = 2; YSpeed = 0;
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated = true;
if ( !m_Model.IsGhostTimeRunning() )
m_Model.StartGhostTimer();
//close the cage
CloseCage();
// m_MazeController.ModifyTile(13,13,16);
// m_MazeController.ModifyTile(14,13,17);
// m_MazeController.ModifyTile(15,13,18);
m_MapDrawn = false; //force a maze redraw
}
}
if ( !speedUpdated )
{
//we're moving up/down, so we need to find an opening for left/right
minX = CurrentX-8;
minY = CurrentY;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
minX = CurrentX+8;
minY = CurrentY;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
}
}
else
{
if ( state == Ghost_HuntRight || state == Ghost_HuntLeft ||
state == Ghost_BlueRight || state == Ghost_BlueLeft )
{
//prevent going back in the cage
if ( !( (CurrentX >= 208 && CurrentX <= 224) && CurrentY == 176) )
{
//we're moving left/right, we need an up/down opening
minX = CurrentX;
minY = CurrentY-8;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
minX = CurrentX;
minY = CurrentY+8;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
}
}
else
{
//we're dead
if ( (CurrentX == 216 && ( CurrentY >= 176 && CurrentY <= 192 )) )
{
//open the cage
m_MazeController.ModifyTile(13,13,0);
m_MazeController.ModifyTile(14,13,0);
m_MazeController.ModifyTile(15,13,0);
m_MapDrawn = false; //force a maze redraw
//and trying to get back in the cage
state = Ghost_DeadDown;
XSpeed = 0; YSpeed = 2;
// speedUpdated = true;
}
else if ( CurrentX == 216 && CurrentY == 224 )
{
// //we're in - revert to Active state
// state = Ghost_ActiveRight;
// XSpeed = 2; YSpeed = 0;
//// speedUpdated = true;
//
// //close the cage
CloseCage();
// m_MazeController.ModifyTile(13,13,16);
// m_MazeController.ModifyTile(14,13,17);
// m_MazeController.ModifyTile(15,13,18);
m_MapDrawn = false; //force a maze redraw
}
else
{
if ( !( (CurrentX >= 208 && CurrentX <= 224) && CurrentY == 176) )
{
//we're moving left/right, we need an up/down opening
minX = CurrentX;
minY = CurrentY-8;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
minX = CurrentX;
minY = CurrentY+8;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret) )
{
m_vecGhostControllers.at(i)->UpdateSpeed(false);
speedUpdated = true;
}
}
}
}
}
}
else if ( state == Ghost_CagedLeft || state == Ghost_CagedRight )
{
//we're still caged so just pace
minX = CurrentX+XSpeed;
minY = CurrentY+YSpeed;
maxX = minX + m_vecGhostControllers.at(i)->GetWidth()-1;
maxY = minY + m_vecGhostControllers.at(i)->GetHeight()-1;
if ( !m_MazeController.CheckMapByPixels( minX, maxX, minY, maxY,ret ) )
{
// if its not OK to keep going the same direction
speedUpdated = false;
XSpeed = -XSpeed;
YSpeed = 0;
if ( state == Ghost_CagedLeft )
{
state = Ghost_CagedRight;
m_vecGhostControllers.at(i)->SetState(state);
}
else
{
state = Ghost_CagedLeft;
m_vecGhostControllers.at(i)->SetState(state);
}
}
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
speedUpdated = true;
}
if ( !speedUpdated )
{
//change state and direction
m_vecGhostControllers.at(i)->SetState(state);
m_vecGhostControllers.at(i)->SetSpeed(XSpeed, YSpeed);
}
speedUpdated = false; //reset for other ghosts
// }
return;
}
void WorldController::CloseCage()
{
//close the cage, but do some checks first
//errors occured when two ghosts pass each other in and out of the cage
bool closecage = true;
long X, Y;
for ( int k=0; k<m_NumGhosts; k++)
{
m_vecGhostControllers.at(k)->GetPosition(X,Y);
if ( X >= 208 && X <= 224)
{
if ( Y > 176 && Y < 224 )
{
closecage=false;
}