[HELP] [MD] how to recall a planet's position and size and calculate distance

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

Realspace
Posts: 1637
Joined: Wed, 15. Nov 06, 10:21
x4

[HELP] [MD] how to recall a planet's position and size and calculate distance

Post by Realspace »

I've been struggling for days to write a code that is capable to determine the distance of the player's ship from a planet in the sector.
I need it to make so that the md script interrupts the travel engine (I transformed into an hyperdrive with very high speed) to avoid the player to travel inside a planet. At very high speed the collision does not work and anyway I want that the speed returns to normal in proximity of a planet, for both immersion and gameplay.

Seems that no matter how I recall the planet, I get a lookup error when asking to calculate player.entity or player.ship distance from it.

First, the only conditions that work are <event_player_travelmode_started/> or <event_player_changed_activity activity="activity.travel" /> and so on.
If I try to use an <event_cue_signalled cue="md.Setup.Start"/> nothing happens, even with instantiate="true".

Then the part that matters most, I tried to call the planet in more than one way, as is used for instance in the md/terraforming.xml.

Code: Select all

<set_value name="$Sector" exact="player.sector"/>
                <set_value name="$Cluster" exact="$Sector.cluster"/>
		<set_value name="$ClosePlanet" exact="$Cluster.planets"/>
		<set_value name="$PlayerShip" exact="player.ship"/>
OR

Code: Select all

 <find_cluster_in_range name="$Cluster" object="$Sector" maxdistance="1" multiple="true" sortbydistanceto="$PlayerShip" sortlimit="1"/>	 
	  <find_object_component name="$ClosePlanet" object="$Cluster" class="class.planet" multiple="true" sortbydistanceto="$PlayerShip" sortlimit="1"/>
then for both ways:

Code: Select all

 <set_value name="$ClosePlanetSize" exact="$ClosePlanet.size"/>
which does not work, gives error (there seems to be no way to extract the planet's size and position and I don't know how the distanceto is calculated, from center or surface of the planet :gruebel:

finally, the not working code:

Code: Select all

   <do_if value="(player.activity == activity.travel or $PlayerShip.travel.active) and ($ClosePlanet.distanceto.{$PlayerShip} lt 200km)">
				 <play_sound sound="'ui_interaction_not_possible'" type="ui" />
				 <set_cockpit_tint resettodefault="true" sethudcolors="true" />
				  <set_player_activity activity="activity.none" />
				  <speak actor="player.computer" line="30224658" priority="1" comment="Planet is nearby. Travel Mode is not possible." />
				  <show_help duration="3s" custom="'You are too close to a planet to use the Travel Drive'"/>
				   <set_playership_throttle value="100" comment="to force stop, no matter what"/>
                <force_player_speed speed="100" comment="Throttle value (defaults to 0) (-1 for full reverse, 0 for stop and 1 for full forward)"/>
					</do_if>
problem always arises as: Property lookup failed: $ClosePlanet.distanceto
* Expression: (player.activity == activity.travel or $PlayerShip.travel.active) and $ClosePlanet.distanceto.{$PlayerShip} lt 200000m


I tried 20000km as test, because it is so big that any distance has to be included, but errors are always, no matter how I calculate the distance or if I use the $ClosePlanetSize

Action is anyway activated but only the (player.activity == activity.travel or $PlayerShip.travel.active) is read so of course (as expected) the traveldrive deactivates always at any distance, because $ClosePlanet.distanceto.{$PlayerShip} is discarded.

I tried several others way to recall the planet in the sector or in the cluster, using simple <find_cluster, <find_sector, nothing seems to work :cry:
User avatar
euclid
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 13486
Joined: Sun, 15. Feb 04, 20:12
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by euclid »

Have you tried $ClosePlanet.{1}.position and print that to log or file? The same with $PlayerShip.position and the difference should give the distance. The planet position should be the centre and the default "kill" value (that is if you get too close) is 10000m if I remember correctly.

Cheers Euclid
"In any special doctrine of nature there can be only as much proper science as there is mathematics therein.”
- Immanuel Kant (1724-1804), Metaphysical Foundations of the Science of Nature, 4:470, 1786
Realspace
Posts: 1637
Joined: Wed, 15. Nov 06, 10:21
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by Realspace »

First of all thanks for answering :-)
But I admit I don't understand how your suggestion works exactly, "print that to log or file" ...(??). I am still very basic in the md despite trying to learn a lot :oops:
User avatar
euclid
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 13486
Joined: Sun, 15. Feb 04, 20:12
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by euclid »

