-
Notifications
You must be signed in to change notification settings - Fork 26
/
actions.h
1480 lines (1360 loc) · 48.9 KB
/
actions.h
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
#ifndef BWGAME_ACTIONS_H
#define BWGAME_ACTIONS_H
#include "bwgame.h"
namespace bwgame {
struct action_state {
action_state() = default;
action_state(const action_state&) = delete;
action_state(action_state&&) = default;
action_state& operator=(const action_state&) = delete;
action_state& operator=(action_state&&) = default;
std::array<int, 12> player_id{};
size_t actions_data_position = 0;
int next_action_frame = 0;
std::array<static_vector<unit_t*, 12>, 8> selection{};
std::array<std::array<static_vector<unit_id, 12>, 10>, 8> control_groups{};
};
static inline action_state copy_state(const action_state& action_st, const state& source_st, const state& dest_st) {
action_state r;
r.player_id = action_st.player_id;
r.actions_data_position = action_st.actions_data_position;
r.next_action_frame = action_st.next_action_frame;
r.selection = action_st.selection;
for (auto& v : r.selection) {
for (auto& v2 : v) {
v2 = const_cast<unit_t*>(dest_st.units_container.at(v2->index));
}
}
r.control_groups = action_st.control_groups;
return r;
}
struct action_functions: state_functions {
action_state& action_st;
explicit action_functions(state& st, action_state& action_st) : state_functions(st), action_st(action_st) {}
bool unit_can_be_multi_selected(const unit_t* u) const {
if (ut_building(u)) return false;
if (ut_flag(u, (unit_type_t::flags_t)0x800)) return false;
if (unit_is_disabled(u)) return false;
auto tid = u->unit_type->id;
if (tid >= UnitTypes::Special_Floor_Missile_Trap && tid <= UnitTypes::Special_Right_Wall_Flame_Trap) return false;
if (tid == UnitTypes::Terran_Vulture_Spider_Mine) return false;
if (tid == UnitTypes::Zerg_Egg) return false;
if (tid == UnitTypes::Critter_Rhynadon) return false;
if (tid == UnitTypes::Critter_Bengalaas) return false;
if (tid == UnitTypes::Critter_Scantid) return false;
if (tid == UnitTypes::Critter_Kakaru) return false;
if (tid == UnitTypes::Critter_Ragnasaur) return false;
if (tid == UnitTypes::Critter_Ursadon) return false;
if (tid == UnitTypes::Spell_Dark_Swarm) return false;
if (tid == UnitTypes::Spell_Disruption_Web) return false;
return true;
}
auto selected_units(int owner) const {
return make_filter_range(action_st.selection.at(owner), [this](unit_t* u) {
return u && !unit_dead(u);
});
}
auto control_group(int owner, size_t group_n) const {
return make_filter_range(make_transform_range(action_st.control_groups.at(owner).at(group_n), [this](unit_id u) {
return get_unit(u);
}), [](unit_t* u) {
return u != nullptr;
});
}
unit_t* get_first_selected_unit(int owner) const {
for (unit_t* u : selected_units(owner)) {
return u;
}
return nullptr;
}
unit_t* get_single_selected_unit(int owner) const {
auto sel = selected_units(owner);
if (sel.empty()) return nullptr;
auto i = sel.begin();
if (std::next(i) != sel.end()) return nullptr;
return *i;
}
virtual void on_unit_deselect(unit_t* u) override {
for (size_t i = 0; i != 8; ++i) {
auto& selection = action_st.selection.at(i);
auto it = std::find(selection.begin(), selection.end(), u);
if (it != selection.end()) selection.erase(it);
}
}
struct group_move_t {
xy original_target_pos;
xy target_pos;
xy move_offset;
bool has_move_offset;
bool target_is_in_unit_bounds;
};
void calc_group_move(group_move_t& g, int owner, xy target_pos, bool can_be_obstructed) {
g.original_target_pos = target_pos;
g.has_move_offset = false;
g.target_is_in_unit_bounds = false;
rect area{{std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}, {0, 0}};
bool any_collision_enabled_units = false;
size_t n_units = 0;
xy sum_pos;
unsigned units_area = 0;
for (const unit_t* u : selected_units(owner)) {
++n_units;
if (u->pathing_flags & 1) any_collision_enabled_units = true;
xy pos = u->sprite->position;
if (pos.x < area.from.x) area.from.x = pos.x;
if (pos.y < area.from.y) area.from.y = pos.y;
if (pos.x > area.to.x) area.to.x = pos.x;
if (pos.y > area.to.y) area.to.y = pos.y;
sum_pos += pos;
int w = u->unit_type->dimensions.from.x + u->unit_type->dimensions.to.x + 1;
int h = u->unit_type->dimensions.from.y + u->unit_type->dimensions.to.y + 1;
units_area += w * h;
}
if (n_units == 0) return;
if (any_collision_enabled_units && can_be_obstructed) {
auto find_target = [&](rect bb, xy source_pos) {
auto* source_region = get_region_at_prefer_walkable(source_pos);
auto* target_region = get_region_at_prefer_walkable(target_pos);
if (!is_walkable(target_pos) || source_region->group_index != target_region->group_index) {
pathfinder pf;
if (pathfinder_find_long_path(pf, source_pos, target_pos)) target_region = pf.long_path.back();
}
target_pos = pathfinder_adjust_destination(target_region, target_pos);
target_pos = pathfinder_adjust_target_pos(bb, target_pos).second;
};
if (n_units == 1) {
unit_t* u = get_first_selected_unit(owner);
if (!unit_type_can_fit_at(u->unit_type, target_pos)) {
const unit_t* u = *selected_units(owner).begin();
find_target(unit_type_inner_bounding_box(u->unit_type), u->sprite->position);
}
} else {
int units_area_square_width = isqrt(units_area * 2);
if (!rectangle_can_fit_at(target_pos, units_area_square_width, units_area_square_width)) {
rect bb;
bb.from.x = -units_area_square_width / 2;
bb.from.y = -units_area_square_width / 2;
bb.to.x = bb.from.x + units_area_square_width;
bb.to.y = bb.from.y + units_area_square_width;
find_target(bb, sum_pos / (int)n_units);
}
}
target_pos = restrict_pos_to_map_bounds(target_pos);
}
if (n_units >= 2) {
if (is_in_inner_bounds(g.original_target_pos, area)) {
g.target_is_in_unit_bounds = true;
g.move_offset = {};
} else if (any_collision_enabled_units) {
if (area.to.x - area.from.x <= 192 && area.to.y - area.from.y <= 192) {
g.move_offset = g.original_target_pos - sum_pos / (int)n_units;
g.has_move_offset = true;
}
} else {
if (area.to.x - area.from.x <= 256 && area.to.y - area.from.y <= 256) {
g.move_offset = g.original_target_pos - sum_pos / (int)n_units;
g.has_move_offset = true;
}
}
}
g.target_pos = target_pos;
}
xy get_group_move_pos(const unit_t* u, xy pos, const group_move_t& g) const {
xy target_pos = u->pathing_flags & 1 ? g.target_pos : g.original_target_pos;
if (g.target_is_in_unit_bounds) {
pos = u->sprite->position;
if (pos.x <= target_pos.x - 32) {
pos.x = target_pos.x - 32;
} else if (pos.x >= target_pos.x + 32) {
pos.x = target_pos.x + 32;
}
if (pos.y <= target_pos.y - 32) {
pos.y = target_pos.y - 32;
} else if (pos.y >= target_pos.y + 32) {
pos.y = target_pos.y + 32;
}
} else {
if (g.has_move_offset) {
pos.x = u->sprite->position.x + g.move_offset.x;
pos.y = u->sprite->position.y + g.move_offset.y;
}
}
pos = restrict_move_target_to_valid_bounds(u, pos);
if (~u->pathing_flags & 1) return pos;
auto* source_region = get_region_at(target_pos);
if (unit_type_can_fit_at(u->unit_type, pos)) {
auto* pos_region = get_region_at(pos);
if (pos_region == source_region) return pos;
for (auto* n : source_region->walkable_neighbors) {
if (n == pos_region) return pos;
}
}
int distance_to_target = xy_length(target_pos - pos);
if (distance_to_target == 0) distance_to_target = 1;
int step_distance = std::min(distance_to_target / 4, 16);
if (step_distance < 2) step_distance = distance_to_target;
for (int distance = step_distance; distance <= distance_to_target; distance += step_distance) {
xy npos = pos + (target_pos - pos) * distance / distance_to_target;
if (unit_type_can_fit_at(u->unit_type, npos)) {
auto* npos_region = get_region_at(npos);
if (npos_region == source_region) return npos;
for (auto* n : source_region->walkable_neighbors) {
if (n == npos_region) return npos;
}
}
}
return target_pos;
}
bool morph_archon_impl(int owner, bool is_dark_archon) {
auto sel = selected_units(owner);
auto i = sel.begin();
if (i == sel.end()) return false;
if (!unit_can_use_tech(*i, get_tech_type(is_dark_archon ? TechTypes::Dark_Archon_Meld : TechTypes::Archon_Warp), owner)) return false;
bool retval = false;
static_vector<unit_t*, 12> units;
for (;i != sel.end(); ++i) {
unit_t* u = *i;
if (unit_is(u, is_dark_archon ? UnitTypes::Protoss_Dark_Templar : UnitTypes::Protoss_High_Templar)) units.push_back(u);
}
if (units.size() < 2) return false;
for (size_t index = 0; index != units.size(); ++index) {
unit_t* u = units[index];
if (!u) continue;
int nearest_distance = std::numeric_limits<int>::max();
unit_t* nearest_unit = nullptr;
for (size_t index2 = index + 1; index2 != units.size(); ++index2) {
unit_t* u2 = units[index2];
if (!u2) continue;
xy relpos = u->sprite->position - u2->sprite->position;
int d = relpos.x * relpos.x + relpos.y * relpos.y;
if (d < nearest_distance) {
units[index2] = nearest_unit;
nearest_distance = d;
nearest_unit = u2;
}
}
if (!nearest_unit) continue;
set_unit_order(u, get_order_type(is_dark_archon ? Orders::DarkArchonMeld : Orders::ArchonWarp), nearest_unit);
set_unit_order(nearest_unit, get_order_type(is_dark_archon ? Orders::DarkArchonMeld : Orders::ArchonWarp), u);
retval = true;
}
return retval;
}
template<typename units_T>
bool action_select(int owner, units_T&& units) {
auto& selection = action_st.selection.at(owner);
selection.clear();
bool retval = false;
for (unit_t* u : units) {
if (u && u->unit_type->id != UnitTypes::Terran_Nuclear_Missile) {
if (std::find(selection.begin(), selection.end(), u) == selection.end()) {
if (!us_hidden(u) && (selection.empty() || unit_can_be_multi_selected(u))) {
if (selection.size() == 12) error("action_select: attempt to select more than 12 units");
selection.push_back(u);
retval = true;
}
}
}
}
return retval;
}
bool action_select(int owner, unit_t* u) {
return action_select(owner, std::array<unit_t*, 1>{u});
}
template<typename units_T>
bool action_shift_select(int owner, units_T&& units) {
auto& selection = action_st.selection.at(owner);
if (selection.size() + units.size() > 12) return false;
bool retval = false;
for (unit_t* u : units) {
if (u) {
if (std::find(selection.begin(), selection.end(), u) == selection.end()) {
if (!us_hidden(u) && (selection.empty() || unit_can_be_multi_selected(u))) {
if (selection.size() == 12) error("action_shift_select: attempt to select more than 12 units");
selection.push_back(u);
retval = true;
}
}
}
}
return retval;
}
bool action_shift_select(int owner, unit_t* u) {
return action_shift_select(owner, std::array<unit_t*, 1>{u});
}
template<typename units_T>
bool action_deselect(int owner, units_T&& units) {
auto& selection = action_st.selection.at(owner);
bool retval = false;
for (unit_t* u : units) {
if (u) {
auto i = std::find(selection.begin(), selection.end(), u);
if (i != selection.end()) {
selection.erase(i);
retval = true;
}
}
}
return retval;
}
bool action_deselect(int owner, unit_t* u) {
return action_deselect(owner, std::array<unit_t*, 1>{u});
}
bool action_train(int owner, const unit_type_t* unit_type) {
unit_t* u = get_single_selected_unit(owner);
if (!u) return false;
if (u->owner != owner) return false;
if (!unit_can_build(u, unit_type)) return false;
if (unit_type->id > UnitTypes::Spell_Disruption_Web) return false;
if (!build_queue_push(u, unit_type)) return false;
set_secondary_order(u, get_order_type(Orders::Train));
return true;
}
bool action_build(int owner, const order_type_t* order_type, const unit_type_t* unit_type, xy_t<size_t> tile_pos) {
unit_t* u = get_single_selected_unit(owner);
if (!u) return false;
if (u->owner != owner) return false;
if (!unit_build_order_valid(u, order_type, unit_type, owner)) return false;
if (ut_addon(unit_type)) {
xy pos(int(32 * tile_pos.x) + unit_type->placement_size.x / 2, int(32 * tile_pos.y) + unit_type->placement_size.y / 2);
if (can_place_building(u, owner, unit_type, pos, false, false)) {
xy builder_pos(int(32 * tile_pos.x + u->unit_type->placement_size.x / 2), int(32 * tile_pos.y + u->unit_type->placement_size.y / 2));
builder_pos.x -= unit_type->addon_position.x / 32 * 32;
builder_pos.y -= unit_type->addon_position.y / 32 * 32;
if (can_place_building(u, owner, u->unit_type, builder_pos, false, false)) {
place_building(u, order_type, unit_type, builder_pos);
}
}
} else {
xy pos(int(32 * tile_pos.x) + unit_type->placement_size.x / 2, int(32 * tile_pos.y) + unit_type->placement_size.y / 2);
if (can_place_building(u, owner, unit_type, pos, false, false)) {
place_building(u, order_type, unit_type, pos);
}
}
return true;
}
bool action_default_order(int owner, xy pos, unit_t* target, const unit_type_t* target_unit_type, bool queue) {
if (!is_in_map_bounds(pos)) return false;
if (target) target_unit_type = nullptr;
else if (target_unit_type) {
if (player_position_is_visible(owner, pos)) {
target = find_unit_noexpand({pos - xy(96, 96), pos + xy(96, 96)}, [&](unit_t* n) {
if (n->unit_type != target_unit_type) return false;
if (n->sprite->position.x + n->unit_type->placement_size.x / 2 - (unsigned)pos.x >= (unsigned)n->unit_type->placement_size.x) return false;
if (n->sprite->position.y + n->unit_type->placement_size.y / 2 - (unsigned)pos.y >= (unsigned)n->unit_type->placement_size.y) return false;
return true;
});
target_unit_type = nullptr;
}
}
group_move_t g;
bool has_calculated_group_move = false;
bool retval = false;
for (unit_t* u : selected_units(owner)) {
const order_type_t* order = get_default_order(default_action(u), u, pos, target, target_unit_type);
if (order) {
if (order->id == Orders::RallyPointUnit) {
if (unit_is_factory(u)) {
retval = true;
if (!target) target = u;
u->building.rally.unit = target;
u->building.rally.pos = target->sprite->position;
}
} else if (target != u) {
if (order->id == Orders::RallyPointTile) {
if (unit_is_factory(u)) {
retval = true;
u->building.rally.unit = nullptr;
u->building.rally.pos = pos;
}
} else {
if (order->id == Orders::AttackDefault) {
if (u->subunit) {
issue_order(u->subunit, queue, u->subunit->unit_type->attack_unit, target);
}
order = u->unit_type->attack_unit;
}
if (unit_can_receive_order(u, order, owner)) {
retval = true;
if (target) {
order_target_t order_target;
order_target.unit = target;
issue_order(u, queue, order, order_target);
} else {
order_target_t order_target;
order_target.position = pos;
order_target.unit_type = target_unit_type;
if (!target_unit_type) {
if (!has_calculated_group_move) {
has_calculated_group_move = true;
calc_group_move(g, owner, pos, true);
}
order_target.position = get_group_move_pos(u, pos, g);
}
issue_order(u, queue, order, order_target);
}
u->user_action_flags |= 2;
}
}
}
}
}
return retval;
}
bool action_order(int owner, const order_type_t* input_order, xy pos, unit_t* target, const unit_type_t* target_unit_type, bool queue) {
if (!is_in_map_bounds(pos)) return false;
switch (input_order->id) {
case Orders::Die:
case Orders::InfestedCommandCenter:
case Orders::SpiderMine:
case Orders::DroneStartBuild:
case Orders::InfestingCommandCenter:
case Orders::PlaceBuilding:
case Orders::PlaceProtossBuilding:
case Orders::CreateProtossBuilding:
case Orders::ConstructingBuilding:
case Orders::PlaceAddon:
case Orders::BuildNydusExit:
case Orders::BuildingLand:
case Orders::BuildingLiftoff:
case Orders::DroneLiftOff:
case Orders::Harvest2:
case Orders::MoveToGas:
case Orders::WaitForGas:
case Orders::HarvestGas:
case Orders::MoveToMinerals:
case Orders::WaitForMinerals:
case Orders::MiningMinerals:
case Orders::GatheringInterrupted:
case Orders::GatherWaitInterrupted:
case Orders::CTFCOP2:
return false;
default:
break;
}
bool can_be_obstructed = input_order->can_be_obstructed;
group_move_t g;
bool has_calculated_group_move = false;
bool retval = false;
for (unit_t* u : selected_units(owner)) {
const order_type_t* order = input_order;
if (order->tech_type == TechTypes::None) {
if (!unit_can_receive_order(u, order, owner)) continue;
} else {
if (!unit_can_use_tech(u, get_tech_type(order->tech_type), owner)) continue;
}
if (u == target) {
if (!unit_order_can_target_self(u, order)) continue;
}
if (u->order_type->id == Orders::NukeLaunch) continue;
if (default_action(u) == 0) {
auto oid = order->id;
if (oid != Orders::RallyPointUnit && oid != Orders::RallyPointTile && oid != Orders::RechargeShieldsBattery && oid != Orders::PickupBunker) continue;
}
if (unit_is(u, UnitTypes::Terran_Medic)) {
if (order->id == Orders::AttackMove || order->id == Orders::AttackDefault) {
order = get_order_type(Orders::HealMove);
}
}
const order_type_t* subunit_order = get_order_type(Orders::Nothing);
if (order->id == Orders::AttackDefault) {
if (target) order = u->unit_type->attack_unit;
else order = u->unit_type->attack_move;
if (order->id == Orders::Nothing) continue;
if (u->subunit) {
if (target) subunit_order = u->subunit->unit_type->attack_unit;
else subunit_order = u->subunit->unit_type->attack_move;
}
}
if (order->id == Orders::RallyPointUnit) {
if (unit_is_factory(u)) {
retval = true;
if (!target) target = u;
u->building.rally.unit = target;
u->building.rally.pos = target->sprite->position;
}
u->user_action_flags |= 2;
} else if (order->id == Orders::RallyPointTile) {
if (unit_is_factory(u)) {
retval = true;
u->building.rally.unit = nullptr;
u->building.rally.pos = pos;
}
u->user_action_flags |= 2;
} else {
retval = true;
if (target) {
order_target_t order_target;
order_target.unit = target;
issue_order(u, queue, order, order_target);
if (subunit_order->id != Orders::Nothing) issue_order(u->subunit, queue, subunit_order, order_target);
} else {
order_target_t order_target;
order_target.position = pos;
order_target.unit_type = target_unit_type;
if (!target_unit_type) {
if (!has_calculated_group_move) {
has_calculated_group_move = true;
calc_group_move(g, owner, pos, can_be_obstructed);
}
order_target.position = get_group_move_pos(u, pos, g);
}
issue_order(u, queue, order, order_target);
if (subunit_order->id != Orders::Nothing) issue_order(u->subunit, queue, subunit_order, order_target);
}
u->user_action_flags |= 2;
}
}
return retval;
}
bool action_stop(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (unit_can_receive_order(u, get_order_type(Orders::Stop), owner)) {
retval = true;
issue_order(u, queue, get_order_type(Orders::Stop), {});
}
}
return retval;
}
bool action_cancel_building_unit(int owner) {
unit_t* u = get_single_selected_unit(owner);
if (!u || u->owner != owner) return false;
cancel_building_unit(u);
return true;
}
bool action_cancel_build_queue(int owner, size_t slot) {
unit_t* u = get_single_selected_unit(owner);
if (!u || u->owner != owner) return false;
if (!u_completed(u)) return false;
if (unit_is(u, UnitTypes::Zerg_Larva)) return false;
if (unit_is(u, UnitTypes::Zerg_Mutalisk)) return false;
if (unit_is(u, UnitTypes::Zerg_Hydralisk)) return false;
if (unit_is(u, UnitTypes::Zerg_Hatchery)) return false;
if (unit_is(u, UnitTypes::Zerg_Lair)) return false;
if (unit_is(u, UnitTypes::Zerg_Spire)) return false;
if (unit_is(u, UnitTypes::Zerg_Creep_Colony)) return false;
if (u->build_queue.empty()) return false;
if (slot == 254) slot = u->build_queue.size() - 1;
if (slot >= u->build_queue.size()) return false;
cancel_build_queue(u, slot);
return true;
}
bool action_control_group(int owner, size_t group_n, int subaction) {
if (group_n >= 10) return false;
bool retval = false;
if (subaction == 1) {
auto& group = action_st.control_groups.at(owner).at(group_n);
if (!group.empty()) {
auto& selection = action_st.selection.at(owner);
selection.clear();
for (size_t i = 0; i != group.size(); ++i) {
unit_t* u = get_unit(group[i]);
if (u && !us_hidden(u) && u->owner == owner && (group.size() == 1 || unit_can_be_multi_selected(u))) {
selection.push_back(u);
retval = true;
} else {
if (i != group.size() - 1) std::swap(group[i], group[group.size() - 1]);
group.pop_back();
--i;
}
}
}
} else if (subaction == 0 || subaction == 2) {
auto& group = action_st.control_groups.at(owner).at(group_n);
if (subaction == 0) {
group.clear();
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) break;
auto uid = get_unit_id(u);
if (group.size() == 12) error("attempt to control group more than 12 units");
group.push_back(uid);
retval = true;
}
} else {
if (!group.empty()) {
unit_t* u = get_unit(group[0]);
if (u && !unit_dead(u) && !unit_can_be_multi_selected(u)) return false;
}
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) break;
auto uid = get_unit_id(u);
if (group.empty() || unit_can_be_multi_selected(u)) {
auto i = std::find(group.begin(), group.end(), uid);
if (i == group.end()) {
if (group.size() == 12) {
// original buffer overflow bug
if (group_n != 9) {
auto& next_group = action_st.control_groups.at(owner).at(group_n + 1);
if (next_group.empty()) next_group.push_back(uid);
else next_group[0] = uid;
}
break;
}
group.push_back(uid);
retval = true;
if (group.size() == 12) break;
}
}
}
}
} else error("action_control_group: unknown subaction %d", subaction);
return retval;
}
bool action_unload_all(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_can_receive_order(u, get_order_type(Orders::Unload), owner)) continue;
if (loaded_units(u).empty()) continue;
issue_order(u, queue, get_order_type(Orders::Unload), {});
}
return retval;
}
bool action_unload(int owner, unit_t* target) {
if (!target) return false;
if (target->owner != owner) return false;
if (!target->connected_unit || !u_loaded(target)) return false;
return unit_unload(target);
}
bool action_liftoff(int owner, xy pos) {
if (!is_in_map_bounds(pos)) return false;
unit_t* u = get_single_selected_unit(owner);
if (!u) return false;
if (unit_is_constructing(u)) return false;
if (unit_is_researching(u) || unit_is_upgrading(u)) return false;
if (!unit_can_receive_order(u, get_order_type(Orders::BuildingLiftoff), owner)) return false;
set_unit_order(u, get_order_type(Orders::BuildingLiftoff), pos);
set_queued_order(u, false, u->unit_type->return_to_idle, pos - xy(0, 42));
return true;
}
bool action_research(int owner, const tech_type_t* tech) {
unit_t* u = get_single_selected_unit(owner);
if (!u) return false;
if (!unit_can_research(u, tech, owner)) return false;
if (!has_available_resources_for(u->owner, tech, true)) return false;
st.current_minerals[u->owner] -= tech->mineral_cost;
st.current_gas[u->owner] -= tech->gas_cost;
u->building.researching_type = tech;
u->building.upgrade_research_time = tech->research_time;
st.tech_researching[u->owner][tech->id] = true;
sprite_run_anim(u->sprite, iscript_anims::IsWorking);
set_unit_order(u, get_order_type(Orders::ResearchTech));
return true;
}
bool action_cancel_research(int owner) {
unit_t* u = get_single_selected_unit(owner);
if (!u || u->owner != owner) return false;
if (!ut_building(u)) return false;
if (!u_completed(u)) return false;
if (!unit_is_researching(u)) return false;
cancel_research(u);
return true;
}
bool action_upgrade(int owner, const upgrade_type_t* upgrade) {
unit_t* u = get_single_selected_unit(owner);
if (!u) return false;
if (!unit_can_upgrade(u, upgrade, owner)) return false;
if (!has_available_resources_for(u->owner, upgrade, true)) return false;
st.current_minerals[u->owner] -= upgrade_mineral_cost(owner, upgrade);
st.current_gas[u->owner] -= upgrade_gas_cost(owner, upgrade);
u->building.upgrading_type = upgrade;
u->building.upgrade_research_time = upgrade_time_cost(owner, upgrade);
u->building.upgrading_level = player_upgrade_level(owner, upgrade->id) + 1;
st.upgrade_upgrading[u->owner][upgrade->id] = true;
sprite_run_anim(u->sprite, iscript_anims::IsWorking);
set_unit_order(u, get_order_type(Orders::Upgrade));
return true;
}
bool action_cancel_upgrade(int owner) {
unit_t* u = get_single_selected_unit(owner);
if (!u || u->owner != owner) return false;
if (!ut_building(u)) return false;
if (!u_completed(u)) return false;
if (!unit_is_upgrading(u)) return false;
cancel_upgrade(u);
return true;
}
bool action_unsiege(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
if (!unit_is_sieged_tank(u)) continue;
if (u->order_type->id == Orders::Unsieging) continue;
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Tank_Siege_Mode))) continue;
issue_order(u, queue, get_order_type(Orders::Unsieging), {});
retval = true;
}
return retval;
}
bool action_siege(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
if (!unit_is_unsieged_tank(u)) continue;
if (u->order_type->id == Orders::Sieging) continue;
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Tank_Siege_Mode))) continue;
issue_order(u, queue, get_order_type(Orders::Sieging), {});
retval = true;
}
return retval;
}
bool action_return_cargo(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
const order_type_t* order = nullptr;
if (u->carrying_flags & 2) order = get_order_type(Orders::ReturnMinerals);
else if (u->carrying_flags & 1) order = get_order_type(Orders::ReturnGas);
if (!order) continue;
if (!unit_can_receive_order(u, order, owner)) continue;
issue_order(u, queue, order, {});
retval = true;
}
return retval;
}
bool action_hold_position(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
const order_type_t* order = get_order_type(Orders::HoldPosition);
if (unit_is_carrier(u)) order = get_order_type(Orders::CarrierHoldPosition);
else if (unit_is_reaver(u)) order = get_order_type(Orders::ReaverHoldPosition);
else if (unit_is_queen(u)) order = get_order_type(Orders::QueenHoldPosition);
else if (unit_is(u, UnitTypes::Zerg_Scourge) || unit_is(u, UnitTypes::Zerg_Infested_Terran)) order = get_order_type(Orders::SuicideHoldPosition);
else if (unit_is(u, UnitTypes::Terran_Medic)) order = get_order_type(Orders::MedicHoldPosition);
if (!unit_can_receive_order(u, order, owner)) continue;
issue_order(u, queue, order, {});
retval = true;
}
return retval;
}
bool action_cloak(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
const tech_type_t* tech = nullptr;
if (unit_is_ghost(u)) tech = get_tech_type(TechTypes::Personnel_Cloaking);
else if (unit_is_wraith(u)) tech = get_tech_type(TechTypes::Cloaking_Field);
if (!tech) continue;
if (!unit_can_use_tech(u, tech)) continue;
if (u_requires_detector(u)) continue;
if (u->energy < fp8::integer(tech->energy_cost)) continue;
u->energy -= fp8::integer(tech->energy_cost);
set_secondary_order(u, get_order_type(Orders::Cloak));
}
return retval;
}
bool action_decloak(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
const tech_type_t* tech = nullptr;
if (unit_is_ghost(u)) tech = get_tech_type(TechTypes::Personnel_Cloaking);
else if (unit_is_wraith(u)) tech = get_tech_type(TechTypes::Cloaking_Field);
if (!tech) continue;
if (!unit_can_use_tech(u, tech)) continue;
set_secondary_order(u, get_order_type(Orders::Decloak));
}
return retval;
}
bool action_set_alliances(int owner, std::array<int, 12> alliances) {
st.alliances.at(owner) = alliances;
for (unit_t* u : ptr(st.player_units[owner])) {
if (u->order_target.unit && u->order_type->targets_enemies) {
if (alliances[u->order_target.unit->owner] && !unit_target_is_enemy(u, u->order_target.unit)) {
u->order_target.unit = nullptr;
}
}
}
return true;
}
bool action_set_shared_vision(int owner, uint32_t flags) {
flags &= 0xff;
flags |= st.shared_vision.at(owner) & 0xff00;
st.shared_vision.at(owner) = flags;
return true;
}
bool action_cancel_addon(int owner) {
unit_t* u = get_single_selected_unit(owner);
if (!u || u->owner != owner) return false;
if (!u_completed(u)) return false;
if (!u_grounded_building(u)) return false;
if (u->secondary_order_type->id != Orders::BuildAddon) return false;
unit_t* addon = u->current_build_unit;
if (!addon) return false;
if (u_completed(addon)) return false;
cancel_building_unit(addon);
return true;
}
bool action_stim_pack(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Stim_Packs))) continue;
if (u->hp <= fp8::integer(10)) continue;
play_sound(lcg_rand(31, 278, 279), u);
unit_deal_damage(u, fp8::integer(10), nullptr, ~0);
if (u->stim_timer < 37) {
u->stim_timer = 37;
update_unit_speed(u);
}
retval = true;
}
return retval;
}
bool action_cancel_nuke(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_is_ghost(u)) continue;
if (!u->connected_unit || !unit_is(u->connected_unit, UnitTypes::Terran_Nuclear_Missile)) continue;
u->connected_unit->connected_unit = nullptr;
u->connected_unit = nullptr;
retval = true;
}
return retval;
}
bool action_morph(int owner, const unit_type_t* unit_type) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
if (unit_is(u, UnitTypes::Zerg_Hydralisk) && unit_is(unit_type, UnitTypes::Zerg_Lurker)) {
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Lurker_Aspect))) continue;
} else if (unit_is(u, UnitTypes::Zerg_Larva) || unit_is(u, UnitTypes::Zerg_Mutalisk)) {
if (!unit_can_build(u, unit_type)) continue;
} else continue;
if (u->order_type->id == Orders::ZergUnitMorph) continue;
if (!build_queue_push(u, unit_type)) continue;
set_unit_order(u, get_order_type(Orders::ZergUnitMorph));
retval = true;
}
return retval;
}
bool action_morph_building(int owner, const unit_type_t* unit_type) {
if (!unit_is_zerg_building(unit_type)) return false;
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
if (!unit_is_zerg_building(u)) continue;
if (!unit_can_build(u, unit_type)) continue;
if (!build_queue_push(u, unit_type)) continue;
if (u->order_type->id != Orders::ZergBuildingMorph) set_unit_order(u, get_order_type(Orders::ZergBuildingMorph));
retval = true;
}
return retval;
}
bool action_burrow(int owner, bool queue) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Burrowing), owner)) continue;
if (u_burrowed(u)) continue;
if (u->order_type->id == Orders::Burrowing) continue;
issue_order(u, queue, get_order_type(Orders::Burrowing), {});
retval = true;
}
return retval;
}
bool action_unburrow(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_can_use_tech(u, get_tech_type(TechTypes::Burrowing), owner)) continue;
if (!ut_can_burrow(u)) continue;
unburrow_unit(u);
retval = true;
}
return retval;
}
bool action_cancel_morph(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!u || u->owner != owner) return false;
if (cancel_building_unit(u)) retval = true;
}
return retval;
}
bool action_morph_archon(int owner) {
return morph_archon_impl(owner, false);
}
bool action_carrier_stop(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_can_receive_order(u, get_order_type(Orders::CarrierStop), owner)) continue;
set_unit_order(u, get_order_type(Orders::CarrierStop));
retval = true;
}
return retval;
}
bool action_train_fighter(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (u->owner != owner) continue;
const unit_type_t* build_type = nullptr;
if (unit_is_carrier(u)) build_type = get_unit_type(UnitTypes::Protoss_Interceptor);
else if (unit_is_reaver(u)) build_type = get_unit_type(UnitTypes::Protoss_Scarab);
if (!build_type) continue;
if (!unit_can_build(u, build_type)) continue;
if (!build_queue_push(u, build_type)) continue;
if (u->secondary_order_state != 2) {
u->secondary_order_type = nullptr;
set_secondary_order(u, get_order_type(Orders::TrainFighter));
}
retval = true;
}
return retval;
}
bool action_morph_dark_archon(int owner) {
return morph_archon_impl(owner, true);
}
bool action_reaver_stop(int owner) {
bool retval = false;
for (unit_t* u : selected_units(owner)) {
if (!unit_can_receive_order(u, get_order_type(Orders::ReaverStop), owner)) continue;
set_unit_order(u, get_order_type(Orders::ReaverStop));
retval = true;
}
return retval;
}
bool action_player_leave(int owner, int reason) {
if (reason == 6) player_dropped(owner);
else player_left(owner);
return true;
}
bool action_cheat(int owner, int flags) {
if (!st.cheats_enabled) return false;
st.cheat_operation_cwal = (flags & 2) != 0;
if (flags & 2) flags &= ~2;
if (flags & 0x10) {
flags &= ~0x10;
for (size_t i = 0; i != 12; ++i) {
if (st.players[i].controller == player_t::controller_occupied) {
st.current_minerals[i] += 10000;
st.current_gas[i] += 10000;
}
}
}
if (flags) error("unknown cheat flag(s) %#x", flags);