Author Topic: [Solved] Custom scripting reference or help?  (Read 4714 times)

Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
[Solved] Custom scripting reference or help?
« on: January 06, 2020, 10:18:23 am »
I'd like to implement an integrated medical system in advanced armors.

The scripting system seems like a bit of a black art to me. I can provide psuedocode of exactly what I want to happen, but have no idea where to even start coding it.  Is there some digestable reference that my quick google-fu has failed to find?

In this context, 'break' means to cease execution. The script should never do more than one thing per turn per soldier.

newTurnUnit
  if IsXComTurn == true
    if SoldierInFancyArmor == true
      if FatalWounds > 0  [Any gaping holes?]
        FatalWounds = FatalWounds - 1
        HP = HP + 3 [Standard medkit heal behaviour]
        break
      if Unconscious
        Administer stim shot
        break
      if MoraleDamagedDueInjury  [How does morale and painkillers work wrt injury, vs buddies dying, vs psi fuckery?]
        Administer a painkiller shot
        break
      if Energy < MaxEnergy * 0.6  [Exhausted from all that running around?]
        Administer a stim shot
        break
      if StunLevel > HP * 0.5  [At risk of incapacitation?]
        Administer a stim shot
        break

Scripts done and tested good as far as I can tell, thanks all!

This is what the pseudocode for the final product looks like:

  newTurnUnit
    if IsXComTurn == true
      if SoldierInFancyArmor == true
        if FatalWoundsNum > 0  [Any gaping holes?]
          HP = HP + DiceRoll * FatalWoundsNum
          break
        if Unconscious
          Administer stim shot
          break
        if Energy < MaxEnergy * 0.6  [Exhausted from all that running around?]
          Administer a stim shot
          break
        if StunLevel > HP * 0.5  [At risk of incapacitation?]
          Administer a stim shot
          break
« Last Edit: February 12, 2023, 02:37:18 pm by Meridian »

Offline Yankes

  • Global Moderator
  • Commander
  • *****
  • Posts: 3350
    • View Profile
Re: Custom scripting reference or help?
« Reply #1 on: January 06, 2020, 05:57:56 pm »
look on https://openxcom.org/forum/index.php/topic,7731.0.html where I answered similar question,

for start, conditions are write in different way usual scripts, closer to lisp than C-like.

`if IsXComTurn == true` should be

Code: [Select]
if eq side 0;
  # ...
end;

where 0 is xcom, 1 is aliens and 2 is civilians

`if SoldierInFancyArmor == true` you need define new tag in armor to check for something like that, example is in liked post

`if FatalWounds > 0` should be `var int fatal; ... unit.getFatalwoundsTotal fatal; ... if gt fatal 0; ... end;`

Code: [Select]
var int fatal;

# ...

unit.getFatalwoundsTotal fatal;

# ...

if gt fatal 0;
  #...
end;

`if Energy < MaxEnergy * 0.6` should be


Code: [Select]
var int energy;
var int energyMax;

# ...

unit.getEnergy energy;
unit.getEnergyMax energyMax;
muldiv energyMax 6 10; // energyMax  = (energyMax  * 6) / 10

# ...

if lt energy energyMax;
  unit.setEnergy 100; #it will try to set energy to value 100
end;

This probably should be enough to start experimenting. Remember to check logs for scripts errors, enable verbose logging for more info about scripts and use `debug_log "some text" some_value "some unit" unit;` to output data from scripts to log.

Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
Re: Custom scripting reference or help?
« Reply #2 on: January 06, 2020, 08:44:29 pm »
Thanks for the starting point Yankes.

Is there a reference anywhere?  googling 'OpenXCom getFatalwoundTotal" yields literally zero results. Any documentation regarding getting game engine data into and out of the script would be much appreciated.

edit: I just found this, and am skimming through it.
« Last Edit: January 06, 2020, 09:02:51 pm by skyhawk »

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1933
  • Flamethrowers fry cyberdisk circuits
    • View Profile
Re: Custom scripting reference or help?
« Reply #3 on: January 06, 2020, 09:17:17 pm »
What you found is the output from the openxcom.log file when you turn debug and verbose logging on in the options.cfg file. I periodically make a copy of that output and post it on github as a reference later, which is what you found. It's likely out of date, so you should run OXCE once with debug + verbose logging and make a copy for your own reference.

