-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGadgetAreaReload.cpp
More file actions
317 lines (258 loc) · 8.12 KB
/
GadgetAreaReload.cpp
File metadata and controls
317 lines (258 loc) · 8.12 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
@file GadgetAreaReload.cpp
(c)2009 Palestar Inc
@author Richard Lyle @date 6/13/2011 9:49:40 PM
*/
//---------------------------------------------------------------------------------------------------
#include "Debug/Assert.h"
#include "Math/Helpers.h"
#include "NounPlanet.h"
#include "GadgetAreaReload.h"
#include "GadgetWeapon.h"
#include "GadgetArmor.h"
#include "GadgetJumpDrive.h"
#include "GameContext.h"
#include "resource.h"
#include "StructureDefense.h"
#include "ShipPlatform.h"
#include "VerbGadgetOff.h"
//----------------------------------------------------------------------------
static Constant GADGET_REPAIR_POINTS( "GADGET_REPAIR_POINTS", 0.03f );
static Constant GADGET_REPAIR_MAX_DRONES( "GADGET_REPAIR_MAX_DRONES", 5 );
//----------------------------------------------------------------------------
IMPLEMENT_ABSTRACT_FACTORY( GadgetAreaReload, NounGadget );
BEGIN_ABSTRACT_PROPERTY_LIST( GadgetAreaReload, NounGadget );
ADD_REPLICATED_PROPERTY( m_Flags, TICKS_PER_SECOND );
ADD_REPLICATED_PROPERTY( m_Reloads, TICKS_PER_SECOND * 10 );
END_PROPERTY_LIST();
GadgetAreaReload::GadgetAreaReload() :
m_nReloadTick( 0 ),
m_nNextReloadIndex( 0 ),
m_Reloads( 0 ),
m_DroneCount( 0 )
{}
//----------------------------------------------------------------------------
void GadgetAreaReload::render( RenderContext & context, const Matrix33 & frame, const Vector3 & position )
{
if ( useActive() )
{
Scene * pUseEffect = useEffect();
if ( pUseEffect != NULL )
pUseEffect->render( context, frame, position );
}
}
//----------------------------------------------------------------------------
void GadgetAreaReload::setup()
{
NounGadget::setup();
m_Reloads = maxReloads();
}
void GadgetAreaReload::release()
{
NounGadget::release();
m_Reloading.release();
}
static int ReloadSort( GadgetAreaReload::NounCollision e1, GadgetAreaReload::NounCollision e2 )
{
return ((int)e1.pNoun->key()) - ((int)e2.pNoun->key());
}
void GadgetAreaReload::simulate( dword nTick )
{
if ( nTick >= m_nReloadTick && useActive() )
{
m_nReloadTick = nTick + (TICKS_PER_SECOND * calculateModifier( MT_RELOAD, true ));
// get our parent ship (might not be attached to a ship)
NounBody * pParent = parentBody();
bool bValid = true;
if ( WidgetCast<NounShip>( pParent )
&& ((NounShip *)pParent)->testFlags( NounShip::FLAG_CLOAKED ) )
bValid = false; // can't reload while cloaked
else if ( m_Reloads <= 0 || destroyed() )
bValid = false; // out of reloads or this device is destroyed
if ( bValid )
{
m_Reloading.release();
context()->proximityCheck( worldPosition(), reloadRange(), m_Reloading, CLASS_KEY(NounShip) );
// sort collisions by ID, so we process them in the same order if possible..
m_Reloading.qsort( ReloadSort );
bool bReloadedShip = false;
for(int i=0;i<m_Reloading.size() && !bReloadedShip;++i)
{
// reload the next ship...
if ( m_nNextReloadIndex >= m_Reloading.size() )
m_nNextReloadIndex = 0;
if ( m_nNextReloadIndex < m_Reloading.size() )
{
NounShip * pReload = WidgetCast<NounShip>( m_Reloading[ m_nNextReloadIndex++ ].pNoun );
if ( pReload != NULL && pReload != pParent
&& pReload->isFriend( pParent )
&& ( pReload->needReload( pParent ) || pReload->needRepair( pParent ) ) )
{
if ( pReload != pParent )
pReload->reload( pParent );
if ( pReload->isOutOfCombat() )
{
// increase repair by 50% if out of combat
pReload->repair( pParent, gadgetRepairRate() * 1.5, true );
pReload->repair( pParent, hullRepairRate() * 1.5, false );
}
else
{
pReload->repair( pParent, gadgetRepairRate(), true );
pReload->repair( pParent, hullRepairRate(), false );
}
--m_Reloads;
if ( isClient() && m_DroneCount < GADGET_REPAIR_MAX_DRONES )
{
m_DroneCount++;
Vector3 offset( RandomVector( -1.0f, 1.0f ) );
offset.normalize();
NounDrone * pDrone = new NounDrone;
pDrone->setHome( this, Vector3( 0.0f ) );
pDrone->setPosition( worldPosition() );
pDrone->setTarget( pReload, offset * pReload->radius() );
pDrone->setSpeed( 10.0f );
pDrone->setVelocity( 0.0f );
pDrone->setDuration( TICKS_PER_SECOND * 2 );
context()->attachNoun( pDrone );
}
if ( pReload != pParent )
gameContext()->gameUser()->onRepair( pParent, GADGET_REPAIR_POINTS );
// set flag to true, we only reload/repair one ship per second.. as more ships enter the AOI
// the longer it will take to repair them.
bReloadedShip = true;
}
}
}
}
}
NounGadget::simulate( nTick );
}
//----------------------------------------------------------------------------
NounGadget::Type GadgetAreaReload::type() const
{
return DRONE_REPAIR;
}
dword GadgetAreaReload::hotkey() const
{
return 'I';
}
CharString GadgetAreaReload::status() const
{
return CharString().format("%d", reloads());
}
bool GadgetAreaReload::usable( Noun * pTarget, bool shift ) const
{
if ( useActive() )
return true; // allow stop reloading
if (! NounGadget::usable( pTarget, shift ) )
return false;
return true;
}
bool GadgetAreaReload::useActive() const
{
return (flags() & FLAG_ACTIVE) != 0;
}
bool GadgetAreaReload::needReload( Noun * pWhom ) const
{
if ( WidgetCast<NounPlanet>( pWhom ) != NULL
|| WidgetCast<ShipPlatform>( pWhom ) )
{
return reloads() < maxReloads();
}
return false;
}
bool GadgetAreaReload::reload( Noun * pWhom )
{
if ( WidgetCast<NounPlanet>( pWhom ) != NULL
|| WidgetCast<ShipPlatform>( pWhom ) )
{
m_Reloads = Clamp( m_Reloads + 100, 0, maxReloads() );
return m_Reloads < maxReloads();
}
// only planets can reload this device!
return false;
}
void GadgetAreaReload::use( dword when, Noun * pTarget, bool shift)
{
if ( useActive() )
NounGadget::use( when, pTarget, shift );
if ( isServer() )
{
// set the device active
setFlags( FLAG_ACTIVE, !useActive() );
message( CharString().format( "<color;ffffff>Engineering: %s %s.", name(), useActive() ? "Active" : "Inactive" ), true );
}
}
bool GadgetAreaReload::updateLogic()
{
if ( WidgetCast<NounShip>( parent() ) )
{
NounShip * pShip = (NounShip *)parent();
bool bUsable = false;
for(int i=0;i<pShip->contactCount() && !bUsable;++i)
{
NounShip * pContact = WidgetCast<NounShip>( pShip->contact(i) );
if ( pContact == NULL )
continue; // not a ship
if (! isFriend( pContact ) )
continue; // not a friend
bUsable = pContact->needReload( pShip ) || pContact->needRepair( pShip );
}
if ( bUsable )
{
if (! useActive() )
pShip->useGadget( this, NULL, false );
}
else
{
if ( useActive() )
pShip->useGadget( this, NULL, false );
}
return true;
}
else if ( WidgetCast<StructureDefense>( parent() ) )
{
StructureDefense * pStructure = (StructureDefense *)parent();
if ( pStructure->active() && !useActive() )
pStructure->useGadget( NULL, this ); // turn on this device
else if ( !pStructure->active() && useActive() )
pStructure->useGadget( NULL, this ); // turn off this device
return true;
}
return false;
}
//----------------------------------------------------------------------------
GadgetAreaReload * GadgetAreaReload::getGadgetReload( NounShip * pShip, bool bUsable /*= false*/ )
{
for(int i=0;i<pShip->childCount();i++)
{
GadgetAreaReload * pReload = WidgetCast<GadgetAreaReload>( pShip->child(i) );
if (! pReload )
continue;
if ( bUsable && (pReload->useActive() || pReload->reloads() <= 0) )
continue;
return pReload;
}
return NULL;
}
//----------------------------------------------------------------------------
void GadgetAreaReload::droneUpdate( NounDrone * pDrone )
{
ASSERT( pDrone != NULL );
ASSERT( pDrone->context() != NULL );
float distance = (pDrone->worldPosition() - worldPosition()).magnitude();
if ( context() == NULL || distance > (reloadRange() * 2) )
{
pDrone->setDetach();
return;
}
}
void GadgetAreaReload::droneAction( NounDrone * pDrone )
{}
void GadgetAreaReload::droneHome( NounDrone * pDrone )
{
m_DroneCount--;
}
//---------------------------------------------------------------------------------------------------
//EOF