aliens

Author Topic: [Solved] This 'Guaranteed Damage Script' isn't working  (Read 2692 times)

Offline The Martian

  • Commander
  • *****
  • Posts: 754
  • "It implores you to listen to its arguments..."
    • View Profile
[Solved] This 'Guaranteed Damage Script' isn't working
« on: November 20, 2021, 06:02:06 am »
(Problem)
Nothing happens when the weapon with the script's tags strikes a unit.

(Example Mod Download)
TestMod (Guaranteed Damage) V0-2.zip

(Details)
I'm trying to make a small script that will cause a weapon to deal X guaranteed damage to the struck targets HP on top of any damage caused by the weapon's normal power.

But it isn't currently yielding results.

The battle_game.flashMessage is not appearing and the health is not changing on the struck unit.


Code: [Select]
extended:
  tags:
    RuleItem:
      GUARANTEED_DAMAGE_AMOUNT_ITEM: int
      GUARANTEED_DAMAGE_ALLOWED_ITEM: int

    RuleArmor:
      GUARANTEED_DAMAGE_ALLOWED_ARMOR: int

  scripts:
    damageUnit:
      - offset: 1
        code: |
          var int DMG 0;
          var int ArmorAllowed 0;
          var int ItemAllowed 0;
          var int invert 0;
          var int WeaponCurrentPower 0;

          var ptr RuleArmor armor_rule;
          var ptr RuleItem item_rule;

          unit.getRuleArmor armor_rule;
          damaging_item.getRuleItem item_rule;


          # Load TAG values from involved item & armor
          item_rule.getTag ItemAllowed Tag.GUARANTEED_DAMAGE_ALLOWED_ARMOR;
          armor_rule.getTag ArmorAllowed Tag.GUARANTEED_DAMAGE_ALLOWED_ARMOR;
          item_rule.getTag DMG Tag.GUARANTEED_DAMAGE_AMOUNT;

          # If Armor & Weapon allow it deal the extra damage
          if eq ItemAllowed ArmorAllowed;

            # Apply Extra Damage Directly To Health
            sub invert DMG;
            unit.addHealth invert;

          end;

          # Get the current power of the attacking weapon's random roll
          set WeaponCurrentPower currPower;

          # Display a "Message" followed by "Extra Damage" then the "current power" of the attacking weapon
          battle_game.flashMessage "STR_TEST_MSG_DAMAGE_STRENGTH" DMG WeaponCurrentPower;


          return;


I've been over the code several times and can't spot why there isn't any interaction when the weapon strikes.

The variable 'currPower' was mentioned in the verbose version of the openxcom.log's 'damageUnit' section and I'm making the possibly incorrect assumption that 'currPower' stores the weapon's current power after it goes through the chosen RandomType: randomization roll.

For convenience I've attached a mod to this post with a test weapon already configured to use the script and also set the "NONE" X-Com armor to be able to be effected by the weapon's script tags.

What am I missing?
« Last Edit: February 12, 2023, 03:10:58 pm by Meridian »

Offline Yankes

  • Global Moderator
  • Commander
  • *****
  • Posts: 3350
    • View Profile
Re: This 'Guaranteed Damage Script' isn't working (OXCE)
« Reply #1 on: November 20, 2021, 01:14:40 pm »
First of all, if you have problems with script use `debug_log` to track what happens exactly in your script.
Second do you have any errors in log? It could be simply some syntax error causing whole script to fail to load.
"is not appearing" <- this indicate that it possible that whole script fail to load as message should be always show.

Code: [Select]
if eq ItemAllowed ArmorAllowed;
This probably do not work as you think, this will pass when both are allowed and when both are NOT allowed (as `eq 0 0` is true)
You would probably want use:
Code: [Select]
if and eq ItemAllowed 1 eq ArmorAllowed 1;

`currPower` yes, this is effective power affecting given unit (aka after random and resistances), but this value is only informative, you can't change it and even if you would change it it would not affect any thing else.


Code: [Select]
unit.addHealth invert;This is not needed, you can easy make `add to_health DAG;` and this would have benedicts that other mods could affect this value too (like item that reduce all damage to health by half).


Offline The Martian

  • Commander
  • *****
  • Posts: 754
  • "It implores you to listen to its arguments..."
    • View Profile
Re: This 'Guaranteed Damage Script' isn't working (OXCE)
« Reply #2 on: November 21, 2021, 05:13:05 am »
Thank you! 

It is fixed except for the part with:

Code: [Select]
battle_game.flashMessage "STR_TEST_MSG_DAMAGE_STRENGTH" DMG WeaponCurrentPower;
For some reason it does display STR_TEST_MSG_DAMAGE_STRENGTH but not DMG or WeaponCurrentPower.

(Corrected Mod Download)
TestMod (Guaranteed Damage) V0-4.zip




`currPower` yes, this is effective power affecting given unit (aka after random and resistances), but this value is only informative, you can't change it and even if you would change it it would not affect any thing else.

This is the first time I've tried using "battle_game.flashMessage" in a script and I've noticed that in the verbose version of the openxcom.log's 'damageUnit' section there are five uses of it:

Code: [Select]
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text] [int] [int] [int] [int] 
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text] [int] [int]             
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text] [int]                   
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text]                         
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text] [int] [int] [int]   

Each one looks like it will display a different number of variables holding an int.

I thought that if two variables were passed into battle_game.flashMessage after the [text] string it would automatically select the version that allowed for it.
Code: [Select]
battle_game.flashMessage "STR_TEST_MSG_DAMAGE_STRENGTH" DMG WeaponCurrentPower;
Code: [Select]
Name: BattleGame.flashMessage                 Args: [ptre BattleGame] [text] [int] [int]             

