Page 93 of 115

Re: Scripters cove

Posted: Fri Sep 15, 2017 9:15 am
by phkb
I'm sorry if this has been posted elsewhere, but I couldn't find it. How can I determine if a point in space is currently behind a planet/moon/sun from where the player currently is?

Re: Scripters cove

Posted: Fri Sep 15, 2017 9:47 am
by spara
You could calculate the viewing angle of the planet, moon or sun. You could also calculate the angle between the vectors from the player to the stellar object and from the player to the point in space. Then you could do some comparison between the angles.

Re: Scripters cove

Posted: Fri Sep 15, 2017 9:49 am
by Cody
Just maths then? <chortles>

Re: Scripters cove

Posted: Fri Sep 15, 2017 10:52 am
by spara
Something like this might work. Or not. Totally untested and possibly over complicated, but hopefully understandable.

Code: Select all

var vStellarBody = bodyEntity.position;
var rStellarBody = bodyEntity.radius;
var vTarget = targetEntity.position;
var vPlayerShip = player.ship.position;

var vPlayerToStellar = vStellarBody.subtract(vPlayerShip);
var vPlayerToTarget = vTarget.subtract(vPlayerShip);

var dPlayerToStellar = vPlayerToStellar.magnitude();
var dPlayerToTarget = vPlayerToTarget.magnitude();

if (dPlayerToStellar < dPlayerToTarget) {
    var aStellar = Math.asin(rStellarBody / dPlayerToStellar);
    var aTarget = vPlayerToStellar.angleTo(vPlayerToTarget);
    if (aStellar > aTarget)
        \\ hidden
    else
        \\ visible
}

Re: Scripters cove

Posted: Fri Sep 15, 2017 11:43 am
by phkb
Thanks spara! That's a great help! I'll let you know how it goes.

Re: Scripters cove

Posted: Fri Sep 15, 2017 12:07 pm
by spara
Not atan, but asin of course. I'm getting sloppy. Fixed now.

Re: Scripters cove

Posted: Fri Sep 15, 2017 12:44 pm
by spara
Here's a bit more elegant (?) solution :). The idea with this one is to move towards the target for the same amount as is the distance from the center of the planetary body and then test if we're inside the body or not.

Code: Select all

var vStellarBody = bodyEntity.position;
var rStellarBody = bodyEntity.radius;
var vTarget = targetEntity.position;
var vPlayerShip = player.ship.position;

var vPlayerToTarget = vTarget.subtract(vPlayerShip);

var dPlayerToStellar = vPlayerShip.distanceTo(vStellarBody);
var dPlayerToTarget = vPlayerToTarget.magnitude();

if (dPlayerToStellar < dPlayerToTarget) {
    var vTest = vPlayerShip.add(vPlayerToTarget.direction().multiply(dPlayerToStellar));
    var dTest = vTest.distanceTo(vStellarBody);
    if (rStellarBody > dTest)
        \\ hidden
    else
        \\ visible
}
Edit: The problem with this one, is that it's not very exact. It's simpler and it roughly works, but if you want to be sure, you'll use the first method :).
Edit2: The correct amount of movement would be Math.sqrt(dPlayerToStellar^2 - rStellarBody^2) instead of dPlayerToStellar. Then it works. I think. :lol:

Re: Scripters cove

Posted: Thu Sep 28, 2017 4:34 am
by phkb
Thanks for these snippets, spara. Very helpful! Both versions work well with planetary bodies.

A further complication: is it possible to do the same thing with stations? I attempted a very rough hack, by just finding the largest side of the station's bounding box, halving that and using it as a radius value. I couldn't get consistent results though, particularly with very non-round stations (eg Torus stations).

Re: Scripters cove

Posted: Sun Oct 29, 2017 5:22 pm
by Astrobe
I'm using Fuel Tanks OXP, which implements the fuel tank as a mine. I noticed some time ago that enemies seem to adopt an "avoid it" behavior (despite fact that the "mine" is destroyed right after launch), but today a bunch of hostiles stopped attacking me a few seconds after I used a fuel tank.

Is it because the fuel level is part of the "threat assessment" by NPCs, or is it because the value of my ship decreased (I raised the price of a fuel tank to 400Cr) or is it because they detected the launch of a mine?

Re: Scripters cove

Posted: Sun Nov 05, 2017 7:11 pm
by Redspear
This doesn't appear to work and I don't know why...

Code: Select all

	//speed adjustments
