[X3TC] Maths: Ceil/Floor values ?

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

Post Reply
PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

[X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Wed, 17. Feb 21, 20:42

I work on a script who increase the skill of my marines every times boarding parties are a success

My first version use multiple intervals conditions to check the final value of the ADDED SKILL

Code: Select all

***
***
***
    if $mar.skill > 90
      $set.skill.add = random value from zero to 2 - 1
    else if $mar.skill <= 90 AND $mar.skill > 75
      $set.skill.add = random value from 1 to 4 - 1
    else if $mar.skill <= 75 AND $mar.skill > 55
      $set.skill.add = random value from 3 to 7 - 1
    else if $mar.skill <= 55 AND $mar.skill > 30
      $set.skill.add = random value from 6 to 11 - 1
    else if $mar.skill < 30
      $set.skill.add = random value from 10 to 16 - 1
    end
***
***
***
For the fun, I want to replace those ugly lines with maths
my calculation line is: (100 - $mar.skill) / 5

My question is How the "random value from <min> to <max>" work ?
Is the returned value is rounded to the closest entire number ?

0.4 -> 0 ???
0.5 -> 1 ???
0.6 -> 1 ???

User avatar
Joubarbe
Posts: 4796
Joined: Tue, 31. Oct 06, 12:11
xr

Re: [X3TC] Maths: Ceil/Floor values ?

Post by Joubarbe » Wed, 17. Feb 21, 21:05

1. MSCI does not know float numbers. random value from zero to 2 - 1 is either 0 or 1.
2. Always floor.
3. You should use X-Studio.

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Wed, 17. Feb 21, 21:21

Many thanks Joubarbe !
I will try X-Studio now !

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Wed, 17. Feb 21, 21:38

So my new code is :

Code: Select all

***
***
***
    $math.rel.min = (100 - $mar.skill) / 6 - 1
    if $math.rel.min == 0
      $math.rel.min = random value from zero to 2 - 1
    end
    $math.rel.max = (100 - $mar.skill) / 5 - 1
    if $math.rel.max == 0
      $math.rel.max = random value from zero to 2 - 1
    end   
    if $math.rel.min > $math.rel.max
      $math.rel.min = $math.rel.max
    end
    $set.skill.add = random value from $math.rel.min to $math.rel.max - 1
***
***
***
It should work 8)

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 22197
Joined: Sun, 14. Nov 04, 23:26
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by Cycrow » Wed, 17. Feb 21, 22:02

if the marine skill is full, 100. then it'll actually end up reducing it.
as min would be -1, max -1, so you are getting a random number between -1 and -2

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Wed, 17. Feb 21, 22:51

if the marine skill is full, 100. then it'll actually end up reducing it.

Code: Select all

***
***
***
* Maths relative value
    $math.rel.min = (100 - $mar.skill) / 6 - 1
    if $math.rel.min == 0
      $math.rel.min = random value from zero to 2 - 1
    end
    $math.rel.max = (100 - $mar.skill) / 5 - 1
    if $math.rel.max == 0
      $math.rel.max = random value from zero to 2 - 1
    end
    if $math.rel.min > $math.rel.max
      $math.rel.min = $math.rel.max
    end
    $set.skill.add = random value from $math.rel.min to $math.rel.max - 1
* Add rel. to initial value
    $new.skill = $mar.skill + $set.skill.add - 1
    if $new.skill >= 100
      $new.skill = 100
    end
    if $type == 'Fighting'
      $mar->set marine skill: engineering=$new.skill
    end
    if $type == 'Hacking'
      $mar->set marine skill: hacking=$new.skill
    end
    if $type == 'Mechanical'
      $mar->set marine skill: mechanical=$new.skill
    end
    if $type == 'Engineering'
      $mar->set marine skill: engineering=$new.skill
    end
  end
end

return null

if $new.skill >= 100
$new.skill = 100
end

So those lines are useless ?
as min would be -1, max -1, so you are getting a random number between -1 and -2
By my knowledge, there is no "get absolute value" command ... if iam right
But thanks for the info Cycrow, its weird the game can return "-2" , outside the range limits ... ???

EDIT:
You mean the command can return: 0 OR 1 ?

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Thu, 18. Feb 21, 13:07

This is what i wanted : )

viewtopic.php?t=286349

viewtopic.php?t=286397

The "- 1" is effective in the calculation

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 22197
Joined: Sun, 14. Nov 04, 23:26
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by Cycrow » Thu, 18. Feb 21, 18:37

The -1 in the random is applied to the 2nd number, so you are basically doing -1 and (-1 - 1)

but as 100 is the max, there doesn't need to be an increase at all.

