Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
PlayerEntity(ScriptingPrivate) Category Reference

Instance Methods

(BOOL) - scriptTestCondition:
 
(NSString *) - expandScriptRightHandSide:
 
(void) - scriptActions:forTarget:missionKey:
 
(NSString *) - expandMessage:
 

Detailed Description

Definition at line 105 of file PlayerEntityLegacyScriptEngine.m.

Method Documentation

◆ expandMessage:

- (NSString *) expandMessage: (NSString *) valueString

Extends class PlayerEntity.

Definition at line 955 of file PlayerEntityLegacyScriptEngine.m.

1181 :(NSString *)valueString
1182{
1183 Random_Seed very_random_seed;
1184 very_random_seed.a = rand() & 255;
1185 very_random_seed.b = rand() & 255;
1186 very_random_seed.c = rand() & 255;
1187 very_random_seed.d = rand() & 255;
1188 very_random_seed.e = rand() & 255;
1189 very_random_seed.f = rand() & 255;
1190 seed_RNG_only_for_planet_description(very_random_seed);
1191 NSString* expandedMessage = OOExpand(valueString);
1192 return [self replaceVariablesInString: expandedMessage];
1193}
#define OOExpand(string,...)
void seed_RNG_only_for_planet_description(Random_Seed s_seed)

◆ expandScriptRightHandSide:

- (NSString *) expandScriptRightHandSide: (NSArray *) rhsComponents

Extends class PlayerEntity.

Definition at line 278 of file PlayerEntityLegacyScriptEngine.m.

610 :(NSArray *)rhsComponents
611{
612 NSMutableArray *result = nil;
613 NSEnumerator *componentEnum = nil;
614 NSArray *component = nil;
615 NSString *value = nil;
616
617 result = [NSMutableArray arrayWithCapacity:[rhsComponents count]];
618
619 for (componentEnum = [rhsComponents objectEnumerator]; (component = [componentEnum nextObject]); )
620 {
621 /* Each component is a two-element array. The second element is a
622 string. The first element is a boolean indicating whether the
623 string is a selector (true) or a literal (false).
624
625 All valid selectors return a string or an NSNumber; in either
626 case, -description gives us a useful value to substitute into
627 the expanded string.
628 */
629
630 value = [component oo_stringAtIndex:1];
631
632 if ([[component objectAtIndex:0] boolValue])
633 {
634 value = [[self performSelector:NSSelectorFromString(value)] description];
635 if (value == nil) value = @"(null)"; // for backwards compatibility
636 }
637
638 [result addObject:value];
639 }
640
641 return [result componentsJoinedByString:@" "];
642}
return nil

◆ scriptActions:forTarget:missionKey:

- (void) scriptActions: (NSArray *) actions
forTarget: (ShipEntity *) target
missionKey: (NSString *) missionKey 

◆ scriptTestCondition:

- (BOOL) scriptTestCondition: (NSArray *) scriptCondition

Extends class PlayerEntity.

Definition at line 278 of file PlayerEntityLegacyScriptEngine.m.

