OSP Code Combat Script to guarantee all melee weapons (1hand, 2hand + polearms)

Users who are viewing this thread

Sorry for necroposting as well as for my english  :oops: May be this script upgrade will be useful for somebody else.

I modding warband for last half-year and now i proceed to module system. For my mod i need tf_guarantee_polearm and tf_guarantee_two_handed so i had to implement they.

Thanks to TS for original script of course.

My main goal: if troop has polearm/two-handed in first inventory slot — he get polearm/two-handed with 100% probability. There is honest chance of needed weapon random choice (so far forth as ingame random function).

See in-code comments for more details:
Code:
("force_weapon",
   [(store_script_param, ":agent", 1),
    (try_begin),
      (this_or_next|multiplayer_is_server),
      (neg|game_in_multiplayer_mode),
      (agent_is_alive, ":agent"),
      (agent_is_non_player, ":agent"),
      (agent_get_troop_id, ":troop", ":agent"),
      # sift out heroes, especially merchants (usual town merchants as well as starting quest merchants)
      (neg|troop_is_hero, ":troop"),
      # take first item from inventory of troop template (from troops.txt/py)
      # by unknown reason inventory of "troop" (template!) has items of specific "agent" (!), i don't know why
      # real template inventory starts from record 11, i.e. 10 from 0
      (troop_get_inventory_slot, ":first_inv_item", ":troop", 10),

      # sift out troops with empty inventory
      (neq, ":first_inv_item", -1),
      # get and memorize type of first item and sift out everybody but with polearm or two-handed
      (item_get_type, ":needed_item_type", ":first_inv_item"),
      (this_or_next|eq, ":needed_item_type", itp_type_two_handed_wpn),
      (eq, ":needed_item_type", itp_type_polearm),

      (assign, ":have_polearm_or_twohanded", 0),
      (assign, ":onehanded_slot", -1),
      (assign, ":empty_slot", -1),
      (try_begin),
        # look over all 4 equipment slots for actual content
        (try_for_range, ":i", 0, 4),
          (agent_get_item_slot, ":item", ":agent", ":i"),
          (try_begin),
            (eq, ":item", -1),
            # memorize empty slot for future use
            (assign, ":empty_slot", ":i"),
          (else_try),
            (item_get_type, ":type", ":item"),
            (eq, ":type", itp_type_one_handed_wpn),
            # memorize slot with one-handed weapon for future use
            (assign, ":onehanded_slot", ":i"),
          (else_try),
            (item_get_type, ":type", ":item"),
            # if equipment has needed type weapon - set the flag
            (eq, ":type", ":needed_item_type"),
            (assign, ":have_polearm_or_twohanded", 1),
          (try_end),
        (try_end),
        # if equipment has needed type weapon - exit
        (neq, ":have_polearm_or_twohanded", 1),
        # pick out equipment slot for replacement
        (assign, ":replace_slot", -1),
        (try_begin),
          (ge, ":empty_slot", 0),
          # pick out empty slot if it was
          (assign, ":replace_slot", ":empty_slot"),
        (else_try),
          (ge, ":onehanded_slot", 0),
          # otherwise pick out slot with one-handed if it was (and it was surely for my troop templates)
          (assign, ":replace_slot", ":onehanded_slot"),
          (agent_get_item_slot, ":item", ":agent", ":onehanded_slot"),
          # Unequip item in this slot for free space for polearm/two-handed
          (agent_unequip_item, ":agent", ":item", ":onehanded_slot"),
        (else_try),
          # paranoid case for dead certainty :-D·
          # pick out last slot
          (assign, ":replace_slot", 3),
        (try_end),
        # Enquire amount of needed weapons in troop inventory
        # we seek from 10 record (see above) to record with nubmer returning by "troop_get_inventory_capacity"
        # in my experiments i get 100
        (troop_get_inventory_capacity, ":inventory_capacity", ":troop"),
        (assign, ":needed_weapons_count", 0),
        (try_for_range, ":i", 10, ":inventory_capacity"),
          (try_begin),
            (troop_get_inventory_slot, ":item", ":troop", ":i"),
            (neq, ":item", -1),
            (item_get_type, ":type", ":item"),
            (eq, ":type", ":needed_item_type"),
            (val_add, ":needed_weapons_count", 1),
          (try_end),
        (try_end),
        # Get random index number of weapon
        (store_random_in_range, ":selected_weapon_num", 0, ":needed_weapons_count"),
        # And get weapon with this index number
        (try_for_range, ":i", 10, ":inventory_capacity"),
          (try_begin),
            (troop_get_inventory_slot, ":item", ":troop", ":i"),
            (neq, ":item", -1),
            (item_get_type, ":type", ":item"),
            (eq, ":type", ":needed_item_type"),
            (val_sub, ":selected_weapon_num", 1),
            (try_begin),
              (eq, ":selected_weapon_num", -1),
              # If we reach choosen index number - place this weapon to slot which we had choosen formerly
              (agent_equip_item, ":agent", ":item", ":replace_slot"),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
]),