so you could just skip 100

if $new.skil < 100
...
end

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Fri, 19. Feb 21, 18:47

Alright, all my scripts work fine now, at least i didnt found any bug or something strange
(I also redone from scratch a script for claim empty ships, based to your "Salvage Command Software", but very basic)

Cycrow, I used your "Boarding Transporter Extension" script to "insert" my codes for the "extra-xp" for marines
(plugin.boarding.tele) just after the "boarding.done"
Also I added a code to keep weapons and shields of the ship after the boarding
I can't play without this awesome script :)

But now I dont know if the vanilla game add "extra-xp" to marines after a boarding ? :oops:

Btw I will continue to play to check for more bugs

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 22197
Joined: Sun, 14. Nov 04, 23:26
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by Cycrow » Sat, 20. Feb 21, 19:49

to add a boost to the standard boarding routines you would have to attach a script to the signal, SIGNAL_CAPTURED. Then get the list of marines and apply it.

there are 2 small issues though, 1 is that you dont actually know which marines were the attackers, if some defending marines switch sides, this would be included.
also, if there isn't enough room for all the marines, some will be ejected, so you will only get those that are left on the ship

PSCO1
Posts: 1087
Joined: Thu, 4. Jun 09, 17:08
x4

Re: [X3TC] Maths: Ceil/Floor values ?

Post by PSCO1 » Sun, 21. Feb 21, 19:09

I don't know how to use "SIGNAL_CAPTURED" for the moment...

I sliced the end part of your "plugin.boarding.tele" script (after the % boarding teleport counter)

1 part for "EMPTY.SHIPS" (ships at NEUTRAL race, bailed crew)
and
1 part for "REGULAR.BOARDING" (Cycrow code)


For the "EMPTY.SHIPS" part my objectives is:
- Keep hull integrity (even if newbies marines in boarding crew)
- Switch quickly victim ship "NEUTRAL RACE" to "PLAYER RACE"
- Delete marines of the boarding crew from the attacker ship
- Re-create marines (sames names, sames skills, etc...) in victim ship
- Check for the RIGHT ARRAY and NUMBER of marines boarding the victim ship
- Keep hardware of boarded ship (as mentioned in previous post)

There is the end lines of "my" "plugin.boarding.tele"

Code: Select all

***
***
***
* boarded
if $done >= 100
  $text = sprintf: pageid=$page.id textid=36, $aim, null, null, null, null
  display subtitle text: text=$text duration=2000 ms
  
  $count = size of array $aTakeMarines
* ------------ SHIP EMPTY ------------------
  $mar.arr = $aim->get marines array
  if $mar.arr == null
    
    $empty.ship = [TRUE]
    = wait randomly from 2000 to 3000 ms
    START speak text: page=13 id=131 priority=10
    $aim->set owner race to {Player}
    
    = $aim->install 1 units of {Cargo Lifesupport System}
    $mars.ejd = array alloc: size=0
    $aMarines = $SHIP->get marines array
    
    $sorted.mar.array = [THIS]->call script 'lib.cycrow.sortmarines' : a.marine=$aMarines a.type=null

    $cnt.aMar = size of array $sorted.mar.array
    while $cnt.aMar
      if $aim -> free space for marines
        $mar = $sorted.mar.array[$cnt.aMar]
        $mar.name = $mar->get pilot name
        $mar.race = $mar->get owner race
        $mar.figh = $mar->get marine fighting skill
        $mar.figh.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.figh  empty.ship=[TRUE]
        $mar.hack = $mar->get marine hacking skill
        $mar.hack.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.hack  empty.ship=[TRUE]
        $mar.mech = $mar->get marine mechanical skill
        $mar.mech.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.mech  empty.ship=[TRUE]
        $mar.engi = $mar->get marine engineering skill
        $mar.engi.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.engi  empty.ship=[TRUE]
        $face = random value from zero to 4 - 1
        $voice = random value from zero to 4 - 1
        $psgr = $aim->create passenger in ship: name=$mar.name race=$mar.race voice=$voice face=$face
        if $psgr->exists
          $psgr->create marine from passenger: fighting=$mar.figh.new hacking=$mar.hack.new mechanical=$mar.mech.new engineering=$mar.engi.new
        end
        append $mar to array $mars.ejd
      end
      dec $cnt.aMar =
      = $aim->add marine $mar to attack group on ship
      $aim->set relation against {Player} to {Friend}
    end
    
    = wait 100 ms
    $sector = [PLAYERSHIP]->get sector
    $flags = [Find.Nearest] | [Find.Multiple]
    $cnt.mars.ejd = size of array $mars.ejd
    $cntr = 0
    while 1
      $astros = find ship: sector=$sector class or type={Astronaut 2064} race=null flags=$flags refobj=$SHIP maxdist=2000 maxnum=100 refpos=null
      if $astros
        $astro = $astros[0]
        $astro->set relation against {Player} to {Friend}
        if $astro->exists
          $astro->destruct: show no explosion=[TRUE]
          inc $cntr =
        end
      end
      if $cntr == $cnt.mars.ejd
        break
      end
      = wait randomly from 100 to 200 ms
    end
    
    = wait 1000 ms
    $text = sprintf: fmt='Boarding success! (%s) tranfering control.', $aim, null, null, null, null
    display subtitle text: text=$text duration=2000 ms
    
  else
