Skip to content

Commit befc6c2

Browse files
authored
Merge pull request #450 from OoliteProject/witchjump_js_enhancements
Witchjump js enhancements
2 parents 10fbe3f + 69976a1 commit befc6c2

4 files changed

Lines changed: 78 additions & 15 deletions

File tree

src/Core/Entities/PlayerEntity.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ typedef enum
370370
OOSystemID system_id;
371371
OOSystemID target_system_id;
372372
OOSystemID info_system_id;
373-
373+
OOSystemID previous_system_id;
374+
374375
float occlusion_dial;
375376

376377
OOSystemID found_system_id;
@@ -510,6 +511,8 @@ typedef enum
510511
OORouteType ANA_mode;
511512
OOTimeDelta witchspaceCountdown;
512513

514+
NSString *_jumpCause;
515+
513516
// player commander data
514517
NSString *_commanderName;
515518
NSString *_lastsaveName;
@@ -857,11 +860,15 @@ typedef enum
857860
- (NSPoint) adjusted_chart_centre;
858861
- (OORouteType) ANAMode;
859862

863+
- (NSString *) jumpCause;
864+
- (void) setJumpCause:(NSString *)value;
860865

861866
- (OOSystemID) systemID;
862867
- (void) setSystemID:(OOSystemID) sid;
863868
- (OOSystemID) targetSystemID;
864869
- (void) setTargetSystemID:(OOSystemID) sid;
870+
- (OOSystemID) previousSystemID;
871+
- (void) setPreviousSystemID:(OOSystemID) sid;
865872
- (OOSystemID) nextHopTargetSystemID;
866873
- (OOSystemID) infoSystemID;
867874
- (void) setInfoSystemID: (OOSystemID) sid moveChart:(BOOL) moveChart;

src/Core/Entities/PlayerEntity.m

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,18 @@ - (void) setSystemID:(OOSystemID) sid
696696
}
697697

698698

