void makePacket(MW244BPacket *p, char status, int killed, bool UpdateSeqNo) { strncpy(p->Name,M->myName_,NAMESIZE ); p-> ID = M->myRatId().value(); cout << "ID from packet: " << M->myRatId().value() << endl; if (UpdateSeqNo) ++my_seq_no; p-> sequence_number = my_seq_no; p -> tagged_rat = killed; p->score = M->score().value(); p->x_pos = MY_X_LOC; p->y_pos = MY_Y_LOC; p-> dir = MY_DIR; p-> status = status; p-> globalID = GLOBAL_ID; p -> time = start_time; }
void ratStates() { /* In our sample version, we don't know about the state of any rats over the net, so this is a no-op */ for (int i = 0; i < 8 ; i++) if (i != M->myRatId().value() && participants[i] == 0 && M->mazeRats_[Mapping_idToIndex.find(i)->second].playing)// no packet from this dude { cout << i << endl; //M->mazeRats_[Mapping_idToIndex.find(i)->second].playing = false; rat_array[Mapping_idToIndex.find(i)->second] = NULL; RatIndexType ratId( Mapping_idToIndex.find(i)->second); ClearRatPosition(ratId); } for (int i = 0; i < 8; i++) cout << "bool" << M->mazeRats_[i].playing << endl; if (!( M->mazeRats_[1].playing || M->mazeRats_[2].playing || M->mazeRats_[3].playing || M->mazeRats_[4].playing || M->mazeRats_[5].playing || M->mazeRats_[6].playing || M->mazeRats_[7].playing)) // this means that there are no other players :O { M->myRatIdIs(0); M->scoreIs(0); SetMyRatIndexType(0); Mapping_idToIndex.clear(); Mapping_indexToId.clear(); play();//main(0,NULL); // start over } }
void shoot() { mw_score_t score; mws_fire_missile(M->state, M->local_id); /* XXX: Should check return code in case of error. */ mws_get_rat_score(M->state, M->local_id, &score); M->scoreIs(score); UpdateScoreCard(M->myRatId().value()); }
/* This is just for the sample version, rewrite your own */ char *GetRatName(RatIndexType ratId) { if (ratId.value() == M->myRatId().value()) { return(M->myName_); } else { return ("Dummy"); } }
/* This is just for the sample version, rewrite your own */ Score GetRatScore(RatIndexType ratId) { if (ratId.value() == M->myRatId().value()) { return(M->score()); } else { return (0); } }
void processPacket (MWEvent *eventPacket) { MW244BPacket *p = new MW244BPacket ; p = eventPacket->eventDetail; /*cout << p->ID<< endl; cout << p->sequence_number<< endl; cout << p->score<< endl; cout << p->x_pos<< endl; cout << p->y_pos<< endl; cout << p->dir<< endl; cout << p->tagged_rat<< endl; cout << p->globalID<< endl; ConvertIncoming(p); cout << p->ID<< endl; cout << p->sequence_number<< endl; cout << p->score<< endl; cout << p->x_pos<< endl; cout << p->y_pos<< endl; cout << p->dir<< endl; cout << p->tagged_rat<< endl; cout << p->globalID<< endl; exit(0);*/ cout << "PP " << p->ID << endl; if (myPacket(p, &eventPacket->eventSource) && join)// BECAUSE WHILE YOU ARE STILL 0, you will get packets with ID 0 { return; } else { if (!join && expected_seqno[p->ID] <= p->sequence_number){ if (p->globalID == 7) // What if there is only player with global ID 7? :/ { cout << "You are one player too many :(" << endl; cout << "Please try again later." << endl; exit(0); } else { for (int i = 0; i < 100; i++) cout << "Incrementing for no reason" << endl; join = true; GLOBAL_ID = p->globalID+1; M->myRatIdIs(GLOBAL_ID); setMapping(); UpdateScoreCard(0); MW244BPacket q; makePacket(&q,'h',-1, updateSeqNo); sendPacketToPlayers(q); participants[p->ID]++; Rat r; r.playing = 1; cout << "NEW " << p->ID << endl; r.x=p->x_pos; r.y=p->y_pos; r.dir=(p->dir); M->mazeRats_[Mapping_idToIndex.find(p->ID)->second] = r; rat_array[p-> ID]=new MW244BPacket; rat_array[Mapping_idToIndex.find(p->ID)->second]=p; Loc x(p->x_pos); Loc y(p->y_pos); Direction dir(p-> dir); RatIndexType ratId(Mapping_idToIndex.find(p->ID)->second); SetRatPosition(ratId, x, y, dir); UpdateScoreCard(Mapping_idToIndex.find(p-> ID)->second); expected_seqno[p->ID] = p->sequence_number + 1; return; } } if (GLOBAL_ID < p->globalID && expected_seqno[p->ID] <= p->sequence_number ) GLOBAL_ID = p->globalID; if (p->status == 'a' && p->tagged_rat == M->myRatId().value()) { // cannot discard expected_seqno[p->ID] <= p->sequence_number // because we're getting this packet because the other side didn't get b :( // MUST send b MW244BPacket q; makePacket(&q,'b',-1, updateSeqNo); sendPacketToPlayers(q); cout<<p->sequence_number<<"Seq number"<<endl; cout<<prevseq_a[p->ID]<<"Previous Seq number"<<endl; if (p->sequence_number > prevseq_a[p->ID]) { // dealing with duplicates DrawString("You have been tagged!",21,200,250); M->scoreIs( M->score().value()-5 ); UpdateScoreCard(0); NewPosition(M); } prevseq_a[p->ID] = p->sequence_number; } else if (p->status == 'a' && expected_seqno[p->ID] <= p->sequence_number){ //RatIndexType ratId(p-> ID); //ClearRatPosition(ratId); RatIndexType ratId( Mapping_idToIndex.find(p->ID)->second); ClearRatPosition(ratId); } if (p->status == 'b' && expected_seqno[p->ID] <= p->sequence_number) { checkingzero = 1; updateSeqNo = true; } if (p->status == 'f' && expected_seqno[p->ID] <= p->sequence_number) { //player has left cout << "Player has gone :(" << endl; participants[p->ID] = -1;// in next section, will be incremented to 0 // automatically dealt with in ratstates RatIndexType ratId( Mapping_idToIndex.find(p->ID)->second); ClearRatPosition(ratId); // WHY IS THIS NOT WORKING? :/ clearSquare(p->x_pos, p->y_pos); cout << p->x_pos << "," << p->y_pos << endl; cout << M->mazeRats_[Mapping_idToIndex.find(p->ID)->second].x.value() << "," << M->mazeRats_[Mapping_idToIndex.find(p->ID)->second].y.value() << endl; } if (expected_seqno[p->ID] <= p->sequence_number) { // if this condition is false, then we are discarding the packet. Rat r; r.playing = 1; cout << "IF " << p->ID << endl; r.x=p->x_pos; r.y=p->y_pos; r.dir=(p->dir); M->mazeRats_[Mapping_idToIndex.find(p->ID)->second] = r; rat_array[p-> ID]=new MW244BPacket; rat_array[Mapping_idToIndex.find(p->ID)->second]=p; Loc x(p->x_pos); Loc y(p->y_pos); Direction dir(p-> dir); RatIndexType ratId(Mapping_idToIndex.find(p->ID)->second); SetRatPosition(ratId, x, y, dir); UpdateScoreCard(Mapping_idToIndex.find(p-> ID)->second); expected_seqno[p->ID] = p->sequence_number + 1; participants[p->ID]++; } } }
bool myPacket(MW244BPacket *p, Sockaddr *remote) { return Mapping_idToIndex.find(p->ID)->second == Mapping_idToIndex.find(M->myRatId().value())->second && p->time == start_time; }
void setMapping() { participation = 0; if (M->myRatId().value() == 7) { Mapping_idToIndex.insert(pair<int, int>(7,0)); for(int i = 0; i < 7 ; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+1)); Mapping_indexToId.insert(pair<int, int>(0,7)); Mapping_indexToId.insert(pair<int, int>(1,0)); Mapping_indexToId.insert(pair<int, int>(2,1)); Mapping_indexToId.insert(pair<int, int>(3,2)); Mapping_indexToId.insert(pair<int, int>(4,3)); Mapping_indexToId.insert(pair<int, int>(5,4)); Mapping_indexToId.insert(pair<int, int>(6,5)); Mapping_indexToId.insert(pair<int, int>(7,6)); } else if (M->myRatId().value() == 6) { Mapping_idToIndex.insert(pair<int, int>(6,0)); Mapping_idToIndex.insert(pair<int, int>(7,1)); for(int i = 0; i < 6; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+2)); Mapping_indexToId.insert(pair<int, int>(0,6)); Mapping_indexToId.insert(pair<int, int>(1,7)); Mapping_indexToId.insert(pair<int, int>(2,0)); Mapping_indexToId.insert(pair<int, int>(3,1)); Mapping_indexToId.insert(pair<int, int>(4,2)); Mapping_indexToId.insert(pair<int, int>(5,3)); Mapping_indexToId.insert(pair<int, int>(6,4)); Mapping_indexToId.insert(pair<int, int>(7,5)); } else if (M->myRatId().value() == 5) { Mapping_idToIndex.insert(pair<int, int>(5,0)); Mapping_idToIndex.insert(pair<int, int>(6,1)); Mapping_idToIndex.insert(pair<int, int>(7,2)); for(int i = 0; i < 5; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+3)); Mapping_indexToId.insert(pair<int, int>(0,5)); Mapping_indexToId.insert(pair<int, int>(1,6)); Mapping_indexToId.insert(pair<int, int>(2,7)); Mapping_indexToId.insert(pair<int, int>(3,0)); Mapping_indexToId.insert(pair<int, int>(4,1)); Mapping_indexToId.insert(pair<int, int>(5,2)); Mapping_indexToId.insert(pair<int, int>(6,3)); Mapping_indexToId.insert(pair<int, int>(7,4)); } else if (M->myRatId().value() == 4) { Mapping_idToIndex.insert(pair<int, int>(4,0)); Mapping_idToIndex.insert(pair<int, int>(5,1)); Mapping_idToIndex.insert(pair<int, int>(6,2)); Mapping_idToIndex.insert(pair<int, int>(7,3)); for(int i = 0; i < 4; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+4)); Mapping_indexToId.insert(pair<int, int>(0,4)); Mapping_indexToId.insert(pair<int, int>(1,5)); Mapping_indexToId.insert(pair<int, int>(2,6)); Mapping_indexToId.insert(pair<int, int>(3,7)); Mapping_indexToId.insert(pair<int, int>(4,0)); Mapping_indexToId.insert(pair<int, int>(5,1)); Mapping_indexToId.insert(pair<int, int>(6,2)); Mapping_indexToId.insert(pair<int, int>(7,3)); } else if (M->myRatId().value() == 3) { Mapping_idToIndex.insert(pair<int, int>(3,0)); Mapping_idToIndex.insert(pair<int, int>(4,1)); Mapping_idToIndex.insert(pair<int, int>(5,2)); Mapping_idToIndex.insert(pair<int, int>(6,3)); Mapping_idToIndex.insert(pair<int, int>(7,4)); for(int i = 0; i < 3; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+5)); Mapping_indexToId.insert(pair<int, int>(0,3)); Mapping_indexToId.insert(pair<int, int>(1,4)); Mapping_indexToId.insert(pair<int, int>(2,5)); Mapping_indexToId.insert(pair<int, int>(3,6)); Mapping_indexToId.insert(pair<int, int>(4,7)); Mapping_indexToId.insert(pair<int, int>(5,0)); Mapping_indexToId.insert(pair<int, int>(6,1)); Mapping_indexToId.insert(pair<int, int>(7,2)); } else if (M->myRatId().value() == 2) { Mapping_idToIndex.insert(pair<int, int>(2,0)); Mapping_idToIndex.insert(pair<int, int>(3,1)); Mapping_idToIndex.insert(pair<int, int>(4,2)); Mapping_idToIndex.insert(pair<int, int>(5,3)); Mapping_idToIndex.insert(pair<int, int>(6,4)); Mapping_idToIndex.insert(pair<int, int>(7,5)); for(int i = 0; i < 2; i++) Mapping_idToIndex.insert(pair<int, int>(i,i+6)); Mapping_indexToId.insert(pair<int, int>(0,2)); Mapping_indexToId.insert(pair<int, int>(1,3)); Mapping_indexToId.insert(pair<int, int>(2,4)); Mapping_indexToId.insert(pair<int, int>(3,5)); Mapping_indexToId.insert(pair<int, int>(4,6)); Mapping_indexToId.insert(pair<int, int>(5,7)); Mapping_indexToId.insert(pair<int, int>(6,0)); Mapping_indexToId.insert(pair<int, int>(7,1)); } else if (M->myRatId().value() == 1) { Mapping_idToIndex.insert(pair<int, int>(1,0)); Mapping_idToIndex.insert(pair<int, int>(2,1)); Mapping_idToIndex.insert(pair<int, int>(3,2)); Mapping_idToIndex.insert(pair<int, int>(4,3)); Mapping_idToIndex.insert(pair<int, int>(5,4)); Mapping_idToIndex.insert(pair<int, int>(6,5)); Mapping_idToIndex.insert(pair<int, int>(7,6)); Mapping_idToIndex.insert(pair<int, int>(0,7)); Mapping_indexToId.insert(pair<int, int>(0,1)); Mapping_indexToId.insert(pair<int, int>(1,2)); Mapping_indexToId.insert(pair<int, int>(2,3)); Mapping_indexToId.insert(pair<int, int>(3,4)); Mapping_indexToId.insert(pair<int, int>(4,5)); Mapping_indexToId.insert(pair<int, int>(5,6)); Mapping_indexToId.insert(pair<int, int>(6,7)); Mapping_indexToId.insert(pair<int, int>(7,0)); } else if (M->myRatId().value() == 0) for(int i = 0; i < 8; i++) { Mapping_idToIndex.insert(pair<int, int>(i,i)); Mapping_indexToId.insert(pair<int, int>(i,i)); } my_seq_no = M->myRatId().value(); // MY ID NO IS MY FIRST SEQ }