File: selection.cpp

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (412 lines) | stat: -rw-r--r-- 11,802 bytes parent folder | download
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
// ==============================================================
//	This file is part of Glest (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================

#include "selection.h"

#include <algorithm>

#include "unit_type.h"
#include "gui.h"
#include "config.h"
#include "world.h"
#include "leak_dumper.h"

using namespace std;

namespace Glest{ namespace Game{

// =====================================================
// 	class Selection
// =====================================================

void Selection::init(Gui *gui, int factionIndex, int teamIndex, bool allowSharedTeamUnits) {
	this->factionIndex 			= factionIndex;
	this->teamIndex 			= teamIndex;
	this->allowSharedTeamUnits	= allowSharedTeamUnits;
	this->gui					= gui;
	clear();
}

Selection::~Selection(){
	clear();
}

bool Selection::canSelectUnitFactionCheck(const Unit *unit) const {
	//check if enemy
	if(unit->getFactionIndex() != factionIndex) {
		if(this->allowSharedTeamUnits == false ||
			unit->getFaction()->getTeam() != teamIndex) {
			return false;
		}
	}

	return true;
}

bool Selection::select(Unit *unit) {
	bool result = false;
	if((int)selectedUnits.size() >= Config::getInstance().getInt("MaxUnitSelectCount",intToStr(maxUnits).c_str())) {
		return result;
	}

	// Fix Bug reported on sourceforge.net: Glest::Game::Selection::select crash with NULL pointer - ID: 3608835
	if(unit != NULL) {
		//check if already selected
		for(int index = 0; index < (int)selectedUnits.size(); ++index) {
			if(selectedUnits[index] == unit) {
				return true;
			}
		}

		//check if dead
		if(unit->isDead() == true) {
			return false;
		}

		//check if commandable
		if(unit->getType()->isCommandable() == false && isEmpty() == false) {
			return false;
		}

		//check if multisel
		if(unit->getType()->getMultiSelect() == false && isEmpty() == false) {
			return false;
		}

		//check if enemy
		if(canSelectUnitFactionCheck(unit) == false && isEmpty() == false) {
			return false;
		}

		//check existing enemy
		//if(selectedUnits.size() == 1 && selectedUnits.front()->getFactionIndex() != factionIndex) {
		if(selectedUnits.size() == 1 && canSelectUnitFactionCheck(selectedUnits.front()) == false) {
			clear();
		}

		//check existing multisel
		if(selectedUnits.size() == 1 &&
				selectedUnits.front()->getType()->getMultiSelect() == false) {
			clear();
		}

		if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit selected [%s]\n",__FILE__,__FUNCTION__,__LINE__,unit->toString().c_str());

		unit->addObserver(this);

		int unitTypeId = unit->getType()->getId();
		bool inserted = false;
		for(int index = 0; index < (int)selectedUnits.size(); ++index) {

			int currentTypeId = selectedUnits[index]->getType()->getId();
			if(unitTypeId <= currentTypeId) {

				//place unit here
				selectedUnits.insert(selectedUnits.begin() + index,unit);
				inserted = true;
				break;
			}
		}
		if(inserted == false) {
			selectedUnits.push_back(unit);
		}
		result = true;
		gui->onSelectionChanged();
	}

	return result;
}

void Selection::select(const UnitContainer &units){

	//add units to gui
	for(UnitIterator it = units.begin(); it != units.end(); ++it) {
		select(*it);
	}
}

void Selection::unSelect(const UnitContainer &units) {

	//add units to gui
	for(UnitIterator it = units.begin(); it != units.end(); ++it) {
		for(int i = 0; i < (int)selectedUnits.size(); ++i) {
			if(selectedUnits[i] == *it) {
				unSelect(i);
			}
		}
	}
}

void Selection::unSelect(int i) {
	selectedUnits.erase(selectedUnits.begin() + i);
	gui->onSelectionChanged();
}

void Selection::clear(){
	selectedUnits.clear();
}

bool Selection::isUniform() const{
	if(selectedUnits.empty() == true) {
		return true;
	}

	const UnitType *ut= selectedUnits.front()->getType();

	for(int i = 0; i < (int)selectedUnits.size(); ++i) {
		if(selectedUnits[i]->getType() != ut) {
            return false;
		}
    }
    return true;
}

bool Selection::isEnemy() const {
	return selectedUnits.size() == 1 &&
			//selectedUnits.front()->getFactionIndex() != factionIndex;
			canSelectUnitFactionCheck(selectedUnits.front()) == false;
}

bool Selection::isObserver() const {
	return selectedUnits.size() == 1 &&
			(teamIndex == (GameConstants::maxPlayers -1 + fpt_Observer));
}

bool Selection::isCommandable() const {
	//printf("\n\n\n\n********* selection.isCommandable() ---> isEmpty() [%d] isEnemy() [%d] selectedUnits.size() [%d]\n\n",isEmpty(),isEnemy(),(int)selectedUnits.size());

	return
		isEmpty() == false &&
		isEnemy() == false &&
		(selectedUnits.size() == 1 && selectedUnits.front()->isAlive() == false) == false &&
		selectedUnits.front()->getType()->isCommandable();
}

bool Selection::isCancelable() const {
	return
		selectedUnits.size() > 1 ||
		(selectedUnits.size() == 1 && selectedUnits[0]->anyCommand(true));
}

bool Selection::isMeetable() const{
	return
		isUniform() &&
		isCommandable() &&
		selectedUnits.front()->getType()->getMeetingPoint();
}

Vec3f Selection::getRefPos() const{
	return getFrontUnit()->getCurrMidHeightVector();
}

bool Selection::hasUnit(const Unit* unit) const {
	return find(selectedUnits.begin(), selectedUnits.end(), unit) != selectedUnits.end();
}

bool Selection::assignGroup(int groupIndex, bool clearGroup,const UnitContainer *pUnits) {
	if(groupIndex < 0 || groupIndex >= maxGroups) {
		throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
	}

	//clear group
	if(true==clearGroup){
		groups[groupIndex].clear();
	}

	//assign new group
	const UnitContainer *addUnits = &selectedUnits;
	if(pUnits != NULL) {
		addUnits = pUnits;
	}

	for(unsigned int i = 0; i < addUnits->size(); ++i) {
		if(false == addUnitToGroup(groupIndex,(*addUnits)[i])){
			// don't try to add more, group is maybe full
			return false;
		}
	}
	return true;
}

/**
 * returns false if unit cannot be added
 */
bool Selection::addUnitToGroup(int groupIndex,Unit *unit) {
	if(groupIndex < 0 || groupIndex >= maxGroups) {
		throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
	}
	bool alreadyExists=false;
	bool groupIsFull=(int)groups[groupIndex].size() >= Config::getInstance().getInt("MaxUnitSelectCount",intToStr(maxUnits).c_str());

	for(int i = 0; i < (int)groups[groupIndex].size(); ++i) {
		if(groups[groupIndex][i] == unit) {
			alreadyExists=true;
			break;
		}
	}

	if(alreadyExists){
		return true;
	}

	// check for non Multiselect units
	if((int)groups[groupIndex].size()>0 ){
		if( !unit->getType()->getMultiSelect()){
			//dont add single selection units to already filled group
			return false;
		}
		Unit* unitInGroup=groups[groupIndex][0];
		if( !unitInGroup->getType()->getMultiSelect()){
			//dont add a unit to a group which has a single selection unit
			return false;
		}
	}

	if(unit != NULL && !groupIsFull) {
		groups[groupIndex].push_back(unit);
		return true;
	}
	else{
		return false;
	}
}

void Selection::removeUnitFromGroup(int groupIndex,int unitId) {
	if(groupIndex < 0 || groupIndex >= maxGroups) {
		throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
	}

	for(unsigned int i = 0; i < groups[groupIndex].size(); ++i) {
		Unit *unit = groups[groupIndex][i];
		if(unit != NULL && unit->getId() == unitId) {
			groups[groupIndex].erase(groups[groupIndex].begin() + i);
			break;
		}
	}
}

vector<Unit*> Selection::getUnitsForGroup(int groupIndex) {
	if(groupIndex < 0 || groupIndex >= maxGroups) {
		throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
	}
	return groups[groupIndex];
}

void Selection::recallGroup(int groupIndex,bool clearSelection){
	if(groupIndex < 0 || groupIndex >= maxGroups) {
		throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex));
	}

