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

#include "FlareProjectile.h"
#include "Game/Camera.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Env/Particles/ProjectileDrawer.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/Textures/TextureAtlas.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Projectiles/WeaponProjectiles/MissileProjectile.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/Unit.h"

CR_BIND_DERIVED(CFlareProjectile, CProjectile, )

CR_REG_METADATA(CFlareProjectile, (
	CR_SETFLAG(CF_Synced),
	CR_MEMBER(activateFrame),
	CR_MEMBER(deathFrame),

	CR_MEMBER(numSubProjs),
	CR_MEMBER(maxSubProjs),
	CR_MEMBER(lastSubProj),
	CR_MEMBER(subProjPos),
	CR_MEMBER(subProjVel),
	CR_MEMBER(alphaFalloff)
))

CFlareProjectile::CFlareProjectile(const float3& pos, const float3& speed, CUnit* owner, int activateFrame):
	// these are synced, but neither weapon nor piece
	// (only created by units that can drop flares)
	CProjectile(pos, speed, owner, true, false, false),

	activateFrame(activateFrame),
	deathFrame(activateFrame + ((owner != nullptr)? owner->unitDef->flareTime: 1)),

	numSubProjs(0),
	maxSubProjs(std::min((owner != nullptr)? owner->unitDef->flareSalvoSize: 0, int(subProjPos.size()))),
	lastSubProj(0)
{
	alphaFalloff = (owner != nullptr)? 1.0f / owner->unitDef->flareTime: 1.0f;
	checkCol = false;
	useAirLos = true;

	SetRadiusAndHeight(45.0f, 0.0f);

	// flares fall slower
	mygravity *= 0.3f;
}


void CFlareProjectile::Update()
{
	const CUnit* owner = CProjectile::owner();
	const UnitDef* ownerDef = (owner != nullptr)? owner->unitDef: nullptr;

	if (gs->frameNum == activateFrame) {
		if (owner != nullptr) {
			SetPosition(owner->pos);
			CWorldObject::SetVelocity(owner->speed);
			CWorldObject::SetVelocity(speed + (owner->rightdir * ownerDef->flareDropVector.x));
			CWorldObject::SetVelocity(speed + (owner->updir    * ownerDef->flareDropVector.y));
			CWorldObject::SetVelocity(speed + (owner->frontdir * ownerDef->flareDropVector.z));
			SetVelocityAndSpeed(speed);
		} else {
			deleteMe = true;
		}
	}
	if (gs->frameNum >= activateFrame) {
		SetPosition(pos + speed);
		SetVelocityAndSpeed((speed * 0.95f) + (UpVector * mygravity));

		//FIXME: just spawn new flares, if new missiles incoming?
		if (owner != nullptr && lastSubProj < (gs->frameNum - ownerDef->flareSalvoDelay) && numSubProjs < maxSubProjs) {
			subProjPos[numSubProjs] = owner->pos;
			subProjVel[numSubProjs] = owner->speed;

			subProjVel[numSubProjs] += (owner->rightdir * ownerDef->flareDropVector.x);
			subProjVel[numSubProjs] += (owner->updir    * ownerDef->flareDropVector.y);
			subProjVel[numSubProjs] += (owner->frontdir * ownerDef->flareDropVector.z);

			numSubProjs += 1;
			lastSubProj = gs->frameNum;

			// try to trick at least one missile into retargeting us
			// (this means flares can steal missiles from each other)
			for (CMissileProjectile* missile: owner->incomingMissiles) {
				if (missile == nullptr)
					continue;

				if (gsRNG.NextFloat() >= ownerDef->flareEfficiency)
					continue;

				missile->SetTargetObject(this);
				missile->AddDeathDependence(this, DEPENDENCE_DECOYTARGET);
			}
		}

		for (int a = 0; a < numSubProjs; ++a) {
			subProjPos[a] += subProjVel[a];
			subProjVel[a] *= 0.95f;
			subProjVel[a].y += mygravity;
		}
	}

	deleteMe |= (gs->frameNum >= deathFrame);
}

void CFlareProjectile::Draw(GL::RenderDataBufferTC* va) const
{
	if (gs->frameNum <= activateFrame)
		return;

	constexpr float radius = 6.0f;
	const     float alpha = std::max(0.0f, 1.0f - (gs->frameNum - activateFrame) * alphaFalloff);

	unsigned char col[4];
	col[0] = (unsigned char) (alpha * 1.0f) * 255;
	col[1] = (unsigned char) (alpha * 0.5f) * 255;
	col[2] = (unsigned char) (alpha * 0.2f) * 255;
	col[3] = 1;

	for (int a = 0; a < numSubProjs; ++a) {
		const float3 interPos = subProjPos[a] + subProjVel[a] * globalRendering->timeOffset;

		#define fpt projectileDrawer->flareprojectiletex
		va->SafeAppend({interPos - camera->GetRight() * radius - camera->GetUp() * radius, fpt->xstart, fpt->ystart, col});
		va->SafeAppend({interPos + camera->GetRight() * radius - camera->GetUp() * radius, fpt->xend,   fpt->ystart, col});
		va->SafeAppend({interPos + camera->GetRight() * radius + camera->GetUp() * radius, fpt->xend,   fpt->yend,   col});

		va->SafeAppend({interPos + camera->GetRight() * radius + camera->GetUp() * radius, fpt->xend,   fpt->yend,   col});
		va->SafeAppend({interPos - camera->GetRight() * radius + camera->GetUp() * radius, fpt->xstart, fpt->yend,   col});
		va->SafeAppend({interPos - camera->GetRight() * radius - camera->GetUp() * radius, fpt->xstart, fpt->ystart, col});
		#undef fpt
	}
}