File: SelectedUnitsHandler.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (1109 lines) | stat: -rw-r--r-- 30,493 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "SelectedUnitsHandler.h"
#include "SelectedUnitsAI.h"
#include "Camera.h"
#include "GlobalUnsynced.h"
#include "WaitCommandsAI.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "UI/CommandColors.h"
#include "UI/GuiHandler.h"
#include "UI/TooltipConsole.h"
#include "ExternalAI/EngineOutHandler.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Rendering/CommandDrawer.h"
#include "Rendering/LineDrawer.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/GL/WideLineAdapter.hpp"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/MoveTypes/MoveDefHandler.h"
#include "Sim/Features/Feature.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/UnitToolTipMap.hpp"
#include "Sim/Units/CommandAI/BuilderCAI.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "Game/UI/Groups/GroupHandler.h"
#include "Game/UI/Groups/Group.h"
#include "System/Config/ConfigHandler.h"
#include "System/Color.h"
#include "System/EventHandler.h"
#include "System/Log/ILog.h"
#include "System/StringUtil.h"
#include "Net/Protocol/NetProtocol.h"
#include "System/Net/PackPacket.h"
#include "System/FileSystem/SimpleParser.h"
#include "System/Input/KeyInput.h"
#include "System/Sound/ISound.h"
#include "System/Sound/ISoundChannels.h"

#include <SDL_mouse.h>
#include <SDL_keycode.h>



CONFIG(bool, BuildIconsFirst).defaultValue(false);
CONFIG(bool, AutoAddBuiltUnitsToFactoryGroup).defaultValue(false).description("Controls whether or not units built by factories will inherit that factory's unit group.");
CONFIG(bool, AutoAddBuiltUnitsToSelectedGroup).defaultValue(false);

CSelectedUnitsHandler selectedUnitsHandler;



void CSelectedUnitsHandler::Init(unsigned numPlayers)
{
	soundMultiselID = sound->GetDefSoundId("MultiSelect");
	buildIconsFirst = configHandler->GetBool("BuildIconsFirst");
	autoAddBuiltUnitsToFactoryGroup = configHandler->GetBool("AutoAddBuiltUnitsToFactoryGroup");
	autoAddBuiltUnitsToSelectedGroup = configHandler->GetBool("AutoAddBuiltUnitsToSelectedGroup");

	netSelected.resize(numPlayers);
}


bool CSelectedUnitsHandler::IsUnitSelected(const CUnit* unit) const
{
	return (unit != nullptr && selectedUnits.find(unit->id) != selectedUnits.end());
}

bool CSelectedUnitsHandler::IsUnitSelected(const int unitID) const
{
	return (IsUnitSelected(unitHandler.GetUnit(unitID)));
}


void CSelectedUnitsHandler::ToggleBuildIconsFirst()
{
	buildIconsFirst = !buildIconsFirst;
	possibleCommandsChanged = true;
}


CSelectedUnitsHandler::AvailableCommandsStruct CSelectedUnitsHandler::GetAvailableCommands()
{
	possibleCommandsChanged = false;

	int commandPage = 1000;
	int foundGroup = -2;
	int foundGroup2 = -2;

	spring::unordered_map<int, int> states;
	std::vector<SCommandDescription> commands;

	for (const int unitID: selectedUnits) {
		const CUnit* u = unitHandler.GetUnit(unitID);
		const CCommandAI* cai = u->commandAI;
		const CGroup* group = u->GetGroup();

		for (const SCommandDescription* cmdDesc: cai->GetPossibleCommands()) {
			states[cmdDesc->id] = cmdDesc->disabled ? 2 : 1;
		}

		if (cai->lastSelectedCommandPage < commandPage)
			commandPage = cai->lastSelectedCommandPage;

		if (foundGroup == -2 && group != nullptr)
			foundGroup = group->id;

		if (group == nullptr || foundGroup != group->id)
			foundGroup = -1;

		if (foundGroup2 == -2 && group != nullptr)
			foundGroup2 = group->id;

		if (foundGroup2 >= 0 && group != nullptr && group->id != foundGroup2)
			foundGroup2 = -1;
	}

	// load the first set (separating build and non-build commands)
	for (const int unitID: selectedUnits) {
		const CUnit* u = unitHandler.GetUnit(unitID);
		const CCommandAI* cai = u->commandAI;

		for (const SCommandDescription* cmdDesc: cai->GetPossibleCommands()) {
			if (buildIconsFirst) {
				if (cmdDesc->id >= 0)
					continue;
			} else {
				if (cmdDesc->id <  0)
					continue;
			}

			if (cmdDesc->showUnique && selectedUnits.size() > 1)
				continue;

			if (states[cmdDesc->id] > 0) {
				commands.push_back(*cmdDesc);
				states[cmdDesc->id] = 0;
			}
		}
	}

	// load the second set (all those that have not already been included)
	for (const int unitID: selectedUnits) {
		const CUnit* u = unitHandler.GetUnit(unitID);
		const CCommandAI* cai = u->commandAI;

		for (const SCommandDescription* cmdDesc: cai->GetPossibleCommands()) {
			if (buildIconsFirst) {
				if (cmdDesc->id < 0)
					continue;
			} else {
				if (cmdDesc->id >= 0)
					continue;
			}

			if (cmdDesc->showUnique && selectedUnits.size() > 1)
				continue;

			if (states[cmdDesc->id] > 0) {
				commands.push_back(*cmdDesc);
				states[cmdDesc->id] = 0;
			}
		}
	}

	AvailableCommandsStruct ac;
	ac.commandPage = commandPage;
	ac.commands = commands;
	return ac;
}