	if(clearSelection==true){
		clear();
	}
	for(int i = 0; i < (int)groups[groupIndex].size(); ++i) {
		select(groups[groupIndex][i]);
	}
}

void Selection::unitEvent(UnitObserver::Event event, const Unit *unit) {

	if(event == UnitObserver::eKill) {

		//remove from selection
		for(int index = 0; index < (int)selectedUnits.size(); ++index) {
			if(selectedUnits[index] == unit){
				selectedUnits.erase(selectedUnits.begin() + index);
				break;
			}
		}

		//remove from groups
		for(int index = 0; index < maxGroups; ++index) {
			for(int index2 = 0; index2 < (int)groups[index].size(); ++index2) {
				if(groups[index][index2] == unit) {
					groups[index].erase(groups[index].begin() + index2);
					break;
				}
			}
		}

		//notify gui only if no more units to execute the command
		//of course the selection changed, but this doesn't matter in this case.
		if( selectedUnits.empty() == true) {
			gui->onSelectionChanged();
		}
	}
}

void Selection::saveGame(XmlNode *rootNode) const {

	std::map<string,string> mapTagReplacements;
	XmlNode *selectionNode = rootNode->addChild("Selection");

	selectionNode->addAttribute("factionIndex",intToStr(factionIndex), mapTagReplacements);
	selectionNode->addAttribute("teamIndex",intToStr(teamIndex), mapTagReplacements);
	selectionNode->addAttribute("allowSharedTeamUnits",intToStr(allowSharedTeamUnits), mapTagReplacements);

	for(unsigned int i = 0; i < selectedUnits.size(); i++) {
		Unit *unit = selectedUnits[i];

		XmlNode *selectedUnitsNode = selectionNode->addChild("selectedUnits");
		selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements);
	}

	for(unsigned int x = 0; x < (unsigned int)maxGroups; ++x) {
		XmlNode *groupsNode = selectionNode->addChild("groups");
		for(unsigned int i = 0; i < (unsigned int)groups[x].size(); ++i) {
			Unit *unit = groups[x][i];

			XmlNode *selectedUnitsNode = groupsNode->addChild("selectedUnits");
			selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements);
		}
	}
}

