Patch: Writable PlayerShip.compassMode

An area for discussing new ideas and additions to Oolite.

Moderators: winston, another_commander

Post Reply
Astrobe
---- E L I T E ----
---- E L I T E ----
Posts: 609
Joined: Sun Jul 21, 2013 12:26 pm

Patch: Writable PlayerShip.compassMode

Post by Astrobe »

Below is a patch generated with git format-patch. I cannot submit a pull request cause I don't have a Github account (yet?).

It makes playerShip.compassMode writable, mainly in order to let scripts set it to COMPASS_MODE_TARGET. I wanted this because the compass is more accurate than the HUD arrow when the target is off-screen, and also shows clearly when the target is behind the ship.

Code: Select all

From c7ccf914d836b53acba4578d175efc789b6c5d16 Mon Sep 17 00:00:00 2001
From: Astrobe <REDACTED@REDACTED>
Date: Sun, 26 Mar 2017 16:13:31 +0200
Subject: [PATCH] Script-writable compass mode

---
 src/Core/Scripting/OOJSPlayerShip.m | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/Core/Scripting/OOJSPlayerShip.m b/src/Core/Scripting/OOJSPlayerShip.m
index 3a35a37..c1ba4c4 100644
--- a/src/Core/Scripting/OOJSPlayerShip.m
+++ b/src/Core/Scripting/OOJSPlayerShip.m
@@ -162,7 +162,7 @@ static JSPropertySpec sPlayerShipProperties[] =
 	// JS name							ID											flags
 	{ "aftShield",						kPlayerShip_aftShield,						OOJS_PROP_READWRITE_CB },
 	{ "aftShieldRechargeRate",			kPlayerShip_aftShieldRechargeRate,			OOJS_PROP_READWRITE_CB },
-	{ "compassMode",					kPlayerShip_compassMode,					OOJS_PROP_READONLY_CB },
+	{ "compassMode",					kPlayerShip_compassMode,					OOJS_PROP_READWRITE_CB },
 	{ "compassTarget",					kPlayerShip_compassTarget,					OOJS_PROP_READONLY_CB },
 	{ "compassType",					kPlayerShip_compassType,					OOJS_PROP_READWRITE_CB },
 	{ "currentWeapon",					kPlayerShip_currentWeapon,					OOJS_PROP_READWRITE_CB },
@@ -602,6 +602,16 @@ static JSBool PlayerShipSetProperty(JSContext *context, JSObject *this, jsid pro
 			}
 			break;
 			
+		case kPlayerShip_compassMode:
+			sValue = OOStringFromJSValue(context, *value);
+			if(sValue != nil)
+			{
+				OOCompassMode mode=OOCompassModeFromJSValue(context, *value);
+				[player setCompassMode:mode];
+				[player validateCompassTarget];
+				return YES;
+			}
+			break;
 		case kPlayerShip_compassType:
 			sValue = OOStringFromJSValue(context, *value);
 			if (sValue != nil)
-- 
1.8.3.msysgit.0
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6682
Joined: Wed Feb 28, 2007 7:54 am

Re: Patch: Writable PlayerShip.compassMode

Post by another_commander »

Looks OK and I think we can roll it. The only thing is, be aware that the compass code is very sensitive due to the way the modes have been programmed and switching modes manually may lead to unpredictable results sometimes. Not saying that it will certainly happen, but let's keep it in mind in case any compass bugs come up.

It should also be noted that this patch allows mode changes without considering whether the player has the advanced compass installed or not. Any mode other than COMPASS_MODE_BASIC is effectively awarding an advanced compass for free, so scripters using this should be careful.
Astrobe
---- E L I T E ----
---- E L I T E ----
Posts: 609
Joined: Sun Jul 21, 2013 12:26 pm

Re: Patch: Writable PlayerShip.compassMode

Post by Astrobe »

another_commander wrote: Sun Mar 26, 2017 3:51 pm
It should also be noted that this patch allows mode changes without considering whether the player has the advanced compass installed or not. Any mode other than COMPASS_MODE_BASIC is effectively awarding an advanced compass for free, so scripters using this should be careful.
On one hand I think it's more efficient if we test for the ASC ourselves, but on the other hand not testing for it may open more possibilities. What do you think? I can republish a fixed version.
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6682
Joined: Wed Feb 28, 2007 7:54 am

Re: Patch: Writable PlayerShip.compassMode

Post by another_commander »

I'd leave it for the scripts to test. More possibilities is good. :-)
Astrobe
---- E L I T E ----
---- E L I T E ----
Posts: 609
Joined: Sun Jul 21, 2013 12:26 pm

Re: Patch: Writable PlayerShip.compassMode

Post by Astrobe »

My ASC got damaged in a fight; I lost the ASC labels and mode symbol but I was still able to switch targets. My script is very basic (if target lock, force ASC to target mode). Unfortunately I repaired it without thinking so I don't know what happens when you relaunch. I'm sure if it qualifies as a bug.
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6682
Joined: Wed Feb 28, 2007 7:54 am

Re: Patch: Writable PlayerShip.compassMode

Post by another_commander »

Anything other than COMPASS_MODE_BASIC is equivalent to an advanced compass. If your scrpit changes it to target mode based only on existing target lock, without first checking its damage status, then you are basically re-awarding it. That's why extra care is required when manually adjusting the compass.
Post Reply