void CSelectedUnitsHandler::GiveCommand(const Command& c, bool fromUser)
{
	if (gu->spectating && gs->godMode == 0)
		return;
	if (selectedUnits.empty())
		return;

	const int cmdID = c.GetID();

	if (fromUser) {
		// add some statistics
		CPlayer* myPlayer = playerHandler.Player(gu->myPlayerNum);
		PlayerStatistics* myPlayerStats = &myPlayer->currentStats;

		myPlayerStats->numCommands++;

		if (selectedGroup != -1) {
			myPlayerStats->unitCommands += uiGroupHandlers[gu->myTeam].GetGroupSize(selectedGroup);
		} else {
			myPlayerStats->unitCommands += selectedUnits.size();
		}
	}

	if (cmdID == CMD_GROUPCLEAR) {
		for (const int unitID: selectedUnits) {
			CUnit* u = unitHandler.GetUnit(unitID);

			if (u->GetGroup() != nullptr) {
				u->SetGroup(nullptr);
				possibleCommandsChanged = true;
			}
		}
		return;
	}

	if (cmdID == CMD_GROUPSELECT) {
		const CUnit* u = unitHandler.GetUnit(*selectedUnits.begin());
		const CGroup* g = u->GetGroup();

		SelectGroup(g->id);
		return;
	}

	if (cmdID == CMD_GROUPADD) {
		const CGroup* group = nullptr;

		for (const int unitID: selectedUnits) {
			const CUnit* u = unitHandler.GetUnit(unitID);
			const CGroup* g = u->GetGroup();

			if (g != nullptr) {
				group = g;
				possibleCommandsChanged = true;
				break;
			}
		}
		if (group != nullptr) {
			for (const int unitID: selectedUnits) {
				CUnit* u = unitHandler.GetUnit(unitID);
				CGroup* g = nullptr;

				if (u == nullptr) {
					assert(false);
					continue;
				}

				if ((g = u->GetGroup()) != nullptr)
					continue;

				// change group, but do not call SUH::AddUnit while iterating
				// (the unit's id is already present in selectedUnits anyway)
				u->SetGroup(const_cast<CGroup*>(group), false, false);
			}

			SelectGroup(group->id);
		}

		return;
	}

	if (cmdID == CMD_TIMEWAIT) {
		waitCommandsAI.AddTimeWait(c);
		return;
	}

	if (cmdID == CMD_DEATHWAIT) {
		waitCommandsAI.AddDeathWait(c);
		return;
	}

	if (cmdID == CMD_SQUADWAIT) {
		waitCommandsAI.AddSquadWait(c);
		return;
	}

	if (cmdID == CMD_GATHERWAIT) {
		waitCommandsAI.AddGatherWait(c);
		return;
	}

	SendCommand(c);

	if (!selectedUnits.empty()) {
		const CUnit* u = unitHandler.GetUnit(*selectedUnits.begin());
		const UnitDef* ud = u->unitDef;
		Channels::UnitReply->PlayRandomSample(ud->sounds.ok, u);
	}
}