421 :(NSArray *)scriptCondition
422{
423 /* Test a script condition sanitized by OOLegacyScriptWhitelist.
424
425 A sanitized condition is an array of the form:
426 (opType, rawString, selector, comparisonType, operandArray).
427
428 opType and comparisonType are NSNumbers containing OOOperationType and
429 OOComparisonType enumerators, respectively.
430
431 rawString is the original textual representation of the condition for
432 display purposes.
433
434 selector is a string, either a method selector or a mission/local
435 variable name.
436
437 operandArray is an array of operands. Each operand is itself an array
438 of two items: a boolean indicating whether it's a method selector
439 (true) or a literal string (false), and a string.
440
441 The special opType OP_FALSE doesn't require any other elements in the
442 array. All other valid opTypes require the array to have five elements.
443
444 For performance reasons, this method assumes the script condition will
445 have been generated by OOSanitizeLegacyScriptConditions() and doesn't
446 perform extensive validity checks.
447 */
448
449 OOOperationType opType;
450 NSString *selectorString = nil;
451 SEL selector = NULL;
452 OOComparisonType comparator;
453 NSArray *operandArray = nil;
454 NSString *lhsString = nil;
455 NSString *expandedRHS = nil;
456 NSArray *rhsComponents = nil;
457 NSString *rhsItem = nil;
458 NSUInteger i, count;
459 NSCharacterSet *whitespace = nil;
460 double lhsValue, rhsValue;
461 BOOL lhsFlag, rhsFlag;
462
463 opType = [scriptCondition oo_unsignedIntAtIndex:0];
464 if (opType == OP_FALSE) return NO;
465
466 selectorString = [scriptCondition oo_stringAtIndex:2];
467 comparator = [scriptCondition oo_unsignedIntAtIndex:3];
468 operandArray = [scriptCondition oo_arrayAtIndex:4];
469
470 // Transform mission/local var ops into string ops.
471 if (opType == OP_MISSION_VAR)
472 {
473 sMissionStringValue = [mission_variables objectForKey:selectorString];
474 selector = @selector(mission_string);
475 opType = OP_STRING;
476 }
477 else if (opType == OP_LOCAL_VAR)
478 {
479 sMissionStringValue = [[self localVariablesForMission:sCurrentMissionKey] objectForKey:selectorString];
480 selector = @selector(mission_string);
481 opType = OP_STRING;
482 }
483 else
484 {
485 selector = NSSelectorFromString(selectorString);
486 }
487
488 expandedRHS = [self expandScriptRightHandSide:operandArray];
489
490 if (opType == OP_STRING)
491 {
492 lhsString = [self performSelector:selector];
493
494 #define DOUBLEVAL(x) ((x != nil) ? [x doubleValue] : 0.0)
495
496 switch (comparator)
497 {
499 return lhsString == nil;
500
501 case COMPARISON_EQUAL:
502 return [lhsString isEqualToString:expandedRHS];
503
505 return ![lhsString isEqualToString:expandedRHS];
506
508 return DOUBLEVAL(lhsString) < DOUBLEVAL(expandedRHS);
509
511 return DOUBLEVAL(lhsString) > DOUBLEVAL(expandedRHS);
512
513 case COMPARISON_ONEOF:
514 {
515 rhsComponents = [expandedRHS componentsSeparatedByString:@","];
516 count = [rhsComponents count];
517
518 whitespace = [NSCharacterSet whitespaceCharacterSet];
519 lhsString = [lhsString stringByTrimmingCharactersInSet:whitespace];
520
521 for (i = 0; i < count; i++)
522 {
523 rhsItem = [[rhsComponents objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace];
524 if ([lhsString isEqualToString:rhsItem])
525 {
526 return YES;
527 }
528 }
529 }
530 return NO;
531 }
532 }
533 else if (opType == OP_NUMBER)
534 {
535 lhsValue = [[self performSelector:selector] doubleValue];
536
537 if (comparator == COMPARISON_ONEOF)
538 {
539 rhsComponents = [expandedRHS componentsSeparatedByString:@","];
540 count = [rhsComponents count];
541
542 for (i = 0; i < count; i++)
543 {
544 rhsItem = [rhsComponents objectAtIndex:i];
545 rhsValue = [rhsItem doubleValue];
546
547 if (lhsValue == rhsValue)
548 {
549 return YES;
550 }
551 }
552
553 return NO;
554 }
555 else
556 {
557 rhsValue = [expandedRHS doubleValue];
558
559 switch (comparator)
560 {
561 case COMPARISON_EQUAL:
562 return lhsValue == rhsValue;
563
565 return lhsValue != rhsValue;
566
568 return lhsValue < rhsValue;
569
571 return lhsValue > rhsValue;
572
574 case COMPARISON_ONEOF:
575 // "Can't happen" - undefined should have been caught by the sanitizer, oneof is handled above.
576 OOLog(@"script.error.unexpectedOperator", @"***** SCRIPT ERROR: in %@, operator %@ is not valid for numbers, evaluating to false.", CurrentScriptDesc(), OOComparisonTypeToString(comparator));
577 return NO;
578 }
579 }
580 }
581 else if (opType == OP_BOOL)
582 {
583 lhsFlag = [[self performSelector:selector] isEqualToString:@"YES"];
584 rhsFlag = [expandedRHS isEqualToString:@"YES"];
585
586 switch (comparator)
587 {
588 case COMPARISON_EQUAL:
589 return lhsFlag == rhsFlag;
590
592 return lhsFlag != rhsFlag;
593
597 case COMPARISON_ONEOF:
598 // "Can't happen" - should have been caught by the sanitizer.
599 OOLog(@"script.error.unexpectedOperator", @"***** SCRIPT ERROR: in %@, operator %@ is not valid for booleans, evaluating to false.", CurrentScriptDesc(), OOComparisonTypeToString(comparator));
600 return NO;
601 }
602 }
603
604 // What are we doing here?
605 OOLog(@"script.error.fallthrough", @"***** SCRIPT ERROR: in %@, unhandled condition '%@' (%@). %@", CurrentScriptDesc(), [scriptCondition objectAtIndex:1], scriptCondition, @"This is an internal error, please report it.");
606 return NO;
607}
#define OOLog(class, format,...)
Definition OOLogging.h:88
unsigned count
#define DOUBLEVAL(x)
NSString * OOComparisonTypeToString(OOComparisonType type)
static NSString * sMissionStringValue

The documentation for this category was generated from the following file: