File: GeometricObjects.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 (156 lines) | stat: -rw-r--r-- 3,823 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "System/creg/STL_Map.h"
#include "GeometricObjects.h"
#include "Map/ReadMap.h"
#include "Sim/Projectiles/ProjectileHandler.h"
#include "Sim/Projectiles/ProjectileMemPool.h"
#include "Rendering/Env/Particles/Classes/GeoSquareProjectile.h"

CR_BIND(CGeometricObjects, )
CR_BIND(CGeometricObjects::GeoGroup, )

CR_REG_METADATA(CGeometricObjects, (
	CR_MEMBER(geoGroups),
	CR_MEMBER(timedGroups),
	CR_MEMBER(firstFreeGroup)
))

CR_REG_METADATA_SUB(CGeometricObjects, GeoGroup, (CR_MEMBER(squares)))


CGeometricObjects* geometricObjects;

CGeometricObjects::~CGeometricObjects()
{
	timedGroups.clear();
	while (!geoGroups.empty()) {
		DeleteGroup(geoGroups.begin()->first);
	}
}

int CGeometricObjects::AddSpline(float3 b1, float3 b2, float3 b3, float3 b4, float width, int arrow, int lifeTime, int group)
{
	if (group == 0)
		group = firstFreeGroup++;

	float3 old1, old2;
	old1 = CalcSpline(0.00f, b1, b2, b3, b4);
	old2 = CalcSpline(0.05f, b1, b2, b3, b4);

	for (int a = 0; a < 20; ++a) {
		const float3 np = CalcSpline(a*0.05f + 0.1f, b1, b2, b3, b4);
		const float3 dir1 = (old2 - old1).ANormalize();
		const float3 dir2 = (np - old2).ANormalize();

		float w1, w2;
		if ((arrow == 1) && (a == 19)) {
			w1 = width;
			w2 = 0;
		} else {
			w1 = width * 0.5f;
			w2 = w1;
		}
		CGeoSquareProjectile* gsp = projMemPool.alloc<CGeoSquareProjectile>(old1, old2, dir1, dir2, w1, w2);
		geoGroups[group].squares.push_back(gsp);
		old1 = old2;
		old2 = np;
	}

	if (lifeTime > 0)
		timedGroups[gs->frameNum + lifeTime].push_back(group);

	return group;
}


void CGeometricObjects::DeleteGroup(int group)
{
	GeoGroup* gg = &geoGroups[group];

	for (auto gi = gg->squares.begin(); gi != gg->squares.end(); ++gi) {
		(*gi)->deleteMe = true;
	}

	geoGroups.erase(group);
}


void CGeometricObjects::SetColor(int group, float r, float g, float b, float a)
{
	GeoGroup* gg = &geoGroups[group];

	for (auto gi = gg->squares.begin(); gi != gg->squares.end(); ++gi) {
		(*gi)->SetColor(r, g, b, a);
	}
}


float3 CGeometricObjects::CalcSpline(float i, const float3& p1, const float3& p2, const float3& p3, const float3& p4)
{
	float ni = 1 - i;

	float3 res = p1*ni*ni*ni + p2*3*i*ni*ni + p3*3*i*i*ni + p4*i*i*i;
	return res;
}


int CGeometricObjects::AddLine(float3 start, float3 end, float width, int arrow, int lifetime, int group)
{
	if (group == 0)
		group = firstFreeGroup++;

	float3 dir = (end - start).SafeANormalize();
	if (arrow) {
		CGeoSquareProjectile* gsp = projMemPool.alloc<CGeoSquareProjectile>(start, start*0.2f + end*0.8f, dir, dir, width*0.5f, width*0.5f);
		geoGroups[group].squares.push_back(gsp);

		gsp = projMemPool.alloc<CGeoSquareProjectile>(start*0.2f + end*0.8f, end, dir, dir, width, 0);
		geoGroups[group].squares.push_back(gsp);

	} else {
		CGeoSquareProjectile* gsp = projMemPool.alloc<CGeoSquareProjectile>(start, end, dir, dir, width*0.5f, width*0.5f);
		geoGroups[group].squares.push_back(gsp);
	}

	if (lifetime > 0)
		timedGroups[gs->frameNum + lifetime].push_back(group);

	return group;
}


void CGeometricObjects::Update()
{
	const auto iter = timedGroups.find(gs->frameNum);

	if (iter == timedGroups.end())
		return;

	for (const int groupID: iter->second) {
		DeleteGroup(groupID);
	}

	timedGroups.erase(iter->first);
}


void CGeometricObjects::MarkSquare(int mapSquare)
{
	float3 startPos;
	startPos.x = (int) (mapSquare * SQUARE_SIZE) % mapDims.mapx;
	startPos.z = (int) (mapSquare * SQUARE_SIZE) / mapDims.mapx;
	startPos.y = readMap->GetCenterHeightMapSynced()[mapSquare];

	float3 endPos = startPos;
	endPos.x += SQUARE_SIZE;
	endPos.z += SQUARE_SIZE;

	AddLine(startPos, endPos, 3, 0, 1000);

	startPos.x += SQUARE_SIZE;
	endPos.x -= SQUARE_SIZE;
	AddLine(startPos, endPos, 3, 0, 1000);
}