void CSelectedUnitsHandler::HandleUnitBoxSelection(const float4& planeRight, const float4& planeLeft, const float4& planeTop, const float4& planeBottom)
{
	CUnit* unit = nullptr;
	const CPlayer* myPlayer = gu->GetMyPlayer();

	int numUnits = 0;
	int minTeam = gu->myTeam;
	int maxTeam = gu->myTeam;

	// any team's units can be *selected*; whether they can
	// be given orders depends on our ability to play god
	if (gu->spectatingFullSelect || gs->godMode != 0) {
		minTeam = 0;
		maxTeam = teamHandler.ActiveTeams() - 1;
	}

	for (int team = minTeam; team <= maxTeam; team++) {
		if (!gu->spectatingFullSelect && !myPlayer->CanControlTeam(team))
			continue;

		for (CUnit* u: unitHandler.GetUnitsByTeam(team)) {
			const float4 vec(u->midPos, 1.0f);

			if (vec.dot4(planeRight) >= 0.0f)
				continue;
			if (vec.dot4(planeLeft) >= 0.0f)
				continue;
			if (vec.dot4(planeTop) >= 0.0f)
				continue;
			if (vec.dot4(planeBottom) >= 0.0f)
				continue;

			if (KeyInput::GetKeyModState(KMOD_CTRL) && (selectedUnits.find(u->id) != selectedUnits.end())) {
				RemoveUnit(u);
				continue;
			}

			AddUnit(unit = u);
			numUnits++;
		}
	}

	switch (numUnits) {
		case 0: {
		} break;
		case 1: {
			Channels::UnitReply->PlayRandomSample(unit->unitDef->sounds.select, unit);
		} break;
		default: {
			Channels::UserInterface->PlaySample(soundMultiselID);
		} break;
	}
}


void CSelectedUnitsHandler::HandleSingleUnitClickSelection(CUnit* unit, bool doInViewTest, bool selectType)
{
	//FIXME make modular?
	if (unit == nullptr)
		return;
	if (unit->team != gu->myTeam && !gu->spectatingFullSelect && gs->godMode == 0)
		return;

	if (!selectType) {
		if (KeyInput::GetKeyModState(KMOD_CTRL) && (selectedUnits.find(unit->id) != selectedUnits.end())) {
			RemoveUnit(unit);
		} else {
			AddUnit(unit);
		}
	} else {
		const CPlayer* myPlayer = gu->GetMyPlayer();

		// double click, select all units of same type (on screen, unless CTRL is pressed)
		int minTeam = gu->myTeam;
		int maxTeam = gu->myTeam;

		if (gu->spectatingFullSelect || gs->godMode != 0) {
			minTeam = 0;
			maxTeam = teamHandler.ActiveTeams() - 1;
		}

		for (int team = minTeam; team <= maxTeam; team++) {
			if (!gu->spectatingFullSelect && !myPlayer->CanControlTeam(team))
				continue;

			for (CUnit* u: unitHandler.GetUnitsByTeam(team)) {
				if (u->unitDef->id != unit->unitDef->id)
					continue;

				if (!doInViewTest || KeyInput::GetKeyModState(KMOD_CTRL) || camera->InView((u)->midPos))
					AddUnit(u);
			}
		}
	}

	Channels::UnitReply->PlayRandomSample(unit->unitDef->sounds.select, unit);
}



void CSelectedUnitsHandler::AddUnit(CUnit* unit)
{
	// if unit is being transported, we should not be able to select it
	const CUnit* trans = unit->GetTransporter();

	if (trans != nullptr && trans->unitDef->IsTransportUnit() && !trans->unitDef->isFirePlatform)
		return;

	if (unit->noSelect)
		return;

	if (selectedUnits.insert(unit->id).second)
		AddDeathDependence(unit, DEPENDENCE_SELECTED);

	selectionChanged = true;
	possibleCommandsChanged = true;

	const CGroup* g = unit->GetGroup();

	if (g == nullptr || g->id != selectedGroup)
		selectedGroup = -1;

	unit->isSelected = true;
}


void CSelectedUnitsHandler::RemoveUnit(CUnit* unit)
{
	if (selectedUnits.erase(unit->id))
		DeleteDeathDependence(unit, DEPENDENCE_SELECTED);

	selectionChanged = true;
	possibleCommandsChanged = true;
	selectedGroup = -1;
	unit->isSelected = false;
}


void CSelectedUnitsHandler::ClearSelected()
{
	for (const int unitID: selectedUnits) {
		CUnit* u = unitHandler.GetUnit(unitID);

		// not possible unless ::RemoveUnit is not called when it should
		if (u == nullptr) {
			assert(false);
			continue;
		}

		u->isSelected = false;
		DeleteDeathDependence(u, DEPENDENCE_SELECTED);
	}

	selectedUnits.clear();
	selectionChanged = true;
	possibleCommandsChanged = true;
	selectedGroup = -1;
}


void CSelectedUnitsHandler::SelectGroup(int num)
{
	ClearSelected();
	selectedGroup = num;
	CGroup* group = uiGroupHandlers[gu->myTeam].GetGroup(num);

	for (const int unitID: group->units) {
		CUnit* u = unitHandler.GetUnit(unitID);

		if (!u->noSelect) {
			u->isSelected = true;
			selectedUnits.insert(u->id);
			AddDeathDependence(u, DEPENDENCE_SELECTED);
		}
	}

	selectionChanged = true;
	possibleCommandsChanged = true;
}


