1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 #include "IPathDrawer.h"
3 #include "DefaultPathDrawer.h"
4 #include "QTPFSPathDrawer.h"
5 #include "Game/SelectedUnitsHandler.h"
6 #include "Sim/MoveTypes/MoveDefHandler.h"
7 #include "Sim/Path/IPathManager.h"
8 #include "Sim/Path/Default/PathManager.h"
9 #include "Sim/Path/QTPFS/PathManager.hpp"
10 #include "Sim/Units/Unit.h"
11 #include "Sim/Units/UnitDef.h"
12 #include "System/EventHandler.h"
13 
14 IPathDrawer* pathDrawer = NULL;
15 
GetInstance()16 IPathDrawer* IPathDrawer::GetInstance() {
17 	static IPathDrawer* pd = NULL;
18 
19 	if (pd == NULL) {
20 		if (dynamic_cast<QTPFS::PathManager*>(pathManager) != NULL) {
21 			return (pd = new QTPFSPathDrawer());
22 		}
23 		if (dynamic_cast<CPathManager*>(pathManager) != NULL) {
24 			return (pd = new DefaultPathDrawer());
25 		}
26 
27 		pd = new IPathDrawer();
28 	}
29 
30 	return pd;
31 }
32 
FreeInstance(IPathDrawer * pd)33 void IPathDrawer::FreeInstance(IPathDrawer* pd) {
34 	delete pd;
35 }
36 
37 
38 
IPathDrawer()39 IPathDrawer::IPathDrawer(): CEventClient("[IPathDrawer]", 271991, false), enabled(false) {
40 	eventHandler.AddClient(this);
41 }
~IPathDrawer()42 IPathDrawer::~IPathDrawer() {
43 	eventHandler.RemoveClient(this);
44 }
45 
GetSelectedMoveDef()46 const MoveDef* IPathDrawer::GetSelectedMoveDef() {
47 	const MoveDef* md = NULL;
48 	const CUnitSet& unitSet = selectedUnitsHandler.selectedUnits;
49 
50 	if (!unitSet.empty()) {
51 		const CUnit* unit = *(unitSet.begin());
52 		md = unit->moveDef;
53 	}
54 
55 	return md;
56 }
57 
GetSpeedModColor(const float sm)58 SColor IPathDrawer::GetSpeedModColor(const float sm) {
59 	SColor col(120, 0, 80);
60 
61 	if (sm > 0.0f) {
62 		col.r = 255 - std::min(sm * 255.0f, 255.0f);
63 		col.g = 255 - col.r;
64 		col.b =   0;
65 	}
66 
67 	return col;
68 }
69 
70 #if 0
71 float IPathDrawer::GetSpeedModNoObstacles(const MoveDef* md, int sqx, int sqz) {
72 	float m = 0.0f;
73 
74 	const int hmIdx = sqz * gs->mapxp1 + sqx;
75 	const int cnIdx = sqz * gs->mapx   + sqx;
76 
77 	const float height = hm[hmIdx];
78 	const float slope = 1.0f - cn[cnIdx].y;
79 
80 	if (md->speedModClass == MoveDef::Ship) {
81 		// only check water depth
82 		m = (height >= (-md->depth))? 0.0f: m;
83 	} else {
84 		// check depth and slope (if hover, only over land)
85 		m = std::max(0.0f, 1.0f - (slope / (md->maxSlope + 0.1f)));
86 		m = (height < (-md->depth))? 0.0f: m;
87 		m = (height <= 0.0f && md->speedModClass == MoveDef::Hover)? 1.0f: m;
88 	}
89 
90 	return m;
91 }
92 #endif
93 
94