File: IPathDrawer.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (93 lines) | stat: -rw-r--r-- 2,260 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "IPathDrawer.h"
#include "DefaultPathDrawer.h"
#include "QTPFSPathDrawer.h"
#include "Game/SelectedUnitsHandler.h"
#include "Sim/MoveTypes/MoveDefHandler.h"
#include "Sim/Path/IPathManager.h"
#include "Sim/Path/Default/PathManager.h"
#include "Sim/Path/QTPFS/PathManager.hpp"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitDef.h"
#include "System/EventHandler.h"

IPathDrawer* pathDrawer = NULL;

IPathDrawer* IPathDrawer::GetInstance() {
	if (pathDrawer == NULL) {
		if (dynamic_cast<QTPFS::PathManager*>(pathManager) != NULL) {
			return (pathDrawer = new QTPFSPathDrawer());
		}
		if (dynamic_cast<CPathManager*>(pathManager) != NULL) {
			return (pathDrawer = new DefaultPathDrawer());
		}

		pathDrawer = new IPathDrawer();
	}

	return pathDrawer;
}

void IPathDrawer::FreeInstance(IPathDrawer* pd) {
	assert(pd == pathDrawer);
	delete pd;
	pathDrawer = NULL;
}



IPathDrawer::IPathDrawer(): CEventClient("[IPathDrawer]", 271991, false), enabled(false) {
	eventHandler.AddClient(this);
}
IPathDrawer::~IPathDrawer() {
	eventHandler.RemoveClient(this);
}

const MoveDef* IPathDrawer::GetSelectedMoveDef() {
	const MoveDef* md = NULL;
	const CUnitSet& unitSet = selectedUnitsHandler.selectedUnits;

	if (!unitSet.empty()) {
		const CUnit* unit = *(unitSet.begin());
		md = unit->moveDef;
	}

	return md;
}

SColor IPathDrawer::GetSpeedModColor(const float sm) {
	SColor col(120, 0, 80);

	if (sm > 0.0f) {
		col.r = 255 - std::min(sm * 255.0f, 255.0f);
		col.g = 255 - col.r;
		col.b =   0;
	}

	return col;
}

#if 0
float IPathDrawer::GetSpeedModNoObstacles(const MoveDef* md, int sqx, int sqz) {
	float m = 0.0f;

	const int hmIdx = sqz * mapDims.mapxp1 + sqx;
	const int cnIdx = sqz * mapDims.mapx   + sqx;

	const float height = hm[hmIdx];
	const float slope = 1.0f - cn[cnIdx].y;

	if (md->speedModClass == MoveDef::Ship) {
		// only check water depth
		m = (height >= (-md->depth))? 0.0f: m;
	} else {
		// check depth and slope (if hover, only over land)
		m = std::max(0.0f, 1.0f - (slope / (md->maxSlope + 0.1f)));
		m = (height < (-md->depth))? 0.0f: m;
		m = (height <= 0.0f && md->speedModClass == MoveDef::Hover)? 1.0f: m;
	}

	return m;
}
#endif