void CSelectedUnitsHandler::SelectUnits(const std::string& line)
{
	for (const std::string& arg : CSimpleParser::Tokenize(line, 0)) {
		if (arg == "clear") {
			selectedUnitsHandler.ClearSelected();
		} else if ((arg[0] == '+') || (arg[0] == '-')) {
			char* endPtr;
			const char* startPtr = arg.c_str() + 1;
			const int unitIndex = strtol(startPtr, &endPtr, 10);
			if (endPtr == startPtr)
				continue; // bad number

			if ((unitIndex < 0) || (static_cast<unsigned int>(unitIndex) >= unitHandler.MaxUnits()))
				continue; // bad index

			CUnit* unit = unitHandler.GetUnit(unitIndex);
			if (unit == nullptr)
				continue;

			if (!gu->spectatingFullSelect && (unit->team != gu->myTeam))
				continue; // not mine to select

			// perform the selection
			if (arg[0] == '+') {
				AddUnit(unit);
			} else {
				RemoveUnit(unit);
			}
		}
	}
}


void CSelectedUnitsHandler::SelectCycle(const std::string& command)
{
	static spring::unordered_set<int> unitIDs;
	static int lastID = -1;

	if (command == "restore") {
		ClearSelected();

		for (const int unitID: unitIDs) {
			CUnit* unit = unitHandler.GetUnit(unitID);

			if (unit == nullptr)
				continue;

			AddUnit(unit);
		}

		return;
	}

	if (selectedUnits.size() >= 2) {
		// assign the cycle units
		unitIDs.clear();

		for (const int unitID: selectedUnits) {
			const CUnit* u = unitHandler.GetUnit(unitID);
			unitIDs.insert(u->id);
		}

		ClearSelected();
		AddUnit(unitHandler.GetUnit(lastID = *unitIDs.begin()));
		return;
	}

	// clean the list
	spring::unordered_set<int> tmpSet;
	for (const int unitID: unitIDs) {
		if (unitHandler.GetUnit(unitID) == nullptr)
			continue;
		tmpSet.insert(unitID);
	}

	unitIDs = std::move(tmpSet);

	if ((lastID >= 0) && (unitHandler.GetUnit(lastID) == nullptr))
		lastID = -1;

	// selectedUnits size is 0 or 1
	ClearSelected();

	if (unitIDs.empty())
		return;

	auto fit = unitIDs.find(lastID);

	if (fit == unitIDs.end()) {
		AddUnit(unitHandler.GetUnit(lastID = *unitIDs.begin()));
		return;
	}

	if ((++fit) != unitIDs.end()) {
		AddUnit(unitHandler.GetUnit(lastID = *fit));
		return;
	}

	AddUnit(unitHandler.GetUnit(lastID = *unitIDs.begin()));
}


