Oolite 1.91.0.7604-240417-a536cbe
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"
27#import "Universe.h"
28#import "ShipEntity.h"
31
32
33// NOTE: these values are documented for scripting, be careful about changing them.
34#define ECM_EFFECT_DURATION 2.0
35#define ECM_PULSE_COUNT 4
36#define ECM_PULSE_INTERVAL (ECM_EFFECT_DURATION / (double)ECM_PULSE_COUNT)
37
38#define ECM_DEBUG_DRAW 0
39
40
41#if ECM_DEBUG_DRAW
42#import "OODebugGLDrawing.h"
43#endif
44
45
46@implementation OOECMBlastEntity
47
48- (id) initFromShip:(ShipEntity *)ship
49{
50 if (ship == nil)
51 {
52 DESTROY(self);
53 }
54 else if ((self = [super init]))
55 {
58 _ship = [ship weakRetain];
59
60 [self setPosition:[ship position]];
61
62 [self setStatus:STATUS_EFFECT];
63 [self setScanClass:CLASS_NO_DRAW];
64 }
65
66 return self;
67}
68
69
70- (void) update:(OOTimeDelta)delta_t
71{
72 _nextBlast -= delta_t;
73 ShipEntity *ship = [_ship weakRefUnderlyingObject];
74 BOOL validShip = (ship != nil) && ([ship status] != STATUS_DEAD);
75
76 if (_nextBlast <= 0.0 && validShip)
77 {
78 // Do ECM stuff.
79 double radius = OOClamp_0_1_d((double)(ECM_PULSE_COUNT - _blastsRemaining + 1) * 1.0 / (double)ECM_PULSE_COUNT);
80 radius *= SCANNER_MAX_RANGE;
82
83 NSArray *targets = [UNIVERSE findEntitiesMatchingPredicate:IsShipPredicate
84 parameter:NULL
85 inRange:radius
86 ofEntity:self];
87 NSUInteger i, count = [targets count];
88 if (count > 0)
89 {
90 JSContext *context = OOJSAcquireContext();
91 jsval ecmPulsesRemaining = INT_TO_JSVAL(_blastsRemaining);
92 jsval whomVal = OOJSValueFromNativeObject(context, ship);
93
94 for (i = 0; i < count; i++)
95 {
96 ShipEntity *target = [targets objectAtIndex:i];
97 ShipScriptEvent(context, target, "shipHitByECM", ecmPulsesRemaining, whomVal);
98 [target reactToAIMessage:@"ECM" context:nil];
99 [target noticeECM];
100 }
101
102 OOJSRelinquishContext(context);
103 }
105 }
106
107 if (_blastsRemaining == 0 || !validShip) [UNIVERSE removeEntity:self];
108}
109
110
111- (void) drawImmediate:(bool)immediate translucent:(bool)translucent
112{
113#if ECM_DEBUG_DRAW && OO_DEBUG
115#endif
116 // Else do nothing, we're invisible!
117}
118
119
120- (BOOL) isECMBlast
121{
122 return YES;
123}
124
125@end
126
127
128@implementation Entity (OOECMBlastEntity)
129
130- (BOOL) isECMBlast
131{
132 return NO;
133}
134
135@end
#define SCANNER_MAX_RANGE
Definition Entity.h:51
#define DESTROY(x)
Definition OOCocoa.h:77
void OODebugDrawPoint(Vector position, OOColor *color)
#define ECM_PULSE_INTERVAL
#define ECM_PULSE_COUNT
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:224
const Vector kZeroVector
Definition OOVector.m:28
#define ShipScriptEvent(context, ship, event,...)
OOEntityStatus status()
Definition Entity.m:793
HPVector position
Definition Entity.h:112
id init()
Definition Entity.m:68
uint_fast8_t _blastsRemaining
OOWeakReference * _ship
OOTimeDelta _nextBlast
void reactToAIMessage:context:(NSString *message,[context] NSString *debugContext)