Just to be sure the array of planets is not empty and if so, the position works. You can check that in-game by printing it to the player log, for example

Code: Select all

 <write_to_logbook category="upkeep" title="'PlanetTest'" text="'First Planet %1 at positionn %2'.[$ClosePlanet.{1},$ClosePlanet.{1}.position]" /> 
Alternatiovely you can print it to an external log file (this requires a command line parameter), for example

Code: Select all

<debug_to_file name="'Planets'" directory="'PlanetTest'" text="'First Planet ' + $ClosePlanet.{1} + ' with position' + $ClosePlanet.{1}.position" output="false" append="true" /> 
You can also print the entire array by removing the .{1}. Be aware that planets usually do not have names and hence all you will see is an array of hex code.

For the command line parameters I recommend to read the sticky I've posted here in this forum. However, those you'll need are, for example:

""E:\Games\Steam\SteamApps\common\X4 Foundations\X4.exe" -logfile debuglog.txt -scriptlogfiles

The debug part is not required to write to an external file but I highly recommend it as you can check the debug log for any errors.

Cheers Euclid
"In any special doctrine of nature there can be only as much proper science as there is mathematics therein.”
- Immanuel Kant (1724-1804), Metaphysical Foundations of the Science of Nature, 4:470, 1786
Realspace
Posts: 1637
Joined: Wed, 15. Nov 06, 10:21
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by Realspace »

Yes I use the log of course but ignored that one could command some specific parts to be logged..thanks.

In the meantime I've found another way to implement travel disrupt through the regions, it works fine. EGO has made a lot of features that are not then fully used in the game. Using damage regions extensivelly causes massive calculation so I discarded them, but found another way to use it without damage calculation (so not stored in the save) and it opened a wonderful new gameplay, adding low orbit disrupt, field disrupt, sun corona's disrupts and damage...great..this game is full of small threasures not used

Btw ..do you know if a way exist to recall in md if the ship is entering a specific region?
User avatar
euclid
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 13486
Joined: Sun, 15. Feb 04, 20:12
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by euclid »

I'm glad you've solved the problem :-)

Concerning the md question, I'm not aware of any event that triggers on entering a specific region. However, you can check if a sector contains hazardous or gravidarobscuring regions.

Cheers Euclid
"In any special doctrine of nature there can be only as much proper science as there is mathematics therein.”
- Immanuel Kant (1724-1804), Metaphysical Foundations of the Science of Nature, 4:470, 1786
h2o.Ava
Posts: 349
Joined: Sun, 12. Oct 08, 04:04
x4

Re: [HELP] [MD] how to recall a planet's position and size and calculate distance

Post by h2o.Ava »

Realspace wrote: Wed, 8. Jan 25, 13:31 Yes I use the log of course but ignored that one could command some specific parts to be logged..thanks.

In the meantime I've found another way to implement travel disrupt through the regions, it works fine. EGO has made a lot of features that are not then fully used in the game. Using damage regions extensivelly causes massive calculation so I discarded them, but found another way to use it without damage calculation (so not stored in the save) and it opened a wonderful new gameplay, adding low orbit disrupt, field disrupt, sun corona's disrupts and damage...great..this game is full of small threasures not used

Btw ..do you know if a way exist to recall in md if the ship is entering a specific region?
what'd you find?

Return to “X4: Foundations - Scripts and Modding”