File: GroupHandler.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; 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 (181 lines) | stat: -rw-r--r-- 4,257 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <SDL_keycode.h>

#include "GroupHandler.h"
#include "Group.h"
#include "Game/SelectedUnitsHandler.h"
#include "Game/CameraHandler.h"
#include "Sim/Units/UnitHandler.h"
#include "System/Input/KeyInput.h"
#include "System/Log/ILog.h"
#include "System/ContainerUtil.h"
#include "System/EventHandler.h"
#include "System/StringHash.h"

std::vector<CGroupHandler> uiGroupHandlers;

CR_BIND(CGroupHandler, (0))
CR_REG_METADATA(CGroupHandler, (
	CR_MEMBER(groups),
	CR_MEMBER(freeGroups),
	CR_MEMBER(changedGroups),
	CR_MEMBER(unitGroups),

	CR_MEMBER(team),
	CR_MEMBER(firstUnusedGroup)

))

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGroupHandler::CGroupHandler(int teamId): team(teamId)
{
	groups.reserve(FIRST_SPECIAL_GROUP);

	for (int groupId = 0; groupId < FIRST_SPECIAL_GROUP; ++groupId) {
		groups.emplace_back(groupId, teamId);
	}
}


void CGroupHandler::Update()
{
	{
		for (CGroup& g: groups) {
			// may invoke RemoveGroup, but can not cause iterator invalidation
			g.Update();
		}
	}

	{
		if (changedGroups.empty())
			return;

		std::vector<int> grpChg;

		// swap containers to prevent recursion through lua
		changedGroups.swap(grpChg);

		for (int i: grpChg) {
			eventHandler.GroupChanged(i);
		}
	}
}

bool CGroupHandler::GroupCommand(int num)
{
	if (KeyInput::GetKeyModState(KMOD_CTRL))
		return GroupCommand(num, (!KeyInput::GetKeyModState(KMOD_SHIFT))? "set": "add");

	if (KeyInput::GetKeyModState(KMOD_SHIFT))
		return GroupCommand(num, "selectadd");

	if (KeyInput::GetKeyModState(KMOD_ALT))
		return GroupCommand(num, "selecttoggle");

	return GroupCommand(num, "");
}

bool CGroupHandler::GroupCommand(int num, const std::string& cmd)
{
	CGroup* group = GetGroup(num);

	switch (hashString(cmd.c_str())) {
		case hashString("set"):
		case hashString("add"): {
			if (cmd[0] == 's')
				group->ClearUnits();

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

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

				// change group, but do not call SUH::AddUnit while iterating
				u->SetGroup(group, false, false);
			}
		} break;

		case hashString("selectadd"): {
			// do not select the group, just add its members to the current selection
			for (const int unitID: group->units) {
				selectedUnitsHandler.AddUnit(unitHandler.GetUnit(unitID));
			}

			return true;
		} break;

		case hashString("selectclear"): {
			// do not select the group, just remove its members from the current selection
			for (const int unitID: group->units) {
				selectedUnitsHandler.RemoveUnit(unitHandler.GetUnit(unitID));
			}

			return true;
		} break;

		case hashString("selecttoggle"): {
			// do not select the group, just toggle its members with the current selection
			const auto& selUnits = selectedUnitsHandler.selectedUnits;

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

				if (selUnits.find(unitID) == selUnits.end()) {
					selectedUnitsHandler.AddUnit(unit);
				} else {
					selectedUnitsHandler.RemoveUnit(unit);
				}
			}

			return true;
		} break;
	}

	if (group->units.empty())
		return false;

	if (selectedUnitsHandler.IsGroupSelected(num)) {
		camHandler->CameraTransition(0.5f);
		camHandler->GetCurrentController().SetPos(group->CalculateCenter());
	}

	selectedUnitsHandler.SelectGroup(num);
	return true;
}


CGroup* CGroupHandler::CreateNewGroup()
{
	if (freeGroups.empty()) {
		groups.emplace_back(firstUnusedGroup++, team);
		return &groups[groups.size() - 1];
	}

	return &groups[ spring::VectorBackPop(freeGroups) ];
}

void CGroupHandler::RemoveGroup(CGroup* group)
{
	if (group->id < FIRST_SPECIAL_GROUP) {
		LOG_L(L_WARNING, "[GroupHandler::%s] trying to remove hot-key group %i", __func__, group->id);
		return;
	}

	if (selectedUnitsHandler.IsGroupSelected(group->id))
		selectedUnitsHandler.ClearSelected();

	group->ClearUnits();
	freeGroups.push_back(group->id);
}

void CGroupHandler::PushGroupChange(int id)
{
	spring::VectorInsertUnique(changedGroups, id, true);
}