void Selection::loadGame(const XmlNode *rootNode, World *world) {

	const XmlNode *selectionNode = rootNode->getChild("Selection");

	factionIndex = selectionNode->getAttribute("factionIndex")->getIntValue();
	teamIndex = selectionNode->getAttribute("teamIndex")->getIntValue();
	if(selectionNode->hasAttribute("allowSharedTeamUnits") == true) {
		allowSharedTeamUnits = (selectionNode->getAttribute("allowSharedTeamUnits")->getIntValue() != 0);
	}

	vector<XmlNode *> selectedUnitsNodeList = selectionNode->getChildList("selectedUnits");
	for(unsigned int i = 0; i < selectedUnitsNodeList.size(); ++i) {
		XmlNode *selectedUnitsNode = selectedUnitsNodeList[i];

		int unitId = selectedUnitsNode->getAttribute("unitId")->getIntValue();
		Unit *unit = world->findUnitById(unitId);
		//assert(unit != NULL);
		//printf("#1 Unit [%s], group: %d\n",unit->getType()->getName().c_str(),i);
		selectedUnits.push_back(unit);
	}

	vector<XmlNode *> groupsNodeList = selectionNode->getChildList("groups");
	for(unsigned int i = 0; i < groupsNodeList.size(); ++i) {
		XmlNode *groupsNode = groupsNodeList[i];

		vector<XmlNode *> selectedGroupsUnitsNodeList = groupsNode->getChildList("selectedUnits");
		for(unsigned int j = 0; j < selectedGroupsUnitsNodeList.size(); ++j) {
			XmlNode *selectedGroupsUnitsNode = selectedGroupsUnitsNodeList[j];

			int unitId = selectedGroupsUnitsNode->getAttribute("unitId")->getIntValue();
			Unit *unit = world->findUnitById(unitId);
			//assert(unit != NULL);
			//printf("Unit #2 [%s], group: %d\n",unit->getType()->getName().c_str(),i);
			groups[i].push_back(unit);
		}
	}
}


}}//end namespace