It works for any troop with polearm or two-handed in first inventory position, you don't need to specify troops in code like in original script. If you don't want such guarantee for some troops — just set their first inventory items to non-polearm/non-two-handed and chance of weapon getting will be usual for MnB.

Now i get what i want for my epic mod: every specialized troop like spearman, partisan, halberdier, doppelsoldner has this primary weapon always, the same for horseman with lances too.
And i don't need to pad inventory with one-type weapons only for tiny additional chance.
 
I have followed the instructions.

Modified scripts and mission_templates

Without the force_weapon-script. everything is fine.

If I implement it, I got an error ingame that says that on script: agent_troop_get_banner_mesh is an error.

I did not alter it! When I remove force_weapon its gone.

What should I do?
 
I apologize for resurrecting this thread.  I carefully followed the instructions to implement this code into my module system and now have intermittent crashes before battles begin.

I am wondering if this code is not compatible with PBOD .92.  If anyone has some insight to this, please let me know.
 
strafer said:
Sorry for necroposting as well as for my english  :oops: May be this script upgrade will be useful for somebody else.

I modding warband for last half-year and now i proceed to module system. For my mod i need tf_guarantee_polearm and tf_guarantee_two_handed so i had to implement they.

Thanks to TS for original script of course.

My main goal: if troop has polearm/two-handed in first inventory slot — he get polearm/two-handed with 100% probability. There is honest chance of needed weapon random choice (so far forth as ingame random function).

See in-code comments for more details:
Code:
("force_weapon",
   [(store_script_param, ":agent", 1),
    (try_begin),
      (this_or_next|multiplayer_is_server),
      (neg|game_in_multiplayer_mode),
      (agent_is_alive, ":agent"),
      (agent_is_non_player, ":agent"),
      (agent_get_troop_id, ":troop", ":agent"),
      # sift out heroes, especially merchants (usual town merchants as well as starting quest merchants)
      (neg|troop_is_hero, ":troop"),
      # take first item from inventory of troop template (from troops.txt/py)
      # by unknown reason inventory of "troop" (template!) has items of specific "agent" (!), i don't know why
      # real template inventory starts from record 11, i.e. 10 from 0
      (troop_get_inventory_slot, ":first_inv_item", ":troop", 10),

      # sift out troops with empty inventory
      (neq, ":first_inv_item", -1),
      # get and memorize type of first item and sift out everybody but with polearm or two-handed
      (item_get_type, ":needed_item_type", ":first_inv_item"),
      (this_or_next|eq, ":needed_item_type", itp_type_two_handed_wpn),
      (eq, ":needed_item_type", itp_type_polearm),

      (assign, ":have_polearm_or_twohanded", 0),
      (assign, ":onehanded_slot", -1),
      (assign, ":empty_slot", -1),
      (try_begin),
        # look over all 4 equipment slots for actual content
        (try_for_range, ":i", 0, 4),
          (agent_get_item_slot, ":item", ":agent", ":i"),
          (try_begin),
            (eq, ":item", -1),
            # memorize empty slot for future use
            (assign, ":empty_slot", ":i"),
          (else_try),
            (item_get_type, ":type", ":item"),
            (eq, ":type", itp_type_one_handed_wpn),
            # memorize slot with one-handed weapon for future use
            (assign, ":onehanded_slot", ":i"),
          (else_try),
            (item_get_type, ":type", ":item"),
            # if equipment has needed type weapon - set the flag
            (eq, ":type", ":needed_item_type"),
            (assign, ":have_polearm_or_twohanded", 1),
          (try_end),
        (try_end),
        # if equipment has needed type weapon - exit
        (neq, ":have_polearm_or_twohanded", 1),
        # pick out equipment slot for replacement
        (assign, ":replace_slot", -1),
        (try_begin),
          (ge, ":empty_slot", 0),
          # pick out empty slot if it was
          (assign, ":replace_slot", ":empty_slot"),
        (else_try),
          (ge, ":onehanded_slot", 0),
          # otherwise pick out slot with one-handed if it was (and it was surely for my troop templates)
          (assign, ":replace_slot", ":onehanded_slot"),
          (agent_get_item_slot, ":item", ":agent", ":onehanded_slot"),
          # Unequip item in this slot for free space for polearm/two-handed
          (agent_unequip_item, ":agent", ":item", ":onehanded_slot"),
        (else_try),
          # paranoid case for dead certainty :-D·
          # pick out last slot
          (assign, ":replace_slot", 3),
        (try_end),
        # Enquire amount of needed weapons in troop inventory
        # we seek from 10 record (see above) to record with nubmer returning by "troop_get_inventory_capacity"
        # in my experiments i get 100
        (troop_get_inventory_capacity, ":inventory_capacity", ":troop"),
        (assign, ":needed_weapons_count", 0),
        (try_for_range, ":i", 10, ":inventory_capacity"),
          (try_begin),
            (troop_get_inventory_slot, ":item", ":troop", ":i"),
            (neq, ":item", -1),
            (item_get_type, ":type", ":item"),
            (eq, ":type", ":needed_item_type"),
            (val_add, ":needed_weapons_count", 1),
          (try_end),
        (try_end),
        # Get random index number of weapon
        (store_random_in_range, ":selected_weapon_num", 0, ":needed_weapons_count"),
        # And get weapon with this index number
        (try_for_range, ":i", 10, ":inventory_capacity"),
          (try_begin),
            (troop_get_inventory_slot, ":item", ":troop", ":i"),
            (neq, ":item", -1),
            (item_get_type, ":type", ":item"),
            (eq, ":type", ":needed_item_type"),
            (val_sub, ":selected_weapon_num", 1),
            (try_begin),
              (eq, ":selected_weapon_num", -1),
              # If we reach choosen index number - place this weapon to slot which we had choosen formerly
              (agent_equip_item, ":agent", ":item", ":replace_slot"),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
]),

