Oolite
Loading...
Searching...
No Matches
OOJSEquipmentInfo.m
Go to the documentation of this file.
1/*
2
3OOJSEquipmentInfo.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 "OOJSEquipmentInfo.h"
27#import "OODebugStandards.h"
28#import "OOJSPlayer.h"
30
31static JSObject* sEquipmentInfoPrototype;
32
33static JSBool EquipmentInfoGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
34static JSBool EquipmentInfoSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
35
36static JSBool EquipmentInfoGetAllEqipment(JSContext* context, JSObject* this, jsid propID, jsval* value);
37
38// Methods
39static JSBool EquipmentInfoStaticInfoForKey(JSContext* context, uintN argc, jsval* vp);
40
41enum {
42 // Property IDs
60 kEquipmentInfo_isExternalStore, // is missile or mine
77 kEquipmentInfo_scriptInfo, // arbitrary data for scripts, dictionary, read-only
81};
82
83static JSPropertySpec sEquipmentInfoProperties[] = {
84 // JS name ID flags
111 { "requiresCleanLegalRecord", kEquipmentInfo_requiresCleanLegalRecord, OOJS_PROP_READONLY_CB },
114 { "requiresFreePassengerBerth", kEquipmentInfo_requiresFreePassengerBerth, OOJS_PROP_READONLY_CB },
117 { "requiresNonCleanLegalRecord", kEquipmentInfo_requiresNonCleanLegalRecord, OOJS_PROP_READONLY_CB },
123 { 0 }
124};
125
126static JSPropertySpec sEquipmentInfoStaticProperties[] = {
128 { 0 }
129};
130
131static JSFunctionSpec sEquipmentInfoMethods[] = {
132 // JS name Function min args
133 { "toString", OOJSObjectWrapperToString, 0 },
134 { 0 }
135};
136
137static JSFunctionSpec sEquipmentInfoStaticMethods[] = {
138 // JS name Function min args
139 { "infoForKey", EquipmentInfoStaticInfoForKey, 0 },
140 { 0 }
141};
142
143static JSClass sEquipmentInfoClass = {
144 "EquipmentInfo",
145 JSCLASS_HAS_PRIVATE,
146
147 JS_PropertyStub, // addProperty
148 JS_PropertyStub, // delProperty
149 EquipmentInfoGetProperty, // getProperty
150 EquipmentInfoSetProperty, // setProperty
151 JS_EnumerateStub, // enumerate
152 JS_ResolveStub, // resolve
153 JS_ConvertStub, // convert
154 OOJSObjectWrapperFinalize, // finalize
155 JSCLASS_NO_OPTIONAL_MEMBERS
156};
157
159
160// *** Public ***
161
168
169OOEquipmentType* JSValueToEquipmentType(JSContext* context, jsval value)
170{
172
173 if (JSVAL_IS_OBJECT(value)) {
174 JSObject* object = JSVAL_TO_OBJECT(value);
175 if (JS_InstanceOf(context, JSVAL_TO_OBJECT(value), &sEquipmentInfoClass, NULL)) {
176 return (OOEquipmentType*)JS_GetPrivate(context, object);
177 }
178 }
179
180 NSString* string = OOStringFromJSValue(context, value);
181 if (string != nil)
183 return nil;
184
186}
187
188NSString* JSValueToEquipmentKey(JSContext* context, jsval value)
189{
190 return [JSValueToEquipmentType(context, value) identifier];
191}
192
193NSString* JSValueToEquipmentKeyRelaxed(JSContext* context, jsval value, BOOL* outExists)
194{
196
197 NSString* result = nil;
198 BOOL exists = NO;
199 id objValue = OOJSNativeObjectFromJSValue(context, value);
200
201 if ([objValue isKindOfClass:[OOEquipmentType class]]) {
202 result = [objValue identifier];
203 exists = YES;
204 } else if ([objValue isKindOfClass:[NSString class]]) {
205 /* To enforce deliberate backwards incompatibility, reject strings
206 ending with _DAMAGED unless someone actually named an equip that
207 way.
208 */
209 exists = [OOEquipmentType equipmentTypeWithIdentifier:objValue] != nil;
210 if (exists || ![objValue hasSuffix:@"_DAMAGED"]) {
211 result = objValue;
212 }
213 }
214
215 if (outExists != NULL)
216 *outExists = exists;
217 return result;
218
220}
221
222// *** Implementation stuff ***
223
224static JSBool EquipmentInfoGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
225{
226 if (!JSID_IS_INT(propID))
227 return YES;
228
229 OOJS_NATIVE_ENTER(context)
230
231 OOEquipmentType* eqType = nil;
232 id result = nil;
233 NSUInteger inst_time;
234
235 if (EXPECT_NOT(!JSEquipmentInfoGetEquipmentType(context, this, &eqType)))
236 return NO;
237
238 switch (JSID_TO_INT(propID)) {
240 result = [eqType identifier];
241 break;
242
244 result = [eqType name];
245 break;
246
248 if ([[eqType identifier] isEqual:@"EQ_FUEL"]) {
249 return JS_NewNumberValue(context, (PLAYER_MAX_FUEL - [OOPlayerForScripting() fuel]) * [eqType price] * [OOPlayerForScripting() fuelChargeRate], value);
250 } else if ([[eqType identifier] isEqual:@"EQ_RENOVATION"]) {
251 return JS_NewNumberValue(context, [OOPlayerForScripting() renovationCosts], value);
252 } else {
253 return JS_NewNumberValue(context, [OOPlayerForScripting() adjustPriceByScriptForEqKey:[eqType identifier] withCurrent:[eqType price]], value);
254 }
256 *value = OOJSValueFromBOOL([eqType canCarryMultiple]);
257 return YES;
258
260 *value = OOJSValueFromBOOL([eqType canBeDamaged]);
261 return YES;
262
264 result = [eqType descriptiveText];
265 break;
266
268 return JS_NewNumberValue(context, [eqType damageProbability], value);
269
271 result = [[eqType displayColor] normalizedArray];
272 break;
273
275 *value = OOJSValueFromBOOL([eqType fastAffinityDefensive]);
276 return YES;
277
279 *value = OOJSValueFromBOOL([eqType fastAffinityOffensive]);
280 return YES;
281
283 result = [eqType defaultActivateKey];
284 break;
285
287 result = [eqType defaultModeKey];
288 break;
289
291 *value = INT_TO_JSVAL((int32_t)[eqType techLevel]);
292 return YES;
293
295 *value = INT_TO_JSVAL((int32_t)[eqType effectiveTechLevel]);
296 return YES;
297
299 return JS_NewNumberValue(context, [eqType price], value);
300
302 result = [eqType providesForScripting];
303 break;
304
306 inst_time = [eqType installTime];
307 if (inst_time == 0) {
308 inst_time = [eqType price] + 600;
309 }
310 *value = INT_TO_JSVAL((int32_t)inst_time);
311 return YES;
312
314 *value = OOJSValueFromBOOL([eqType isAvailableToAll]);
315 return YES;
316
318 *value = OOJSValueFromBOOL([eqType isAvailableToNPCs]);
319 return YES;
320
322 *value = OOJSValueFromBOOL([eqType isAvailableToPlayer]);
323 return YES;
324
326 *value = INT_TO_JSVAL((int32_t)[eqType repairTime]);
327 return YES;
328
330 *value = OOJSValueFromBOOL([eqType requiresEmptyPylon]);
331 return YES;
332
334 *value = OOJSValueFromBOOL([eqType requiresMountedPylon]);
335 return YES;
336
338 *value = OOJSValueFromBOOL([eqType requiresCleanLegalRecord]);
339 return YES;
340
342 *value = OOJSValueFromBOOL([eqType requiresNonCleanLegalRecord]);
343 return YES;
344
346 *value = OOJSValueFromBOOL([eqType requiresFreePassengerBerth]);
347 return YES;
348
350 *value = OOJSValueFromBOOL([eqType requiresFullFuel]);
351 return YES;
352
354 *value = OOJSValueFromBOOL([eqType requiresNonFullFuel]);
355 return YES;
356
358 *value = OOJSValueFromBOOL([eqType isMissileOrMine]);
359 return YES;
360
362 *value = OOJSValueFromBOOL([eqType isPortableBetweenShips]);
363 return YES;
364
366 *value = OOJSValueFromBOOL([eqType isVisible]);
367 return YES;
368
370 *value = INT_TO_JSVAL((int32_t)[eqType requiredCargoSpace]);
371 return YES;
372
374 result = [[eqType requiresEquipment] allObjects];
375 break;
376
378 result = [[eqType requiresAnyEquipment] allObjects];
379 break;
380
382 result = [[eqType incompatibleEquipment] allObjects];
383 break;
384
386 result = [eqType scriptInfo];
387 if (result == nil)
388 result = [NSDictionary dictionary]; // empty rather than null
389 break;
390
392 result = [eqType scriptName];
393 if (result == nil)
394 result = @"";
395 break;
396
398 result = [eqType weaponInfo];
399 if (result == nil)
400 result = [NSDictionary dictionary]; // empty rather than null
401 break;
402
403 default:
405 return NO;
406 }
407
408 *value = OOJSValueFromNativeObject(context, result);
409 return YES;
410
412}
413
414static JSBool EquipmentInfoSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
415{
416 if (!JSID_IS_INT(propID))
417 return YES;
418
419 OOJS_NATIVE_ENTER(context)
420
421 OOEquipmentType* eqType = nil;
422 int32 iValue;
423 OOColor* colorForScript = nil;
424
425 if (EXPECT_NOT(!JSEquipmentInfoGetEquipmentType(context, this, &eqType)))
426 return NO;
427
428 switch (JSID_TO_INT(propID)) {
430 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
431 if (colorForScript != nil || JSVAL_IS_NULL(*value)) {
432 [eqType setDisplayColor:colorForScript];
433 return YES;
434 }
435 break;
437 OOStandardsDeprecated([NSString stringWithFormat:@"TL99 for variable tech level is deprecated for %@", [eqType identifier]]);
438 if (!OOEnforceStandards() && [eqType techLevel] == kOOVariableTechLevel) {
439 if (JSVAL_IS_NULL(*value)) {
440 // reset mission variable
442 forKey:[@"mission_TL_FOR_" stringByAppendingString:[eqType identifier]]];
443 return YES;
444 }
445 if (JS_ValueToInt32(context, *value, &iValue)) {
446 if (iValue < 0)
447 iValue = 0;
448 if (15 < iValue && iValue != kOOVariableTechLevel)
449 iValue = 15;
450 [OOPlayerForScripting() setMissionVariable:[NSString stringWithFormat:@"%u", iValue]
451 forKey:[@"mission_TL_FOR_" stringByAppendingString:[eqType identifier]]];
452 return YES;
453 }
454 } else {
455 OOJSReportWarning(context, @"Cannot modify effective tech level for %@, because its base tech level is not 99.", [eqType identifier]);
456 return YES;
457 }
458 break;
459
460 default:
462 return NO;
463 }
464
465 OOJSReportBadPropertyValue(context, this, propID, sEquipmentInfoProperties, *value);
466 return NO;
467
469}
470
471static JSBool EquipmentInfoGetAllEqipment(JSContext* context, JSObject* this, jsid propID, jsval* value)
472{
473 OOJS_NATIVE_ENTER(context)
474
475 *value = OOJSValueFromNativeObject(context, [OOEquipmentType allEquipmentTypes]);
476 return YES;
477
479}
480
481@implementation OOEquipmentType (OOJavaScriptExtensions)
482
483- (jsval)oo_jsValueInContext:(JSContext*)context
484{
485 if (_jsSelf == NULL) {
486 _jsSelf = JS_NewObject(context, &sEquipmentInfoClass, sEquipmentInfoPrototype, NULL);
487 if (_jsSelf != NULL) {
488 if (!JS_SetPrivate(context, _jsSelf, [self retain]))
489 _jsSelf = NULL;
490 }
491 }
492
493 return OBJECT_TO_JSVAL(_jsSelf);
494}
495
496- (NSString*)oo_jsClassName
497{
498 return @"EquipmentInfo";
499}
500
501- (void)oo_clearJSSelf:(JSObject*)selfVal
502{
503 if (_jsSelf == selfVal)
504 _jsSelf = NULL;
505}
506
507@end
508
509// *** Static methods ***
510
511// infoForKey(key : String): EquipmentInfo
512static JSBool EquipmentInfoStaticInfoForKey(JSContext* context, uintN argc, jsval* vp)
513{
514 OOJS_NATIVE_ENTER(context)
515
516 NSString* key = nil;
517
518 if (argc > 0)
519 key = OOStringFromJSValue(context, OOJS_ARGV[0]);
520 if (key == nil) {
521 OOJSReportBadArguments(context, @"EquipmentInfo", @"infoForKey", MIN(argc, 1U), OOJS_ARGV, nil, @"string");
522 return NO;
523 }
524
525 OOJS_RETURN_OBJECT([OOEquipmentType equipmentTypeWithIdentifier:key]);
526
528}
void OOStandardsDeprecated(NSString *message)
BOOL OOEnforceStandards(void)
#define EXPECT_NOT(x)
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
static JSBool EquipmentInfoGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
OOEquipmentType * JSValueToEquipmentType(JSContext *context, jsval value)
static JSObject * sEquipmentInfoPrototype
static JSClass sEquipmentInfoClass
static JSFunctionSpec sEquipmentInfoMethods[]
@ kEquipmentInfo_defaultActivateKey
@ kEquipmentInfo_installationTime
@ kEquipmentInfo_requiresAnyEquipment
@ kEquipmentInfo_canCarryMultiple
@ kEquipmentInfo_calculatedPrice
@ kEquipmentInfo_weaponInfo
@ kEquipmentInfo_effectiveTechLevel
@ kEquipmentInfo_requiresEquipment
@ kEquipmentInfo_equipmentKey
@ kEquipmentInfo_displayColor
@ kEquipmentInfo_isAvailableToAll
@ kEquipmentInfo_requiresFreePassengerBerth
@ kEquipmentInfo_damageProbability
@ kEquipmentInfo_requiresCleanLegalRecord
@ kEquipmentInfo_isVisible
@ kEquipmentInfo_isPortableBetweenShips
@ kEquipmentInfo_defaultModeKey
@ kEquipmentInfo_name
@ kEquipmentInfo_price
@ kEquipmentInfo_fastAffinityDefensive
@ kEquipmentInfo_isExternalStore
@ kEquipmentInfo_isAvailableToPlayer
@ kEquipmentInfo_canBeDamaged
@ kEquipmentInfo_description
@ kEquipmentInfo_techLevel
@ kEquipmentInfo_scriptName
@ kEquipmentInfo_scriptInfo
@ kEquipmentInfo_repairTime
@ kEquipmentInfo_provides
@ kEquipmentInfo_requiresFullFuel
@ kEquipmentInfo_requiresNonFullFuel
@ kEquipmentInfo_requiredCargoSpace
@ kEquipmentInfo_isAvailableToNPCs
@ kEquipmentInfo_incompatibleEquipment
@ kEquipmentInfo_requiresNonCleanLegalRecord
@ kEquipmentInfo_requiresMountedPylon
@ kEquipmentInfo_requiresEmptyPylon
@ kEquipmentInfo_fastAffinityOffensive
static JSBool EquipmentInfoSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
void InitOOJSEquipmentInfo(JSContext *context, JSObject *global)
static JSFunctionSpec sEquipmentInfoStaticMethods[]
static JSPropertySpec sEquipmentInfoProperties[]
static JSBool EquipmentInfoStaticInfoForKey(JSContext *context, uintN argc, jsval *vp)
static JSBool EquipmentInfoGetAllEqipment(JSContext *context, JSObject *this, jsid propID, jsval *value)
NSString * JSValueToEquipmentKeyRelaxed(JSContext *context, jsval value, BOOL *outExists)
NSString * JSValueToEquipmentKey(JSContext *context, jsval value)
static JSPropertySpec sEquipmentInfoStaticProperties[]
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
void OOJSReportWarning(JSContext *context, NSString *format,...)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
#define MIN(A, B)
Definition OOMaths.h:91
return nil
@ kOOVariableTechLevel
Definition OOTypes.h:175
#define PLAYER_MAX_FUEL
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
NSArray * normalizedArray()
Definition OOColor.m:465
void setDisplayColor:(OOColor *newColor)
NSDictionary * weaponInfo()
NSString * scriptName()
NSSet * incompatibleEquipment()
NSDictionary * scriptInfo()
OOColor * displayColor()
NSArray * defaultActivateKey()
NSArray * defaultModeKey()
NSSet * requiresEquipment()
NSSet * requiresAnyEquipment()
OOEquipmentType * equipmentTypeWithIdentifier:(NSString *identifier)
OOCreditsQuantity price()
NSUInteger installTime()
NSArray * providesForScripting()
NSString * identifier()
NSString * descriptiveText()
void setMissionVariable:forKey:(NSString *value,[forKey] NSString *key)