Skip to content

Commit

Permalink
Introducing the %G###### string expander.
Browse files Browse the repository at this point in the history
It works like the %J### one, but it takes two triplets of numbers rather than one, namely the system ID followed by the galaxy ID. This way we can refer to planet names in galaxies other than where the player is. %J### remains for compatibility reasons. Updated Constrictor Hunt mission strings to eliminate hard coded references to Xeer in the mission texts. (#290)
  • Loading branch information
AnotherCommander committed Mar 6, 2018
1 parent 0d445de commit 91a36b8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Resources/Config/missiontext.plist
Expand Up @@ -14,9 +14,9 @@

"constrictor_hunt_brief1" = "---INCOMING MESSAGE\n\nGreetings Commander,\n\nI am Captain Curruthers of Her Imperial Majesty’s Space Navy and I beg a moment of your valuable time. We would like you to do a little job for us.\nThe ship you see here is a new model, the Constrictor, equipped with a top secret new shield generator.\n\nUnfortunately it’s been stolen.\n";
"constrictor_hunt_brief1a" = "It went missing from our ship yard on %J150 five months ago and was last seen at %J036. Your mission, should you decide to accept it, is to seek and destroy this ship. You are cautioned that only Military Lasers will get through the new shields and that the Constrictor is fitted with an E.C.M. System.\n\nGood Luck, Commander.\n\n---MESSAGE ENDS.";
"constrictor_hunt_brief1b" = "It went missing from Xeer Shipyards five months ago and is believed to have jumped to this galaxy. Your mission, should you decide to accept it, is to seek and destroy this ship. You are cautioned that only Military Lasers will get through the new shields and that the Constrictor is fitted with an E.C.M. System.\n\nGood Luck, Commander.\n\n---MESSAGE ENDS.";
"constrictor_hunt_brief1b" = "It went missing from %G150000 Shipyards five months ago and is believed to have jumped to this galaxy. Your mission, should you decide to accept it, is to seek and destroy this ship. You are cautioned that only Military Lasers will get through the new shields and that the Constrictor is fitted with an E.C.M. System.\n\nGood Luck, Commander.\n\n---MESSAGE ENDS.";
"constrictor_hunt_info1a" = "Hunt for the Constrictor stolen from %J150.";
"constrictor_hunt_info1b" = "Hunt for the Constrictor stolen from Xeer Shipyards.";
"constrictor_hunt_info1b" = "Hunt for the Constrictor stolen from %G150000 Shipyards.";
"constrictor_hunt_debrief" = "---INCOMING MESSAGE\n\nCongratulations Commander!\n\nThere will always be a place for you in Her Imperial Majesty’s Space Navy.\n\nAnd maybe sooner than you think…\n\n---MESSAGE ENDS.";
"constrictor_hunt_thief_captured" = "You captured the thief who stole the constrictor! Her Imperial Navy awards you a bonus of 1000 credits!\n";
"constrictor_hunt_thief_captured2" = "You captured an empty escape pod. The thief who stole the constrictor is still on board and you left the ship intact! Her Imperial Navy gives you a second chance!\n";
Expand Down
3 changes: 3 additions & 0 deletions src/Core/OOStringExpander.h
Expand Up @@ -90,6 +90,9 @@ typedef NSUInteger OOExpandOptions;
* %R is like %N but, due to a bug, misses some possibilities. Deprecated.
* %JNNN, where NNN is a three-digit integer, is replaced with the name
of system ID NNN in the current galaxy.
* %GNNNNNN, where NNNNNN is a six-digit integer, is replaced with the
name of system ID NNN (first triplet) in the specified galaxy
(second triplet).
* %% is replaced with %.
* %[ is replaced with [.
* %] is replaced with ].
Expand Down
61 changes: 59 additions & 2 deletions src/Core/OOStringExpander.m
Expand Up @@ -116,6 +116,7 @@ Total stack limit for strings being parsed (in UTF-16 code elements,
static SEL LookUpLegacySelector(NSString *key);

static NSString *ExpandPercentEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength);
static NSString *ExpandSystemNameForGalaxyEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength);
static NSString *ExpandSystemNameEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength);
static NSString *ExpandPercentR(OOStringExpansionContext *context, NSString *input);
#if WARNINGS
Expand Down Expand Up @@ -921,6 +922,7 @@ static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSStri
%N
%R
%J###, where ### are three digits
%G######, where ### are six digits
%%
%[
%]
Expand All @@ -936,7 +938,7 @@ static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSStri
NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
NSCParameterAssert(characters[idx] == '%');

// All %-escapes except %J are 2 characters.
// All %-escapes except %J and %G are 2 characters.
*replaceLength = 2;
unichar selector = characters[idx + 1];

Expand All @@ -957,6 +959,9 @@ static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSStri
context->hasPercentR = true;
return @"%R";

case 'G':
return ExpandSystemNameForGalaxyEscape(context, characters, size, idx, replaceLength);

case 'J':
return ExpandSystemNameEscape(context, characters, size, idx, replaceLength);

Expand Down Expand Up @@ -1030,6 +1035,58 @@ static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSStri
}


/* ExpandSystemNameForGalaxyEscape(context, characters, size, idx, replaceLength)
Expand a %G###### code by looking up the corresponding system name in any
cgalaxy.
*/
static NSString *ExpandSystemNameForGalaxyEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
{
NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
NSCParameterAssert(characters[idx + 1] == 'G');

// A valid %G escape is always eight characters including the six digits.
*replaceLength = 8;

#define kInvalidGEscapeMessage @"String escape code %G must be followed by six integers."
if (EXPECT_NOT(size - idx < 8))
{
// Too close to end of string to actually have six characters, let alone six digits.
SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidGEscapeMessage);
return nil;
}