699+
- (OOSystemID) previousSystemID
700+
{
701+
return previous_system_id;
702+
}
703+
704+
705+
- (void) setPreviousSystemID:(OOSystemID) sid
706+
{
707+
previous_system_id = sid;
708+
}
709+
710+
699711
- (OOSystemID) targetSystemID
700712
{
701713
return target_system_id;
@@ -909,13 +921,16 @@ - (NSDictionary *) commanderDataDictionary
909921
NSString *gal_id = [NSString stringWithFormat:@"%u", galaxy_number];
910922
NSString *sys_id = [NSString stringWithFormat:@"%d", system_id];
911923
NSString *tgt_id = [NSString stringWithFormat:@"%d", target_system_id];
924+
NSString *prv_id = [NSString stringWithFormat:@"%d", previous_system_id];
925+
912926
// Variable requiredCargoSpace not suitable for Oolite as it currently stands: it retroactively changes a savegame cargo space.
913927
//unsigned passenger_space = [[OOEquipmentType equipmentTypeWithIdentifier:@"EQ_PASSENGER_BERTH"] requiredCargoSpace];
914928
//if (passenger_space == 0) passenger_space = PASSENGER_BERTH_SPACE;
915929

916930
[result setObject:gal_id forKey:@"galaxy_id"];
917931
[result setObject:sys_id forKey:@"system_id"];
918932
[result setObject:tgt_id forKey:@"target_id"];
933+
[result setObject:prv_id forKey:@"previous_system_id"];
919934
[result setObject:[NSNumber numberWithFloat:saved_chart_zoom] forKey:@"chart_zoom"];
920935
[result setObject:[NSNumber numberWithInt:ANA_mode] forKey:@"chart_ana_mode"];
921936
[result setObject:[NSNumber numberWithInt:longRangeChartMode] forKey:@"chart_colour_mode"];
@@ -1260,6 +1275,7 @@ - (BOOL)setCommanderDataFromDictionary:(NSDictionary *) dict
12601275
if (longRangeChartMode == OOLRC_MODE_UNKNOWN) longRangeChartMode = OOLRC_MODE_SUNCOLOR;
12611276

12621277
target_system_id = [dict oo_intForKey:@"target_id" defaultValue:system_id];
1278+
previous_system_id = [dict oo_intForKey:@"previous_system_id" defaultValue:system_id];
12631279
info_system_id = target_system_id;
12641280
coord_vals = ScanTokensFromString([[UNIVERSE systemManager] getProperty:@"coordinates" forSystem:target_system_id inGalaxy:galaxy_number]);
12651281
cursor_coordinates.x = [coord_vals oo_unsignedCharAtIndex:0];
@@ -2414,6 +2430,8 @@ - (void) dealloc
24142430

24152431
DESTROY(dockingReport);
24162432

2433+
DESTROY(_jumpCause);
2434+
24172435
[self destroySound];
24182436

24192437
DESTROY(scannedWormholes);
@@ -3874,7 +3892,7 @@ - (void) performWitchspaceExitUpdates:(OOTimeDelta)delta_t
38743892
// similarly reset the misjump range to the traditional 0.5
38753893
[self setScriptedMisjumpRange:0.5];
38763894

3877-
[self doScriptEvent:OOJSID("shipExitedWitchspace")];
3895+
[self doScriptEvent:OOJSID("shipExitedWitchspace") withArgument:[self jumpCause]];
38783896

38793897
[self doBookkeeping:delta_t]; // arrival frame updates
38803898

@@ -7489,9 +7507,14 @@ - (void) enterGalacticWitchspace
74897507

74907508

74917509
[self setStatus:STATUS_ENTERING_WITCHSPACE];
7492-
ShipScriptEventNoCx(self, "shipWillEnterWitchspace", OOJSSTR("galactic jump"), INT_TO_JSVAL(destGalaxy));
7510+
JSContext *context = OOJSAcquireContext();
7511+
[self setJumpCause:@"galactic jump"];
7512+
[self setPreviousSystemID:[self currentSystemID]];
7513+
ShipScriptEvent(context, self, "shipWillEnterWitchspace", STRING_TO_JSVAL(JS_InternString(context, [[self jumpCause] UTF8String])), INT_TO_JSVAL(destGalaxy));
7514+
OOJSRelinquishContext(context);
7515+
74937516
[self noteCompassLostTarget];
7494-
7517+
74957518
[self witchStart];
74967519

74977520
[UNIVERSE removeAllEntitiesExceptPlayer];
@@ -7585,7 +7608,11 @@ - (void) enterWormhole:(WormholeEntity *) w_hole
75857608
wormhole = [w_hole retain];
75867609
[self addScannedWormhole:wormhole];
75877610
[self setStatus:STATUS_ENTERING_WITCHSPACE];
7588-
ShipScriptEventNoCx(self, "shipWillEnterWitchspace", OOJSSTR("wormhole"), INT_TO_JSVAL([w_hole destination]));
7611+
JSContext *context = OOJSAcquireContext();
7612+
[self setJumpCause:@"wormhole"];
7613+
[self setPreviousSystemID:[self currentSystemID]];
7614+
ShipScriptEvent(context, self, "shipWillEnterWitchspace", STRING_TO_JSVAL(JS_InternString(context, [[self jumpCause] UTF8String])), INT_TO_JSVAL([w_hole destination]));
7615+
OOJSRelinquishContext(context);
75897616
if ([self scriptedMisjump])
75907617
{
75917618
misjump = YES; // a script could just have changed this to true;
@@ -7653,7 +7680,11 @@ - (void) enterWitchspace
76537680
[self addScannedWormhole:wormhole];
76547681

76557682
[self setStatus:STATUS_ENTERING_WITCHSPACE];
7656-
ShipScriptEventNoCx(self, "shipWillEnterWitchspace", OOJSSTR("standard jump"), INT_TO_JSVAL(jumpTarget));
7683+
JSContext *context = OOJSAcquireContext();
7684+
[self setJumpCause:@"standard jump"];
7685+
[self setPreviousSystemID:[self currentSystemID]];
7686+
ShipScriptEvent(context, self, "shipWillEnterWitchspace", STRING_TO_JSVAL(JS_InternString(context, [[self jumpCause] UTF8String])), INT_TO_JSVAL(jumpTarget));
7687+
OOJSRelinquishContext(context);
76577688

76587689
[self updateSystemMemory];
76597690
NSUInteger legality = [self legalStatusOfCargoList];
@@ -7854,7 +7885,7 @@ - (void) leaveWitchspace
78547885
[self doScriptEvent:OOJSID("playerEnteredNewGalaxy") withArgument:[NSNumber numberWithUnsignedInt:galaxy_number]];
78557886
}
78567887

7857-
[self doScriptEvent:OOJSID("shipWillExitWitchspace")];
7888+
[self doScriptEvent:OOJSID("shipWillExitWitchspace") withArgument:[self jumpCause]];
78587889
[UNIVERSE setUpBreakPattern:[self breakPatternPosition] orientation:orientation forDocking:NO];
78597890
}
78607891

@@ -12955,6 +12986,20 @@ - (void) setDockTarget:(ShipEntity *)entity
1295512986
}
1295612987

1295712988