It currently should be displaying "Damage: [Health Lost Amount] [Current Power Of attack]" but instead it just shows the "Damage:" message





First of all, if you have problems with script use `debug_log` to track what happens exactly in your script.
Second do you have any errors in log? It could be simply some syntax error causing whole script to fail to load.
"is not appearing" <- this indicate that it possible that whole script fail to load as message should be always show.

It is as you said. In the openxcom.log it is detecting an error on one of the lines:

Code: [Select]
[19-11-2021_20-54-28] [ERROR] Can't match overload for operator 'RuleItem.getTag' for:
[19-11-2021_20-54-28] [ERROR]   [var ptr RuleItem] [var int] [RuleArmor.Tag]
[19-11-2021_20-54-28] [ERROR] Expected:
[19-11-2021_20-54-28] [ERROR]   [ptr RuleItem] [var int] [RuleItem.Tag]
[19-11-2021_20-54-28] [ERROR] Error in parsing script 'damageUnit' for 'Global Event Script': invalid operation in line: 'item_rule.getTag ItemAllowed Tag.GUARANTEED_DAMAGE_ALLOWED_ARMOR;'

I made the mistake of changing the variables names defined in the [extended:] [tags:] section to be more readable, but then forgetting to update their names in the rest of the script.



Code: [Select]
if eq ItemAllowed ArmorAllowed;
This probably do not work as you think, this will pass when both are allowed and when both are NOT allowed (as `eq 0 0` is true)
You would probably want use:
Code: [Select]
if and eq ItemAllowed 1 eq ArmorAllowed 1;


Well spotted. Using a 0/1(True/False) toggle would normally be the easier solution:
Code: [Select]
          if gt ArmorAllowed 0;
But in this case I'm trying to create matching weapon armor sets that cause extra damage only when both are present.
Code: [Select]
          if eq ItemAllowed ArmorAllowed;
Code: [Select]
So Armor 0 & Weapon 0 = True
So Armor 1 & Weapon 0 = False
So Armor 0 & Weapon 1 = False
So Armor 1 & Weapon 1 = True
So Armor 2 & Weapon 0 = False
So Armor 0 & Weapon 2 = False
So Armor 2 & Weapon 2 = True
Etc . . .

The current limit is that if I want any overlap so that a Weapon(3)-Armor(3) combination also works with Weapon(0)-Armor(3) I'll need to code extra catches into the script as an if statement like this:

Code: [Select]
          if eq ArmorAllowed 3;
            if eq ItemAllowed 0;

            # [=] Code to deal the extra damage [=]

            end;
          end;

But that seems simple enough that it won't be a problem.



Code: [Select]
unit.addHealth invert;This is not needed, you can easy make `add to_health DAG;` and this would have benedicts that other mods could affect this value too (like item that reduce all damage to health by half).


That is much better, I'll change the code to use the more flexible to_health variable.

I'm assuming that to_health is the calculated amount of health reduction achieved by the current attack.

Can anything in the game effect the added amount of health reduction this mod is introducing? (Aside from another script or mod)

For example item settings like: [items:] [damageAlter:] [ToHealth:] or [RandomHealth:]

I am using settings like those on a lot of other items and to achieve my desired effect it needs the value of damage dealt by this script to be consistently 100% of its value every time.
« Last Edit: November 21, 2021, 05:17:47 am by The Martian »

Offline Buscher

  • Colonel
  • ****
  • Posts: 168
    • View Profile
Re: This 'Guaranteed Damage Script' isn't working (OXCE)
« Reply #3 on: November 21, 2021, 11:53:52 am »
You should have a look at other mods which have scripts. Your issue here is that your string does not contain format indices similar to C#. In this case they are also {n}.

The Vigilo Confido mod is very ambitious in terms of scripting so it's always a good mod to take a peek at.

Have a look at these two lines inside Ruleset/globalBalance.rul
Code: [Select]
                  battle_game.flashMessage "STR_CRIT_DAMAGE_DEALT_SHRED_HP_LEFT" to_health healthLeft to_armor; # Line 243
...

      STR_CRIT_DAMAGE_DEALT_SHRED_HP_LEFT: "CRIT: {0}>{1} - Shred: {2}" # Line 306
« Last Edit: November 21, 2021, 12:10:45 pm by Buscher »

Offline The Martian

  • Commander
  • *****
  • Posts: 754
  • "It implores you to listen to its arguments..."
    • View Profile
Re: This 'Guaranteed Damage Script' isn't working (OXCE)
« Reply #4 on: November 22, 2021, 08:00:49 am »
Thank you for the help.

That is really handy being able to place the [int] anywhere inside the message's text string instead of just after it.



Since it is working I'll attach the final version of the test mod to this post as a functional example.

(Working Mod Download)
TestMod (Guaranteed Damage) V0-5.zip
« Last Edit: November 22, 2021, 08:30:41 am by The Martian »

Offline Yankes

  • Global Moderator
  • Commander
  • *****
  • Posts: 3350
    • View Profile
Re: This 'Guaranteed Damage Script' isn't working (OXCE)
« Reply #5 on: November 23, 2021, 11:12:00 pm »
Reason `debug_log` work different to `BattleGame.flashMessage` because first one is buildin script operation and another is normal function like `setHelth`.
Another is that `flashMessage` can be translated like any other message in game, `debug_log` dump values to log without any processing but thanks to this it work with any value available in scripts.