void CSelectedUnitsHandler::Draw()
{
	glAttribStatePtr->DisableDepthMask();
	glAttribStatePtr->DisableDepthTest();
	glAttribStatePtr->EnableBlendMask(); // for line smoothing
	glAttribStatePtr->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glAttribStatePtr->PolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	SColor udColor(cmdColors.unitBox);
	SColor mdColor(cmdColors.unitBox);

	mdColor.r = 255 - mdColor.r;
	mdColor.g = 255 - mdColor.g;
	mdColor.b = 255 - mdColor.b;

	GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
	Shader::IProgramObject* shader = buffer->GetShader();
	GL::WideLineAdapterC* wla = GL::GetWideLineAdapterC();

	shader->Enable();
	shader->SetUniformMatrix4x4<float>("u_movi_mat", false, camera->GetViewMatrix());
	shader->SetUniformMatrix4x4<float>("u_proj_mat", false, camera->GetProjectionMatrix());
	wla->Setup(buffer, globalRendering->viewSizeX, globalRendering->viewSizeY, cmdColors.UnitBoxLineWidth(), camera->GetViewProjectionMatrix());

	if (udColor.a > 0) {
		const auto* unitSet = &selectedUnits;

		// note: units in this set are not necessarily all selected themselves (eg.
		// if autoAddBuiltUnitsToSelectedGroup is true) so we check IsUnitSelected
		// for each
		if (selectedGroup != -1) {
			const CGroupHandler* gh = &uiGroupHandlers[gu->myTeam];
			const CGroup* g = gh->GetGroup(selectedGroup);

			unitSet = &g->units;
		}

		for (const int unitID: *unitSet) {
			const CUnit* unit = unitHandler.GetUnit(unitID);
			const MoveDef* moveDef = unit->moveDef;

			if (unit->isIcon)
				continue;
			if (!IsUnitSelected(unit))
				continue;

			const float3& drawPos = unit->drawPos;

			{
				const int uhxsize = (unit->xsize * SQUARE_SIZE) >> 1;
				const int uhzsize = (unit->zsize * SQUARE_SIZE) >> 1;

				// UnitDef footprint corners (rotated)
				const float3 udVerts[4] = {
					{drawPos.x + uhxsize * 1.0f, drawPos.y, drawPos.z + uhzsize * 1.0f},
					{drawPos.x - uhxsize * 1.0f, drawPos.y, drawPos.z + uhzsize * 1.0f},
					{drawPos.x - uhxsize * 1.0f, drawPos.y, drawPos.z - uhzsize * 1.0f},
					{drawPos.x + uhxsize * 1.0f, drawPos.y, drawPos.z - uhzsize * 1.0f},
				};

				wla->SafeAppend({udVerts[0], udColor});
				wla->SafeAppend({udVerts[1], udColor});
				wla->SafeAppend({udVerts[2], udColor});
				wla->SafeAppend({udVerts[3], udColor});
			}


			if (moveDef == nullptr)
				continue;
			if (!globalRendering->drawDebug)
				continue;

			{
				const int mhxsize = (moveDef->xsize * SQUARE_SIZE) >> 1;
				const int mhzsize = (moveDef->zsize * SQUARE_SIZE) >> 1;

				// MoveDef footprint corners
				const float3 mdVerts[4] = {
					{drawPos.x + mhxsize * 1.0f, drawPos.y, drawPos.z + mhzsize * 1.0f},
					{drawPos.x - mhxsize * 1.0f, drawPos.y, drawPos.z + mhzsize * 1.0f},
					{drawPos.x - mhxsize * 1.0f, drawPos.y, drawPos.z - mhzsize * 1.0f},
					{drawPos.x + mhxsize * 1.0f, drawPos.y, drawPos.z - mhzsize * 1.0f},
				};

				wla->SafeAppend({mdVerts[0], mdColor});
				wla->SafeAppend({mdVerts[1], mdColor});
				wla->SafeAppend({mdVerts[2], mdColor});
				wla->SafeAppend({mdVerts[3], mdColor});
			}
		}

		wla->Submit(GL_QUADS);
	}

	if (cmdColors.buildBox[3] > 0.0f) {
		const auto& guiCommands = guihandler->commands;

		const bool shiftPressed = (cmdColors.BuildBoxesOnShift() && KeyInput::GetKeyModState(KMOD_SHIFT));
		const bool buildQueued = (size_t(guihandler->inCommand) < guiCommands.size() && guiCommands[guihandler->inCommand].id < 0);

		// draw queued build sites if a GUI build-icon has been
		// clicked or whenever the shift key is being held down
		if (!selectedUnits.empty() && (shiftPressed || buildQueued)) {
			for (const auto bi: unitHandler.GetBuilderCAIs()) {
				const CBuilderCAI* builderCAI = bi.second;
				const CUnit* builder = builderCAI->owner;

				if (builder->team == gu->myTeam) {
					commandDrawer->SetBuildQueueSquareColor(cmdColors.buildBox);
					commandDrawer->DrawQueuedBuildingSquares(builderCAI);
					continue;
				}
				if (teamHandler.AlliedTeams(builder->team, gu->myTeam)) {
					commandDrawer->SetBuildQueueSquareColor(cmdColors.allyBuildBox);
					commandDrawer->DrawQueuedBuildingSquares(builderCAI);
				}
			}
		}
	}

	shader->Disable();

	glAttribStatePtr->PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glAttribStatePtr->DisableBlendMask();
	glAttribStatePtr->EnableDepthTest();
	glAttribStatePtr->EnableDepthMask();
}


void CSelectedUnitsHandler::DependentDied(CObject* o)
{
	selectedUnits.erase(static_cast<CUnit*>(o)->id);

	selectionChanged = true;
	possibleCommandsChanged = true;
}


// handles NETMSG_SELECT's
void CSelectedUnitsHandler::NetSelect(std::vector<int>& s, int playerId)
{
	assert(unsigned(playerId) < netSelected.size());
	netSelected[playerId] = s;
}

// handles NETMSG_COMMAND's
void CSelectedUnitsHandler::NetOrder(Command& c, int playerId)
{
	assert(unsigned(playerId) < netSelected.size());

	if (netSelected[playerId].empty())
		return;

	selectedUnitsAI.GiveCommandNet(c, playerId);
	eoh->PlayerCommandGiven(netSelected[playerId], c, playerId);
}

void CSelectedUnitsHandler::ClearNetSelect(int playerId)
{
	netSelected[playerId].clear();
}

