-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_func.cpp
More file actions
992 lines (968 loc) · 36.7 KB
/
robot_func.cpp
File metadata and controls
992 lines (968 loc) · 36.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
#include "head/function.h"
// 按地图大小计算机器人数目和分配
void AllocateRobot()
{
// int robot_buy_link_to_berth[MAX_ROBOT_BUYING_NUM] = {0}; // 统计有多少港口跟购买点连通
// int buy_index = 0;
// for (buy_index = 0; buy_index < RobotBuyingNum; buy_index++)
// {
// if (buy_index == 0)
// {
// // 0号购买点直接bfs
// AreaBuying[buy_index] = CalculateArea(buy_index);
// Area += AreaBuying[buy_index];
// AllocateRobotNum[buy_index] = buy_index;
// }
// else
// {
// // 对其他的要看之前的购买点是否连通
// bool is_link = false; // 是否连通
// for (int pre_buy_index = 0; pre_buy_index < buy_index; pre_buy_index++)
// {
// // 之前的港口
// int bei = 0;
// for (bei = 0; bei < BerthNum; bei++)
// {
// // 根据跟港口的连通性判断
// if (BerthPathLength[bei][RobotBuyings[pre_buy_index].x][RobotBuyings[pre_buy_index].y] > 0)
// {
// // 之前连通
// if (BerthPathLength[bei][RobotBuyings[buy_index].x][RobotBuyings[buy_index].y] > 0)
// {
// // 我也连通
// is_link = true;
// break;
// }
// else
// {
// continue;
// }
// }
// }
// if (is_link)
// {
// // 有连通,相同面积
// AreaBuying[buy_index] = AreaBuying[pre_buy_index];
// AllocateRobotNum[buy_index] = pre_buy_index; // 分配数目跟bei相同
// break;
// }
// }
// if (!is_link)
// {
// // 无连通,重新bfs
// AreaBuying[buy_index] = CalculateArea(buy_index);
// Area += AreaBuying[buy_index];
// AllocateRobotNum[buy_index] = buy_index;
// }
// }
// }
// double a = 1.22508984e-04;
// double b = 4.74150604e-01;
// double c = 10.218760209081996;
// MAX_BUY_ROBOT_NUM = (int)ceil(a * (double)Area + b * (double)BerthNum + c);
// double a = 0.00011365;
// double b = 0.000231;
// double c = 11.663314621437582;
// MAX_BUY_ROBOT_NUM = (int)ceil(a * (double)Area + b * (double)(Area / BerthNum) + c);
if (Map == 1)
{
MAX_BUY_ROBOT_NUM = 16;
}
else if (Map == 2)
{
MAX_BUY_ROBOT_NUM = 16;
}
else
{
MAX_BUY_ROBOT_NUM = 16;
}
// double a = -9.35046108e-03;
// double b = 1.83585986e-07;
// double c = 127.90528015346925;
// MAX_BUY_ROBOT_NUM = (int)ceil(a * (double)Area + b * (double)(Area*Area) + c);
// // 开始分配
// for (buy_index = 0; buy_index < RobotBuyingNum; buy_index++){
// if (AllocateRobotNum[buy_index] == buy_index){
// int num = 0;
// for (int after_buy_index = buy_index; after_buy_index < RobotBuyingNum; after_buy_index++){
// // 统计之后几个人跟我连通
// if (AllocateRobotNum[after_buy_index] == buy_index){
// num++;
// }
// }
// AllocateRobotNum[buy_index] = (int)(MAX_BUY_ROBOT_NUM*((double)(AreaBuying[buy_index])/(double)(Area))/(double)(num));
// }
// else{
// AllocateRobotNum[buy_index] = AllocateRobotNum[AllocateRobotNum[buy_index]];
// }
// }
// 统计连通的港口数
// for (buy_index = 0; buy_index < RobotBuyingNum; buy_index++)
// {
// if (AllocateRobotNum[buy_index] == buy_index)
// {
// for (int bei = 0; bei < BerthNum; bei++)
// {
// if (BerthPathLength[bei][RobotBuyings[buy_index].x][RobotBuyings[buy_index].y] > 0)
// {
// robot_buy_link_to_berth[buy_index]++;
// }
// }
// }
// else
// {
// robot_buy_link_to_berth[buy_index] = robot_buy_link_to_berth[AllocateRobotNum[buy_index]];
// }
// }
//
// int rest_robot[MAX_ROBOT_BUYING_NUM] = {0}; // 分配的余数
// for (buy_index = 0; buy_index < RobotBuyingNum; buy_index++)
// {
// if (AllocateRobotNum[buy_index] == buy_index)
// {
// rest_robot[buy_index] = (int)((int)ceil(a * (double)AreaBuying[buy_index] + b * (double)(AreaBuying[buy_index] / robot_buy_link_to_berth[buy_index]) + c));
// }
// }
//
// // 开始分配
// for (buy_index = 0; buy_index < RobotBuyingNum; buy_index++)
// {
// if (AllocateRobotNum[buy_index] == buy_index)
// {
// int num = 0;
// for (int after_buy_index = buy_index; after_buy_index < RobotBuyingNum; after_buy_index++)
// {
// // 统计之后几个人跟我连通
// if (AllocateRobotNum[after_buy_index] == buy_index)
// {
// num++;
// }
// }
// AllocateRobotNum[buy_index] = (int)((int)ceil(a * (double)AreaBuying[buy_index] + b * (double)(AreaBuying[buy_index] / robot_buy_link_to_berth[buy_index]) + c) / (double)(num));
// rest_robot[buy_index] -= AllocateRobotNum[buy_index] * num;
// }
// else
// {
// int rest = 0; // 余数
// if (rest_robot[AllocateRobotNum[buy_index]] > 0)
// {
// rest = rest_robot[AllocateRobotNum[buy_index]];
// rest_robot[AllocateRobotNum[buy_index]] = 0;
// }
// AllocateRobotNum[buy_index] = AllocateRobotNum[AllocateRobotNum[buy_index]];
// AllocateRobotNum[buy_index] += rest;
// }
// }
}
// 没拿物品的机器人之间比较要拿物品的性价比
bool NoGoodsRobotsCompare(int ri, int rj)
{
int goodsi = Robots[ri].goods_index; // 机器人i要拿的物品
int goodsj = Robots[rj].goods_index; // 机器人j要拿的物品
// 考虑没拿物品的机器人本身没有匹配到物品(但是在移动)
if (goodsi < 0 && goodsj >= 0)
{ // i没有要拿的物品而j有,则j优先
return false;
}
else if (goodsi >= 0 && goodsj < 0)
{ // j没有要拿的物品而i有,则i优先
return true;
}
else if (goodsi < 0 && goodsj < 0)
{ // 都没有要拿的物品则,id大的优先
return ri > rj;
}
double ri_val = 0;
double rj_val = 0;
// if (RobotNum < MAX_BUY_ROBOT_NUM)
// {
// ri_val = (double)AllGoods[goodsi].val / (Robots[ri].goods_distance * TO_GOODS_WEIGHT + BerthPathLength[Robots[ri].berth_index][AllGoods[goodsi].x][AllGoods[goodsi].y]); // 机器人i要拿物品的性价比
// rj_val = (double)AllGoods[goodsj].val / (Robots[rj].goods_distance * TO_GOODS_WEIGHT + BerthPathLength[Robots[rj].berth_index][AllGoods[goodsj].x][AllGoods[goodsj].y]); // 机器人j要拿物品的性价比
ri_val = (double)AllGoods[goodsi].val; // 机器人i要拿物品的性价比
rj_val = (double)AllGoods[goodsj].val; // 机器人j要拿物品的性价比
// }
// else
// {
// double ri_val = (double)AllGoods[goodsi].val * ri_rest_time_weight / (Robots[ri].goods_distance * TO_GOODS_WEIGHT + BerthPathLength[Robots[ri].berth_index][AllGoods[goodsi].x][AllGoods[goodsi].y]); // 机器人i要拿物品的性价比
// double rj_val = (double)AllGoods[goodsj].val * rj_rest_time_weight / (Robots[rj].goods_distance * TO_GOODS_WEIGHT + BerthPathLength[Robots[rj].berth_index][AllGoods[goodsj].x][AllGoods[goodsj].y]); // 机器人j要拿物品的性价比
// }
if (ri_val >= rj_val)
{
return true;
}
else
{
return false;
}
}
// 都拿物品的机器人之间比较所拿物品的性价比
bool GetGoodsRobotsCompare(int ri, int rj)
{
int goodsi = Robots[ri].goods_index; // 机器人i拿的物品
int goodsj = Robots[rj].goods_index; // 机器人j拿的物品
// double ri_val = (double)AllGoods[goodsi].val / BerthPathLength[Robots[ri].berth_index][Robots[ri].x][Robots[ri].y]; // 机器人i物品的性价比
// double rj_val = (double)AllGoods[goodsj].val / BerthPathLength[Robots[rj].berth_index][Robots[rj].x][Robots[rj].y]; // 机器人j物品的性价比
double ri_val = (double)AllGoods[goodsi].val;
double rj_val = (double)AllGoods[goodsj].val;
if (ri_val >= rj_val)
{
return true;
}
else
{
return false;
}
}
// 机器人多线程BFS
void RobotBFSToGoods(int ri, priority_queue<Road, vector<Road>, Road::Comparator> &roads_pq, mutex &roads_pq_mutex)
{
int robot_x = Robots[ri].x;
int robot_y = Robots[ri].y;
// 定义BFS最短路径及长度,坐标值以及队列
vector<vector<int>> goods_path(200, vector<int>(200, -1));
vector<vector<int>> goods_path_length(200, vector<int>(200, -1));
queue<pair<int, int>> q;
int ri_road_num = 0;
q.push({robot_x, robot_y});
goods_path_length[robot_x][robot_y] = 0;
while (!q.empty())
{
// 从队列中取出一个点
pair<int, int> cur_pos = q.front();
q.pop();
int goods_id = IsOnGoods(cur_pos.first, cur_pos.second);
int cur_path_length = goods_path_length[cur_pos.first][cur_pos.second];
if (goods_id > 0)
{ // 如果现在的位置有物品,
// 先看到了之后它是否消失
if (cur_path_length < (1000 - (Frame - AllGoods[goods_id].fresh)))
{
// 计算最近港口和性价比,并将路径加入
if ((Robots[ri].goods_index == -1 && AllGoods[goods_id].robot_id == -1) || Robots[ri].goods_index == goods_id)
{ // 如果自己没东西要拿,并且现在找到一个没人要拿的 或者 找到了自己要拿的
int to_berth_index = -1;
double value = 0;
value = CalculateGoodsValue(ri, goods_id, cur_path_length, to_berth_index, 0);
{
lock_guard<mutex> lock(roads_pq_mutex); // 锁住互斥锁
roads_pq.push(Road(ri, goods_id, cur_path_length, to_berth_index, goods_path[cur_pos.first][cur_pos.second], value));
}
ri_road_num++;
if (Robots[ri].goods_index == goods_id)
{ // 如果这个机器人找到了自己要拿的 就不找了
break;
}
}
}
}
// 限制搜索的范围以控制时间
if (cur_path_length >= MAX_ROAD_LEN || ri_road_num > MAX_ROAD_NUM)
{
break;
}
// 四个方向随机遍历
for (int i = 0; i < 4; i++)
{ // 遍历到下一个点
int dir = (ri + i) % 4;
int nx = cur_pos.first + DX[dir];
int ny = cur_pos.second + DY[dir];
if (IsLandValid(nx, ny) && goods_path_length[nx][ny] < 0)
{ // 判断该点是否可以到达(没有越界&&为空地或者泊位&&之前没有到达过)
// 路径长度+1
goods_path_length[nx][ny] = cur_path_length + 1;
if (cur_path_length == 0)
{ // 记录路径的方向(只记录路径第一步的方向)
goods_path[nx][ny] = dir;
}
else
{
goods_path[nx][ny] = goods_path[cur_pos.first][cur_pos.second];
}
// 将该点加入队列
q.push({nx, ny});
}
}
}
}
// 机器人调度
void RobotDispatchGreedy()
{
vector<int> robots_match(RobotNum, 0); // 机器人是否匹配
set<int> goods_match; // 被匹配的商品
int robot_match_num = 0; // 被匹配的机器人数
for (int ri = 0; ri < RobotNum; ri++)
{ // 对于每个机器人
if ((Robots[ri].type == 0 && Robots[ri].is_goods == 1) || (Robots[ri].type == 1 && Robots[ri].is_goods == 2))
{ // 如果机器人已经装满拿着物品就直接找港口
int now_berth_id = IsOnBerth(Robots[ri].x, Robots[ri].y);
if (now_berth_id >= 0 && now_berth_id == Robots[ri].berth_index)
{ // TODO 如果到达港口,放下货物,继续找其他货物(要判断是不是自己要去的港口)
while (!Robots[ri].goods_stack.empty())
{
int goods_id = Robots[ri].goods_stack.top();
Robots[ri].goods_stack.pop();
RobotMoney += AllGoods[goods_id].val;
Robots[ri].action = 1;
Berths[Robots[ri].berth_index].goods_queue.push(goods_id); // 港口放入货物
Berths[Robots[ri].berth_index].total_goods_num++; // 港口放过的总货物数量
Robots[ri].goods_index = -1;
Robots[ri].goods_distance = MAX_LENGTH;
Robots[ri].is_goods = 0;
}
}
else
{ // 没到港口(或者不是自己要去的港口),沿着图走(先看有没有找到被标记的优先港口)
int berth_id = LastMinBerth(Robots[ri].x, Robots[ri].y);
if (berth_id == -1)
{ // 没标记
Robots[ri].dir = PsbDirToBerth(Robots[ri].berth_index, Robots[ri].x, Robots[ri].y);
}
else
{ // 有标记
Robots[ri].berth_index = berth_id; // 重置要去港口号
Robots[ri].dir = PsbDirToBerth(Robots[ri].berth_index, Robots[ri].x, Robots[ri].y); // 重置机器人方向
}
robot_match_num++;
robots_match[ri] = 1;
continue;
}
}
}
// 以上情况之外,其他机器人都要BFS找货物(使用多线程)
priority_queue<Road, vector<Road>, Road::Comparator> roads_pq; // 维护一个Road优先队列,每次找一条最有价值的路径匹配
mutex roads_pq_mutex; // 多线程互斥锁
vector<thread> robots_threads; // 线程
for (int ri = 0; ri < RobotNum; ri++)
{ // 对没有匹配的机器人进行BFS
if (robots_match[ri] == 0)
{
robots_threads.emplace_back(RobotBFSToGoods, ri, ref(roads_pq), ref(roads_pq_mutex));
}
}
// 等待所有线程结束
for (auto &thread : robots_threads)
{
thread.join();
}
// 开始机器人和物品的匹配
while (!roads_pq.empty())
{ // 价值最大的路径
if (robot_match_num >= RobotNum)
{
break;
}
Road road = roads_pq.top();
int ri = road.robot_index;
if (robots_match[ri] == 0 && goods_match.find(road.goods_index) == goods_match.end())
{ // 如果该机器人没被匹配并且该商品没被匹配,则匹配该机器人和商品
Robots[ri].goods_index = road.goods_index;
Robots[ri].berth_index = road.berth_index;
// if (Robots[ri].type == 1 && Robots[ri].is_goods == 1)
// { // 已经拿了一个货的不定港口
// road.berth_index = -1;
// }
Robots[ri].dir = road.next_dir;
Robots[ri].goods_distance = road.goods_distance;
// 如果该机器人在自己想要拿的物品上,就拿起并且朝港口走
int goods_id = IsOnGoods(Robots[ri].x, Robots[ri].y);
if (goods_id == Robots[ri].goods_index)
{
if ((Robots[ri].type == 1 && Robots[ri].is_goods == 1) || Robots[ri].type == 0)
{ // 如果拿满了,朝着港口走
Robots[ri].dir = PsbDirToBerth(Robots[ri].berth_index, Robots[ri].x, Robots[ri].y);
WorldGoods[Robots[ri].x][Robots[ri].y] = 0;
}
else
{ // 金牌机器人没拿满
// 已经拿了要拿的了,还没确定下次要拿的,只能停一帧再抉择
Robots[ri].goods_index = -1;
Robots[ri].dir = -1;
}
Robots[ri].action = 0;
Robots[ri].goods_stack.push(goods_id);
Robots[ri].is_goods++;
}
robot_match_num++;
robots_match[ri] = 1;
AllGoods[road.goods_index].robot_id = ri;
goods_match.insert(road.goods_index);
}
roads_pq.pop();
}
}
// 让位函数
bool CrashAvoid(int ri)
{
// ri优先让两边
int new_dir = Robots[ri].dir; // 新方向
if (new_dir == 0 || new_dir == 1)
{
// 当前方向是上或下,所以选择左或右
int nx1 = Robots[ri].x + DX[2]; // 向左的坐标
int ny1 = Robots[ri].y + DY[2];
int nx2 = Robots[ri].x + DX[3]; // 向右的坐标
int ny2 = Robots[ri].y + DY[3];
int nx3 = Robots[ri].x + DX[(new_dir + 1) % 2]; // 向后的坐标
int ny3 = Robots[ri].y + DY[(new_dir + 1) % 2];
if (IsLandValid(nx1, ny1) && IsLandValid(nx2, ny2))
{ // 两个方向都可行,随机选一个
new_dir = 2 + ri % 2; // 生成 2 或 3
}
else if (IsLandValid(nx1, ny1))
{
new_dir = 2; // 左
}
else if (IsLandValid(nx2, ny2))
{
new_dir = 3; // 右
}
else if (IsLandValid(nx3, ny3))
{
new_dir = (new_dir + 1) % 2; // 后退
}
else
{
return false;
}
}
else
{
// 当前方向是左或右,所以选择上或下
int nx1 = Robots[ri].x + DX[0]; // 向上的坐标
int ny1 = Robots[ri].y + DY[0];
int nx2 = Robots[ri].x + DX[1]; // 向下的坐标
int ny2 = Robots[ri].y + DY[1];
int nx3 = Robots[ri].x + DX[5 - new_dir]; // 向后的坐标
int ny3 = Robots[ri].y + DY[5 - new_dir];
if (IsLandValid(nx1, ny1) && IsLandValid(nx2, ny2))
{ // 两个方向都可行,随机选一个
new_dir = ri % 2; // 生成 0 或 1
}
else if (IsLandValid(nx1, ny1))
{
new_dir = 0; // 上
}
else if (IsLandValid(nx2, ny2))
{
new_dir = 1; // 下
}
else if (IsLandValid(nx3, ny3))
{ // 后退
new_dir = 5 - new_dir;
}
else
{
return false;
}
}
Robots[ri].dir = new_dir; // 改变ri方向
return true;
}
// 对冲让位函数
void HedgeAvoid(int ri, int rj, bool (&is_collision_robot)[MAX_ROBOT_NUM])
{
// 性价比低的机器人避让(优先让两边,实在不行往后面退,再不行就让性价比高的让)
if (Robots[ri].is_goods == Robots[ri].type+1 && Robots[rj].is_goods == Robots[rj].type+1)
{ // 都拿满
if ((is_collision_robot[ri] && is_collision_robot[rj]) || (!is_collision_robot[ri] && !is_collision_robot[rj]))
{ // 若都有/都没有碰撞过
if (GetGoodsRobotsCompare(ri, rj))
{ // 我拿的好
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 别人拿的好
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
else if (is_collision_robot[ri])
{ // 只有我有冲突过
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 只有他冲突过
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
else if (Robots[ri].is_goods < Robots[ri].type+1 && Robots[rj].is_goods < Robots[rj].type+1)
{ // 都没拿满物品
if ((is_collision_robot[ri] && is_collision_robot[rj]) || (!is_collision_robot[ri] && !is_collision_robot[rj]))
{ // 若都有/都没有碰撞过
if (NoGoodsRobotsCompare(ri, rj))
{ // 我拿的好
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 别人拿的好
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
else if (is_collision_robot[ri])
{ // 只有我有冲突过
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 只有他冲突过
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
else if (Robots[ri].is_goods < Robots[ri].type+1 && Robots[rj].is_goods == Robots[rj].type+1)
{ // 我没拿满,别人拿了,我让,ri优先让两边
if ((is_collision_robot[ri] && is_collision_robot[rj]) || (!is_collision_robot[ri] && !is_collision_robot[rj]))
{ // 若都有/都没有碰撞过
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
else if (is_collision_robot[ri])
{ // 只有我有冲突过
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 只有他冲突过
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
else
{ // 别人没拿满,我拿了,别人让
if ((is_collision_robot[ri] && is_collision_robot[rj]) || (!is_collision_robot[ri] && !is_collision_robot[rj]))
{ // 若都有/都没有碰撞过
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else if (is_collision_robot[ri])
{ // 只有我有冲突过
if (!CrashAvoid(rj))
{
CrashAvoid(ri);
is_collision_robot[ri] = true;
}
else
{
is_collision_robot[rj] = true;
}
}
else
{ // 只有他冲突过
if (!CrashAvoid(ri))
{
CrashAvoid(rj);
is_collision_robot[rj] = true;
}
else
{
is_collision_robot[ri] = true;
}
}
}
}
// 抢位让位函数
void RushPositionAvoid(int ri, int rj, bool (&is_collision_robot)[MAX_ROBOT_NUM])
{
if ((is_collision_robot[ri] && is_collision_robot[rj]) || (!is_collision_robot[ri] && !is_collision_robot[rj]))
{ // 若都有/都没有碰撞过
// 有货物的优先走,另一个停下
if (Robots[ri].is_goods && !Robots[rj].is_goods)
{
Robots[rj].dir = -1;
is_collision_robot[rj] = true;
}
else if (Robots[rj].is_goods && !Robots[ri].is_goods)
{
Robots[ri].dir = -1;
is_collision_robot[ri] = true;
}
// 都有货物或者都没有货物则比较性价比
else if (Robots[ri].is_goods && Robots[rj].is_goods)
{
if (GetGoodsRobotsCompare(ri, rj))
{ // 我拿的好,别人停
Robots[rj].dir = -1;
is_collision_robot[rj] = true;
}
else
{ // 别人拿的好,我停
Robots[ri].dir = -1;
is_collision_robot[ri] = true;
}
}
else
{
if (NoGoodsRobotsCompare(ri, rj))
{ // 我要拿的好,别人停
Robots[rj].dir = -1;
is_collision_robot[rj] = true;
}
else
{ // 别人要拿的好,我停
Robots[ri].dir = -1;
is_collision_robot[ri] = true;
}
}
}
else if (is_collision_robot[ri])
{ // 只有我有冲突过
Robots[rj].dir = -1;
is_collision_robot[rj] = true;
}
else
{ // 只有他有冲突过
Robots[ri].dir = -1;
is_collision_robot[ri] = true;
}
}
// 碰撞检测与规避
void AvoidCollision()
{
bool is_collision = true; // 本次是否有碰撞
int detect_num = 0; // 检测的次数
bool is_collision_robot[MAX_ROBOT_NUM] = {false}; // 对每个机器人判断本轮是否有冲突
while (is_collision)
{
detect_num++;
is_collision = false;
// 对每个机器人进行碰撞检测
for (int ri = 0; ri < RobotNum; ri++)
{
// 如果这个机器人没有被困死并且下一步会移动(对于每个机器人只考虑主动碰撞,如果是被碰,会被其他机器人主动碰)
if (Robots[ri].dir >= 0)
{
// 机器人i下一步的位置
int nx_ri = Robots[ri].x + DX[Robots[ri].dir];
int ny_ri = Robots[ri].y + DY[Robots[ri].dir];
// 若在主干道上,就不管
if (IsOnMainRoad(nx_ri, ny_ri))
{
continue;
}
// 检测除自己之外其他机器人会不会在这个位置上(不移动的机器人看当前位置,会移动的机器人分析是对冲还是抢位置)
for (int rj = 0; rj < RobotNum; rj++)
{
if (ri != rj)
{
// 碰上不移动的机器人j
if (Robots[rj].dir < 0 && Robots[rj].x == nx_ri && Robots[rj].y == ny_ri)
{
// 若不动的在主干道
if (IsOnMainRoad(Robots[rj].x, Robots[rj].y))
{
continue;
}
is_collision = true;
// 若不动的只是在罚站,他垂直移动
if (is_collision_robot[ri] && is_collision_robot[rj])
{ // 若都有碰撞过
CrashAvoid(rj); // 他垂直移动
is_collision_robot[rj] = true;
}
else if (is_collision_robot[ri])
{ // 若我有碰撞过
CrashAvoid(rj); // 他垂直移动
is_collision_robot[rj] = true;
}
else if (is_collision_robot[rj])
{ // 若他有碰撞过
CrashAvoid(ri); // 我垂直移动
is_collision_robot[ri] = true;
}
else
{ // 若都没有碰撞过
CrashAvoid(rj); // 他垂直移动
is_collision_robot[rj] = true;
}
break;
}
// 碰上移动的机器人j
if (Robots[rj].dir >= 0)
{
int nx_rj = Robots[rj].x + DX[Robots[rj].dir];
int ny_rj = Robots[rj].y + DY[Robots[rj].dir];
// 对冲
if (Robots[rj].x == nx_ri && Robots[rj].y == ny_ri && Robots[ri].x == nx_rj && Robots[ri].y == ny_rj)
{
is_collision = true;
// 若他下一步要进我主干道
if (IsOnMainRoad(nx_rj, ny_rj))
{
Robots[ri].dir = -1;
break;
}
HedgeAvoid(ri, rj, is_collision_robot);
break;
}
// 抢位
if (nx_ri == nx_rj && ny_ri == ny_rj)
{
is_collision = true;
// 判断中间有没有机器人
int rk = 0;
for (rk = 0; rk < RobotNum; rk++)
{
if (Robots[rk].x == nx_ri && Robots[rk].y == ny_ri)
{
break;
}
}
if (rk < RobotNum)
{ // 有机器人
int nx_rk = Robots[rk].x + DX[Robots[rk].dir]; // rk下一步位置
int ny_rk = Robots[rk].y + DY[Robots[rk].dir];
if (Robots[rk].x == nx_ri && Robots[rk].y == ny_ri && Robots[ri].x == nx_rk && Robots[ri].y == ny_rk)
{ // k和i对冲
HedgeAvoid(ri, rk, is_collision_robot);
}
else if (Robots[rk].x == nx_rj && Robots[rk].y == ny_rj && Robots[rj].x == nx_rk && Robots[rj].y == ny_rk)
{ // k和j对冲
HedgeAvoid(rj, rk, is_collision_robot);
}
else
{ // k都不对冲,判断i,j抢位
RushPositionAvoid(ri, rj, is_collision_robot);
}
}
else
{ // 没机器人,判断i,j抢位
RushPositionAvoid(ri, rj, is_collision_robot);
}
break;
}
}
}
}
// 如果有碰撞并且规避了,就跳出,需要重新检测碰撞
if (is_collision)
{
break;
}
}
}
// 检测100次就不检测了
if (detect_num >= 100)
{
break;
}
}
}
// 打印机器人指令
void PrintRobotsIns()
{
// move:0:右,1:左,2:上,3:下
for (int i = 0; i < RobotNum; i++)
{
if (Robots[i].action >= 0)
{
if (Robots[i].action == 0)
{ // get
printf("get %d\n", i);
}
else if (Robots[i].action == 1)
{ // pull
printf("pull %d\n", i);
}
}
if (Robots[i].dir == 0)
{ // 上
printf("move %d 2\n", i);
}
else if (Robots[i].dir == 1)
{ // 下
printf("move %d 3\n", i);
}
else if (Robots[i].dir == 2)
{ // 左
printf("move %d 1\n", i);
}
else if (Robots[i].dir == 3)
{ // 右
printf("move %d 0\n", i);
}
}
}
// 买一个机器人
void BuyARobot(int robot_buying_index, int type)
{
printf("lbot %d %d %d\n", RobotBuyings[robot_buying_index].x, RobotBuyings[robot_buying_index].y, type); // 输出指令
Robots[RobotNum] = Robot(RobotBuyings[robot_buying_index].x, RobotBuyings[robot_buying_index].y, type); // 机器人的初始化
RobotNum++;
Money -= ROBOT_BUY_MONEY[type]; // 花钱
OurMoney -= ROBOT_BUY_MONEY[type]; // 花钱
if (RobotNum >= MAX_BUY_ROBOT_NUM)
{
// 清除所有港口的聚焦
for (int i = 0; i < BerthNum; i++)
{
// Berths[i].focus = 0;
}
}
}
// 购买机器人,用最大数量以及船的数量来限制,能买就买 (xmc)
int BuyRobotsXmc()
{
int buy = 0;
if (Frame == 1)
{
for (int i = 0; i < RobotBuyingNum; i++)
{
for (int j = 0; j < InitRobotType0ToBuy[i]; j++)
{
if (InitRobotType0ToBuy[j] == 0)
break;
BuyARobot(i, 0);
buy = 1;
}
for (int j = 0; j < InitRobotType1ToBuy[i]; j++)
{
if (InitRobotType1ToBuy[j] == 0)
break;
BuyARobot(i, 1);
buy = 1;
}
}
}
else
{ // 后续购买
int type = 0;
if (Money < ROBOT_BUY_MONEY[type] || RobotNum >= MAX_BUY_ROBOT_NUM)
{
return buy;
}
else
{ // 要买
int goods_stack = MAX_GOODS_NUM; // 货物堆积最少处的最近位置买
int robot_buy_index;
// 对每个港口
for (int bi = 0; bi < BerthNum; bi++)
{
if (BerthNearestBuying[bi] < 0)
{ // 船不可达的港口不去
continue;
}
else
{ // 船可达的港口
int length_ = MAX_LENGTH;
// 对每个机器人购买点
for (int rbi = 0; rbi < RobotBuyingNum; rbi++)
{
int x = RobotBuyings[rbi].x;
int y = RobotBuyings[rbi].y;
if (BerthPathLength[bi][x][y] < 0)
{ // 机器人不可达
continue;
}
else
{
// if (AllocateRobotNum[rbi] <= 0)
// {
// // 不该在该港口买了
// continue;
// }
if (goods_stack >= Berths[bi].goods_queue.size() && length_ >= BerthPathLength[bi][x][y])
{
goods_stack = (int)Berths[bi].goods_queue.size();
length_ = BerthPathLength[bi][x][y];
robot_buy_index = rbi;
}
}
}
}
}
// AllocateRobotNum[robot_buy_index]--;
BuyARobot(robot_buy_index, type);
buy = 1;
}
}
return buy;
}