It works for any troop with polearm or two-handed in first inventory position, you don't need to specify troops in code like in original script. If you don't want such guarantee for some troops — just set their first inventory items to non-polearm/non-two-handed and chance of weapon getting will be usual for MnB.

Now i get what i want for my epic mod: every specialized troop like spearman, partisan, halberdier, doppelsoldner has this primary weapon always, the same for horseman with lances too.
And i don't need to pad inventory with one-type weapons only for tiny additional chance.

Have you made any changes to this script?  How is it working for you.  I am interesting in trying it in my own mod.
 
Hmm, excuse the necro, but I'd thought I'd like to share what I'm doing using Strafers script, and perhaps anyone can help me.

The OP has allowed me to have spearman with guaranteed spears while having one-handed weapons. However, this applies to all troops for some reason. I persumed that this code below would allow me to exclude certain troops to have guaranteed polearms/2h, but apprently it applied to just about everyone.

(this_or_next|eq, ":troop", "trp_YOUR_TROOP"),
(this_or_next|eq, ":troop", "trp_YOUR_TROOP2"),
(this_or_next|eq, ":troop", "trp_YOUR_TROO3"),

So I used Strafers script, which indicates that the first inventory item of the python troop file (item1, item2, item3), in which if a 2h/pole weapon is on the first item, the troop would have a guaranteed use of the 2h/pole weapon. I've directly used his script since it applies to all troops. I'm using Morgh's Editor to sort the troops out.

So far, it works for 2h weapons, there is randomisation of 2h weapons if it's not placed on the first inventory item. The random distribution is just like native, which I believe it's 20-30% ish?

But for polearms it's abit iffy. I placed spears at the last of the inventory list and they still show up on every troop, and what's funny that only about 10% of a chance troops would not get spears. Tried to place the spear inventory in the middle of two other 1h weapons, but didn't work.

Any thoughts for this issue? I'm not code savvy myself so its rather difficult for me to figure this.

 
Are you using PBOD in your mod?  There is some code in PBOD that has to do with how weapons are selected that may be interfering with your modified Strafer's script.  I had similar problems with the Somekidds force polearm script on the first page of this thread.  It seemed to work but would occasionally lead to fairly frequent game crash when going into battle, something that never happened before I installed the scripts.

 
Redleg said:
Are you using PBOD in your mod?  There is some code in PBOD that has to do with how weapons are selected that may be interfering with your modified Strafer's script.  I had similar problems with the Somekidds force polearm script on the first page of this thread.  It seemed to work but would occasionally lead to fairly frequent game crash when going into battle, something that never happened before I installed the scripts.

Crashing is never a problem for me though, not that I've ever encountered any.

Never used PBOD, although I intend to add it into my mod, that sort of sucks though, that's a very essential mod for tactical battles.
 
Johao said:
(this_or_next|eq, ":troop", "trp_YOUR_TROOP"),
(this_or_next|eq, ":troop", "trp_YOUR_TROOP2"),
(this_or_next|eq, ":troop", "trp_YOUR_TROO3"),

check if you have this:

Code:
(this_or_next|eq, blah blah blah),
(eq,                blah blah blah),

you can't use this_or_next in the last test
 
some time ago,
I implemented this code for my other mod, and it worked perfect,
but I need it now, to ensure a musket in multiplayer troops,
Now I changed itp_type_one_handed_wpn by itp_type_muasket, and let the troops tf_guarantee_all_wo_ranged, but just go without weapons (musket)
I intend to use to separate infantry of archers ....
or is there another solution?  :neutral:
 
Back
Top Bottom