char hundreds = characters[idx + 2];
char tens = characters[idx + 3];
char units = characters[idx + 4];
char galHundreds = characters[idx + 5];
char galTens = characters[idx + 6];
char galUnits = characters[idx + 7];

if (!(isdigit(hundreds) && isdigit(tens) && isdigit(units) && isdigit(galHundreds) && isdigit(galTens) && isdigit(galUnits)))
{
SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidGEscapeMessage);
return nil;
}

OOSystemID sysID = (hundreds - '0') * 100 + (tens - '0') * 10 + (units - '0');
if (sysID > kOOMaximumSystemID)
{
SyntaxError(context, @"strings.expand.invalidJEscape.range", @"String escape code %%G%3u for system is out of range (must be less than %u).", sysID, kOOMaximumSystemID + 1);
return nil;
}

OOGalaxyID galID = (galHundreds - '0') * 100 + (galTens - '0') * 10 + (galUnits - '0');
if (galID > kOOMaximumGalaxyID)
{
SyntaxError(context, @"strings.expand.invalidJEscape.range", @"String escape code %%G%3u for galaxy is out of range (must be less than %u).", galID, kOOMaximumGalaxyID + 1);
return nil;
}

return [UNIVERSE getSystemName:sysID forGalaxy:galID];
}


/* ExpandSystemNameEscape(context, characters, size, idx, replaceLength)
Expand a %J### code by looking up the corresponding system name in the
Expand All @@ -1043,7 +1100,7 @@ static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSStri
// A valid %J escape is always five characters including the three digits.
*replaceLength = 5;

#define kInvalidJEscapeMessage @"String escape code %%J must be followed by three integers."
#define kInvalidJEscapeMessage @"String escape code %J must be followed by three integers."
if (EXPECT_NOT(size - idx < 5))
{
// Too close to end of string to actually have three characters, let alone three digits.
Expand Down
1 change: 1 addition & 0 deletions src/Core/Universe.h
Expand Up @@ -638,6 +638,7 @@ enum
- (id) systemDataForGalaxy:(OOGalaxyID) gnum planet:(OOSystemID) pnum key:(NSString *)key;
- (NSArray *) systemDataKeysForGalaxy:(OOGalaxyID)gnum planet:(OOSystemID)pnum;
- (NSString *) getSystemName:(OOSystemID) sys;
- (NSString *) getSystemName:(OOSystemID) sys forGalaxy:(OOGalaxyID) gnum;
- (OOGovernmentID) getSystemGovernment:(OOSystemID) sys;
- (NSString *) getSystemInhabitants:(OOSystemID) sys;
- (NSString *) getSystemInhabitants:(OOSystemID) sys plural:(BOOL)plural;
Expand Down
18 changes: 15 additions & 3 deletions src/Core/Universe.m
Expand Up @@ -7792,7 +7792,13 @@ - (id) systemDataForGalaxy:(OOGalaxyID)gnum planet:(OOSystemID)pnum key:(NSStrin

- (NSString *) getSystemName:(OOSystemID) sys
{
return [systemManager getProperty:@"name" forSystem:sys inGalaxy:galaxyID];
return [self getSystemName:sys forGalaxy:galaxyID];
}


- (NSString *) getSystemName:(OOSystemID) sys forGalaxy:(OOGalaxyID) gnum
{
return [systemManager getProperty:@"name" forSystem:sys inGalaxy:gnum];
}


Expand Down Expand Up @@ -10242,7 +10248,8 @@ - (void) dumpSystemDescriptionGraphViz
@"\tpercent_I [label=\"%I\\nInhabitants\" shape=diamond]\n"
"\tpercent_H [label=\"%H\\nSystem name\" shape=diamond]\n"
"\tpercent_RN [label=\"%R/%N\\nRandom name\" shape=diamond]\n"
"\tpercent_J [label=\"%J\\nNumbered system name\" shape=diamond]\n\t\n"];
"\tpercent_J [label=\"%J\\nNumbered system name\" shape=diamond]\n"
"\tpercent_G [label=\"%G\\nNumbered system name in chart number\" shape=diamond]\n\t\n"];

// Toss in the Thargoid curses, too
[graphViz appendString:@"\tsubgraph cluster_thargoid_curses\n\t{\n\t\tlabel = \"Thargoid curses\"\n"];
Expand Down Expand Up @@ -10351,11 +10358,16 @@ - (void) addNumericRefsInString:(NSString *)string toGraphViz:(NSMutableString *
[graphViz appendFormat:@"\t%@ -> percent_RN [color=\"0,0,0.65\"]\n", fromNode];
}

// TODO: test graphViz output for @"%Jxxx"
// TODO: test graphViz output for @"%Jxxx" and @"%Gxxxxxx"
if ([string rangeOfString:@"%J"].location != NSNotFound)
{
[graphViz appendFormat:@"\t%@ -> percent_J [color=\"0,0,0.75\"]\n", fromNode];
}

if ([string rangeOfString:@"%G"].location != NSNotFound)
{
[graphViz appendFormat:@"\t%@ -> percent_G [color=\"0,0,0.85\"]\n", fromNode];
}
}


Expand Down

0 comments on commit 91a36b8

Please sign in to comment.