* ------------ NORMAL BOARDING ------------------
    $empty.ship = [FALSE]
    while $count
      dec $count =
      $m = $aTakeMarines[$count]
      = $aim->add marine $m to attack group on ship
    end
  end
  
* PSCO1 keep initial ship's hardware BEGIN
  $hw.arr = $aim->get ship hardware as array
  $cnt.hw = array alloc: size=0
  $size.hw = size of array $hw.arr
  
* Get initial hardware qty
  $i = 0
  while $i < $size.hw
    $item = $hw.arr[$i]
    $item.cnt = $aim->get amount of ware $item in cargo bay
    append $item.cnt to array $cnt.hw
    inc $i =
  end
  
* Wait for boarding success
  while [PLAYERSHIP]->is $aim a enemy
    = wait 1000 ms
  end
  
* Install for Shields
* Add for Equipment
  = wait 2000 ms
* Clear hardware qty
  sub.hwRemove:
  $hw.arr2 = $aim->get ship hardware as array
  $size.hw2 = size of array $hw.arr2
  while $size.hw2 > 0
    $item = $hw.arr2[0]
    $item.cntremv = $aim->get amount of ware $item in cargo bay
    $item.cntremv = -$item.cntremv
    = $aim->install $item.cntremv units of $item
* = $aim->add $item.cnt units of $item
    $text = sprintf: fmt='%s Removed', $item, null, null, null, null
    display subtitle text: text=$text duration=800 ms
    = wait 1000 ms
    gosub sub.hwRemove
  end
  endsub
  
* Restore hardware qty
  $i = 0
  while $i < $size.hw
    $item = $hw.arr[$i]
    $item.qty = $cnt.hw[$i]
    = $aim->install $item.qty units of $item
    inc $i =
  end
  $aim->set current shield strength to 0
  
* PSCO1 Marines gain XP
  if $empty.ship == [FALSE]
    $bMarines = $aim->get marines array
    $cnt.aMar = size of array $bMarines
    while $cnt.aMar
      dec $cnt.aMar =
      $mar = $bMarines[$cnt.aMar]
      $mar.name = $mar->get pilot name
      $mar.race = $mar->get owner race
      
      $mar.figh = $mar->get marine fighting skill
      $mar.figh.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.figh  empty.ship=[FALSE]
      $mar->set marine skill: engineering=$mar.figh.new
      
      $mar.hack = $mar->get marine hacking skill
      $mar.hack.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.hack  empty.ship=[FALSE]
      $mar->set marine skill: hacking=$mar.hack.new
      
      $mar.mech = $mar->get marine mechanical skill
      $mar.mech.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.mech  empty.ship=[FALSE]
      $mar->set marine skill: mechanical=$mar.mech.new
      
      $mar.engi = $mar->get marine engineering skill
      $mar.engi.new = [THIS]->call script 'plugin.boarding.marinesxp.solo' : skill=$mar.engi  empty.ship=[FALSE]
      $mar->set marine skill: engineering=$mar.engi.new
    end
  end
* -----------------  PSCO1 keep initial ship's hardware END --------------------
  
* failed
else
  START speak text: page=13 id=604 priority=10
  $text = sprintf: pageid=$page.id textid=37, $aim, null, null, null, null
  display subtitle text: text=$text duration=2000 ms
  $count = size of array $aTakeMarines
  while $count
    dec $count =
    $m = $aTakeMarines[$count]
    $m->destruct: show no explosion=[TRUE]
  end
end

return null
This code work approximatively well, but I ve no choice to delete fast as possible all the ejected boarding crew :sceptic:
All my marines are teleported to the victim ship with the right array and number (with delete/re-create code)
The vanilla boarding routine work but just ~10 seconds then stop (maybe because the victim ship is already PLAYER RACE)
I didnt found how to just delete some marines in the attacker ship...
Certainly not conventionnal way but it work...

Post Reply

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