Oolite
Loading...
Searching...
No Matches
PlayerEntityScriptMethods.m
Go to the documentation of this file.
1/*
2
3PlayerEntityScriptMethods.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
27
29#import "OOConstToString.h"
30#import "OOStringParsing.h"
31#import "Universe.h"
32
33#import "OOStringExpander.h"
35
36#import "StationEntity.h"
37
38@implementation PlayerEntity (ScriptMethods)
39
40- (unsigned)score
41{
42 return ship_kills;
43}
44
45- (void)setScore:(unsigned)value
46{
47 ship_kills = value;
48}
49
51{
52 return 0.1 * credits;
53}
54
55- (void)setCreditBalance:(double)value
56{
57 credits = OODeciCreditsFromDouble(value * 10.0);
58}
59
61{
62 return [[self dockedStation] name];
63}
64
66{
67 return [[self dockedStation] displayName];
68}
69
71{
72 return [self status] == STATUS_DOCKED && [self dockedStation] == [UNIVERSE station];
73}
74
75- (void)awardCommodityType:(OOCommodityType)type amount:(OOCargoQuantity)amount
76{
77 OOMassUnit unit;
78
79 if (![[UNIVERSE commodities] goodDefined:type]) {
80 return;
81 }
82
83 OOLog(@"script.debug.note.awardCargo", @"Going to award cargo: %d x '%@'", amount, type);
84
85 unit = [shipCommodityData massUnitForGood:type];
86
87 if ([self status] != STATUS_DOCKED) {
88 // in-flight
89 while (amount) {
90 if (unit != UNITS_TONS) {
91 if (specialCargo) {
92 // is this correct behaviour?
93 [shipCommodityData addQuantity:amount forGood:type];
94 } else {
95 int amount_per_container = (unit == UNITS_KILOGRAMS) ? 1000 : 1000000;
96 while (amount > 0) {
97 int smaller_quantity = 1 + ((amount - 1) % amount_per_container);
98 if ([cargo count] < [self maxAvailableCargoSpace]) {
99 ShipEntity* container = [UNIVERSE newShipWithRole:@"1t-cargopod"];
100 if (container) {
101 // the cargopod ship is just being set up. If ejected, will call UNIVERSE addEntity
102 // [container wasAddedToUniverse]; // seems to be not needed anymore for pods
103 [container setScanClass:CLASS_CARGO];
104 [container setStatus:STATUS_IN_HOLD];
105 [container setCommodity:type andAmount:smaller_quantity];
106 [cargo addObject:container];
107 [container release];
108 }
109 }
110 amount -= smaller_quantity;
111 }
112 }
113 } else if (!specialCargo)
114 // no adding TCs while special cargo in hold
115 {
116 // put each ton in a separate container
117 while (amount) {
118 if ([cargo count] < [self maxAvailableCargoSpace]) {
119 ShipEntity* container = [UNIVERSE newShipWithRole:@"1t-cargopod"];
120 if (container) {
121 // the cargopod ship is just being set up. If ejected, will call UNIVERSE addEntity
122 // [container wasAddedToUniverse]; // seems to be not needed anymore for pods
123 [container setScanClass:CLASS_CARGO];
124 [container setStatus:STATUS_IN_HOLD];
126 [cargo addObject:container];
127 [container release];
128 }
129 }
130 amount--;
131 }
132 }
133 }
134 } else { // docked
135 // like purchasing a commodity
136 int manifest_quantity = [shipCommodityData quantityForGood:type];
137 while ((amount) && (current_cargo < [self maxAvailableCargoSpace])) {
138 manifest_quantity++;
139 amount--;
140 if (unit == UNITS_TONS)
141 current_cargo++;
142 }
143 [shipCommodityData setQuantity:manifest_quantity forGood:type];
144 }
146}
147
149{
150 scanner_zoom_rate = SCANNER_ZOOM_RATE_DOWN;
151}
152
154{
155 return galaxy_number;
156}
157
159{
160 if ([UNIVERSE sun] == nil)
161 return -1; // Interstellar space
162 return [UNIVERSE currentSystemID];
163}
164
165- (void)setMissionChoice:(NSString*)newChoice
166{
167 [self setMissionChoice:newChoice keyPress:@"" withEvent:YES];
168}
169
170- (void)setMissionChoice:(NSString*)newChoice withEvent:(BOOL)withEvent
171{
172 [self setMissionChoice:newChoice keyPress:@"" withEvent:withEvent];
173}
174
175- (void)setMissionChoice:(NSString*)newChoice keyPress:(NSString*)keyPress
176{
177 [self setMissionChoice:newChoice keyPress:keyPress withEvent:YES];
178}
179
180- (void)setMissionChoice:(NSString*)newChoice keyPress:(NSString*)keyPress withEvent:(BOOL)withEvent
181{
182 BOOL equal = [newChoice isEqualToString:missionChoice] || (newChoice == missionChoice); // Catch both being nil as well
183 if (!equal) {
184 if (newChoice == nil) {
185 NSString* oldChoice = missionChoice;
186 [missionChoice autorelease];
187 missionChoice = nil;
188 if (withEvent)
189 [self doScriptEvent:OOJSID("missionChoiceWasReset") withArgument:oldChoice];
190 } else {
191 [missionChoice autorelease];
192 missionChoice = [newChoice copy];
193 }
194 }
195 equal = [keyPress isEqualToString:missionKeyPress] || (keyPress == missionKeyPress);
196 if (!equal) {
197 [missionKeyPress autorelease];
198 missionKeyPress = [keyPress copy];
199 }
200}
201
203{
204 _missionAllowInterrupt = YES;
205}
206
208{
209 return script_time;
210}
211
212/* FIXME: these next three functions seed the RNG when called. That
213 * could cause unwanted effects - should save its state, and then
214 * reset it after generating the number. */
216{
217 seed_RNG_only_for_planet_description([[UNIVERSE systemManager] getRandomSeedForCurrentSystem]);
218 return (gen_rnd_number() * 256 + gen_rnd_number()) % 100;
219}
220
222{
223 seed_RNG_only_for_planet_description([[UNIVERSE systemManager] getRandomSeedForCurrentSystem]);
224 return gen_rnd_number();
225}
226
228{
229 seed_RNG_only_for_planet_description([[UNIVERSE systemManager] getRandomSeedForCurrentSystem]);
230 unsigned a = gen_rnd_number();
231 unsigned b = gen_rnd_number();
232 unsigned c = gen_rnd_number();
233
234 a = (a << 16) | (b << 8) | c;
235 return (double)a / (double)0x01000000;
236}
237
238- (NSDictionary*)passengerContractMarker:(OOSystemID)system
239{
240 return [[[NSDictionary dictionaryWithObjectsAndKeys:
241 [NSNumber numberWithInt:system], @"system",
242 MISSION_DEST_LEGACY, @"name",
243 @"orangeColor", @"markerColor",
244 @"MARKER_DIAMOND", @"markerShape",
245 nil] retain] autorelease];
246}
247
248- (NSDictionary*)parcelContractMarker:(OOSystemID)system
249{
250 return [[[NSDictionary dictionaryWithObjectsAndKeys:
251 [NSNumber numberWithInt:system], @"system",
252 MISSION_DEST_LEGACY, @"name",
253 @"orangeColor", @"markerColor",
254 @"MARKER_PLUS", @"markerShape",
255 nil] retain] autorelease];
256}
257
258- (NSDictionary*)cargoContractMarker:(OOSystemID)system
259{
260 return [[[NSDictionary dictionaryWithObjectsAndKeys:
261 [NSNumber numberWithInt:system], @"system",
262 MISSION_DEST_LEGACY, @"name",
263 @"orangeColor", @"markerColor",
264 @"MARKER_SQUARE", @"markerShape",
265 nil] retain] autorelease];
266}
267
268- (NSDictionary*)defaultMarker:(OOSystemID)system
269{
270 return [[[NSDictionary dictionaryWithObjectsAndKeys:
271 [NSNumber numberWithInt:system], @"system",
272 MISSION_DEST_LEGACY, @"name",
273 @"redColor", @"markerColor",
274 @"MARKER_X", @"markerShape",
275 nil] retain] autorelease];
276}
277
278- (NSDictionary*)validatedMarker:(NSDictionary*)marker
279{
280 OOSystemID dest = [marker oo_intForKey:@"system"];
281 // FIXME: parameters
282 if (dest < 0 || dest > kOOMaximumSystemID) {
283 return nil;
284 }
285 NSString* group = [marker oo_stringForKey:@"name" defaultValue:MISSION_DEST_LEGACY];
286
287 return [[[NSDictionary dictionaryWithObjectsAndKeys:
288 [NSNumber numberWithInt:dest], @"system",
289 group, @"name",
290 [marker oo_stringForKey:@"markerColor"
291 defaultValue:@"redColor"],
292 @"markerColor",
293 [marker oo_stringForKey:@"markerShape"
294 defaultValue:@"MARKER_X"],
295 @"markerShape",
296 [NSNumber numberWithFloat:[marker oo_floatForKey:@"markerScale" defaultValue:1.0]], @"markerScale",
297 nil] retain] autorelease];
298}
299
300// Implements string expansion code [credits_number].
302{
303 return OOStringFromDeciCredits([self deciCredits], YES, NO);
304}
305
306/* Implements string expansion code [_oo_legacy_credits_number].
307
308 Literal uses of [credits_number] in legacy scripts are converted to
309 [_oo_legacy_credits_number] in the script sanitizer. These are shown
310 unlocalized because legacy scripts may use it for arithmetic.
311*/
313{
314 OOCreditsQuantity tenthsOfCredits = [self deciCredits];
315 unsigned long long integerCredits = tenthsOfCredits / 10;
316 unsigned long long tenths = tenthsOfCredits % 10;
317
318 return [NSString stringWithFormat:@"%llu.%llu", integerCredits, tenths];
319}
320
321// Implements string expansion code [commander_bounty].
323{
324 return [NSString stringWithFormat:@"%i", [self legalStatus]];
325}
326
327// Implements string expansion code [commander_kills].
329{
330 return [NSString stringWithFormat:@"%i", [self score]];
331}
332
333// utilising new keyconfig2.plist data
334- (NSString*)keyBindingDescription2:(NSString*)binding
335{
336 if ([keyconfig2_settings objectForKey:binding] == nil) {
337 // no such setting
338 return nil;
339 }
340 NSArray* keyList = (NSArray*)[keyconfig2_settings objectForKey:binding];
341 return [self getKeyBindingDescription:keyList];
342}
343
344- (NSString*)getKeyBindingDescription:(NSArray*)keyList
345{
346 NSUInteger i = 0;
347 NSMutableString* final = [NSMutableString string];
348 for (i = 0; i < [keyList count]; i++) {
349 if (i != 0)
350 final = [NSMutableString stringWithFormat:@"%@ or ", final];
351 NSDictionary* def = [keyList objectAtIndex:i];
352 NSString* key = [def objectForKey:@"key"];
353 OOKeyCode k_int = (OOKeyCode)[key integerValue];
354 NSString* desc = [self keyCodeDescription:k_int];
355 // 0 = key not set
356 if (k_int != 0) {
357 if ([[def objectForKey:@"mod2"] boolValue] == YES)
358 final = [NSMutableString stringWithFormat:@"%@%@+", final, keyMod2Text];
359 if ([[def objectForKey:@"mod1"] boolValue] == YES)
360 final = [NSMutableString stringWithFormat:@"%@%@+", final, keyMod1Text];
361 if ([[def objectForKey:@"shift"] boolValue] == YES)
362 final = [NSMutableString stringWithFormat:@"%@%@+", final, keyShiftText];
363 final = [NSMutableString stringWithFormat:@"%@%@", final, desc];
364 }
365 }
366 return final;
367}
368
369- (NSString*)keyCodeDescription:(OOKeyCode)code
370{
371 switch (code) {
372 case 0:
373 return DESC(@"oolite-keycode-unset");
374 case 9:
375 return DESC(@"oolite-keycode-tab");
376 case 13:
377 return DESC(@"oolite-keycode-enter");
378 case 27:
379 return DESC(@"oolite-keycode-esc");
380 case 32:
381 return DESC(@"oolite-keycode-space");
382 case gvFunctionKey1:
383 return DESC(@"oolite-keycode-f1");
384 case gvFunctionKey2:
385 return DESC(@"oolite-keycode-f2");
386 case gvFunctionKey3:
387 return DESC(@"oolite-keycode-f3");
388 case gvFunctionKey4:
389 return DESC(@"oolite-keycode-f4");
390 case gvFunctionKey5:
391 return DESC(@"oolite-keycode-f5");
392 case gvFunctionKey6:
393 return DESC(@"oolite-keycode-f6");
394 case gvFunctionKey7:
395 return DESC(@"oolite-keycode-f7");
396 case gvFunctionKey8:
397 return DESC(@"oolite-keycode-f8");
398 case gvFunctionKey9:
399 return DESC(@"oolite-keycode-f9");
400 case gvFunctionKey10:
401 return DESC(@"oolite-keycode-f10");
402 case gvFunctionKey11:
403 return DESC(@"oolite-keycode-f11");
404 case gvArrowKeyRight:
405 return DESC(@"oolite-keycode-right");
406 case gvArrowKeyLeft:
407 return DESC(@"oolite-keycode-left");
408 case gvArrowKeyDown:
409 return DESC(@"oolite-keycode-down");
410 case gvArrowKeyUp:
411 return DESC(@"oolite-keycode-up");
412 case gvHomeKey:
413 return DESC(@"oolite-keycode-home");
414 case gvEndKey:
415 return DESC(@"oolite-keycode-end");
416 case gvInsertKey:
417 return DESC(@"oolite-keycode-insert");
418 case gvDeleteKey:
419 return DESC(@"oolite-keycode-delete");
420 case gvPageUpKey:
421 return DESC(@"oolite-keycode-pageup");
422 case gvPageDownKey:
423 return DESC(@"oolite-keycode-pagedown");
424 case gvNumberPadKey0:
425 return DESC(@"oolite-keycode-numpad0");
426 case gvNumberPadKey1:
427 return DESC(@"oolite-keycode-numpad1");
428 case gvNumberPadKey2:
429 return DESC(@"oolite-keycode-numpad2");
430 case gvNumberPadKey3:
431 return DESC(@"oolite-keycode-numpad3");
432 case gvNumberPadKey4:
433 return DESC(@"oolite-keycode-numpad4");
434 case gvNumberPadKey5:
435 return DESC(@"oolite-keycode-numpad5");
436 case gvNumberPadKey6:
437 return DESC(@"oolite-keycode-numpad6");
438 case gvNumberPadKey7:
439 return DESC(@"oolite-keycode-numpad7");
440 case gvNumberPadKey8:
441 return DESC(@"oolite-keycode-numpad8");
442 case gvNumberPadKey9:
443 return DESC(@"oolite-keycode-numpad9");
444 case gvPrintScreenKey:
445 return DESC(@"oolite-keycode-printscreen");
446 case gvPauseKey:
447 return DESC(@"oolite-keycode-pause");
449 return DESC(@"oolite-keycode-numpad/");
451 return DESC(@"oolite-keycode-numpad=");
453 return DESC(@"oolite-keycode-numpad-");
455 return DESC(@"oolite-keycode-numpad*");
457 return DESC(@"oolite-keycode-numpad.");
459 return DESC(@"oolite-keycode-numpad+");
461 return DESC(@"oolite-keycode-numpadenter");
462
463 default:
464 return [NSString stringWithFormat:@"%C", code];
465 }
466}
467
468- (NSString*)keyCodeDescriptionShort:(OOKeyCode)code
469{
470 switch (code) {
471 case 0:
472 return DESC(@"oolite-keycode-short-unset");
473 case 9:
474 return DESC(@"oolite-keycode-short-tab");
475 case 13:
476 return DESC(@"oolite-keycode-short-enter");
477 case 27:
478 return DESC(@"oolite-keycode-short-esc");
479 case 32:
480 return DESC(@"oolite-keycode-short-space");
481 case gvFunctionKey1:
482 return DESC(@"oolite-keycode-short-f1");
483 case gvFunctionKey2:
484 return DESC(@"oolite-keycode-short-f2");
485 case gvFunctionKey3:
486 return DESC(@"oolite-keycode-short-f3");
487 case gvFunctionKey4:
488 return DESC(@"oolite-keycode-short-f4");
489 case gvFunctionKey5:
490 return DESC(@"oolite-keycode-short-f5");
491 case gvFunctionKey6:
492 return DESC(@"oolite-keycode-short-f6");
493 case gvFunctionKey7:
494 return DESC(@"oolite-keycode-short-f7");
495 case gvFunctionKey8:
496 return DESC(@"oolite-keycode-short-f8");
497 case gvFunctionKey9:
498 return DESC(@"oolite-keycode-short-f9");
499 case gvFunctionKey10:
500 return DESC(@"oolite-keycode-short-f10");
501 case gvFunctionKey11:
502 return DESC(@"oolite-keycode-short-f11");
503 case gvArrowKeyRight:
504 return DESC(@"oolite-keycode-short-right");
505 case gvArrowKeyLeft:
506 return DESC(@"oolite-keycode-short-left");
507 case gvArrowKeyDown:
508 return DESC(@"oolite-keycode-short-down");
509 case gvArrowKeyUp:
510 return DESC(@"oolite-keycode-short-up");
511 case gvHomeKey:
512 return DESC(@"oolite-keycode-short-home");
513 case gvEndKey:
514 return DESC(@"oolite-keycode-short-end");
515 case gvInsertKey:
516 return DESC(@"oolite-keycode-short-insert");
517 case gvDeleteKey:
518 return DESC(@"oolite-keycode-short-delete");
519 case gvPageUpKey:
520 return DESC(@"oolite-keycode-short-pageup");
521 case gvPageDownKey:
522 return DESC(@"oolite-keycode-short-pagedown");
523 case gvNumberPadKey0:
524 return DESC(@"oolite-keycode-short-numpad0");
525 case gvNumberPadKey1:
526 return DESC(@"oolite-keycode-short-numpad1");
527 case gvNumberPadKey2:
528 return DESC(@"oolite-keycode-short-numpad2");
529 case gvNumberPadKey3:
530 return DESC(@"oolite-keycode-short-numpad3");
531 case gvNumberPadKey4:
532 return DESC(@"oolite-keycode-short-numpad4");
533 case gvNumberPadKey5:
534 return DESC(@"oolite-keycode-short-numpad5");
535 case gvNumberPadKey6:
536 return DESC(@"oolite-keycode-short-numpad6");
537 case gvNumberPadKey7:
538 return DESC(@"oolite-keycode-short-numpad7");
539 case gvNumberPadKey8:
540 return DESC(@"oolite-keycode-short-numpad8");
541 case gvNumberPadKey9:
542 return DESC(@"oolite-keycode-short-numpad9");
543 case gvPrintScreenKey:
544 return DESC(@"oolite-keycode-short-printscreen");
545 case gvPauseKey:
546 return DESC(@"oolite-keycode-short-pause");
548 return DESC(@"oolite-keycode-short-numpad/");
550 return DESC(@"oolite-keycode-short-numpad=");
552 return DESC(@"oolite-keycode-short-numpad-");
554 return DESC(@"oolite-keycode-short-numpad*");
556 return DESC(@"oolite-keycode-short-numpad.");
558 return DESC(@"oolite-keycode-short-numpad+");
560 return DESC(@"oolite-keycode-short-numpadenter");
561 default:
562 return [NSString stringWithFormat:@"%C", code];
563 }
564}
565
566@end
567
568Vector OOGalacticCoordinatesFromInternal(NSPoint internalCoordinates)
569{
570 return (Vector) { (float)internalCoordinates.x * 0.4f, (float)internalCoordinates.y * 0.2f, 0.0f };
571}
572
573NSPoint OOInternalCoordinatesFromGalactic(Vector galacticCoordinates)
574{
575 return (NSPoint) { (float)galacticCoordinates.x * 2.5f, (float)galacticCoordinates.y * 5.0f };
576}
@ gvNumberPadKeyDivide
@ gvNumberPadKeyMultiply
@ gvNumberPadKeyPeriod
@ gvFunctionKey2
@ gvInsertKey
@ gvNumberPadKeyPlus
@ gvNumberPadKey9
@ gvNumberPadKey1
@ gvFunctionKey10
@ gvFunctionKey5
@ gvDeleteKey
@ gvArrowKeyDown
@ gvNumberPadKey2
@ gvFunctionKey9
@ gvNumberPadKey3
@ gvFunctionKey4
@ gvEndKey
@ gvHomeKey
@ gvNumberPadKeyEquals
@ gvNumberPadKey6
@ gvNumberPadKeyEnter
@ gvNumberPadKey5
@ gvNumberPadKey4
@ gvPauseKey
@ gvNumberPadKeyMinus
@ gvNumberPadKey7
@ gvPrintScreenKey
@ gvFunctionKey11
@ gvFunctionKey8
@ gvPageDownKey
@ gvNumberPadKey8
@ gvFunctionKey3
@ gvArrowKeyUp
@ gvArrowKeyRight
@ gvFunctionKey6
@ gvArrowKeyLeft
@ gvFunctionKey7
@ gvPageUpKey
@ gvNumberPadKey0
@ gvFunctionKey1
#define OOLog(class, format,...)
Definition OOLogging.h:84
unsigned count
return nil
NSString * OOStringFromDeciCredits(OOCreditsQuantity tenthsOfCredits, BOOL includeDecimal, BOOL includeSymbol)
uint16_t OOKeyCode
Definition OOTypes.h:163
@ kOOMaximumSystemID
Definition OOTypes.h:187
NSString * OOCommodityType
Definition OOTypes.h:97
uint64_t OOCreditsQuantity
Definition OOTypes.h:160
int16_t OOSystemID
Definition OOTypes.h:183
uint8_t OOGalaxyID
Definition OOTypes.h:182
uint32_t OOCargoQuantity
Definition OOTypes.h:155
OOMassUnit
Definition OOTypes.h:111
@ UNITS_TONS
Definition OOTypes.h:112
@ UNITS_KILOGRAMS
Definition OOTypes.h:113
double OOTimeDelta
Definition OOTypes.h:193
OOCreditsQuantity OODeciCreditsFromDouble(double doubleDeciCredits)
NSPoint OOInternalCoordinatesFromGalactic(Vector galacticCoordinates)
Vector OOGalacticCoordinatesFromInternal(NSPoint internalCoordinates)
#define SCANNER_ZOOM_RATE_DOWN
#define UNIVERSE
Definition Universe.h:827
#define DESC(key)
Definition Universe.h:832
void setScanClass:(OOScanClass sClass)
Definition Entity.m:738
OOEntityStatus status()
Definition Entity.m:733
void setMissionChoice:keyPress:withEvent:(NSString *newChoice,[keyPress] NSString *keyPress,[withEvent] BOOL withEvent)
StationEntity * dockedStation()
OOCreditsQuantity deciCredits()
void calculateCurrentCargo()
NSString * keyCodeDescription:(OOKeyCode code)
NSString * getKeyBindingDescription:(NSArray *keyList)
void setStatus:(OOEntityStatus stat)
NSString * name
void doScriptEvent:withArgument:(OOJSPropID message,[withArgument] id argument)
void setCommodity:andAmount:(OOCommodityType co_type,[andAmount] OOCargoQuantity co_amount)
NSString * displayName
void seed_RNG_only_for_planet_description(Random_Seed s_seed)
int gen_rnd_number(void)
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33