-
Notifications
You must be signed in to change notification settings - Fork 0
/
toplevel.cpp
1553 lines (1332 loc) · 48.2 KB
/
toplevel.cpp
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
/*
* FILE: toplevel.c
* AUTHOR: name (email)
* DATE: March 31 23:59:59 PST 2013
* DESCR:
*/
#include "main.h"
#include "mazewar.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
static int shoot_timer = 0;
static int cloak_timer = 0;
static bool updateView; /* true if update needed */
MazewarInstance::Ptr M;
/* Use this socket address to send packets to the multi-cast group. */
static Sockaddr groupAddr;
/*
event information
*/
static event_unprocessed events_all_player; //saved event to processed in the end of time slot
static std::map<uint32_t, respond_set> local_event_resend; //events sent from local this time slot
static uint32_t HeartbeatID = 1;
static uint32_t NextEventID = 1;
//static uint32_t CommittedEvent = 0;
#define MAX_OTHER_RATS (MAX_RATS - 1)
int main(int argc, char *argv[]) {
Loc x(1);
Loc y(5);
Direction dir(0);
char *ratName;
signal(SIGHUP, quit);
signal(SIGINT, quit);
signal(SIGTERM, quit);
getName((char *)"Welcome to CS244B MazeWar!\n\nYour Name", &ratName);
ratName[strlen(ratName) - 1] = 0;
M = MazewarInstance::mazewarInstanceNew(string(ratName));
MazewarInstance *a = M.ptr();
strncpy(M->myName_, ratName, NAMESIZE);
free(ratName);
if (argc == 5) {
M->xlocIs(atoi(argv[2]));
M->ylocIs(atoi(argv[3]));
// match input to direction representation
if (!strcmp(argv[4], "n"))
M->dirIs(NORTH);
else if (!strcmp(argv[4], "s"))
M->dirIs(SOUTH);
else if (!strcmp(argv[4], "e"))
M->dirIs(EAST);
else if (!strcmp(argv[4], "w"))
M->dirIs(WEST);
else
M->dirIs(NORTH);
}
MazeInit(argc, argv);
JoinGame();
ShowAllPositions();
play();
return 0;
}
/* ----------------------------------------------------------------------- */
void JoinGame(){
MWEvent event;
MW244BPacket incoming;
event.eventDetail = &incoming;
//send SI request
packetInfo packet;
int temp_val = M->myRatId().value();
packet.SIReq_.sourceId = temp_val;
sendPacketToPlayer(STATEREQUEST, packet);
int join_phase_finsh = 0;
//wait for 10 time slots i.e. 2 seconds, respond to hb, save SI response
while(join_phase_finsh<10){
NextEvent(&event, M->theSocket());
switch(event.eventType){
case EVENT_NETWORK:
processPacket(&event, JOINPHASE);
break;
case EVENT_TIMEOUT:
join_phase_finsh++;
break;
case EVENT_INT:
quit(0);
break;
}
}
//Update own infomation, like x, y, dir
Rat temp_rat = M->rat(0);
//temp_rat.Name = M->myName_;
temp_rat.dir = M->dir();
temp_rat.score = M->score();
temp_rat.x = M->xloc();
temp_rat.y = M->yloc();
temp_rat.cloaked = FALSE;
for(int i=0; i<MAX_MISSILES; ++i)
temp_rat.RatMissile[i].exist = FALSE;
M->ratIs(temp_rat, 0);
//send born event
eventSpecificData eventData;
eventData.bornData.position.x = temp_rat.y.value();
eventData.bornData.position.y = temp_rat.y.value();
eventData.bornData.direction = temp_rat.dir.value();
packet = eventPacketGenerator(EVENTBORN, eventData);
sendPacketToPlayer(EVENT, packet);
}
/* ----------------------------------------------------------------------- */
void play(void) {
MWEvent event;
MW244BPacket incoming;
event.eventDetail = &incoming;
while (TRUE) {
NextEvent(&event, M->theSocket());
if (!M->peeking())
switch (event.eventType) {
case EVENT_TIMEOUT: {
shoot_timer--;
cloak_timer--;
ratStates();
manageMissiles();
//After consistency resolved, state and view might be changed
updateView = TRUE;
DoViewUpdate();
} break;
case EVENT_A:
aboutFace();
break;
case EVENT_S:
leftTurn();
break;
case EVENT_D:
forward();
break;
case EVENT_F:
rightTurn();
break;
case EVENT_G:
backward();
break;
case EVENT_C:
cloak();
break;
case EVENT_BAR:
shoot();
break;
case EVENT_LEFT_D:
peekLeft();
break;
case EVENT_RIGHT_D:
peekRight();
break;
case EVENT_NETWORK:
processPacket(&event, PLAYPHASE);
break;
case EVENT_INT:
quit(0);
break;
}
else
switch (event.eventType) {
case EVENT_TIMEOUT: {
ratStates(); /* clean house */
manageMissiles();
updateView = TRUE;
DoViewUpdate();
} break;
case EVENT_RIGHT_U:
case EVENT_LEFT_U:
peekStop();
break;
case EVENT_NETWORK:
processPacket(&event, PLAYPHASE);
break;
}
DoViewUpdate();
/* Any info to send over network? */
}
}
/* ----------------------------------------------------------------------- */
static Direction _aboutFace[NDIRECTION] = {SOUTH, NORTH, WEST, EAST};
static Direction _leftTurn[NDIRECTION] = {WEST, EAST, NORTH, SOUTH};
static Direction _rightTurn[NDIRECTION] = {EAST, WEST, SOUTH, NORTH};
absoluteInfo absoluteInfoGenerator(){
Rat temp_rat = M->rat(0);
absoluteInfo info;
info.score = temp_rat.score.value();
info.position.x = temp_rat.x.value();
info.position.y = temp_rat.y.value();
info.direction = temp_rat.dir.value();
info.cloak = temp_rat.cloaked;
info.missileNumber = 0;
Missile* temp_missile = temp_rat.RatMissile;
for(int i=0; i<MAX_MISSILES; ++i){
if(temp_missile->exist){
info.missiles[i].exist = TRUE;
info.missiles[i].position.x = temp_missile->x.value();
info.missiles[i].position.y = temp_missile->y.value();
info.missiles[i].direction = temp_missile->dir.value();
info.missileNumber += 1 << i;
}else {
info.missiles[i].exist = FALSE;
}
temp_missile++;
}
return info;
}
packetInfo eventPacketGenerator(uint8_t eventtype, eventSpecificData eventData){
packetInfo info;
info.ev_.type = eventtype;
int temp_val = M->myRatId().value();
info.ev_.sourceId = temp_val; info.ev_.eventId = NextEventID;
NextEventID++;
info.ev_.absoInfo = absoluteInfoGenerator();
info.ev_.eventData = eventData;
return info;
}
void aboutFace(void) {
M->dirIs(_aboutFace[MY_DIR]);
updateView = TRUE;
eventSpecificData eventData;
eventData.moveData.direction = _aboutFace[MY_DIR].value();
eventData.moveData.speed = 0;
packetInfo info = eventPacketGenerator(EVENTMOVE, eventData);
sendPacketToPlayer(EVENT, info);
}
/* ----------------------------------------------------------------------- */
void leftTurn(void) {
M->dirIs(_leftTurn[MY_DIR]);
updateView = TRUE;
eventSpecificData eventData;
eventData.moveData.direction = _leftTurn[MY_DIR].value();
eventData.moveData.speed = 0;
packetInfo info = eventPacketGenerator(EVENTMOVE, eventData);
sendPacketToPlayer(EVENT, info);
}
/* ----------------------------------------------------------------------- */
void rightTurn(void) {
M->dirIs(_rightTurn[MY_DIR]);
updateView = TRUE;
eventSpecificData eventData;
eventData.moveData.direction = _rightTurn[MY_DIR].value();
eventData.moveData.speed = 0;
packetInfo info = eventPacketGenerator(EVENTMOVE, eventData);
sendPacketToPlayer(EVENT, info);
}
/* ----------------------------------------------------------------------- */
/* remember ... "North" is to the right ... positive X motion */
void forward(void) {
register int tx = MY_X_LOC;
register int ty = MY_Y_LOC;
switch (MY_DIR) {
case NORTH:
if (!M->maze_[tx + 1][ty] && !M->occupy_[tx + 1][ty])
tx++;
break;
case SOUTH:
if (!M->maze_[tx - 1][ty] && !M->occupy_[tx - 1][ty])
tx--;
break;
case EAST:
if (!M->maze_[tx][ty + 1] && !M->occupy_[tx][ty + 1])
ty++;
break;
case WEST:
if (!M->maze_[tx][ty - 1] && !M->occupy_[tx][ty - 1])
ty--;
break;
default:
MWError((char *)"bad direction in Forward");
}
if ((MY_X_LOC != tx) || (MY_Y_LOC != ty)) {
M->xlocIs(Loc(tx));
M->ylocIs(Loc(ty));
updateView = TRUE;
eventSpecificData eventData;
eventData.moveData.direction = MY_DIR;
eventData.moveData.speed = 1;
packetInfo info = eventPacketGenerator(EVENTMOVE, eventData);
sendPacketToPlayer(EVENT, info);
}
}
/* ----------------------------------------------------------------------- */
void backward() {
register int tx = MY_X_LOC;
register int ty = MY_Y_LOC;
switch (MY_DIR) {
case NORTH:
if (!M->maze_[tx - 1][ty] && !M->occupy_[tx - 1][ty])
tx--;
break;
case SOUTH:
if (!M->maze_[tx + 1][ty] && !M->occupy_[tx + 1][ty])
tx++;
break;
case EAST:
if (!M->maze_[tx][ty - 1] && !M->occupy_[tx][ty - 1])
ty--;
break;
case WEST:
if (!M->maze_[tx][ty + 1] && !M->occupy_[tx][ty + 1])
ty++;
break;
default:
MWError((char *)"bad direction in Backward");
}
if ((MY_X_LOC != tx) || (MY_Y_LOC != ty)) {
M->xlocIs(Loc(tx));
M->ylocIs(Loc(ty));
updateView = TRUE;
eventSpecificData eventData;
eventData.moveData.direction = MY_DIR;
eventData.moveData.speed = 2;
packetInfo info = eventPacketGenerator(EVENTMOVE, eventData);
sendPacketToPlayer(EVENT, info);
}
}
/* ----------------------------------------------------------------------- */
void peekLeft() {
M->xPeekIs(MY_X_LOC);
M->yPeekIs(MY_Y_LOC);
M->dirPeekIs(MY_DIR);
switch (MY_DIR) {
case NORTH:
if (!M->maze_[MY_X_LOC + 1][MY_Y_LOC]) {
M->xPeekIs(MY_X_LOC + 1);
M->dirPeekIs(WEST);
}
break;
case SOUTH:
if (!M->maze_[MY_X_LOC - 1][MY_Y_LOC]) {
M->xPeekIs(MY_X_LOC - 1);
M->dirPeekIs(EAST);
}
break;
case EAST:
if (!M->maze_[MY_X_LOC][MY_Y_LOC + 1]) {
M->yPeekIs(MY_Y_LOC + 1);
M->dirPeekIs(NORTH);
}
break;
case WEST:
if (!M->maze_[MY_X_LOC][MY_Y_LOC - 1]) {
M->yPeekIs(MY_Y_LOC - 1);
M->dirPeekIs(SOUTH);
}
break;
default:
MWError((char *)"bad direction in PeekLeft");
}
/* if any change, display the new view without moving! */
if ((M->xPeek() != MY_X_LOC) || (M->yPeek() != MY_Y_LOC)) {
M->peekingIs(TRUE);
updateView = TRUE;
}
}
/* ----------------------------------------------------------------------- */
void peekRight() {
M->xPeekIs(MY_X_LOC);
M->yPeekIs(MY_Y_LOC);
M->dirPeekIs(MY_DIR);
switch (MY_DIR) {
case NORTH:
if (!M->maze_[MY_X_LOC + 1][MY_Y_LOC]) {
M->xPeekIs(MY_X_LOC + 1);
M->dirPeekIs(EAST);
}
break;
case SOUTH:
if (!M->maze_[MY_X_LOC - 1][MY_Y_LOC]) {
M->xPeekIs(MY_X_LOC - 1);
M->dirPeekIs(WEST);
}
break;
case EAST:
if (!M->maze_[MY_X_LOC][MY_Y_LOC + 1]) {
M->yPeekIs(MY_Y_LOC + 1);
M->dirPeekIs(SOUTH);
}
break;
case WEST:
if (!M->maze_[MY_X_LOC][MY_Y_LOC - 1]) {
M->yPeekIs(MY_Y_LOC - 1);
M->dirPeekIs(NORTH);
}
break;
default:
MWError((char *)"bad direction in PeekRight");
}
/* if any change, display the new view without moving! */
if ((M->xPeek() != MY_X_LOC) || (M->yPeek() != MY_Y_LOC)) {
M->peekingIs(TRUE);
updateView = TRUE;
}
}
/* ----------------------------------------------------------------------- */
void peekStop() {
M->peekingIs(FALSE);
updateView = TRUE;
}
/* ----------------------------------------------------------------------- */
void shoot() {
//Add cooldown timer
if(shoot_timer > 0){
printf("Missile Projection still in Cooldown!\n");
return;
}
//set shoot timer = 3 seconds
shoot_timer = 15;
Rat temp_rat = M->rat(0);
int i;
for(i=0; i<MAX_MISSILES; ++i){
if(!temp_rat.RatMissile[i].exist) break;
}
if(i == MAX_MISSILES){
printf("Max missile limit %d", MAX_MISSILES);
return;
}
Missile temp_missile;
temp_missile.exist = TRUE;
temp_missile.x = temp_rat.x;
temp_missile.y = temp_rat.y;
temp_missile.dir = temp_rat.dir;
eventSpecificData eventData;
eventData.missileProjData.missileId = i;
eventData.missileProjData.position.x = temp_rat.x.value();
eventData.missileProjData.position.y = temp_rat.y.value();
eventData.missileProjData.direction = temp_rat.dir.value();
sendPacketToPlayer(EVENT, eventPacketGenerator(EVENTSHOOT, eventData));
temp_rat.RatMissile[i] = temp_missile;
M->ratIs(temp_rat, 0);
}
/* ----------------------------------------------------------------------- */
void cloak() {
if(cloak_timer > 0){
printf("Cloak still in Cooldown!\n");
return;
}
cloak_timer = 15;
//implement timer here
eventSpecificData eventData;
eventData.cloak = TRUE;
packetInfo info = eventPacketGenerator(EVENTSHOOT, eventData);
sendPacketToPlayer(EVENT, info);
}
/* ----------------------------------------------------------------------- */
/*
* Exit from game, clean up window
*/
void quit(int sig) {
StopWindow();
exit(0);
}
/* ----------------------------------------------------------------------- */
void NewPosition(MazewarInstance::Ptr m) {
Loc newX(MY_X_LOC);
Loc newY(MY_Y_LOC);
Direction dir(MY_DIR); /* start on occupied square */
while (!M->maze_[newX.value()][newY.value()] && M->occupy_[newX.value()][newY.value()]) {
/* MAZE[XY]MAX is a power of 2 */
newX = Loc(random() & (MAZEXMAX - 1));
newY = Loc(random() & (MAZEYMAX - 1));
/* In real game, also check that square is
unoccupied by another rat */
}
/* prevent a blank wall at first glimpse */
if (!m->maze_[(newX.value()) + 1][(newY.value())])
dir = Direction(NORTH);
if (!m->maze_[(newX.value()) - 1][(newY.value())])
dir = Direction(SOUTH);
if (!m->maze_[(newX.value())][(newY.value()) + 1])
dir = Direction(EAST);
if (!m->maze_[(newX.value())][(newY.value()) - 1])
dir = Direction(WEST);
m->xlocIs(newX);
m->ylocIs(newY);
m->dirIs(dir);
}
/* ----------------------------------------------------------------------- */
void MWError(char s[])
{
StopWindow();
fprintf(stderr, "CS244BMazeWar: %s\n", s);
perror("CS244BMazeWar");
exit(-1);
}
/* ----------------------------------------------------------------------- */
/* This is just for the sample version, rewrite your own */
Score GetRatScore(RatIndexType ratIndex) {
if (ratIndex.value() == M->myIndex().value()) {
return (M->score());
} else {
return M->rat(ratIndex).score;
}
}
/* ----------------------------------------------------------------------- */
/* This is just for the sample version, rewrite your own */
char *GetRatName(RatIndexType ratIndex) {
if (ratIndex.value() == M->myIndex().value()) {
return (M->myName_);
} else {
char buf[1];
sprintf(buf, "%d", ratIndex.value());
return ((char *)"Player %s", buf);
}
return ((char *)"Dummy");
}
/* ----------------------------------------------------------------------- */
/* This is just for the sample version, rewrite your own if necessary */
void ConvertIncoming(MW244BPacket *p) {}
/* ----------------------------------------------------------------------- */
/* This is just for the sample version, rewrite your own if necessary */
void ConvertOutgoing(MW244BPacket *p) {}
/* ----------------------------------------------------------------------- */
Rat setAbsoInfo(Rat temp_rat, absoluteInfo absoInfo){
temp_rat.cloaked = absoInfo.cloak;
temp_rat.dir = absoInfo.direction;
temp_rat.x = absoInfo.position.x;
temp_rat.y = absoInfo.position.y;
temp_rat.score = absoInfo.score;
for(int i=0; i<MAX_MISSILES; ++i){
if(absoInfo.missiles[i].exist){
temp_rat.RatMissile[i].exist = TRUE;
temp_rat.RatMissile[i].x = absoInfo.missiles[i].position.x;
temp_rat.RatMissile[i].y = absoInfo.missiles[i].position.y;
temp_rat.RatMissile[i].dir = absoInfo.missiles[i].direction;
}
}
return temp_rat;
}
Rat processEvent(Rat temp_rat, eventSpecificData eventData, uint8_t type){
switch(type){
case EVENTCLOAK: {
temp_rat.cloaked = eventData.cloak;
}break;
case EVENTMOVE: {
if(eventData.moveData.speed == 0){//change direction
temp_rat.dir = eventData.moveData.direction;
}else {//move
//offset = 1 if forward, otherwise -1
int offset = (eventData.moveData.speed == 1)? 1: -1;
int x = temp_rat.x.value();
int y = temp_rat.y.value();
switch(temp_rat.dir.value()){
case NORTH:
if (!M->maze_[x + offset][y])
temp_rat.x = x + offset;
break;
case SOUTH:
if (!M->maze_[x - offset][y])
temp_rat.x = x - offset;
break;
case EAST:
if (!M->maze_[x][y + offset])
temp_rat.y = y + offset;
break;
case WEST:
if (!M->maze_[x][y - offset])
temp_rat.y = y - offset;
break;
}
}
}break;
case EVENTBORN: {
temp_rat.dir = eventData.bornData.direction;
temp_rat.x = eventData.bornData.position.x;
temp_rat.y = eventData.bornData.position.y;
}break;
case EVENTSHOOT: {
int missileId = eventData.missileProjData.missileId;
if(missileId < 0 || missileId >= MAX_MISSILES){
MWError((char*)"Wrong missileId\n");
break;
}
temp_rat.RatMissile[missileId].exist = TRUE;
temp_rat.RatMissile[missileId].x = eventData.missileProjData.position.x;
temp_rat.RatMissile[missileId].y = eventData.missileProjData.position.y;
temp_rat.RatMissile[missileId].dir = eventData.missileProjData.direction;
}break;
case EVENTHIT: {
//Here only change the score, disappear the missile
temp_rat.score = temp_rat.score.value() - (5 + (temp_rat.cloaked)? 2:0);
//If the owner exist, add score, disapper the missile
if(M->AllRats.find(eventData.missileHitData.ownerId) != M->AllRats.end()){
int ratindex = (M->AllRats.find(eventData.missileHitData.ownerId))->second;
Rat owner = M->rat(ratindex);
owner.score = owner.score.value() + 11 + (temp_rat.cloaked)? 2:0;
owner.RatMissile[eventData.missileHitData.missileId].exist = FALSE;
}
}break;
}
return temp_rat;
}
void clearOccupy(void) {
int i, j;
for (i = 0; i < MAZEXMAX; i++) {
for (j = 0; j < MAZEYMAX; j++) {
M->occupy_[i][j] = FALSE;
}
}
}
/* This is just for the sample version, rewrite your own */
//Here process all events to update status in mazeRats_[], triggered by a event_timeout
void ratStates() {
int ratindex;
packetInfo info;
Rat temp_rat;
//Process all events saved in events_all_player by ID
event_unprocessed_iter iter = events_all_player.begin();
while(iter != events_all_player.end()){
//Process this player's state
if(M->AllRats.find(iter->first) != M->AllRats.end()){
int tempxxx = (M->AllRats.find(iter->first))->first;
ratindex = (M->AllRats.find(iter->first))->second;
player_unprocessed event_per_player = iter->second;
//Already been sorted by EventID
player_unprocessed_iter iter_event;
for(iter_event=event_per_player.begin(); iter_event!=event_per_player.end(); iter_event++){
info = iter_event->second;
absoluteInfo absoInfo = info.ev_.absoInfo;
eventSpecificData eventData = info.ev_.eventData;
if(iter_event == event_per_player.begin()) temp_rat = setAbsoInfo(temp_rat, absoInfo);
temp_rat = processEvent(temp_rat, eventData, info.ev_.type);
}
M->ratIs(temp_rat, ratindex);
}
iter++;
}
if(0){
//Process hbACK_set, if value > 50(no response for 10 secs), think it's dropped; else, add 1
int index = 0;
for(respond_set_iter iter = hbACK_set.begin(); iter!=hbACK_set.end(); iter++){
if(iter->second >= 50){
M->mazeplay[index] = FALSE;
temp_rat = M->rat(index);
temp_rat.playing = FALSE;
M->ratIs(temp_rat, index);
}
else iter->second++;
index++;
}
}
//Send heartbeat
info.hb_.heartbeatId = HeartbeatID;
HeartbeatID++;
int temp_val = M->myRatId().value();
info.hb_.sourceId = temp_val;
sendPacketToPlayer(HEARTBEAT, info);
//Clear uncommitted events
for(iter = events_all_player.begin(); iter != events_all_player.end(); iter++){
player_unprocessed empty_events = player_unprocessed();
iter->second = empty_events;
}
//Update own state in M
temp_rat = M->rat(0);
int temp;
//temp = M->dir.value();
M->dirIs(temp_rat.dir);
temp = M->xloc().value();
M->xlocIs(temp_rat.x);
temp = M->yloc().value();
M->ylocIs(temp_rat.y);
temp = M->score().value();
M->scoreIs(temp_rat.score);
//update M->occupy_
clearOccupy();
for(int i=0; i<MAX_RATS; ++i){
if(M->rat(i).playing){
int x = M->rat(i).y.value();
int y = M->rat(i).x.value();
M->occupy_[x][y] = TRUE;
}
}
}
/* ----------------------------------------------------------------------- */
/* This is just for the sample version, rewrite your own */
void manageMissiles() {
/*Here updates all missiles in all rats, if the missile hit yourself, send a missilehit packet and born event, if the missile hit a wall,
change it's exist to FALSE
*/
Rat temp_rat;
//move all missiles forward, if it hits wall, disappear it; if it hits self, send hit event
for(int i=0; i<MAX_RATS; ++i){
//Check all rats
temp_rat = M->rat(i);
if(temp_rat.playing){
for(int j=0; j<MAX_MISSILES; ++j){
if(temp_rat.RatMissile[j].exist){
//Move it forward
int tx = temp_rat.RatMissile[j].x.value();
int ty = temp_rat.RatMissile[j].y.value();
switch (temp_rat.RatMissile[j].dir.value()) {
case NORTH:
if (!M->maze_[tx + 1][ty])
tx++;
break;
case SOUTH:
if (!M->maze_[tx - 1][ty])
tx--;
break;
case EAST:
if (!M->maze_[tx][ty + 1])
ty++;
break;
case WEST:
if (!M->maze_[tx][ty - 1])
ty--;
break;
}
if(tx != temp_rat.RatMissile[j].x.value() || ty != temp_rat.RatMissile[j].y.value()){
//if this missile belongs to self, show it, set update view
if(i == 0)
showMissile(Loc(tx), Loc(ty), temp_rat.RatMissile[j].dir, temp_rat.RatMissile[j].x, temp_rat.RatMissile[j].y, TRUE);
temp_rat.RatMissile[j].x = tx;
temp_rat.RatMissile[j].y = ty;
//if this missile belongs to others, check if self tagged
if(tx == M->xloc().value() && ty == M->yloc().value()){
eventSpecificData eventData;
for(std::map<uint32_t, int>::iterator iter = M->AllRats.begin(); iter != M->AllRats.end(); iter++){
if(iter->second == i) eventData.missileHitData.ownerId = iter->first;
}
eventData.missileHitData.missileId = j;
eventData.missileHitData.position.x = tx;
eventData.missileHitData.position.y = ty;
eventData.missileHitData.direction = temp_rat.RatMissile[j].dir.value();
packetInfo info = eventPacketGenerator(EVENTHIT, eventData);
sendPacketToPlayer(EVENT, info);
}
} else{
//Hit the wall, disappear
temp_rat.RatMissile[j].exist = FALSE;
if(i == 0)
clearSquare(temp_rat.RatMissile[j].x, temp_rat.RatMissile[j].y);
}
M->ratIs(temp_rat, i);
}
}
}
}
}
/* ----------------------------------------------------------------------- */
void DoViewUpdate() {
if (updateView) { /* paint the screen */
ShowPosition(MY_X_LOC, MY_Y_LOC, MY_DIR);
if (M->peeking())
ShowView(M->xPeek(), M->yPeek(), M->dirPeek());
else
ShowView(MY_X_LOC, MY_Y_LOC, MY_DIR);
updateView = FALSE;
}
}
/* ----------------------------------------------------------------------- */
void copybit(uint32_t *temp, uint32_t src, uint8_t offset, uint8_t length) {
if(length > 32 || offset < 0 || length + offset > 32){
MWError((char*)"Set packet bits error\n");
}
if(length != 32) {
uint32_t mask = (1 << length) - 1;
src &= mask;
src = src << offset;
}
*temp |= src;
return;
}
parsedInfo parsebit(uint32_t src, uint8_t offset, uint8_t length) {
parsedInfo info;
if(length > 32 || offset < 0 || length + offset > 32) {
MWError((char*)"Set packet bits error\n");
}
src = src >> offset;
if(length == 1) {
info.bit_1 = src & 1;
}else if(length <= 8) {
uint32_t mask = 1<<length;
info.bit_8 = src & (mask - 1);
}else if(length <= 16) {
uint32_t mask = 1<<length;
info.bit_16 = src & (mask - 1);
}else {
info.bit_32 = src;
}
return info;
}
absoluteInfo parseAbsoluteInfo(uint8_t* address){
absoluteInfo result;
uint32_t temp;
uint16_t temp16;
memcpy(&result.score, address, 4);
memset(&temp, 0, 4);
memcpy(&temp, ((uint8_t*)address)+4, 4);
result.position.x = parsebit(temp, 27, 5).bit_16;
result.position.y = parsebit(temp, 23, 4).bit_16;
result.direction = parsebit(temp, 21, 2).bit_8;
result.cloak = parsebit(temp, 20, 1).bit_1;
result.missileNumber = parsebit(temp, 16, 4).bit_8;
for(int count=0; count<MAX_MISSILES; ++count){
memset(&temp16, 0, 2);
memcpy(&temp16, ((uint8_t*)address)+8+count*2, 2);
result.missiles[count].exist = parsebit(temp16, 14, 2).bit_8;
result.missiles[count].position.x = parsebit(temp16, 9, 5).bit_16;
result.missiles[count].position.y = parsebit(temp16, 5, 4).bit_16;
result.missiles[count].direction = parsebit(temp16, 3, 2).bit_8;
}
return result;
}
eventSpecificData parseEventData(uint8_t* address, uint8_t type){
uint32_t temp1, temp2;
memset(&temp1, 0, 4);
memset(&temp2, 0, 4);
memcpy(&temp1, address, 4);
memcpy(&temp2, ((uint8_t*)address)+4, 8);
eventSpecificData result;
switch(type){
case EVENTCLOAK:
result.cloak = parsebit(temp1, 31, 1).bit_1;
break;
case EVENTMOVE:
result.moveData.direction = parsebit(temp1, 30, 2).bit_8;
result.moveData.speed = parsebit(temp1, 28, 2).bit_8;
break;
case EVENTBORN:
result.bornData.position.x = parsebit(temp1, 27, 5).bit_16;
result.bornData.position.y = parsebit(temp1, 23, 4).bit_16;
result.bornData.direction = parsebit(temp1, 21, 2).bit_8;
break;
case EVENTSHOOT:
result.missileProjData.missileId = parsebit(temp1, 30, 2).bit_8;
result.missileProjData.position.x = parsebit(temp1, 25, 5).bit_16;
result.missileProjData.position.y = parsebit(temp1, 21, 4).bit_16;
result.missileProjData.direction = parsebit(temp1, 19, 2).bit_8;
break;
case EVENTHIT:
result.missileHitData.ownerId = parsebit(temp1, 0, 32).bit_32;
result.missileHitData.missileId = parsebit(temp2, 30, 2).bit_8;
result.missileHitData.position.x = parsebit(temp2, 25, 5).bit_16;
result.missileHitData.position.y = parsebit(temp2, 21, 4).bit_16;
result.missileHitData.direction = parsebit(temp2, 19, 2).bit_8;
break;
}
return result;
}
uncommittedAction parseUncommit(uint8_t* address){
uncommittedAction result;
uint32_t temp;
memset(&temp, 0, 4);