aliens

Author Topic: [Answered] Script help needed.  (Read 7106 times)

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
[Answered] Script help needed.
« on: March 01, 2020, 08:59:46 pm »
Here is a script for auto choosing weakest armor side when hit.
But it is not working. Maybe someone know why?
Code: [Select]
extended:
  tags:
    RuleItem:
      ITEM_IS_PRESSURE: int
  scripts:
    hitUnit:
      - offset: 5 #Pressure side
        code: |
          var int frontarmor;
          var int leftarmor;
          var int reararmor;
          var int rightarmor;
          var int underarmor;
          var int temp;
          var int temparmor;
          var ptr RuleItem itemRuleset;

          damaging_item.getRuleItem itemRuleset;
          itemRuleset.getTag temp Tag.ITEM_IS_PRESSURE;
                   
          if eq temp 0;
            return power part side;
          end;

          unit.getArmor frontarmor 0;
          unit.getArmor leftarmor 1;
          unit.getArmor reararmor 2;
          unit.getArmor rightarmor 3;
          unit.getArmor underarmor 4;
          if gt frontarmor leftarmor;
            set temp 1;
            set temparmor leftarmor;
          else;
            set temp 0;
            set temparmor frontarmor;
          end;
          if gt temparmor reararmor;
            set temp 2;
            set temparmor reararmor;
          end;
          if gt temparmor rightarmor;
            set temp 3;
            set temparmor rightarmor;
          end;
          if gt temparmor underarmor;
            set temp 4;
          end;
         
          set side temp;
         
          return power part side;
Thanks.
« Last Edit: February 12, 2023, 02:42:21 pm by Meridian »

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #1 on: March 01, 2020, 10:35:53 pm »
What exactly is not working?

Did you check the openxcom.log output? Does it say anything?

Are you aware of the difference between damaging_item and weapon_item?

Did you try adding debug_log output to make debugging easier? Does the script get executed at all?

Also, are you aware how the damage formula works, and that you need more than zero power to arrive at this stage of the damage calculation in order for your script to get executed?

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #2 on: March 02, 2020, 08:24:55 am »
Are you aware of the difference between damaging_item and weapon_item?
I am not sure. Can you please  explain or provide a link to description?
Quote
Did you try adding debug_log output to make debugging easier? Does the script get executed at all?
Good idea. I can debug where it fails indeed. Thanks.
Quote
Also, are you aware how the damage formula works, and that you need more than zero power to arrive at this stage of the damage calculation in order for your script to get executed?
Do you mean damage before  armor counts, or damage to health?

Thanks for reply, you give me a way to move. I will write reply if i have some results.

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #3 on: March 02, 2020, 08:51:27 am »
damaging_will be set to the ammo item on weapons with ammo, while weapon_item is the weapon itself.

As for damage, I think that armor resistances gets applied before hitUnit executes. Meaning if the armor resistance reduces the damage to less than 1 hitUnit won't get called.

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #4 on: March 02, 2020, 09:24:18 am »
damaging_will be set to the ammo item on weapons with ammo, while weapon_item is the weapon itself.
Thanks for explanation. Here i use weapon without ammo though.
Quote
As for damage, I think that armor resistances gets applied before hitUnit executes. Meaning if the armor resistance reduces the damage to less than 1 hitUnit won't get called.
That's odd. I think this can be a reason. But armor resistances is applied before armor values, and they are done via hitunit. Dont you think about damagingunit?(or something like that)

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #5 on: March 02, 2020, 09:56:51 am »
So, turns out I am both right and wrong.

The power gets reduced by the resistances before hitUnit script gets called, but the check if the power is at least 1 is done beforehand.

Code: [Select]
# power of hitUnit / original_damage of damageUnit = (power of weapon +/- randomRange%) * damageModifier(Armor,Type)
# damage of damageUnit = (power output of hitUnit) - Armor(Side) * ArmorEffectiveness
# output of damageUnit is applied immediately

hitUnit and damageUnit are always called together. If hitUnit gets called then so will damageUnit. It is just if the power is not enough to begin with, that none of them gets called.

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #6 on: March 02, 2020, 03:36:39 pm »
So if weapon power>0, they both will be called. and then goes armor.  Looks like i did something wrong elsewhere. Going to debug...

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1933
  • Flamethrowers fry cyberdisk circuits
    • View Profile