12989+
- (NSString *) jumpCause
12990+
{
12991+
return _jumpCause;
12992+
}
12993+
12994+
12995+
- (void) setJumpCause:(NSString *)value
12996+
{
12997+
NSParameterAssert(value != nil);
12998+
[_jumpCause autorelease];
12999+
_jumpCause = [value copy];
13000+
}
13001+
13002+
1295813003
- (NSString *) commanderName
1295913004
{
1296013005
return _commanderName;

src/Core/Scripting/OOJSPlayerShip.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
kPlayerShip_targetSystem, // target system id, int, read-write
161161
kPlayerShip_nextSystem, // next hop system id, read-only
162162
kPlayerShip_infoSystem, // info (F7 screen) system id, int, read-write
163+
kPlayerShip_previousSystem, // previous system id, read-only
163164
kPlayerShip_torusEngaged, // torus in use, boolean, read-only
164165
kPlayerShip_viewDirection, // view direction identifier, string, read-only
165166
kPlayerShip_viewPositionAft, // view position offset, vector, read-only
@@ -230,6 +231,7 @@
230231
{ "targetSystem", kPlayerShip_targetSystem, OOJS_PROP_READWRITE_CB },
231232
{ "nextSystem", kPlayerShip_nextSystem, OOJS_PROP_READONLY_CB },
232233
{ "infoSystem", kPlayerShip_infoSystem, OOJS_PROP_READWRITE_CB },
234+
{ "previousSystem", kPlayerShip_previousSystem, OOJS_PROP_READONLY_CB },
233235
{ "torusEngaged", kPlayerShip_torusEngaged, OOJS_PROP_READONLY_CB },
234236
{ "viewDirection", kPlayerShip_viewDirection, OOJS_PROP_READONLY_CB },
235237
{ "viewPositionAft", kPlayerShip_viewPositionAft, OOJS_PROP_READONLY_CB },
@@ -477,6 +479,10 @@ static JSBool PlayerShipGetProperty(JSContext *context, JSObject *this, jsid pro
477479
*value = INT_TO_JSVAL([player infoSystemID]);
478480
return YES;
479481

482+
case kPlayerShip_previousSystem:
483+
*value = INT_TO_JSVAL([player previousSystemID]);
484+
return YES;
485+
480486
case kPlayerShip_routeMode:
481487
{
482488
OORouteType route = [player ANAMode];

src/Core/Universe.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,12 @@ - (void) carryPlayerOn:(StationEntity*)carrier inWormhole:(WormholeEntity*)wormh
10321032

10331033
[player setWormhole:wormhole];
10341034
[player addScannedWormhole:wormhole];
1035-
1036-
ShipScriptEventNoCx(player, "shipWillEnterWitchspace", OOJSSTR("carried"), INT_TO_JSVAL(dest));
1037-
1035+
JSContext *context = OOJSAcquireContext();
1036+
[player setJumpCause:@"carried"];
1037+
[player setPreviousSystemID:[player systemID]];
1038+
ShipScriptEvent(context, player, "shipWillEnterWitchspace", STRING_TO_JSVAL(JS_InternString(context, [[player jumpCause] UTF8String])), INT_TO_JSVAL(dest));
1039+
OOJSRelinquishContext(context);
1040+
10381041
[self allShipsDoScriptEvent:OOJSID("playerWillEnterWitchspace") andReactToAIMessage:@"PLAYER WITCHSPACE"];
10391042

10401043
[player setRandom_factor:(ranrot_rand() & 255)]; // random factor for market values is reset
@@ -1076,8 +1079,8 @@ - (void) carryPlayerOn:(StationEntity*)carrier inWormhole:(WormholeEntity*)wormh
10761079
alpha:0.0f];
10771080

10781081
[self setWitchspaceBreakPattern:YES];
1079-
[player doScriptEvent:OOJSID("shipWillExitWitchspace")];
1080-
[player doScriptEvent:OOJSID("shipExitedWitchspace")];
1082+
[player doScriptEvent:OOJSID("shipWillExitWitchspace") withArgument:[player jumpCause]];
1083+
[player doScriptEvent:OOJSID("shipExitedWitchspace") withArgument:[player jumpCause]];
10811084
[player setWormhole:nil];
10821085

10831086
}
@@ -1095,7 +1098,8 @@ - (void) setUpUniverseFromStation
10951098
// check the nearest system
10961099
OOSystemID sys = [self findSystemNumberAtCoords:coords withGalaxy:[player galaxyNumber] includingHidden:YES];
10971100
BOOL interstel =[dockedStation interstellarUndockingAllowed];// && (s_seed.d != coords.x || s_seed.b != coords.y); - Nikos 20110623: Do we really need the commented out check?
1098-
1101+
[player setPreviousSystemID:[player currentSystemID]];
1102+
10991103
// remove everything except the player and the docked station
11001104
if (dockedStation && !interstel)
11011105
{ // jump to the nearest system
@@ -1148,8 +1152,9 @@ - (void) setUpUniverseFromStation
11481152
[dockedStation setPosition: pos];
11491153
}
11501154
[self setWitchspaceBreakPattern:YES];
1151-
[player doScriptEvent:OOJSID("shipWillExitWitchspace")];
1152-
[player doScriptEvent:OOJSID("shipExitedWitchspace")];
1155+
[player setJumpCause:@"carried"];
1156+
[player doScriptEvent:OOJSID("shipWillExitWitchspace") withArgument:[player jumpCause]];
1157+
[player doScriptEvent:OOJSID("shipExitedWitchspace") withArgument:[player jumpCause]];
11531158
}
11541159
}
11551160
}

0 commit comments

Comments
 (0)