The only other reference material are the scripts used in other mods - Piratez and XCF use the same set of scripts I wrote with some minor changes. You can find the base file here on github as well.

Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
Re: Custom scripting reference or help?
« Reply #4 on: January 07, 2020, 05:22:31 am »
I don't suppose there's any flow control? I'd just about kill for a for or while loop right about now.
I haven't gotten to the point where I need it yet, but a break; statement would also save me a whole lot of if statements.

Edit again: I just noticed 'return;' in some examples - maybe that's the break; I'm looking for.
« Last Edit: January 07, 2020, 08:29:39 am by skyhawk »

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Custom scripting reference or help?
« Reply #5 on: January 07, 2020, 10:58:54 pm »
AFAIK, loops are not added yet. The return statement is for returning control back to the game. Basically exiting your current script.

Offline Yankes

  • Global Moderator
  • Commander
  • *****
  • Posts: 3350
    • View Profile
Re: Custom scripting reference or help?
« Reply #6 on: January 07, 2020, 11:01:10 pm »
Right now we have only `if`and `else`, loop wait in line when I add scoped variables then I could add proper `loop` operation.

Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
Re: Custom scripting reference or help?
« Reply #7 on: January 07, 2020, 11:49:02 pm »
AFAIK, loops are not added yet. The return statement is for returning control back to the game. Basically exiting your current script.
Which is exactly what I was doing with break in my psuedocode. Perfect.

Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
Re: Custom scripting reference or help?
« Reply #8 on: January 08, 2020, 12:52:54 am »
How do I write

if ((a == b) or (a < b)) ?

I think it's
if or lt a b eq a b

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Custom scripting reference or help?
« Reply #9 on: January 08, 2020, 08:35:35 am »
That is correct.
You also have le (less than or equal) ge (greater than or equal) and neq (not equal).

Gesendet von meinem Pixel 2 mit Tapatalk


Offline skyhawk

  • Colonel
  • ****
  • Posts: 115
    • View Profile
Re: Custom scripting reference or help?
« Reply #10 on: January 08, 2020, 09:49:51 am »
Thanks for your help all!

I've got a working script that I'm pretty happy with. Couldn't do the healing fatal wounds thing, but really that's ok because that's what medikits are for. Also decided painkillers weren't worth the effort. [I'm sure my soldiers love me]

I have uncovered possibly incorrect behavior in the stun mechanic though!

[07-01-2020_23-42-57]   [DEBUG]   Script debug log at    0x6f6: IMS - Incapacitation detected for ID 2 . health = 20 , stun =  29 , maxStun =  180
[07-01-2020_23-42-57]   [DEBUG]   Script debug log at    0x87c: IMS - Shooting some stims for ID 2 , Energy now  87 , Stun now  25
Soldier is a heap on the floor

[07-01-2020_23-43-15]   [DEBUG]   Script debug log at    0x6f6: IMS - Incapacitation detected for ID 2 . health = 20 , stun =  24 , maxStun =  180
[07-01-2020_23-43-15]   [DEBUG]   Script debug log at    0x87c: IMS - Shooting some stims for ID 2 , Energy now  87 , Stun now  20
Soldier is a heap on the floor

[07-01-2020_23-43-43]   [DEBUG]   Script debug log at    0x780: IMS - Incapacitation risk for ID 2 . health = 20 , stun =  19 , maxStun =  180
[07-01-2020_23-43-43]   [DEBUG]   Script debug log at    0x87c: IMS - Shooting some stims for ID 2 , Energy now  87 , Stun now  15
Even before my mod pumped a stimmie into this soldier, he should be on his feet, but he's not!

[07-01-2020_23-45-52]   [DEBUG]   Script debug log at    0x780: IMS - Incapacitation risk for ID 2 . health = 20 , stun =  14 , maxStun =  180
[07-01-2020_23-45-52]   [DEBUG]   Script debug log at    0x87c: IMS - Shooting some stims for ID 2 , Energy now  87 , Stun now  10
NOW the soldier is finally up and about.