// handles NETMSG_AICOMMAND{S}'s sent by AICallback / LuaUnsyncedCtrl (!)
void CSelectedUnitsHandler::AINetOrder(int unitID, int aiTeamID, int playerID, const Command& c)
{
	CUnit* unit = unitHandler.GetUnit(unitID);

	if (unit == nullptr)
		return;

	const CPlayer* player = playerHandler.Player(playerID);

	if (player == nullptr)
		return;

	// no warning; will result in false bug reports due to latency between
	// time of giving valid orders on units which then change team through
	// e.g. LuaRules
	// AI's are hosted by players, but do not have any Player representation
	// themselves and should not be automatically controllable by their host
	// on the other hand they should always be able to control their OWN team
	if ((aiTeamID == MAX_TEAMS && !player->CanControlTeam(unit->team)) || (aiTeamID != MAX_TEAMS && aiTeamID != unit->team))
		return;

	// always pulled from net, synced command by definition
	// (fromSynced determines whether CMD_UNLOAD_UNITS uses
	// synced or unsynced randomized position sampling, etc)
	unit->commandAI->GiveCommand(c, playerID, true, false);
}


/******************************************************************************/
//
//  GetDefaultCmd() and friends
//

static bool targetIsEnemy = false;
static const CUnit* targetUnit = nullptr;
static const CFeature* targetFeature = nullptr;


static inline bool IsBetterLeader(const UnitDef* newDef, const UnitDef* oldDef)
{
	// There is a lot more that could be done here to make better
	// selections, but the users may prefer simplicity over smarts.

	if (targetUnit != nullptr) {
		if (targetIsEnemy) {
			const bool newCanDamage = newDef->CanDamage();
			const bool oldCanDamage = oldDef->CanDamage();

			if ( newCanDamage && !oldCanDamage)
				return true;
			if (!newCanDamage &&  oldCanDamage)
				return false;
			if (!targetUnit->unitDef->CanDamage()) {
				if ( newDef->canReclaim && !oldDef->canReclaim)
					return true;
				if (!newDef->canReclaim &&  oldDef->canReclaim)
					return false;
			}
		} else { // targetIsAlly
			if (targetUnit->health < targetUnit->maxHealth) {
				if ( newDef->canRepair && !oldDef->canRepair)
					return true;
				if (!newDef->canRepair &&  oldDef->canRepair)
					return false;
			}

			const bool newCanLoad = (newDef->transportCapacity > 0);
			const bool oldCanLoad = (oldDef->transportCapacity > 0);

			if ( newCanLoad && !oldCanLoad)
				return true;
			if (!newCanLoad &&  oldCanLoad)
				return false;
			if ( newDef->canGuard && !oldDef->canGuard)
				return true;
			if (!newDef->canGuard &&  oldDef->canGuard)
				return false;
		}
	}
	else if (targetFeature != nullptr) {
		if (targetFeature->udef != nullptr) {
			if ( newDef->canResurrect && !oldDef->canResurrect)
				return true;
			if (!newDef->canResurrect &&  oldDef->canResurrect)
				return false;
		}
		if ( newDef->canReclaim && !oldDef->canReclaim)
			return true;
		if (!newDef->canReclaim &&  oldDef->canReclaim)
			return false;
	}

	return (newDef->speed > oldDef->speed); // CMD_MOVE?
}


// CALLINFO:
// DrawMapStuff --> CGuiHandler::GetDefaultCommand --> GetDefaultCmd
// CMouseHandler::DrawCursor --> DrawCentroidCursor --> CGuiHandler::GetDefaultCommand --> GetDefaultCmd
// LuaUnsyncedRead::GetDefaultCommand --> CGuiHandler::GetDefaultCommand --> GetDefaultCmd
int CSelectedUnitsHandler::GetDefaultCmd(const CUnit* unit, const CFeature* feature)
{
	// return the default if there are no units selected
	if (selectedUnits.empty())
		return CMD_STOP;

	// setup the locals for IsBetterLeader()
	targetUnit = unit;
	targetFeature = feature;

	if (targetUnit != nullptr)
		targetIsEnemy = !teamHandler.Ally(gu->myAllyTeam, targetUnit->allyteam);

	// find the best leader to pick the command
	const CUnit* leaderUnit = unitHandler.GetUnit(*selectedUnits.begin());
	const UnitDef* leaderDef = leaderUnit->unitDef;

	for (const int unitID: selectedUnits) {
		const CUnit* testUnit = unitHandler.GetUnit(unitID);
		const UnitDef* testDef = testUnit->unitDef;

		if (testDef == leaderDef)
			continue;

		if (!IsBetterLeader(testDef, leaderDef))
			continue;

		leaderDef = testDef;
		leaderUnit = testUnit;
	}

	int cmd = leaderUnit->commandAI->GetDefaultCmd(unit, feature);
	eventHandler.DefaultCommand(unit, feature, cmd);
	return cmd;
}


/******************************************************************************/

