[Discussion] Generic X3TC S&M questions II

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

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

User avatar
s9ilent
Posts: 2033
Joined: Wed, 29. Jun 05, 01:45
x4

Post by s9ilent »

How do I make a ship stop it's current task, and start a new one?

(I seem to have forgotten....)
e.g. If it already has a priority 0, script on task 0
How can I make it run a different one?
(I tried everything :S but I can't seem to make it work :S)
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

If you run the starting script globally you can simply say:
(1) START $ship -> call 'script.xyz':
or
(2) $ship -> start task 0 with script 'script.xyz' and ...

If you run the starting script locally on the corresponding ship (1) and (2) don't work if the starting script itself is running on task 0 (in other words: You cannot start a script on the same task as the starting script is running).
But they work just fine, if you run them on a different task.

Greetings,
ScRaT
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

Code: Select all

$Numbers = array alloc: size=6
$Numbers[0] = 4
$Numbers[1] = 2
$Numbers[2] = 24
$Numbers[3] = 6
$Numbers[4] = 12
$Numbers[5] = 1
 
$Numbers = sort array $Numbers
 
write to player logbook $Numbers
Output: ARRAY(1, 12, 2, 24, 4, 6)

My attitude toward whomever's implementation this was

---EDIT:

Code: Select all

$Numbers = sort array: data=$Numbers sort values=$Numbers
That works fine, however.
Sorenson
Posts: 340
Joined: Thu, 9. Apr 09, 00:43
x3tc

Post by Sorenson »

I'm trying to rig a setup file that governs the execution of several other scripts to only execute said scripts if they're not already running. I've tried using a custom value approach, but it didn't seem to take, so I'mm pretty certain I've made some kind of mistake here but can't really think of where it is:

Code: Select all

= wait 1000 ms
$NSBTS = get global variable: name='naval.shuffle.bts'
if not $NSBTS 
  START [THIS]->call script 'Naval.Shuffle.BuyTerranStations' :
  set global variable: name='naval.shuffle.bts' value=$NSBTS
else
  return null
end
= wait 1000 ms
$NSFDS = get global variable: name='naval.shuffle.fds'
if not $NSFDS 
  START [THIS]->call script 'Naval.Shuffle.FixDronesSats' :
  set global variable: name='naval.shuffle.fds' value=$NSFDS
else
  return null
end
= wait 1000 ms
$NS204 = get global variable: name='naval.shuffle.204'
if not $NS204 
  START [THIS]->call script 'Naval.Shuffle.FixTech204' :
  set global variable: name='naval.shuffle.204' value=$NS204
else
  return null
end
= wait 1000 ms
$NSSFF = get global variable: name='naval.shuffle.sff'
if not $NSSFF 
  START [THIS]->call script 'Naval.Shuffle.SplitFoodFix' :
  set global variable: name='naval.shuffle.sff' value=$NSSFF
else
  return null
end
= wait 1000 ms
return null
I was thinking of "if playing time is < X then return null" deal, but that would limit script execution to the absolute beginning of a game and screw over anybody who doesn't understand how to manually execute scripts in the editor if for whatever reason the scripts died.

EDIT: Hmm. That didn't seem to take, either. I guess I'll go the playtime route, since the following sequence was able to work well enough:

Code: Select all

= wait 1000 ms
$Playtime = playing time
if $Playtime <= 30 
  START [THIS]->call script 'Naval.Shuffle.BuyTerranStations' :
  = wait 1000 ms
  START [THIS]->call script 'Naval.Shuffle.FixDronesSats' :
  = wait 1000 ms
  START [THIS]->call script 'Naval.Shuffle.FixTech204' :
  = wait 1000 ms
  START [THIS]->call script 'Naval.Shuffle.SplitFoodFix' :
else if $Playtime >= 30 
  return null
end
return null
Not too many chances to save+reload in the first 30 seconds of a game, so that should make it almost impossible for duplicates to run.
Last edited by Sorenson on Sat, 20. Nov 10, 23:56, edited 2 times in total.
X3TC Naval Shuffle: My ships are fast; my shields are strong; and my guns are very, very large.
User avatar
ScRaT_GER
Posts: 1962
Joined: Tue, 8. Jan 08, 18:19
x3tc

Post by ScRaT_GER »

Quite easy: If the script is already running, you immediately return null.

I've rewritten your code a bit, so that the problem becomes really clear.

Code: Select all

$NSBTS = get global variable: name='naval.shuffle.bts'
if not $NSBTS
  START [THIS]->call script 'Naval.Shuffle.BuyTerranStations' :
  set global variable: name='naval.shuffle.bts' value=$NSBTS
else if $NSBTS
  return null
end 
To solve the problem, simply remove all "else - return null" statments from your code.

Greetings,
ScRaT
User avatar
Gazz
Posts: 13244
Joined: Fri, 13. Jan 06, 16:39
x4

Post by Gazz »

There is a safer way and you don't need to mess with any variables.

All scripts are always started by the setup blindly.

The first line of the global scripts fetches "last load time".

In their loop they do that again. If last load time changed from the original value: return null.
Another instance of the script is doing the job now...

This can't go wrong and you never need an uninstall script / signal for them.
My complete script download page. . . . . . I AM THE LAW!
There is no sense crying over every mistake. You just keep on trying till you run out of cake.
mark_a_condren
Posts: 1468
Joined: Wed, 3. Aug 05, 05:05
x3tc

Post by mark_a_condren »

Just another suggestion from your code, if you use the 'set global variable' command for anything it's a good idea to define the value. In your code the 'value=$NSBTS' hasn't actually been defined a value. '$NSBTS' would be null.

You could do it by changing it to

Code: Select all

set global variable: name='naval.shuffle.bts' value=TRUE

MarCon
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Anyone know what the most efficient way of getting all the sectors belonging to a race is?

I need to get every sector of $Race as an array and loop through them, and loop through their ships, and then mark each one down as having however many ships of a certain race/type etc., then output the arrays of the sectors as High, Low, Medium or whatever, and put them in a list...

So,

High Enemy Activity
Sector 1
Sector 2

Medium...

Etc. etc.

I just don't know the most efficient way of getting all those sectors, and I'm not too sure about putting an array of sectors into the menu either. Thanks.
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

EmperorJon wrote:Anyone know what the most efficient way of getting all the sectors belonging to a race is?
Sorry, brother. You're going to have to grab them all with "find all sector within jumps", loop through that array adding only the sectors to a new array that have $Owner == $Race.
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Gateless sectors. :(

How about using the coordinates?

Inc X until it's 20 or what have you, then set to 0 and Inc Y, then start again...
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

Aaah, yes. I keep forgeting that. I don't have gateless sectors in my game.

Sounds like you know how to do it, but I hear you. I always look for a more efficient way too. Unfortunately, you're going to have to use that loop.

If it's going to be called automatically at a set interval (like an AL plugin), I'd just call it once at setup. and store them in an array. I'd still check for ownership, though, as some plugins change ownership dynamically.
User avatar
EmperorJon
Posts: 9378
Joined: Mon, 29. Dec 08, 20:58
x3tc

Post by EmperorJon »

Due to what I need it for, I will need to run it every single time. -.-
______
I'm Jon. I'm mostly not around any more. If you want to talk, please message me! It's cool.
______
User avatar
s9ilent
Posts: 2033
Joined: Wed, 29. Jun 05, 01:45
x4

Post by s9ilent »

@Sorenson
Just one thing your code does this:

$NSBTS = get global variable: name='naval.shuffle.bts'
if not $NSBTS
START [THIS]->call script 'Naval.Shuffle.BuyTerranStations' :
set global variable: name='naval.shuffle.bts' value=$NSBTS
else if $NSBTS
return null
end


essentially, your doing,
If null
-$start (Does this script set the global variable internally??)
-Set global variable to null
else
-return
end
Your global variable will only ever be null







Also,
As other suggested above, if you can do it with out a global variable, by all means do it.
Otherwise, you should set the variable PRIOR to starting
Also, if your script does not execute immediately, and you need to give it some kind of "shutdown" signal, I would recommend making your variable three stated, Running, "shut down at next read" and null (nothing)
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

Anyone know how to get the opposite of Unitraders script:

Code: Select all

Input: 

1: alpha , Var/Number , 'Angle alpha (0-65536)' - Angle Alpha (Number from 0 to 65536) 

2: beta , Var/Number , 'Angle beta (0-65536)' - Angle Beta (Number from 0 to 65536) (use Alpha & Beta of a Ship to get a position in front or behind of it ) 

3: dis , Var/Number , 'Distance in m' - Like the Text says - the Dinstance from the RefPos ^^ (when use with Ships: Positive Int -> in front of a Ship, Negative Int -> behind a ship ) 

4: pos0.arr , Value , 'position array' - RefPos as X3-Position-Array (IF null THEN RefPos = 0/0/0) ; Improvement by Gazz: you can use an Object as RefPos. In this case there are no alpha- and beta-angles necessary, they will be taken from the RefObj  

Output: 

ARRAY ( X , Y , Z ) X3-Position array



I'm looking for the opposite. Given two points (object 1, object 2), I need the angles returned that would make object 1 face object 2.
User avatar
s9ilent
Posts: 2033
Joined: Wed, 29. Jun 05, 01:45
x4

Post by s9ilent »

Oooh... tricky :S

This game does not have inverse trig scripts


Mathematically you would do the following

let dy dy dz = the displacement (for x y z)
let Dist1 = sqrt (dx^2 + dy^2)
let dist2 = sqrt (dist1^2 + dz ^2)

cos alpha = dy / dist1
alpha = arccos ( dy / dist1 )

cos beta = dist1 / dist2
beta = arccos ( dist1 / dist2 )




And unfortunately, the approximations for inverse sin and cos involve fractions and indicies, so the loss of accuracy by using integers would be huge
http://en.wikipedia.org/wiki/Trigonomet ... _functions
(read the equation just under the line:
Just like the sine and cosine, the inverse trigonometric functions can also be defined in terms of infinite series. For example,
)

By using excel, using that formula (assuming no loss of accuracy through decimals) the value is off by, when using

% off, max square term
26, 3
21, 5
18, 7
16, 9

And realistically, the best you could do in x3 is to the third power (with an even greater loss of accuracy with decimals)






So, given that you can not calculate the inverse cos, you can make tfiles with 65k entries which you can look up from thou :P
Last edited by s9ilent on Sun, 21. Nov 10, 22:51, edited 1 time in total.
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

Thanks, dude. I have a nine digit floating point library, so I'm not concered about loss of accuracy. I'm actually not using UniTrader's script, as I wrote my own. But it was easy, as there's a fixed sin and cos.

I can do everything you wrote except the arccos! Looks like you're better at math than I am. Any ideas?
User avatar
s9ilent
Posts: 2033
Joined: Wed, 29. Jun 05, 01:45
x4

Post by s9ilent »

Honestly, to maintain accuracy, I would use a look up table instead


Precalculate the inverse cos (I would do inverse sin for good measure as well)
for angles 0/65k -> 65k/65k and stick it in a tfile
The loss of accuracy with the taylor series expansion is fairly high and given the computing power required to generate it, it is better to just use a look up. (And x3 can handle large volumes of text, as it already has thousands of tfile entries)


But wait, you have a floating point maths library :O show me show me show me


(I edited my previous post with more details as well)
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

I'll PM you. It's a bastardized version of several works here. On its way.
mark_a_condren
Posts: 1468
Joined: Wed, 3. Aug 05, 05:05
x3tc

Post by mark_a_condren »

@ EmperorJon

Hi mate,

Exactly what you are after with the 'get sectors by race' is available from my MarCon Utilities / Demos , just look under the 'Library Scripts' section.

Or you can DL the script directly from here. I would suggest reading the info available from my "utilities' threads about it though.

MarCon
User avatar
Litcube
Posts: 4254
Joined: Fri, 20. Oct 06, 19:02
xr

Post by Litcube »

s9ilent wrote:let dy dy dz = the displacement (for x y z)
let Dist1 = sqrt (dx^2 + dy^2)
let dist2 = sqrt (dist1^2 + dz ^2)

cos alpha = dy / dist1
alpha = arccos ( dy / dist1 )

cos beta = dist1 / dist2
beta = arccos ( dist1 / dist2 )

I did it! And to a rediculous accuracy. I can perfectly center on an object about 3500 km our from center sector. The only decimals I dropped was from SQRT, and it's still more accurate than you'd need in game. I could probably fire a firelance missile at a Q 2000 KM out and still hit it, if it stayed still.

Couldn't have done it without your help. Thanks!

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”