-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGadgetDropPod.cpp
More file actions
206 lines (165 loc) · 4.84 KB
/
GadgetDropPod.cpp
File metadata and controls
206 lines (165 loc) · 4.84 KB
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
/*
GadgetDropPod.h
Gadget that creates an infantry unit in a pod for boarding
*/
#include "Debug/Assert.h"
#include "GadgetDropPod.h"
#include "NounPod.h"
#include "NounShip.h"
#include "NounUnit.h"
#include "World/VerbChat.h"
#include "GameContext.h"
IMPLEMENT_ABSTRACT_FACTORY( GadgetDropPod, NounGadget );
BEGIN_ABSTRACT_PROPERTY_LIST( GadgetDropPod, NounGadget );
ADD_TRANSMIT_UPDATE_PROPERTY( m_Energy );
END_PROPERTY_LIST();
GadgetDropPod::GadgetDropPod()
{}
void GadgetDropPod::setup()
{
NounGadget::setup();
m_Energy = energyNeeded();
}
void GadgetDropPod::initialize()
{
NounGadget::initialize();
loadUnit();
}
dword GadgetDropPod::hotkey() const
{
return 'V';
}
//---------------------------------------------------------
// NounGadget interface
NounGadget::Type GadgetDropPod::type() const
{
return SPECIAL;
}
NounGadget::EnergyClass GadgetDropPod::energyClass() const
{
return ENERGY_CLASS_SPECIAL;
}
int GadgetDropPod::usableWhen()
{
float percentleft = (energyNeeded() - m_Energy) / energyNeeded();
return 100 - (int)(percentleft * 100);
}
bool GadgetDropPod::usable( Noun * pTarget, bool shift ) const
{
NounShip * pShip = WidgetCast<NounShip>( parent() );
NounShip * pShipTarget = WidgetCast<NounShip>( pTarget );
NounPlanet * pPlanetTarget = WidgetCast<NounPlanet>( pTarget );
int teamId = -1;
if( pShipTarget != NULL)
{
teamId = pShipTarget->teamId();
}
else if( pPlanetTarget != NULL)
{
teamId = pPlanetTarget->teamId();
}
else
{
return false; //if both possible target types are null then this device can't be used
}
if (! NounGadget::usable( pTarget, shift ) )
return false;
if ( destroyed() ) //destroyed gadget
return false;
if ( m_Energy < energyNeeded() ) //recharging
return false;
if ( pShip->teamId() == teamId ) //same team
return false;
if ( pShip == NULL ) //parent is not a ship
return false;
if( !canDropOnPlanet() && pShipTarget == NULL ) //not targetting a ship and can't drop on planet
return false;
if ( pShipTarget == pShip || shift ) //targeting self
return false;
if ( pShip->testFlags( NounShip::FLAG_CLOAKED|NounShip::FLAG_IN_SAFE_ZONE ) )
return false;
if ( pShipTarget != NULL && pShip->testFlags( NounShip::FLAG_CLOAKED|NounShip::FLAG_IN_SAFE_ZONE ) )
return false;
float distance = (worldPosition() - pTarget->worldPosition()).magnitude();
if(distance > range())
return false;
return true;
}
void GadgetDropPod::use( dword when, Noun * pTarget, bool shift)
{
if( shift ) return;
if(WidgetCast<NounPlanet>( pTarget ) && !canDropOnPlanet() ) return;
// Can't drop on planets if you're not allowed
NounGadget::use( when, pTarget, shift );
createUseEffect( false );
m_Energy = 0;
m_nEnergyTick = when;
//spawn the drop pod at target
NounShip * pShip = WidgetCast<NounShip>( parent() );
NounGame * pDropTarget = WidgetCast<NounGame>( pTarget );
if( isServer() && pShip != NULL && pDropTarget != NULL )
{
//Set up the infantry
NounUnit * pSpawn = (NounUnit *)m_pUnit->copy();
ASSERT ( pSpawn );
pSpawn->setTeamId( pShip->teamId() );
pSpawn->setParent( pShip );
pSpawn->setHex( 0xffff );
pSpawn->setExperience( 0.40f );
pSpawn->setup();
pSpawn->setTick( tick() );
Noun * pHold = NounGame::findCargoHold( pDropTarget, pSpawn );
if ( pHold != NULL )
{
//Shove the infantry into the ship or planet
Vector3 vLocalPosition( pHold->worldFrame() * (pDropTarget->worldPosition() - pHold->worldPosition()) );
pSpawn->setPosition( vLocalPosition );
pHold->attachNode( pSpawn );
pDropTarget->context()->user()->saveStorage(pDropTarget);
pShip->message( CharString( "<color;0000ff>Security: Infantry have been launched!" ) );
}
}
}
int GadgetDropPod::useEnergy( dword nTick, int energy )
{
int nElapsed = nTick - m_nEnergyTick;
m_nEnergyTick = nTick;
if ( m_Energy < energyNeeded() )
{
m_fChargeRate += chargeRate() * nElapsed;
int chargeRate = floor( m_fChargeRate );
m_fChargeRate -= chargeRate; // leave fractional amount in the float for next update..
int charge = Min( Min( energyNeeded() - m_Energy, chargeRate ), energy );
energy -= charge;
m_Energy += charge;
}
return energy;
}
void GadgetDropPod::loadUnit()
{
m_pBuild = WidgetCast<Scene>( nounContext()->resource( "UNIT" ) );
if ( m_pBuild.valid() )
{
BaseNode * pRoot = m_pBuild->node();
ASSERT( pRoot );
for(int i=0;i<pRoot->childCount();i++)
{
NounUnit * pUnit = WidgetCast<NounUnit>( pRoot->child(i) );
if ( pUnit != NULL )
{
m_pUnit = pUnit;
return;
}
}
}
}
bool GadgetDropPod::updateLogic()
{
NounShip * pShip = WidgetCast<NounShip>(parent());
if( pShip == NULL ) //our parent isn't a ship? don't use this
return true;
if(! usable( pShip->commandTarget(), false ) ) //Device isn't ready? Don't use it yet
return true;
pShip->useGadget(this, pShip->commandTarget() , false );
return false;
}