void CSelectedUnitsHandler::PossibleCommandChange(CUnit* sender)
{
	possibleCommandsChanged |= (sender == nullptr || selectedUnits.find(sender->id) != selectedUnits.end());
}

// CALLINFO:
// CGame::Draw --> DrawCommands
// CMiniMap::DrawForReal --> DrawCommands
void CSelectedUnitsHandler::DrawCommands(bool onMiniMap)
{
	lineDrawer.Configure(cmdColors.UseColorRestarts(),
	                     cmdColors.UseRestartColor(),
	                     cmdColors.restart,
	                     cmdColors.RestartAlpha());
	lineDrawer.SetupLineStipple();
	lineDrawer.SetWidth(CLineDrawer::LineWidth::QueuedCmd);

	glAttribStatePtr->DisableDepthTest();
	glAttribStatePtr->EnableBlendMask();
	glAttribStatePtr->BlendFunc((GLenum) cmdColors.QueuedBlendSrc(), (GLenum) cmdColors.QueuedBlendDst());

	if (selectedGroup != -1) {
		const auto& groupHandler = uiGroupHandlers[gu->myTeam];
		const auto& groupUnits = groupHandler.GetGroup(selectedGroup)->units;

		for (const int unitID: groupUnits) {
			commandDrawer->Draw((unitHandler.GetUnit(unitID))->commandAI, onMiniMap);
		}
	} else {
		for (const int unitID: selectedUnits) {
			commandDrawer->Draw((unitHandler.GetUnit(unitID))->commandAI, onMiniMap);
		}
	}

	// draw the commands from AIs
	waitCommandsAI.DrawCommands();

	lineDrawer.SetWidth(CLineDrawer::LineWidth::Default);

	glAttribStatePtr->EnableDepthTest();
}


// CALLINFO:
// CTooltipConsole::Draw --> CMouseHandler::GetCurrentTooltip
// LuaUnsyncedRead::GetCurrentTooltip --> CMouseHandler::GetCurrentTooltip
// CMouseHandler::GetCurrentTooltip --> CMiniMap::GetToolTip --> GetTooltip
// CMouseHandler::GetCurrentTooltip --> GetTooltip
std::string CSelectedUnitsHandler::GetTooltip()
{
	std::string s;

	{
		if (selectedUnits.empty())
			return s;

		const CUnit* unit = unitHandler.GetUnit(*selectedUnits.begin());
		const CTeam* team = nullptr;

		// show the player name instead of unit name if it has FBI tag showPlayerName
		if (unit->unitDef->showPlayerName) {
			team = teamHandler.Team(unit->team);
			s = team->GetControllerName();
		} else {
			s = unitToolTipMap.Get(unit->id);
		}
	}

	const std::string custom = std::move(eventHandler.WorldTooltip(nullptr, nullptr, nullptr));
	if (!custom.empty())
		return custom;

	{
		#define NO_TEAM -32
		#define MULTI_TEAM -64
		int ctrlTeam = NO_TEAM;

		SUnitStats stats;

		for (const int unitID: selectedUnits) {
			const CUnit* unit = unitHandler.GetUnit(unitID);
			stats.AddUnit(unit, false);

			if (ctrlTeam == NO_TEAM) {
				ctrlTeam = unit->team;
			} else if (ctrlTeam != unit->team) {
				ctrlTeam = MULTI_TEAM;
			}
		}

		s += CTooltipConsole::MakeUnitStatsString(stats);

		const char* ctrlName = "";

		if (ctrlTeam == MULTI_TEAM) {
			ctrlName = "(Multiple teams)";
		} else if (ctrlTeam != NO_TEAM) {
			ctrlName = teamHandler.Team(ctrlTeam)->GetControllerName();
		}

		s += "\n\xff\xff\xff\xff";
		s += ctrlName;
		return s;
	}
}


void CSelectedUnitsHandler::SetCommandPage(int page)
{
	for (const int unitID: selectedUnits) {
		CUnit* u = unitHandler.GetUnit(unitID);
		CCommandAI* c = u->commandAI;
		c->lastSelectedCommandPage = page;
	}
}



void CSelectedUnitsHandler::SendCommand(const Command& c)
{
	if (selectionChanged) {
		// send new selection; first gather unit IDs
		selectedUnitIDs.clear();
		selectedUnitIDs.resize(selectedUnits.size(), 0);

		std::copy(selectedUnits.begin(), selectedUnits.end(), selectedUnitIDs.begin());

		clientNet->Send(CBaseNetProtocol::Get().SendSelect(gu->myPlayerNum, selectedUnitIDs));
		selectionChanged = false;
	}

	clientNet->Send(CBaseNetProtocol::Get().SendCommand(gu->myPlayerNum, c.GetID(), c.GetTimeOut(), c.GetOpts(), c.GetNumParams(), c.GetParams()));
}


