Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOJSEntity.m File Reference
+ Include dependency graph for OOJSEntity.m:

Go to the source code of this file.

Enumerations

enum  {
  kEntity_collisionRadius , kEntity_distanceTravelled , kEntity_energy , kEntity_heading ,
  kEntity_mass , kEntity_maxEnergy , kEntity_orientation , kEntity_owner ,
  kEntity_position , kEntity_scanClass , kEntity_spawnTime , kEntity_status ,
  kEntity_isPlanet , kEntity_isPlayer , kEntity_isShip , kEntity_isStation ,
  kEntity_isDock , kEntity_isSubEntity , kEntity_isSun , kEntity_isSunlit ,
  kEntity_isValid , kEntity_isInSpace , kEntity_isVisible , kEntity_isVisualEffect ,
  kEntity_isWormhole
}
 

Functions

static JSBool EntityGetProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
static JSBool EntitySetProperty (JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
 
static JSBool EntityDumpState (JSContext *context, uintN argc, jsval *vp)
 
void InitOOJSEntity (JSContext *context, JSObject *global)
 
BOOL JSValueToEntity (JSContext *context, jsval value, Entity **outEntity)
 
BOOL EntityFromArgumentList (JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, Entity **outEntity, uintN *outConsumed)
 

Variables

JSObject * gOOEntityJSPrototype
 
JSClass gOOEntityJSClass
 
static JSPropertySpec sEntityProperties []
 
static JSFunctionSpec sEntityMethods []
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kEntity_collisionRadius 
kEntity_distanceTravelled 
kEntity_energy 
kEntity_heading 
kEntity_mass 
kEntity_maxEnergy 
kEntity_orientation 
kEntity_owner 
kEntity_position 
kEntity_scanClass 
kEntity_spawnTime 
kEntity_status 
kEntity_isPlanet 
kEntity_isPlayer 
kEntity_isShip 
kEntity_isStation 
kEntity_isDock 
kEntity_isSubEntity 
kEntity_isSun 
kEntity_isSunlit 
kEntity_isValid 
kEntity_isInSpace 
kEntity_isVisible 
kEntity_isVisualEffect 
kEntity_isWormhole 

Definition at line 65 of file OOJSEntity.m.

66{
67 // Property IDs
68 kEntity_collisionRadius, // collision radius, double, read-only.
69 kEntity_distanceTravelled, // distance travelled, double, read-only.
70 kEntity_energy, // energy, double, read-write.
71 kEntity_heading, // heading, vector, read-only (like orientation but ignoring twist angle)
72 kEntity_mass, // mass, double, read-only
73 kEntity_maxEnergy, // maxEnergy, double, read-only.
74 kEntity_orientation, // orientation, quaternion, read/write
75 kEntity_owner, // owner, Entity, read-only. (Parent ship for subentities, station for defense ships, launching ship for missiles etc)
76 kEntity_position, // position in system space, Vector, read/write
77 kEntity_scanClass, // scan class, string, read-only
78 kEntity_spawnTime, // spawn time, double, read-only.
79 kEntity_status, // entity status, string, read-only
80 kEntity_isPlanet, // is planet, boolean, read-only.
81 kEntity_isPlayer, // is player, boolean, read-only.
82 kEntity_isShip, // is ship, boolean, read-only.
83 kEntity_isStation, // is station, boolean, read-only.
84 kEntity_isDock, // is dock, boolean, read-only.
85 kEntity_isSubEntity, // is subentity, boolean, read-only.
86 kEntity_isSun, // is sun, boolean, read-only.
87 kEntity_isSunlit, // is sunlit, boolean, read-only.
88 kEntity_isValid, // is not stale, boolean, read-only.
89 kEntity_isInSpace, // is in space, boolean, read-only.
90 kEntity_isVisible, // is within drawing distance, boolean, read-only.
91 kEntity_isVisualEffect, // is visual effect, boolean, read-only.
92 kEntity_isWormhole, // is visual effect, boolean, read-only.
93};
@ kEntity_owner
Definition OOJSEntity.m:75
@ kEntity_distanceTravelled
Definition OOJSEntity.m:69
@ kEntity_scanClass
Definition OOJSEntity.m:77
@ kEntity_isVisible
Definition OOJSEntity.m:90
@ kEntity_isDock
Definition OOJSEntity.m:84
@ kEntity_isInSpace
Definition OOJSEntity.m:89
@ kEntity_status
Definition OOJSEntity.m:79
@ kEntity_orientation
Definition OOJSEntity.m:74
@ kEntity_isStation
Definition OOJSEntity.m:83
@ kEntity_isSunlit
Definition OOJSEntity.m:87
@ kEntity_mass
Definition OOJSEntity.m:72
@ kEntity_isValid
Definition OOJSEntity.m:88
@ kEntity_collisionRadius
Definition OOJSEntity.m:68
@ kEntity_isVisualEffect
Definition OOJSEntity.m:91
@ kEntity_isWormhole
Definition OOJSEntity.m:92
@ kEntity_isSubEntity
Definition OOJSEntity.m:85
@ kEntity_energy
Definition OOJSEntity.m:70
@ kEntity_position
Definition OOJSEntity.m:76
@ kEntity_heading
Definition OOJSEntity.m:71
@ kEntity_isShip
Definition OOJSEntity.m:82
@ kEntity_maxEnergy
Definition OOJSEntity.m:73
@ kEntity_isPlanet
Definition OOJSEntity.m:80
@ kEntity_isPlayer
Definition OOJSEntity.m:81
@ kEntity_isSun
Definition OOJSEntity.m:86
@ kEntity_spawnTime
Definition OOJSEntity.m:78

Function Documentation

◆ EntityDumpState()

static JSBool EntityDumpState ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 400 of file OOJSEntity.m.

401{
403
404 Entity *thisEnt = nil;
405 OOJSEntityGetEntity(context, OOJS_THIS, &thisEnt);
406 [thisEnt dumpState];
407
409
411}
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_ENTER
#define OOJS_THIS
#define OOJS_RETURN_VOID
return nil

References nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJS_RETURN_VOID, and OOJS_THIS.

◆ EntityFromArgumentList()

BOOL EntityFromArgumentList ( JSContext * context,
NSString * scriptClass,
NSString * function,
uintN argc,
jsval * argv,
Entity ** outEntity,
uintN * outConsumed )

Definition at line 157 of file OOJSEntity.m.

158{
160
161 // Sanity checks.
162 if (outConsumed != NULL) *outConsumed = 0;
163 if (EXPECT_NOT(argc == 0 || argv == NULL || outEntity == NULL))
164 {
166 return NO;
167 }
168
169 // Get value, if possible.
170 if (EXPECT_NOT(!JSValueToEntity(context, argv[0], outEntity)))
171 {
172 // Failed; report bad parameters, if given a class and function.
173 if (scriptClass != nil && function != nil)
174 {
175 OOJSReportWarning(context, @"%@.%@(): expected entity, got %@.", scriptClass, function, [NSString stringWithJavaScriptParameters:argv count:1 inContext:context]);
176 return NO;
177 }
178 }
179
180 // Success.
181 if (outConsumed != NULL) *outConsumed = 1;
182 return YES;
183
185}
#define EXPECT_NOT(x)
BOOL JSValueToEntity(JSContext *context, jsval value, Entity **outEntity)
Definition OOJSEntity.m:146
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOLogGenericParameterError()
Definition OOLogging.h:125
unsigned count

References count, EXPECT_NOT, JSValueToEntity(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSReportWarning(), and OOLogGenericParameterError.

+ Here is the call graph for this function:

◆ EntityGetProperty()

static JSBool EntityGetProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 188 of file OOJSEntity.m.

189{
190 if (!JSID_IS_INT(propID)) return YES;
191
192 OOJS_NATIVE_ENTER(context)
193
194 Entity *entity = nil;
195 id result = nil;
196
197 if (EXPECT_NOT(!OOJSEntityGetEntity(context, this, &entity))) return NO;
198 if (OOIsStaleEntity(entity))
199 {
200 if (JSID_TO_INT(propID) == kEntity_isValid) *value = JSVAL_FALSE;
201 else { *value = JSVAL_VOID; }
202 return YES;
203 }
204
205 switch (JSID_TO_INT(propID))
206 {
208 return JS_NewNumberValue(context, [entity collisionRadius], value);
209
210 case kEntity_position:
211 return HPVectorToJSValue(context, [entity position], value);
212
214 return QuaternionToJSValue(context, [entity normalOrientation], value);
215
216 case kEntity_heading:
217 return VectorToJSValue(context, vector_forward_from_quaternion([entity normalOrientation]), value);
218
219 case kEntity_status:
220 *value = OOJSValueFromEntityStatus(context, [entity status]);
221 return YES;
222
224 *value = OOJSValueFromScanClass(context, [entity scanClass]);
225 return YES;
226
227 case kEntity_mass:
228 return JS_NewNumberValue(context, [entity mass], value);
229
230 case kEntity_owner:
231 result = [entity owner];
232 if (result == entity) result = nil;
233 break;
234
235 case kEntity_energy:
236 return JS_NewNumberValue(context, [entity energy], value);
237
239 return JS_NewNumberValue(context, [entity maxEnergy], value);
240
241 case kEntity_isValid:
242 *value = [entity status] == STATUS_DEAD ? JSVAL_FALSE : JSVAL_TRUE;
243 return YES;
244
246 *value = OOJSValueFromBOOL([entity isInSpace]);
247 return YES;
248
249 case kEntity_isShip:
250 *value = OOJSValueFromBOOL([entity isShip]);
251 return YES;
252
254 *value = OOJSValueFromBOOL([entity isStation]);
255 return YES;
256
257 case kEntity_isDock:
258 *value = OOJSValueFromBOOL([entity isDock]);
259 return YES;
260
262 *value = OOJSValueFromBOOL([entity isSubEntity]);
263 return YES;
264
265 case kEntity_isPlayer:
266 *value = OOJSValueFromBOOL([entity isPlayer]);
267 return YES;
268
269 case kEntity_isPlanet:
270 *value = OOJSValueFromBOOL([entity isPlanet]);
271 return YES;
272
273 case kEntity_isSun:
274 *value = OOJSValueFromBOOL([entity isSun]);
275 return YES;
276
277 case kEntity_isSunlit:
278 *value = OOJSValueFromBOOL([entity isSunlit]);
279 return YES;
280
282 *value = OOJSValueFromBOOL([entity isVisible]);
283 return YES;
284
286 *value = OOJSValueFromBOOL([entity isVisualEffect]);
287 return YES;
288
290 *value = OOJSValueFromBOOL([entity isWormhole]);
291 return YES;
292
294 return JS_NewNumberValue(context, [entity distanceTravelled], value);
295
297 return JS_NewNumberValue(context, [entity spawnTime], value);
298
299 default:
300 OOJSReportBadPropertySelector(context, this, propID, sEntityProperties);
301 }
302
303 *value = OOJSValueFromNativeObject(context, result);
304 return YES;
305
307}
OOINLINE jsval OOJSValueFromScanClass(JSContext *context, OOScanClass value)
OOINLINE jsval OOJSValueFromEntityStatus(JSContext *context, OOEntityStatus value)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
OOINLINE BOOL OOIsStaleEntity(Entity *entity)
Definition OOJSEntity.h:71
static JSPropertySpec sEntityProperties[]
Definition OOJSEntity.m:96
BOOL QuaternionToJSValue(JSContext *context, Quaternion quaternion, jsval *outValue) NONNULL_FUNC
BOOL HPVectorToJSValue(JSContext *context, HPVector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:228
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:185
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
Vector vector_forward_from_quaternion(Quaternion quat)
OOEntityStatus status()
Definition Entity.m:793
id owner()
Definition Entity.m:583

References EXPECT_NOT, HPVectorToJSValue(), kEntity_collisionRadius, kEntity_distanceTravelled, kEntity_energy, kEntity_heading, kEntity_isDock, kEntity_isInSpace, kEntity_isPlanet, kEntity_isPlayer, kEntity_isShip, kEntity_isStation, kEntity_isSubEntity, kEntity_isSun, kEntity_isSunlit, kEntity_isValid, kEntity_isVisible, kEntity_isVisualEffect, kEntity_isWormhole, kEntity_mass, kEntity_maxEnergy, kEntity_orientation, kEntity_owner, kEntity_position, kEntity_scanClass, kEntity_spawnTime, kEntity_status, nil, OOIsStaleEntity(), OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSValueFromBOOL(), OOJSValueFromEntityStatus(), OOJSValueFromNativeObject(), OOJSValueFromScanClass(), Entity::owner, QuaternionToJSValue(), sEntityProperties, Entity::status, vector_forward_from_quaternion(), and VectorToJSValue().

+ Here is the call graph for this function:

◆ EntitySetProperty()

static JSBool EntitySetProperty ( JSContext * context,
JSObject * this,
jsid propID,
JSBool strict,
jsval * value )
static

Definition at line 310 of file OOJSEntity.m.

311{
312 if (!JSID_IS_INT(propID)) return YES;
313
314 OOJS_NATIVE_ENTER(context)
315
316 Entity *entity = nil;
317 double fValue;
318 HPVector hpvValue;
319 Quaternion qValue;
320
321 if (EXPECT_NOT(!OOJSEntityGetEntity(context, this, &entity))) return NO;
322 if (OOIsStaleEntity(entity)) return YES;
323
324 switch (JSID_TO_INT(propID))
325 {
326 case kEntity_position:
327 if (JSValueToHPVector(context, *value, &hpvValue))
328 {
329 [entity setPosition:hpvValue];
330 if ([entity isShip])
331 {
332 [(ShipEntity *)entity resetExhaustPlumes];
333 [(ShipEntity *)entity forceAegisCheck];
334 }
335 return YES;
336 }
337 break;
338
340 if (JSValueToQuaternion(context, *value, &qValue))
341 {
342 [entity setNormalOrientation:qValue];
343 return YES;
344 }
345 break;
346
347 case kEntity_energy:
348 if (JS_ValueToNumber(context, *value, &fValue))
349 {
350 fValue = OOClamp_0_max_d(fValue, [entity maxEnergy]);
351 [entity setEnergy:fValue];
352 return YES;
353 }
354 break;
355
357 if (JS_ValueToNumber(context, *value, &fValue))
358 {
359 if (fValue <= 0.0)
360 {
361 OOJSReportError(context, @"entity.maxEnergy must be positive.");
362 return NO;
363 }
364 [entity setMaxEnergy:fValue];
365 return YES;
366 }
367 break;
368
369
371 if ([entity isShip] && ![entity isPlayer])
372 {
373 OOScanClass newClass = OOScanClassFromJSValue(context, *value);
374 if (newClass == CLASS_NOT_SET || newClass == CLASS_NO_DRAW || newClass == CLASS_TARGET || newClass == CLASS_WORMHOLE || newClass == CLASS_PLAYER || newClass == CLASS_VISUAL_EFFECT)
375 {
376 OOJSReportError(context, @"entity.scanClass cannot be set to that value.");
377 return NO;
378 }
379 [entity setScanClass:newClass];
380 return YES;
381 }
382 else
383 {
384 OOJSReportError(context, @"entity.scanClass is read-only except on NPC ships.");
385 return NO;
386 }
387 default:
388 OOJSReportBadPropertySelector(context, this, propID, sEntityProperties);
389 return NO;
390 }
391
392 OOJSReportBadPropertyValue(context, this, propID, sEntityProperties, *value);
393 return NO;
394
396}
OOScanClass
Definition Entity.h:71
OOINLINE OOScanClass OOScanClassFromJSValue(JSContext *context, jsval value)
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
BOOL JSValueToHPVector(JSContext *context, jsval value, HPVector *outVector) NONNULL_FUNC
Definition OOJSVector.m:252
void OOJSReportError(JSContext *context, NSString *format,...)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
void setMaxEnergy:(GLfloat amount)
Definition Entity.m:823
void setNormalOrientation:(Quaternion quat)
Definition Entity.m:744
void setScanClass:(OOScanClass sClass)
Definition Entity.m:799
void setEnergy:(GLfloat amount)
Definition Entity.m:811
void setPosition:(HPVector posn)
Definition Entity.m:647

References EXPECT_NOT, JSValueToHPVector(), JSValueToQuaternion(), kEntity_energy, kEntity_maxEnergy, kEntity_orientation, kEntity_position, kEntity_scanClass, nil, OOIsStaleEntity(), OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOJSReportError(), OOScanClassFromJSValue(), sEntityProperties, Entity::setEnergy:, Entity::setMaxEnergy:, Entity::setNormalOrientation:, Entity::setPosition:, and Entity::setScanClass:.

+ Here is the call graph for this function:

◆ InitOOJSEntity()

void InitOOJSEntity ( JSContext * context,
JSObject * global )

Definition at line 139 of file OOJSEntity.m.

140{
141 gOOEntityJSPrototype = JS_InitClass(context, global, NULL, &gOOEntityJSClass, OOJSUnconstructableConstruct, 0, sEntityProperties, sEntityMethods, NULL, NULL);
143}
JSClass gOOEntityJSClass
Definition OOJSEntity.m:48
JSObject * gOOEntityJSPrototype
Definition OOJSEntity.m:38
static JSFunctionSpec sEntityMethods[]
Definition OOJSEntity.m:128
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)

References gOOEntityJSClass, gOOEntityJSPrototype, OOJSBasicPrivateObjectConverter(), OOJSRegisterObjectConverter(), OOJSUnconstructableConstruct(), sEntityMethods, and sEntityProperties.

+ Here is the call graph for this function:

◆ JSValueToEntity()

BOOL JSValueToEntity ( JSContext * context,
jsval value,
Entity ** outEntity )

Definition at line 146 of file OOJSEntity.m.

147{
148 if (JSVAL_IS_OBJECT(value))
149 {
150 return OOJSEntityGetEntity(context, JSVAL_TO_OBJECT(value), outEntity);
151 }
152
153 return NO;
154}

Referenced by ConsoleInspectEntity(), EntityFromArgumentList(), GetRelativeToAndRange(), PlayerShipSetProperty(), and ShipSetProperty().

+ Here is the caller graph for this function:

Variable Documentation

◆ gOOEntityJSClass

JSClass gOOEntityJSClass
Initial value:
=
{
"Entity",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
JSCLASS_NO_OPTIONAL_MEMBERS
}
static JSBool EntitySetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSEntity.m:310
static JSBool EntityGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSEntity.m:188
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)

Definition at line 48 of file OOJSEntity.m.

49{
50 "Entity",
51 JSCLASS_HAS_PRIVATE,
52
53 JS_PropertyStub, // addProperty
54 JS_PropertyStub, // delProperty
55 EntityGetProperty, // getProperty
56 EntitySetProperty, // setProperty
57 JS_EnumerateStub, // enumerate
58 JS_ResolveStub, // resolve
59 JS_ConvertStub, // convert
61 JSCLASS_NO_OPTIONAL_MEMBERS
62};

Referenced by InitOOJSEntity(), and JSEntityClass().

◆ gOOEntityJSPrototype

JSObject* gOOEntityJSPrototype

Definition at line 38 of file OOJSEntity.m.

Referenced by InitOOJSEntity(), and JSEntityPrototype().

◆ sEntityMethods

JSFunctionSpec sEntityMethods[]
static
Initial value:
=
{
{ "toString", OOJSObjectWrapperToString, 0 },
{ "dumpState", EntityDumpState, 0 },
{ 0 }
}
static JSBool EntityDumpState(JSContext *context, uintN argc, jsval *vp)
Definition OOJSEntity.m:400
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)

Definition at line 128 of file OOJSEntity.m.

129{
130 // JS name Function min args
131 { "toString", OOJSObjectWrapperToString, 0 },
132#ifndef NDEBUG
133 { "dumpState", EntityDumpState, 0 },
134#endif
135 { 0 }
136};

Referenced by InitOOJSEntity().

◆ sEntityProperties

JSPropertySpec sEntityProperties[]
static
Initial value:
=
{
{ 0 }
}
#define OOJS_PROP_READWRITE_CB
#define OOJS_PROP_READONLY_CB

Definition at line 96 of file OOJSEntity.m.

97{
98 // JS name ID flags
99 { "collisionRadius", kEntity_collisionRadius, OOJS_PROP_READONLY_CB },
100 { "distanceTravelled", kEntity_distanceTravelled, OOJS_PROP_READONLY_CB },
116 { "isSubEntity", kEntity_isSubEntity, OOJS_PROP_READONLY_CB },
122 { "isVisualEffect", kEntity_isVisualEffect, OOJS_PROP_READONLY_CB },
123 { "isWormhole", kEntity_isWormhole, OOJS_PROP_READONLY_CB },
124 { 0 }
125};

Referenced by EntityGetProperty(), EntitySetProperty(), and InitOOJSEntity().