this.startUpComplete = this.equipmentAdded = this.playerBoughtNewShip = function()
{	if(player.ship.equipmentStatus("EQ_TRANSIT_GRADE") == "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_TRANSIT_DEFAULT") != "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_PERFORMANCE_DEFAULT") != "EQUIPMENT_OK")
	{player.ship.maxSpeed -= 50;}
	
	if(player.ship.equipmentStatus("EQ_PERFORMANCE_GRADE") == "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_PERFORMANCE_DEFAULT") != "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_TRANSIT_DEFAULT") != "EQUIPMENT_OK")
	{player.ship.maxSpeed += 50;}
	
	if(player.ship.equipmentStatus("EQ_TRANSIT_GRADE") == "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_PERFORMANCE_DEFAULT") == "EQUIPMENT_OK")
	{player.ship.maxSpeed -= 100;}
	
	if(player.ship.equipmentStatus("EQ_PERFORMANCE_GRADE") == "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_TRANSIT_DEFAULT") == "EQUIPMENT_OK")
	{player.ship.maxSpeed += 100;}
}
Hmm... I'll try it as an 'else' argument and report back but I don't think that should be necessary, should it? :?

Re: Scripters cove

Posted: Sun Nov 05, 2017 7:17 pm
by Redspear
Hmm... I'll try it as an 'else' argument and report back but I don't think that should be necessary, should it? :?
Nope, still not working.

Any ideas?

Re: Scripters cove

Posted: Sun Nov 05, 2017 9:19 pm
by phkb
Can you change the code to this and report the log file results?

Code: Select all

this.startUpComplete = this.equipmentAdded = this.playerBoughtNewShip = function()
{	
	var p = player.ship;
	var td = p.equipmentStatus("EQ_TRANSIT_DEFAULT");
	var tg = p.equipmentStatus("EQ_TRANSIT_GRADE");
	var pd = p.equipmentStatus("EQ_PERFORMANCE_DEFAULT");
	var pg = p.equipmentStatus("EQ_PERFORMANCE_GRADE");

	log(this.name, "transit def " + td + ", gde " + tg);
	log(this.name, "perform def " + pd + ", gde " + pg);
	log(this.name, "maxspeed pre  = " + p.maxSpeed);
	if(tg == "EQUIPMENT_OK" && td != "EQUIPMENT_OK" && pd != "EQUIPMENT_OK") {p.maxSpeed -= 50;}
	if(pg == "EQUIPMENT_OK" && pd != "EQUIPMENT_OK" && td != "EQUIPMENT_OK") {p.maxSpeed += 50;}
	if(tg == "EQUIPMENT_OK" && pd == "EQUIPMENT_OK") {p.maxSpeed -= 100;}
	if(pg == "EQUIPMENT_OK" && td == "EQUIPMENT_OK") {p.maxSpeed += 100;}
	log(this.name, "maxspeed post = " + p.maxSpeed);
}

Re: Scripters cove

Posted: Sun Nov 05, 2017 9:21 pm
by Astrobe
Traces. Make sure the function is actually called. Print out the value of the variables you are testing.

Re: Scripters cove

Posted: Sun Nov 05, 2017 10:23 pm
by Redspear
phkb wrote:
Can you change the code to this and report the log file results?
Relevant section of log:

Code: Select all

[shipclassequip-grade]: transit def EQUIPMENT_UNAVAILABLE, gde EQUIPMENT_UNAVAILABLE
[shipclassequip-grade]: perform def EQUIPMENT_UNAVAILABLE, gde EQUIPMENT_UNAVAILABLE
[shipclassequip-grade]: maxspeed pre  = 350
[shipclassequip-grade]: maxspeed post = 350
Hmm... I'm guessing that's not good...

Astrobe wrote:
Traces. Make sure the function is actually called. Print out the value of the variables you are testing.
Yeah, that's good advice... there's so much basic scripting I have to learn. I'm just a beginner fumbling with bits and pieces from the wiki...

Thanks guys :D

Re: Scripters cove

Posted: Sun Nov 05, 2017 10:34 pm
by Redspear
OK, so switching to transit grade in game didn't register.
So I tried the same switch, saved and then restarted with the save game...

Code: Select all

[shipclassequip-grade]: transit def EQUIPMENT_UNAVAILABLE, gde EQUIPMENT_OK
[shipclassequip-grade]: perform def EQUIPMENT_UNAVAILABLE, gde EQUIPMENT_UNAVAILABLE
[shipclassequip-grade]: maxspeed pre  = 350
[shipclassequip-grade]: maxspeed post = 300
Well the numbers appear to be correct...
I'll give it another try with numeric HUD.

(edit:)

And... it works from a save game but not without a save...

Code: Select all

this.equipmentAdded =
Should recognise a change in grade but it's only happening on start up at present.

I'll have a rethink...