Oolite
Loading...
Searching...
No Matches
OOECMBlastEntity.m
Go to the documentation of this file.
1/*
2
3OOECMBlastEntity.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOECMBlastEntity.h"
28#import "OOJavaScriptEngine.h"
29#import "ShipEntity.h"
30#import "Universe.h"
31
32// NOTE: these values are documented for scripting, be careful about changing them.
33#define ECM_EFFECT_DURATION 2.0
34#define ECM_PULSE_COUNT 4
35#define ECM_PULSE_INTERVAL (ECM_EFFECT_DURATION / (double)ECM_PULSE_COUNT)
36
37#define ECM_DEBUG_DRAW 0
38
39#if ECM_DEBUG_DRAW
40#import "OODebugGLDrawing.h"
41#endif
42
43@implementation OOECMBlastEntity
44
45- (id)initFromShip:(ShipEntity*)ship
46{
47 if (ship == nil) {
48 DESTROY(self);
49 } else if ((self = [super init])) {
53
54 [self setPosition:[ship position]];
55
56 [self setStatus:STATUS_EFFECT];
57 [self setScanClass:CLASS_NO_DRAW];
58 }
59
60 return self;
61}
62
63- (void)update:(OOTimeDelta)delta_t
64{
65 _nextBlast -= delta_t;
67 BOOL validShip = (ship != nil) && ([ship status] != STATUS_DEAD);
68
69 if (_nextBlast <= 0.0 && validShip) {
70 // Do ECM stuff.
71 double radius = OOClamp_0_1_d((double)(ECM_PULSE_COUNT - _blastsRemaining + 1) * 1.0 / (double)ECM_PULSE_COUNT);
72 radius *= SCANNER_MAX_RANGE;
74
75 NSArray* targets = [UNIVERSE findEntitiesMatchingPredicate:IsShipPredicate
76 parameter:NULL
77 inRange:radius
78 ofEntity:self];
79 NSUInteger i, count = [targets count];
80 if (count > 0) {
81 JSContext* context = OOJSAcquireContext();
82
83 for (i = 0; i < count; i++) {
84 ShipEntity* target = [targets objectAtIndex:i];
85 // ShipScriptEvent() frees its arguments, so they are created for each target.
86 OOJSVal ecmPulsesRemaining = OOJSValueFromDouble(context, _blastsRemaining);
87 OOJSVal whomVal = OOJSValueFromNativeObject(context, ship);
88 ShipScriptEvent(context, target, "shipHitByECM", ecmPulsesRemaining, whomVal);
89 [target reactToAIMessage:@"ECM" context:nil];
91 }
92
93 OOJSRelinquishContext(context);
94 }
96 }
97
98 if (_blastsRemaining == 0 || !validShip)
99 [UNIVERSE removeEntity:self];
100}
101
102- (void)drawImmediate:(bool)immediate translucent:(bool)translucent
103{
104#if ECM_DEBUG_DRAW && OO_DEBUG
105 OODebugDrawPoint(kZeroVector, [OOColor cyanColor]);
106#endif
107 // Else do nothing, we're invisible!
108}
109
111{
112 return YES;
113}
114
115@end
116
117@implementation Entity (OOECMBlastEntity)
118
120{
121 return NO;
122}
123
124@end
#define SCANNER_MAX_RANGE
Definition Entity.h:53
#define ECM_PULSE_INTERVAL
#define ECM_PULSE_COUNT
OOINLINE double OOClamp_0_1_d(double value) INLINE_CONST_FUNC
#define DESTROY(x)
OOINLINE jsval OOJSValueFromDouble(JSContext *context, double value)
jsval OOJSVal
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
unsigned count
return nil
double OOTimeDelta
Definition OOTypes.h:193
const Vector kZeroVector
Definition OOVector.m:27
#define ShipScriptEvent(context, ship, event,...)
void setScanClass:(OOScanClass sClass)
Definition Entity.m:738
OOEntityStatus status()
Definition Entity.m:733
HPVector position
Definition Entity.m:576
id init()
Definition Entity.m:64
void setStatus:(OOEntityStatus stat)
Definition Entity.m:728
void setPosition:(HPVector posn)
Definition Entity.m:608
OOWeakReference * _ship
OOTimeDelta _nextBlast
void reactToAIMessage:context:(NSString *message,[context] NSString *debugContext)