Re: Script help needed.
« Reply #7 on: March 02, 2020, 03:50:37 pm »
You can try using the debug_log script command to put stuff in the log file to help with debugging. For example, if you just want to know whether or not the item has the tag and what the power was, you could add this right after you get the tag:

Code: [Select]
debug_log 0 "power = " power;
debug_log 1 "tag = " temp;

Once you hit a unit, check your openxcom.log file and you should see the output from your script.

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #8 on: March 02, 2020, 06:00:08 pm »
Ok, i got:
Code: [Select]
          debug_log "Started, side:" side;
          weapon_item.getRuleItem itemRuleset;
          itemRuleset.getAccuracyMelee temp;
          debug_log "Accu:" temp;
          itemRuleset.getTag temp Tag.ITEM_IS_PRESSURE;
                   
          if eq temp 0;
            debug_log "Fail" temp;
            return power part side;
          end;
And got "Accu: 0" and "Fail 0". Looks like i can not acquire item properties.
How to get it then, if
Code: [Select]
          weapon_item.getRuleItem itemRuleset;
          itemRuleset.getTag temp Tag.ITEM_IS_PRESSURE;
is wrong?

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #9 on: March 02, 2020, 06:07:07 pm »
Code: [Select]
weapon_item.getRuleItem itemRuleset;
itemRuleset.getTag temp Tag.ITEM_IS_PRESSURE;

Looks good to me.

Where did you assign the tag to the item?

Also try this:

Code: [Select]
debug_log weapon_item;  # is this what you expect it to be?

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #10 on: March 02, 2020, 06:26:57 pm »
To make things a bit more transparent, here is how OXCE handles damage wrt. to the scripts:

Code: [Select]
random_damage = getRandomDamage(power_of_weapon);  // apply damageRange (0-200% in vanilla ufo)
TileEngine.hitUnit(targetUnit, random_damage)
{
  targetUnit.damage(random_damage)
}
BattleUnit.damage(random_damage)
{
  if (random_damage < 0) return; // DO NOTHING
  random_damage = reduceByResistance(random_damage, ResistType); // apply armor damage modifiers (resistances)
  CALL SCRIPT: HitUnit(random_damage as power)
  adjusted_damage -= getArmor(side) * ArmorEffectiveness; // apply armor on respective side, taking armor effectiveness into account
  CALL SCRIPT: DamageUnit(random_damage as original_damage, adjusted_damage as damage)
  => Apply Damage directly from output of DamageUnit script
}

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #11 on: March 02, 2020, 07:35:12 pm »
Code: [Select]
weapon_item.getRuleItem itemRuleset;
itemRuleset.getTag temp Tag.ITEM_IS_PRESSURE;

Looks good to me.

Where did you assign the tag to the item?
In the same file, like this:
Code: [Select]
...
items:
  - type: GULPGULP
    meleeAnimation: 0
    power: 8
    damageType: 6
    blastRadius: 0
    damageAlter:
      RandomType: 3
      ArmorEffectiveness: 4
      ToWound: 0
      ToArmor: 0
      RandomStun: false
    accuracyMelee: 101
    costMelee:
      time: 1
    battleType: 3
    clipSize: -1
    invWidth: 2
    invHeight: 2
    recover: false
    tags:
      ITEM_IS_PRESSURE: 1
Quote
Also try this:

Code: [Select]
debug_log weapon_item;  # is this what you expect it to be?
[02-03-2020_20-29-32]   [DEBUG]   Script debug log at     0x58: null
:-(
Maybe all it is because this weapon is not exactly weapon, but environment effect?

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #12 on: March 02, 2020, 07:41:12 pm »
Aha! Yes, the environmental effect does not pass the weapon to the hitUnit method call.

Offline Nord

  • Commander
  • *****
  • Posts: 1746
  • The Gate is open.
    • View Profile
Re: Script help needed.
« Reply #13 on: March 02, 2020, 07:51:07 pm »
So, no chances?

Offline memmaker

  • Captain
  • ***
  • Posts: 96
    • View Profile
Re: Script help needed.
« Reply #14 on: March 02, 2020, 07:56:37 pm »
That would be a question for Meridian. I stumbled upon the same issue some days ago, not sure what the reasons are.