-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCell.cpp
More file actions
1393 lines (1239 loc) · 40.6 KB
/
Copy pathCell.cpp
File metadata and controls
1393 lines (1239 loc) · 40.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
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: "Cell.cpp"
//
// Programmer: Ross Mead
// Last modified: 30Nov2009
//
// Description: This class implements a robot cell.
//
// preprocessor directives
#include "Cell.h"
#include "Environment.h"
#define FLT_MAX 1E+37
/*bool isNumber(const GLfloat& n) {
return (n==n);
}
bool isInfNum(const GLfloat& n) {
return (n <= FLT_MAX && n >= -FLT_MAX);
}*/
// <protected static data members>
GLint Cell::nCells = 0;
// <constructors>
//
// Cell(dx, dy, dz, theta, colorIndex)
// Last modified: 06Nov2009
//
// Default constructor that initializes
// this cell to the parameterized values.
//
// Returns: <none>
// Parameters:
// dx in the initial x-coordinate of the cell (default 0)
// dy in the initial y-coordinate of the cell (default 0)
// dz in the initial z-coordinate of the cell (default 0)
// theta in the initial heading of the cell (default 0)
// colorIndex in the initial array index of the color of the cell
//
Cell::Cell(const GLfloat dx, const GLfloat dy, const GLfloat dz,
const GLfloat theta, const Color colorIndex,bool ins)
: State(), Neighborhood(), Robot(dx, dy, dz, theta, colorIndex)
{
init(dx, dy, dz, theta, colorIndex,ins);
ID = nCells++;
numBids = 0;
//insertion = ins;
} // Cell(const GLfloat..<4>, const Color)
//
// Cell(c)
// Last modified: 27Aug2006
//
// Copy constructor that copies the contents of
// the parameterized cell into this cell.
//
// Returns: <none>
// Parameters:
// c in/out the cell being copied
//LgTxpqYm
Cell::Cell(const Cell &c): State(c), Neighborhood(c), Robot(c)
{
leftNbr = c.leftNbr;
rightNbr = c.rightNbr;
lftNbrID = c.lftNbrID;
rghtNbrID = c.rghtNbrID;
} // Cell(const Cell &)
// <destructors>
//
// ~Cell()
// Last modified: 27Aug2006
//
// Destructor that clears this cell.
//
// Returns: <none>
// Parameters: <none>
//
Cell::~Cell()
{
} // ~Cell()
// <public mutator functions>
//
// bool setState(s)
// Last modified: 27Aug2006
//
// Attempts to set the state to the parameterized state,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// s in/out the state to be set to
//
bool Cell::setState(const State &s)
{
*this = s;
return true;
} // setState(const State &)
//
// bool setNbrs(nh)
// Last modified: 27Aug2006
//
// Attempts to set the neighborhood to the parameterized neighborhood,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// nh in/out the neighborhood to be set to
//
bool Cell::setNbrs(Neighborhood &nh)
{
*this = nh;
return true;
} // setNbrs(Neighborhood &)
//
// bool setRobot(r)
// Last modified: 14May2007
//
// Attempts to set the robot to the parameterized robot,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// r in/out the robot to be set to
//
bool Cell::setRobot(const Robot &r)
{
//if(VERBOSE) printf("in setRobot() ============\n");
//changed to cast *this as a Robot variable
(Robot)*this = r;
//1if(VERBOSE) printf("robot set============\n");
return true;
} // setRobot(const Robot &)
bool Cell::setRobotP(Robot *r)
{
//(Robot *)this = r;
return true;
}
// <public accessor functions>
//
// State getState() const
// Last modified: 27Aug2006
//
// Returns the state of this cell.
//
// Returns: the state of this cell
// Parameters: <none>
//
State Cell::getState() const
{
return (State)*this;
} // getState() const
//
// Neighborhood getNbrs() const
// Last modified: 27Aug2006
//
// Returns the neighborhood of this cell.
//
// Returns: the neighborhood of this cell
// Parameters: <none>
//
Neighborhood Cell::getNbrs() const
{
return (Neighborhood)*this;
} // getNbrs() const
//
// Robot getRobot() const
// Last modified: 27Aug2006
//
// Returns the robot of this cell.
//
// Returns: the robot of this cell
// Parameters: <none>
//
Robot Cell::getRobot() const
{
return (Robot)*this;
} // getRobot() const
// <virtual public utility functions>
//
// void draw()
// Last modified: 27Aug2006
//
// Renders the cell as a robot.
//
// Returns: <none>
// Parameters: <none>
//
void Cell::draw()
{
Robot::draw();
} // draw()
//
// void step()
// Last modified: 30Nov2009
//
// Processes packets received and updates the state of the cell,
// which is then broadcast within the neighborhood of the cell.
//
// Returns: <none>
// Parameters: <none>
//
Cell* Cell::cStep()
{
//cout << "################################## ---- CALLING CSTEP ----- ###########################################################"<<endl;
Cell* answer = NULL;
if(rghtNbrID!=-1)
{
rightNbr = nbrWithID(rghtNbrID);
}
if(lftNbrID!=-1)
{
leftNbr = nbrWithID(lftNbrID);
}
if((getState().transError.magnitude()<CONVERGENCE_ERROR_MAX)&&(converged<0))
{
converged = env->stepCount;
}
if(processPackets())
{
//if (processPackets())
//{
//cout << "done here 1" << endl;
if (getNNbrs() > 0)
{
//cout << "done here 2" << endl;
updateState();
//cout << "done here 3" << endl;
sendStateToNbrs();
//cout << "done here 4" << endl;
}
}
moveError();
// }
//if((gradient.magnitude() < 0.05f)&&(ID!=formation.getSeedID()))
//{
//cout << "Cell " << ID << " thinks its gradient magnitude is " << gradient.magnitude() << endl;
//cout << " x=" << x << " y=" << y << endl;
//}
if(auctionStepCount>0)
{
auctionStepCount++;
}
//cout << " Done with stepwise accounting in cStep oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo insertion = " << insertion << endl;
Robot::step();
updateDistanceTraveled();
if(!insertion){
//if((AUTONOMOUS_INIT)&&(env->getRobots().size()>0))
//{
if((getNNbrs() < NEIGHBORHOOD_SIZE)&&(auctionStepCount==0))
{
if(((getState().transError.magnitude()>0)||
(getID()==formation.getSeedID()))&&
(getState().transError.magnitude() < max_trans_error))//MAX_TRANSLATIONAL_ERROR))
{
//if(auctionStepCount==0)
//{
//cout << "Cell["<<this->ID<<"]->gradient = " << this->gradient << endl;
//if(getID()==formation.getSeedID())
//{
answer = this;
//}
//}
}
}
//}
}else{
bidOnInsertionAuction();
//cout << "finished call to bidOnInsertionAuction()" << endl;
}
//insertion_auctions.clear();
/*if(CELL_INFO_VIEW)
{
cout << "============================="<<endl;
cout << "cell.getID() = " << getID() << endl;
cout << "cell.getNNbrs() = " << getNNbrs() << endl;
cout << "cell.rightNbr->getID() = ";
if(rightNbr != NULL)
{
cout << rightNbr->ID << endl;
}else
{
cout << " NULL " << endl;
}
cout << "cell.leftNbr->getID() = ";
if(leftNbr != NULL)
{
cout << leftNbr->ID << endl;
}else
{
cout << " NULL " << endl;
}
cout << "rotError = " << rotError << endl;
cout << "transError = " << transError.magnitude() << endl;
cout << "behavior.getStatus() = " << behavior.getStatus() << endl;
cout << "seedID = " << formation.getSeedID() << endl;
cout << "gradient = " << gradient << endl;
cout << "formationID = " << formation.getFormationID() << endl;
cout << "x,y location = " << x << ","<<y<<endl;
cout <<"================================" << endl << endl;
}*/
return answer;
}
//
// void updateState()
// Last modified: 30Nov2009
//
// Updates the state of the cell based upon the
// current states of the neighbors of the cell.
//
// Returns: <none>
// Parameters: <none>
//
void Cell::updateState()
{
/*if ((getNNbrs() == 0) ||
(nbrWithMinStep()->tStep < tStep) ||
((formation.getSeedID() != ID) &&
(nbrWithMaxStep()->tStep == tStep))) return;*/
// update actual relationships to neighbors
Neighbor *currNbr = NULL;
for (GLint i = 0; i < size(); ++i)
{
currNbr = getNbr(i);
if (currNbr == NULL) break;
// change formation if a neighbor has changed formation
if ((currNbr->formation.getFormationID() > formation.getFormationID()))//&&(currNbr->ID == nbrWithMinGradient()->ID))
{
/*if((ID ==3)||(ID==2))
{
cout << "cell "<<ID<<" called changeFormation( " << currNbr->ID << " ) with formationID = " << formation.getFormationID() << endl;
}*/
changeFormation(currNbr->formation, *currNbr);
}
currNbr->relActual = getRelationship(currNbr->ID);
}
rels = getRelationships();
// reference the neighbor with the minimum gradient
// to establish the correct position in formation
if (getNNbrs() > 0)
{
Neighbor *refNbr = nbrWithMinGradient(
formation.getSeedGradient());
if(ID==3)
{
//cout << "Cell 3 thinks refNbr is " << refNbr->ID << endl;
}
Relationship *nbrRelToMe = relWithID(refNbr->rels, ID);
if ((formation.getSeedID() != ID) &&
(refNbr != NULL) &&
(nbrRelToMe != NULL))
{
// error (state) is based upon the
// accumulated error in the formation
Vector nbrRelToMeDesired = nbrRelToMe->relDesired;
nbrRelToMeDesired.rotateRelative(-refNbr->rotError);
GLfloat theta = scaleDegrees(nbrRelToMe->relActual.angle() - (-refNbr->relActual).angle());
rotError = scaleDegrees(theta + refNbr->rotError);
transError = nbrRelToMeDesired - nbrRelToMe->relActual + refNbr->transError;
transError.rotateRelative(-theta);
}
}
tStep = max(tStep + 1, nbrWithMaxStep()->tStep);
} // updateState()
/*void Cell::updateState()
{
Neighbor currNbr;
for (GLint i = 0; i < getNNbrs(); ++i)
{
if (!getHead(currNbr)) break;
// change formation if a neighbor has changed formation
if (getNbr(0)->formation.getFormationID() > formation.getFormationID())
changeFormation(getNbr(0)->formation, *getNbr(0));
getNbr(0)->relActual = getRelationship(currNbr.ID);
++(*this);
}
rels = getRelationships();
// reference the neighbor with the smallest gradient
// to establish correct position in formation
Neighbor *refNbr = nbrWithMinGradient();
Relationship *nbrRel = relWithID(refNbr->rels, ID);
if ((formation.getSeedID() != ID) && (refNbr != NULL) && (nbrRel != NULL))
{
// error (state) is based upon the accumulated error in the formation
nbrRel->relDesired.rotateRelative(-refNbr->rotError);
GLfloat theta = scaleDegrees(nbrRel->relActual.angle() -
(-refNbr->relActual).angle());
rotError = scaleDegrees(theta + refNbr->rotError);
transError = nbrRel->relDesired - nbrRel->relActual +
refNbr->transError;
transError.rotateRelative(-theta);
if (transError.norm() > threshold()) moveArc(transError);
else
if (abs(rotError) > angThreshold())
moveArc(0.0, degreesToRadians(-rotError));
//if (abs(scaleDegrees(refNbr->relActual.angle() -
// refNbr->relDesired.angle())) > angThreshold())
//orientTo(refNbr->relActual, refNbr->relDesired.angle());
else moveStop();
}
else moveStop();
} // updateState()*/
/*void Cell::updateState()
{
if(VERBOSE) printf("entering updateState()\n");
Neighbor currNbr;
for (GLint i = 0; i < getNNbrs(); ++i)
{
if (!getHead(currNbr)) break;
// change formation if a neighbor has changed formation
if (getNbr(0)->formation.getFormationID() > formation.getFormationID())
changeFormation(getNbr(0)->formation, *getNbr(0));
getNbr(0)->relActual = getRelationship(currNbr.ID);
++(*this);
}
rels = getRelationships();
// reference the neighbor with the smallest gradient
// to establish correct position in formation
if(VERBOSE) printf("updateState() -- BEFORE nbrWithMinGradient()\n");
Neighbor *refNbr = nbrWithMinGradient(formation.getSeedGradient());
if(VERBOSE) printf("updateState() -- AFTER nbrWithMinGradient()\n");
Relationship *nbrRel;
if(refNbr == NULL){
//printf("refNbr == NULL\n");
}else{
nbrRel = relWithID(refNbr->rels, ID);
if(VERBOSE) printf("updateState() -- AFTER relWithID()\n");
}
if ((formation.getSeedID() != ID) && (refNbr != NULL) && (nbrRel != NULL))
{
// error (state) is based upon the accumulated error in the formation
nbrRel->relDesired.rotateRelative(-refNbr->rotError);
GLfloat theta = scaleDegrees(nbrRel->relActual.angle() -
(-refNbr->relActual).angle());
rotError = scaleDegrees(theta + refNbr->rotError);
transError = nbrRel->relDesired - nbrRel->relActual +
refNbr->transError;
transError.rotateRelative(-theta);
if (transError.norm() > threshold()) moveArc(transError);
else
if (abs(rotError) > angThreshold())
moveArc(0.0, degreesToRadians(-rotError));
//if (abs(scaleDegrees(refNbr->relActual.angle() -
// refNbr->relDesired.angle())) > angThreshold())
//orientTo(refNbr->relActual, refNbr->relDesired.angle());
else moveStop();
}
else moveStop();
if(VERBOSE) printf("leaving updateState()\n");
} // updateState()*/
// <virtual public neighborhood functions>
//
// bool changeFormation()
// Last modified: 30Nov2009
//
// Attempts to change the formation of the cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// f in/out the formation to change to
// n in the neighbor instigating the formation change
//
bool Cell::changeFormation(const Formation &f, Neighbor n)
{
//cout << endl;
//cout << "Cell["<<ID<<"] has received a changeFormation command from "<<n.ID << endl;
formation = f;
if (formation.getSeedID() == ID)
{
gradient = formation.getSeedGradient();
transError = Vector();
rotError = 0.0f;
}
else
{
Relationship *nbrRelToMe = relWithID(n.rels, ID);
//cout << "\n\n\n"<<"Cell " << ID << " thinks that it's neighbor to link with is " << n.ID << "\n\n\n";
//cout << " cell " << ID << " is using ID " << nbrRelToMe->ID << " to calc gradient......................................................... from neighbnor ID " << n.ID << endl;
//cout << " formationID = " << formation.getFormationID() << endl;
if (nbrRelToMe == NULL) return false;
nbrRelToMe->relDesired.rotateRelative(n.formation.getHeading());
//cout << " relDesired = " << nbrRelToMe->relDesired << " and n.gradient = " << n.gradient;
gradient = n.gradient + nbrRelToMe->relDesired;
//cout << " which makes my grad = " << gradient << endl;
transError = Vector();
rotError = 0.0f;
}
vector<Vector> r = formation.getRelationships(gradient);
{
/*cout << "For Cell["<<ID<<"]"<<endl;
for(int i=0;i<r.size(),i++)
{
cout << "ID at i="<<i<<" is " << r[i].ID << endl;
}*/
/*--ROSS--
GLfloat currDist = 0.0f, closestDist = GLfloat(RAND_MAX);
GLint closestNbrIndex = -1;
//GLint closestRelIndex = -1;
cout << "myID = " << ID << endl;
cout << "+ nRels = " << r.getSize() << endl;
cout << "+ nNbrs = " << getNNbrs() << endl;
vector<GLint> assignedIDs;
for (GLint i = 0; i < r.getSize(); ++i)
{
closestDist = GLfloat(RAND_MAX);
closestNbrIndex = -1;
Vector currRel;
if (r.getHead(currRel))
{
for (GLint j = 0; j < getNNbrs(); ++j)
{
Neighbor currNbr;
if (getHead(currNbr))
{
currDist = (getNbr(0)->relDesired - r[0]).magnitude();
if (currDist < closestDist)
{
bool assignedID = false;
for (GLint k = 0; k < assignedIDs.getSize(); ++k)
{
GLint currID = -1;
if ((assignedIDs.getHead(currID)) &&
(currID == getNbr(0)->ID))
{
assignedID = true;
break;
}
++assignedIDs;
}
if (!assignedID)
{
closestDist = currDist;
closestNbrIndex = j;
}
}
}
++(*this);
}
if ((closestNbrIndex >= 0) && (closestNbrIndex < getNNbrs()))
{
assignedIDs.insertTail(getNbr(closestNbrIndex)->ID);
cout << " -nbrID[" << i << "] = "
<< getNbr(closestNbrIndex)->ID;
cout << " | rel = " << r[0] << endl;
getNbr(closestNbrIndex)->relDesired = r[0];
++r;
}
else
{
cout << " -nbrID[" << i << "] = " << ID_NO_NBR << endl;
r.removeHead();
}
}
else ++r;
}
--ROSS--*/
/*--ROSS--
for (GLint i = 0; i < getNNbrs(); ++i)
{
closestDist = GLfloat(RAND_MAX);
closestRelIndex = -1;
if (getHead(currNbr))
{
for (GLint j = 0; j < r.getSize(); ++j)
{
Vector currRel;
if (r.getHead(currRel))
{
currDist = (getNbr(0)->relDesired - currRel).magnitude();
if (currDist < closestDist)
{
closestDist = currDist;
closestRelIndex = j;
}
}
++r;
}
cout << " -nbrID[" << i << "] = " << getNbr(0)->ID;
if ((closestRelIndex >= 0) && (closestRelIndex < r.getSize()))
{
cout << " | rel = " << r[closestRelIndex] << endl;
getNbr(0)->relDesired = r[closestRelIndex];
r.remove(closestRelIndex);
++(*this);
}
else
{
cout << " | [DELETING]" << endl;
removeHead();
}
}
else ++(*this);
}
//setNbrs(nh);
--ROSS--*/
}
Relationship rLeft,rRight;
if (leftNbr != NULL) leftNbr->relDesired = r[LEFT_NBR_INDEX];
if (rightNbr != NULL) rightNbr->relDesired = r[RIGHT_NBR_INDEX];
/*if(r.size()>2)
{
showNeighbors();
cout << "Exiting, too many neighbors." << endl;
exit(1);
}
for(int i=0;i<r.size();i++)
{
if(r[i].ID == leftNbr->ID)
{
rLeft = r[i];
}else if(r[i].ID== rightNbr->ID)
{
rRight = r[i];
}
}*/
/*if (leftNbr != NULL)
{
leftNbr->relDesired = r[lftNbrIndex];
}
if (rightNbr != NULL)
{
rightNbr->relDesired = r[rghtNbrIndex];
}*/
//cout << "Cell["<<ID<<"] has a gradient of " << gradient.magnitude()<< endl;
//if (leftNbr != NULL)cout << "just set leftNbr->relDesired to the Vector w/ = " << r[LEFT_NBR_INDEX].angle() << endl;
//if (rightNbr != NULL)cout << "just set rightNbr->relDesired to the Vector w/ angle = " << r[RIGHT_NBR_INDEX].angle() << endl;
//cout << endl;
//if (leftNbr != NULL) leftNbr->relDesired = nbrWithID(lftNbrID)->relDesired;//r[lftNbrID];
//if (rightNbr != NULL) rightNbr->relDesired = nbrWithID(rghtNbrID)->relDesired;//r[rghtNbrID];
return true;
} // changeFormation(const Formation &, Neighbor)
void Cell::setNbrIndex()
{
vector<Neighbor> n = getNbrs();
int r=-1,l=-1;
if(rightNbr)
{
r = rightNbr->ID;
}
if(leftNbr)
{
l = leftNbr->ID;
}
for(int i=0;i<n.size();i++)
{
if(n[i].ID==r)
{
rghtNbrIndex = i;
} else if (n[i].ID==l)
{
lftNbrIndex = i;
}
}
}
//
// bool sendStateToNbrs()
// Last modified: 27Aug2006
//
// Attempts to broadcast the state of the cell
// to the neighborhood of the cell, returning
// true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters: <none>
//
bool Cell::sendStateToNbrs()
{
Neighbor curr;
if(VERBOSE)printf("cellID=%d\n",getID());
for (GLint i = 0; i < getNNbrs(); ++i)
{
if(VERBOSE)printf("sending state to id= %d\n",getNbr(i)->ID);
//if((getNbr(i)->ID)==NULL
//if(VERBOSE)printf("
if (!sendState(getNbr(i)->ID))
{
//printf("sendState returned false\n");
return false;
}
}
//printf("leaving sendStateToNbrs()\n");
return true;
} // sendStateToNbrs()
//
// bool sendState(toID)
// Last modified: 27Aug2006
//
// Attempts to send the state of the cell
// to the neighbor with the parameterized ID,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// toID in the ID of the receiving neighbor
//
bool Cell::sendState(const GLint toID)
{
//printf("in sendState()\n");
State *s = new State(*this);
//printf("number of relations of %d is %d \n",toID,s->rels.getSize());
//printf("calling cell = %d\n", this->ID);
//printf("number of relations of %d is %d \n",this->ID,this->rels.getSize());
bool answer = sendMsg(s, toID, STATE);
//printf("leaving sendState()\n");
return answer;
} // sendState(const GLint)
//
// bool processPackets()
// Last modified: 30Nov2009
//
// Attempts to process all packets received by the cell,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters: <none>
//
bool Cell::processPackets()
{
bool success = true;
Packet p;
while (!msgQueue.empty())
{
p = msgQueue.front();
if (!processPacket(p)) success = false;
msgQueue.pop();
}
//cout << " done processing packets " << endl;
return success;
} // processPackets()
//
// bool processPacket(p)
// Last modified: 22Dec2006
//
// Attempts to process the parameterized packet,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// p in/out the packet to be processed
//
bool Cell::processPacket(Packet &p)
{
bool success = false;
if ((p.fromOperator()) && (p.type == CHANGE_FORMATION))
{
success = changeFormation(*((Formation *)p.msg));
}else if(p.type == PUSH_AUCTION_ANNOUNCEMENT)
{
if(ALLOW_CELL_BIDS)
{
}
success = true;
}
else if(p.type ==INSERTION_AUCTION_ANNOUNCEMENT)
{
insertion_auctions.push_back((Insertion_Auction_Announcement *)p.msg);
success = true;
}
else if(p.type == BID)
{
if(p.msg!=NULL)
{
bids.push_back((Bid*)p.msg);
success = true;
numBids++;
//cout << "bid received, total = " << numBids << endl;
}
}
else if(p.type == BUMP)
{
if(p.fromID == nbrWithMinGradient(this->gradient)->ID)
{
State* s = (State*)p.msg;
float a = s->gradient.magnitude() /(float) s->formation.radius;
this->gradient = (s->gradient *= (a+1/a));
int next = nbrWithMaxGradient(this->gradient)->ID;
if(next!=ID && next!=ID_NO_NBR) //if there exists another neighbor, bump it
{
env->sendMsg(&this->getState(), next,ID, BUMP);
}
}
}
else if(p.type == NEW_NEIGHBOR)
{
int i = *(int*)p.msg;
if(!addNbr(i))
{
success = false;
}
else
{
}
}
else if(p.type == DROP_NEIGHBOR)
{
int i = *(int*)p.msg;
if(!removeNbr(i))
{
success = false;
}
else
{
if(rightNbr->ID == i)
{
rightNbr = NULL;
}
else if(leftNbr->ID == i)
{
leftNbr = NULL;
}
}
}
else if ((isNbr(p.fromID)) || (p.fromBroadcast()))
{
switch(p.type)
{
case STATE:
//cout << "State update..." << endl;
success = (p.msg == NULL) ?
false : updateNbr(p.fromID, *((State *)p.msg));
delete (State *)p.msg;
p.msg = NULL;
break;
default: break;
}
}
return success;
} // processPacket(Packet &)
// <public primitive behaviors>
//
// Behavior moveError()
// Last modified: 07Nov2009
//
// Moves the robot cell using the current translational and
// rotational errors, activating and returning the appropriate
// robot behavior.
//
// Returns: the appropriate robot behavior
// Parameters: <none>
//
Behavior Cell::moveError()
{
return behavior = moveErrorBehavior(transError, rotError);
} // moveError()
//
// Behavior moveError(tError, rError)
// Last modified: 07Nov2009
//
// Moves the robot cell using the parameterized translational and
// rotational errors, activating and returning the appropriate
// robot behavior.
//
// Returns: the appropriate robot behavior
// Parameters:
// tError in the translational error
// rError in the rotational error
//
Behavior Cell::moveError(const Vector tError, const GLfloat rError)
{
return behavior = moveErrorBehavior(tError, rError);
} // moveError(const Vector, const GLfloat)
//
// Behavior moveErrorBehavior(tError, rError)
// Last modified: 07Nov2009
//
// Moves the robot using the parameterized translational and
// rotational errors, returning the appropriate robot behavior.
//
// Returns: the appropriate robot behavior
// Parameters:
// tError in the translational error
// rError in the rotational error
//
Behavior Cell::moveErrorBehavior(const Vector tError, const GLfloat rError)
{
if (transError.magnitude() > threshold())
return moveArc(transError);
else if (abs(rotError) > angThreshold())
return moveArc(0.0, degreesToRadians(-rotError));
return moveStop();
} // moveErrorBehavior(const Vector, const GLfloat)
// <virtual overloaded operators>
//
// Cell& =(s)
// Last modified: 27Aug2006
//
// Copies the contents of the parameterized state into this cell.
//
// Returns: this cell