// despite the NETMSG_AICOMMANDS packet-id, this only services LuaUnsyncedCtrl
void CSelectedUnitsHandler::SendCommandsToUnits(const std::vector<int>& unitIDs, const std::vector<Command>& commands, bool pairwise)
{
	// do not waste bandwidth (units can be selected
	// by any spectator, but not given orders without
	// god-mode)
	// note: clients verify this every NETMSG_SELECT
	if (gu->spectating && gs->godMode == 0)
		return;

	const unsigned unitIDCount  = unitIDs.size();
	const unsigned commandCount = commands.size();

	if ((unitIDCount == 0) || (commandCount == 0))
		return;

	uint32_t totalParams = 0;

	// if all commands share the same ID / options / number of parameters,
	// insert only these values into the packet to save a bit of bandwidth
	int32_t refCmdID = commands[0].GetID();
	uint8_t refCmdOpts = commands[0].GetOpts();
	int32_t refCmdSize = commands[0].GetNumParams();

	for (unsigned int c = 0; c < commandCount; c++) {
		totalParams += commands[c].GetNumParams();

		if (refCmdID != 0 && refCmdID != commands[c].GetID())
			refCmdID = 0;
		if (refCmdOpts != 0xFF && refCmdOpts != commands[c].GetOpts())
			refCmdOpts = 0xFF;
		if (refCmdSize != 0xFFFF && refCmdSize != commands[c].GetNumParams())
			refCmdSize = 0xFFFF;
	}

	unsigned int optBytesPerCmd = 0;
	unsigned int totalPacketLen = 0;

	// optional data per command (cmdID, cmdOpts, #cmdParams)
	optBytesPerCmd += (sizeof(uint32_t) * (refCmdID   == 0     ));
	optBytesPerCmd += (sizeof(uint8_t ) * (refCmdOpts == 0xFF  ));
	optBytesPerCmd += (sizeof(uint16_t) * (refCmdSize == 0xFFFF));

	// msg type, msg size
	totalPacketLen += (sizeof(uint8_t) + sizeof(static_cast<uint16_t>(totalPacketLen)));
	// player ID, AI ID, pairwise
	totalPacketLen += (sizeof(uint8_t) * 3);
	totalPacketLen += (
		sizeof(static_cast<uint32_t>(refCmdID  )) +
		sizeof(static_cast<uint8_t >(refCmdOpts)) +
		sizeof(static_cast<uint16_t>(refCmdSize))
	);

	totalPacketLen += sizeof(static_cast<uint16_t>(unitIDCount));
	totalPacketLen += (unitIDCount * sizeof(uint16_t));

	totalPacketLen += sizeof(static_cast<uint16_t>(commandCount));
	totalPacketLen += (commandCount * optBytesPerCmd);
	totalPacketLen += (totalParams * sizeof(float)); // params are floats

	if (totalPacketLen > 8192) {
		LOG_L(L_WARNING, "[%s] discarded oversized (len=%i) NETMSG_AICOMMANDS packet", __func__, totalPacketLen);
		return; // do not send oversized packets
	}

	netcode::PackPacket* packet = new netcode::PackPacket(totalPacketLen);
	*packet << static_cast<uint8_t >(NETMSG_AICOMMANDS)
	        << static_cast<uint16_t>(totalPacketLen)

	        << static_cast<uint8_t>(gu->myPlayerNum)
	        << static_cast<uint8_t>(MAX_AIS)
	        // << static_cast<uint8_t>(MAX_TEAMS)

	        << static_cast<uint8_t >(pairwise)
	        << static_cast<uint32_t>(refCmdID)
	        << static_cast<uint8_t >(refCmdOpts)
	        << static_cast<uint16_t>(refCmdSize);

	// NOTE: does not check for invalid unitIDs
	*packet << static_cast<uint16_t>(unitIDCount);
	for (const int unitID: unitIDs) {
		*packet << static_cast<int16_t>(unitID);
	}

	*packet << static_cast<uint16_t>(commandCount);

	for (unsigned int i = 0; i < commandCount; ++i) {
		const Command& cmd = commands[i];

		if (refCmdID == 0)
			*packet << static_cast<uint32_t>(cmd.GetID());
		if (refCmdOpts == 0xFF)
			*packet << static_cast<uint8_t>(cmd.GetOpts());
		if (refCmdSize == 0xFFFF)
			*packet << static_cast<uint16_t>(cmd.GetNumParams());

		for (unsigned int j = 0, n = cmd.GetNumParams(); j < n; j++) {
			*packet << cmd.GetParam(j);
		}
	}

	clientNet->Send(std::shared_ptr<netcode::RawPacket>(packet));
}