-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
1936 lines (1685 loc) · 49.2 KB
/
Copy pathEnvironment.cpp
File metadata and controls
1936 lines (1685 loc) · 49.2 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
//
// Filename: "Environment.cpp"
//
// Programmer: Ross Mead
// Last modified: 30Nov2009
//
// Description: This class implements a robot cell environment.
//
// preprocessor directives
#include "Environment.h"
// <constructors>
//
// Environment(n, f, colorIndex)
// Last modified: 08Nov2009
//
// Default constructor that initializes
// this environment to the parameterized values.
//
// Returns: <none>
// Parameters:
// n in the initial number of cells
// f in the initial formation
// colorIndex in the initial array index of the color of the cells
//
Environment::Environment(const GLint n,
const Formation f,
const Color colorIndex,
const int insert,
const float max_t_error,
string randInput)
{
//string s = randInput;
inputFile = randInput;
if (!init(n, f, colorIndex, insert,max_t_error)) clear();
} // Environment(const GLint, const Formation, const Color)
//
// Environment(e)
// Last modified: 27Aug2006
//
// Copy constructor that copies the contents of
// the parameterized environment into this environment.
//
// Returns: <none>
// Parameters:
// e in/out the environment being copied
//
Environment::Environment(const Environment &e)
: cells(e.cells), msgQueue(e.msgQueue)
{
} // Environment(const Environment &)
// <destructors>
//
// ~Environment()
// Last modified: 27Aug2006
//
// Destructor that clears this environment.
//
// Returns: <none>
// Parameters: <none>
//
Environment::~Environment()
{
clear();
} // ~Environment()
// <virtual public mutator functions>
//
// bool setColor(r, g, b)
// Last modified: 03Sep2006
//
// Attempts to set the color to the parameterized RGB color values,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// r in/out the red in the color to be set to
// g in/out the green in the color to be set to
// b in/out the blue in the color to be set to
//
bool Environment::setColor(const GLfloat r, const GLfloat g, const GLfloat b)
{
color[GLUT_RED] = r;
color[GLUT_GREEN] = g;
color[GLUT_BLUE] = b;
return true;
} // setColor(const GLfloat, const GLfloat, const GLfloat)
//
// bool setColor(clr)
// Last modified: 03Sep2006
//
// Attempts to set the color to the parameterized RGB color values,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// clr in/out the color to be set to
//
bool Environment::setColor(const GLfloat clr[3])
{
return setColor(clr[GLUT_RED], clr[GLUT_GREEN], clr[GLUT_BLUE]);
} // setColor(const GLfloat [])
//
// bool setColor(colorIndex)
// Last modified: 27Aug2006
//
// Attempts to set the color to the parameterized RGB color values,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// colorIndex in/out the index of the color to be set to
//
bool Environment::setColor(const Color colorIndex)
{
return setColor(COLOR[(GLint)colorIndex]);
} // setColor(const Color)
// <public mutator functions>
//
// bool addCell(c)
// Last modified: 27Aug2006
//
// Attempts to add a cell to the environment,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// c in the cell being added
//
bool Environment::addCell(Cell *c)
{
if(c==NULL)
{
if ((c == NULL) && ((c = new Cell()) == NULL)) return false;
}
bool done = true;
c->setEnvironment(this);
c->getState().formation.setFormationID(formation.getFormationID());
cout << " new cell " << c->getID() << " has a formation ID = " << c->getState().formation.getFormationID() << endl;
c->insertion = insertion;
c->max_trans_error = max_trans_error;
// attempt to add this cell to the cell list
cells.push_back(c);
return true;
} // addCell(Cell *)
//
// bool removeCell()
// Last modified: 27Aug2006
//
// Attempts to remove a cell from the environment,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters: <none>
//
bool Environment::removeCell()
{
Cell *c = cells[cells.size()-1];
if (!removeCell(c)) return false;
delete c;
return true;
} // removeCell()
//
// bool removeCell(c)
// Last modified: 27Aug2006
//
// Attempts to remove a cell from the environment,
// storing the address of the removed cell and
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// c in/out set to the cell being removed
//
bool Environment::removeCell(Cell* c)
{
if(c==NULL)
{
return false;
}
bool answer = false;
for(GLint i=0;i< cells.size();i++)
{
if(c==cells[i])
{
cells.erase(cells.begin()+i);
answer = true;
break;
}
}
return answer;
} // removeCell(Cell* &)
// <public accessor functions>
//
// Cell* getCell() const
// Last modified: 27Aug2006
//
// Returns the cell at the parameterized position.
//
// Returns: the cell at the parameterized position
// Parameters: <none>
//
Cell* Environment::getCell(GLint pos) const
{
Cell *c = NULL;
for(GLint i=0;i< cells.size();i++)
{
if(pos==cells[i]->getID())
{
c = cells[i];
break;
}
}
return c;
} // getCell(GLint) const
//
// vector<Cell *> getCells()
// Last modified: 27Aug2006
//
// Returns all of the cells in the environment.
//
// Returns: all of the cells in the environment
// Parameters: <none>
//
vector<Cell *> Environment::getCells()
{
return cells;
} // getCells()
//
// vector<Robot *> getRobots()
// Last modified: 08Nov2009
//
// Returns all of the [free] robots in the environment.
//
// Returns: all of the [free] robots in the environment
// Parameters: <none>
//
vector<Robot *> Environment::getRobots()
{
return robots;
} // getRobots()
//
// GLint getNCells() const
// Last modified: 27Aug2006
//
// Returns the number of cells in the environment.
//
// Returns: the number of cells in the environment
// Parameters: <none>
//
GLint Environment::getNCells() const
{
return cells.size();
} // getNCells() const
//
// GLint getNFreeRobots() const
// Last modified: 08Nov2009
//
// Returns the number of [free] robots in the environment.
//
// Returns: the number of [free] robots in the environment
// Parameters: <none>
//
GLint Environment::getNFreeRobots() const
{
return robots.size();
} // getNFreeRobots() const
// <virtual public utility functions>
//
// void draw()
// Last modified: 08Nov2009
//
// Renders the environment.
//
// Returns: <none>
// Parameters: <none>
//
void Environment::draw()
{
// draw cells
if (cells.size() > 0)
{
getCell(formation.getSeedID())->setColor(GREEN);
}
for (GLint i = 0; i < getNCells(); ++i)
{
cells[i]->draw();
}
// draw [free] robots
Robot *currRobot = NULL;
for (GLint i = 0; i < robots.size(); ++i)
{
robots[i]->draw();
}
} // draw()
//
// void step()
// Last modified: 27Aug2006
//
// Executes the next step in each cell in the environment
// and forwards all sent packets to their destinations.
//
// Returns: <none>
// Parameters: <none>
//
bool Environment::step()
{
if(startFormation)
{
if(insertion)
{
//vector<Robot*> auctionCalls;
//Robot *auctionCall = NULL;
for(GLint i = 0; i < robots.size(); ++i)
{
//auctionCall =
if(robots[i]->auctioningStep()!=NULL)
{
//auctionCalls.push_back(auctionCall);
Robot* a = robots[i];
Formation f = formation;
Insertion_Auction_Announcement* aa = new Insertion_Auction_Announcement(a->getID());
sendMsg((Message)aa, ID_BROADCAST, a->getID(), INSERTION_AUCTION_ANNOUNCEMENT);
a->setAuctionStepCount(1);
}
}
//cout << "done stepping cells" << endl;
//forwardPackets();
/*for(int i=0; i<auctionCalls.size();i++)
{
}*/
//forwardPackets();
for(int i=0;i<cells.size();i++)
{
cells[i]->cStep();
}
//forwardPackets();
//cout << "after forwardPackets " << endl;
//auctionCalls.clear();
//cout << " after auctionCalls.clear() " << endl;
bool auctionSettled = false;
for(int i=0; i<robots.size(); i++)
{
//cout << "hitting robot["<<robots[i]->getID()<<"]"<<endl;
//if(robots[i]->getAuctionStepCount() >= AUCTION_STEP_COUNT)
//{
robots[i]->processPackets();
//cout << "robot processSaucePacketsess" << endl;
if(!auctionSettled)
{
auctionSettled = robots[i]->settleAuction();
//if(auctionSettled) cout << "settled an auction..." << endl;
}
robots[i]->bids.clear();
//}
}
//cout << "done with env->step()" << endl;
for(int i=0;i<cells.size();i++)
{
cells[i]->outstandingBid = 0;
}
} else { //Push Auctioning
//vector<Cell*> auctionCalls;
//Cell *auctionCall = NULL;
vector<int> cells_with_auctions;
for (GLint i = 0; i < getNCells(); ++i)
{
if(cells[i]->cStep())
//if(cells[i]->getNNbrs() < NEIGHBORHOOD_SIZE)
{
//if(cells[i]->cStep())
//{
Cell* a = cells[i];
cells_with_auctions.push_back(i);
auctions.push(a);
State s = a->getState() ;
Formation f = formation;
bool dir;
if (a->rightNbr == NULL)
{
dir = true;
} else {
dir = false;
}
Push_Auction_Announcement* aa = new Push_Auction_Announcement(a->getState().gradient, s, dir);
sendMsg((Message)aa, ID_BROADCAST, a->getID(), PUSH_AUCTION_ANNOUNCEMENT);
a->setAuctionStepCount(1);
//}
}
}
for(int i=0;i<robots.size();i++)
{
robots[i]->processPackets();
robots[i]->step();
}
//forwardPackets();
//auctionCalls.clear();
/*
cout << "contents of cells_with_auctions:" << endl;
for(int i=0;i<cells_with_auctions.size();i++)
{
int q = cells_with_auctions[i];
cout << q << endl;
//cells[q]->settleAuction();
}
for(int i=0; i<getNCells(); i++)
{
if(cells[i]->getAuctionStepCount() >= AUCTION_STEP_COUNT)
{
cells[i]->settleAuction();
}
}
/*
cout << "contents of cells_with_auctions:" << endl;
for(int i=0;i<cells_with_auctions.size();i++)
{
int q = cells_with_auctions[i];
cout << q << endl;
cells[q]->settleAuction();
}
*/
int s = auctions.size();
for(int i=0;i<s;i++)
{
if(auctions.front()->getAuctionStepCount() >= AUCTION_STEP_COUNT)
{
auctions.front()->settleAuction();
auctions.pop();
}else{
Cell * c = auctions.front();
auctions.pop();
auctions.push(c);
}
}
}
}
gatherMessages();
gatherDistances();
gatherError();
stepCount++;
if(quiescence())
{
return false;
}
//cout << "end of turn - press key to continue: ";
//string thing;
//getline(cin, thing);
return true;
} // step()
//
// void clear()
// Last modified: 27Aug2006
//
// Clears this environment.
//
// Returns: <none>
// Parameters: <none>
//
void Environment::clear()
{
while (removeCell());
} // clear()
// <public utility functions>
//
// Vector getRelationship(toID, fromID)
// Last modified: 27Aug2006
//
// Returns the relationship between the two cells
// with the parameterized ID's.
//
// Returns: the relationship between two cells
// Parameters:
// toID in the ID of the cell being related to
// fromID in the ID of the cell being related from
//
Vector Environment::getRelationship(const GLint toID, const GLint fromID)
{
Cell *toCell = getCell(toID), *fromCell = getCell(fromID);
if ((toCell == NULL) || (fromCell == NULL)) return Vector();
Vector temp = *toCell - *fromCell;
temp.rotateRelative(-fromCell->getHeading());
return temp;
} // getRelationship(const GLint, const GLint)
//
// bool sendMsg(msg, toID, fromID, type)
// Last modified: 07Sep2006
//
// Attempts to send a packet to its destination
// based upon the given parameters, returning
// true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// msg in/out the message being sent
// toID in the ID of the cell receiving the packet
// fromID in the ID of the cell sending the packet
// type in the type of message being sent
//
//removed const from parameters
bool Environment::sendMsg(const Message &msg,
const GLint toID,
const GLint fromID,
const GLint type)
{
Message msg_c = msg;
bool answer = sendPacket(Packet(msg_c, toID, fromID, type));
//if(VERBOSE)printf("Received sendPacket answer\n");
return answer;
} // sendMsg(const Message &, const GLint, const GLint, const GLint)
//
// bool sendPacket(p)
// Last modified: 07Sep2006
//
// Attempts to send a packet to its destination,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// p in/out the packet being sent
//
//removed const
bool Environment::sendPacket(const Packet &p)
{
// discrete message passing
//if (msgQueue.enqueue(p)) return true;
// continuous message passing
//if(p.type==AUCTION_ANNOUNCEMENT)printf("auctionannouncement in sendPacket()\n");
if (forwardPacket(p)) return true;
//if(VERBOSE)printf("just before delete p.msg;\n");
//(cast as message type -- delete (state *)
if(p.msg != NULL)
{
if(p.type==STATE)
{
//if(VERBOSE) printf("attempting to delete STATE message\n");
delete (State *)p.msg;
//if(VERBOSE) printf("successfully deleted STATE message\n");
}else if(p.type==CHANGE_FORMATION)
{
//if(VERBOSE) printf("attempting to delete CHANGE_FORMATION message\n");
delete (Formation *)p.msg;
}
}
//p.msg = NULL;
if(VERBOSE) printf("finished sendPacket()\n");
return false;
} // sendPacket(const Packet &)
//
// bool forwardPacket(p)
// Last modified: 29Nov2009
//
// Attempts to forward a packet to its destination,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// p in/out the packet being forwarded
//
//remove const
bool Environment::forwardPacket(const Packet &p)
{
totalMessages++;
//messagesPerStep.push_back(p);
if(p.toID < ID_BROADCAST)
{
//cout << "Forwarding a packet to robot["<<p.toID<<"] from "<< p.fromID << endl;
Robot *r;
if(!p.fromBroadcast())
{
r = getRobot(p.toID);
}
if(r !=NULL)
{
//cout << "not nullsy for robot id = "<<r->getID() << endl;
r->msgQueue.push(p);
//cout << "finished sending packet to robot" << endl;
return true;
}else{
//cout << "could not locate robot["<<p.toID<<"]"<< endl;
return false;
}
}
Cell *c;
if((!p.fromBroadcast()) &&(p.toID!=ID_OPERATOR))
{
//cout << "sending a message directly to cell cell["<< p.toID << "]." << endl;
c = getCell(p.toID);
}
int to = p.toID;
if (c != NULL)
{
c->msgQueue.push(p);
return true;
}
//if (c == NULL) printf("CELL is NULL!\n");
//if p.msg != NULL
//if(p.msg != NULL) delete p.msg;
if(p.msg != NULL)
{
if(p.type==STATE)
{
delete (State *)p.msg;
}else if(p.type==CHANGE_FORMATION)
{
delete (Formation *)p.msg;
}else if(p.type == PUSH_AUCTION_ANNOUNCEMENT)
{
//Robot* r;
//cout << "Sending PUSH_AUCTION_ANNOUNCEMENT" << endl;
for(int i=0;i<robots.size();i++)
{
robots[i]->msgQueue.push(p);
}
}else if(p.type == INSERTION_AUCTION_ANNOUNCEMENT)
{
for(int i=0;i<cells.size();i++)
{
cells[i]->msgQueue.push(p);
}
}
}
if(VERBOSE) printf("finished forwarding Packet to %d\n",to);
return false;
} // forwardPacket(const Packet &)
//
// bool forwardPackets()
// Last modified: 29Nov2009
//
// Attempts to forward all packets to their destinations,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters: <none>
//
bool Environment::forwardPackets()
{
Packet p;
while (!msgQueue.empty())
{
p = msgQueue.front();
msgQueue.pop();
if (!forwardPacket(p)) return false;
}
return true;
} // forwardPackets()
// <public utility cell functions>
//
// bool showLine(show)
// Last modified: 28Aug2006
//
// Attempts to display the line of the heading vector of each cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// show in display flag for showing the line
//
bool Environment::showLine(const bool show)
{
for (GLint i = 0; i < getNCells(); ++i)
{
cells[i]->heading.showLine = show;
}
return true;
} // showPos(const bool)
//
// bool showHead(show)
// Last modified: 28Aug2006
//
// Attempts to display the head of the heading vector of each cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// show in display flag for showing the head
//
bool Environment::showHead(const bool show)
{
for (GLint i = 0; i < getNCells(); ++i)
{
cells[i]->heading.showHead = show;
}
return true;
} // showHeading(const bool)
//
// bool showPos(show)
// Last modified: 28Aug2006
//
// Attempts to display the position vector of each cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// show in display flag for showing the position vector
//
bool Environment::showPos(const bool show)
{
for (GLint i = 0; i < getNCells(); ++i)
{
cells[i]->showPos = show;
}
return true;
} // showPos(const bool)
//
// bool showHeading(show)
// Last modified: 28Aug2006
//
// Attempts to display the heading vector of each cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// show in display flag for showing the heading vector
//
bool Environment::showHeading(const bool show)
{
for (GLint i = 0; i < getNCells(); ++i)
{
cells[i]->showHeading = show;
}
return true;
} // showHeading(const bool)
// <virtual protected utility functions>
//
// bool init(n, f, colorIndex)
// Last modified: 07Nov2009
//
// Initializes the environment to the parameterized values,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// n in the initial number of robots
// f in the initial formation
// colorIndex in the initial array index of the color of the cells
//
bool Environment::init(const GLint n,
const Formation f,
const Color colorIndex,
const int insert,
const float max_t_error)
{
srand(time(NULL));
nRobots = n;
formation = f;
formation.setFormationID(0);
formationID = 0;
stepCount = 0;
qCount = 0;
defaultColor = colorIndex;
insertion = insert;
max_trans_error = max_t_error;
//char * name = "input.txt";
//strncpy(name,inputFile,9);
//inputFile[9]='\0';
ifstream inp;
inp.open(inputFile.c_str());
for(int i=0;i<(nRobots*2);i++)
{
float loc;
inp >> loc;
initLocs.push(loc);
}
cout << "initLocs.size() = " << initLocs.size() << endl;
inp.close();
if(insertion){
printf("\n\nUsing Insertion Auction Algorithm\n\n");
}
totalMessages = 0;
bool result = true;
startFormation = false;
//initCells(n, f);
//system("PAUSE");
initRobots();
if (VERBOSE) printf("finished initCells()\n");
return result && setColor(colorIndex);
} // init(const GLint, const Formation, const Color)
bool Environment::initRobots()
{
if(initLocs.size()==nRobots*2)
{
for(int i=0;i<nRobots;i++)
{
float x,y;
x = initLocs.front();
initLocs.pop();
y = initLocs.front();
initLocs.pop();
addRobot(x,y,0.0,randSign() * frand(0.0f, 180.0f));
}
} else {
for (GLint i = 0; i < nRobots; ++i)
addRobot(randSign() * frand(),
randSign() * frand(),
0.0f,
randSign() * frand(0.0f, 180.0f));
}
return true;
}
bool Environment::addRobot(GLfloat x, GLfloat y, GLfloat z, GLfloat theta)
{
if (VERBOSE)
printf("new Robot(x = %.2f, y = %.2f, z = %.2f, theta = %.2f)\n",
x, y, z, theta);
Robot *r = new Robot(x, y, z, theta, DEFAULT_ROBOT_COLOR);
r->setEnvironment(this);
robots.push_back(r);
return true;
}
//
// bool initCells(n, f)
// Last modified: 28Aug2006
//
// Initializes each cell to the parameterized values,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// n in the initial number of cells
// f in the initial formation
//
/*bool Environment::initCells(const GLint n, const Formation f)
{
srand(time(NULL));
for (GLint i = 0; i < n; ++i) if (!addCell()) return false;
// initialize each robot's neighborhood
if (!initNbrs()) return false;
// organizes the cells into an initial formation (default line)
Cell *c = NULL;
for (GLint i = 0; i < getNCells(); ++i)
{
if (!cells.getHead(c)) return false;
c->x = formation.getRadius() *
((GLfloat)i - (GLfloat)(getNCells() - 1) / 2.0f);
c->y = 0.0f;
c->setColor(DEFAULT_ROBOT_COLOR);
c->setHeading(formation.getHeading());
++cells;
if(VERBOSE) printf("iterating through cells...\n");
}
newestCell = c;
//printf("newestCell == %d\n",c->getID());
return (getNCells() == n) &&
sendMsg(new Formation(formation), formation.getSeedID(),
ID_OPERATOR, CHANGE_FORMATION);
}*/ // initCells(const GLint, const Formation f)
//
// bool initNbrs(nNbrs)
// Last modified: 03Sep2006
//
// Initializes the neighborhood of each cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// nNbrs in the initial number of neighbors
//
/*bool Environment::initNbrs(const GLint nNbrs)
{
Cell *c = NULL;
for (GLint i = 0; i < getNCells(); ++i)
{
if (!cells.getHead(c)) return false;
c->clearNbrs();
if ((i > 0) && (!c->addNbr(i - 1))) return false;
else c->leftNbr = c->nbrWithID(i - 1);
if ((i < getNCells() - 1) && (!c->addNbr(i + 1))) return false;
else c->rightNbr = c->nbrWithID(i + 1);
++cells;
}
if(VERBOSE) printf("finished initNbrs()\n");
return true;
} */ // initNbrs(const GLint)
Robot * Environment::getNearestRobot(Cell *c)
{
Robot *r,*rr;
GLfloat minDistance = 999999.9, distance;
distance = minDistance;
for (GLint i = 0; i < getNFreeRobots(); ++i)
{