OSP Code Combat [WB] Weapon breaking trigger

Users who are viewing this thread

Since you've already modified the item modifier on that item's slot in the troop's inventory, the next time the script is called it fetches that modifier again. There's no way to set/get wielded item modifiers - similar to how items that the player agent pick up aren't tracked by his troop's item slots, as you may have noticed. Dropping the item (G) spawns it at your feet with its current modifier. Try doing that after it degrades and see if the modifier is retained or changed.

Anyways, if it doesn't change immediately, what you can do is unequip the item (so it's no longer available), change the imod slot (so it has an effect after battle) and then spawn the item (with the new modifier) at the player's feet (agent_get_position, set_spawn_position). Keep in mind that regular agents will not attempt to pick it up (at all), so limit this to the player, otherwise random equipment will litter the battlefield. Also, changing the item modifier on a regular troop's slot affects the equipment of all troops of that type globally, even after the battle and possibly inside savegames - not sure if this is what you intended.
 
...I see.
Dropping a normal weapon after the imod has been changed to chipped and the normal weapon is still normal.

The change is only made permanent AFTER exiting the battle, when the weapon is chipped in the inventory.
 
Baron Conrad said:
Numbers can be changed based on your preference, remember not to use decimals! That is why I multiply by 101 ect and then divide by 100.
Any questions, please ask.

Baron Conrad, I'm new to the modding world and I haven't been able to find a coding doc that lists the function calls so I'll ask.  You use the val_mul then val_divide to increase the breakchance correct?  How does teh game treat numbers less than 1?  Does it underflow into a really big number?  Any idea?
thanks!
 
Mystery said:
Baron Conrad said:
Numbers can be changed based on your preference, remember not to use decimals! That is why I multiply by 101 ect and then divide by 100.
Any questions, please ask.

Baron Conrad, I'm new to the modding world and I haven't been able to find a coding doc that lists the function calls so I'll ask.  You use the val_mul then val_divide to increase the breakchance correct?  How does teh game treat numbers less than 1?  Does it underflow into a really big number?  Any idea?
thanks!

Badly. It treats them badly. The bottom line is they do not work. The only real way is as Baron Conrad does it by multiplying and then dividing by whatever multiple of 10 gets you the number of decimal places you want. And any non-integer decimal gets truncated. So if your result is 7.9, the game stores 7.
 
Okay, here's my try at it.

There are three main mission templates: the multiplayer template in which each client runs the code independetly, the multi and singleplayer template in which either the server loops through every agent each 1 second (player not affected performance wise, might cause some lag in crappy servers) or does all that singleplayer, and finally the singleplayer template in which the player itself is checked.

In addition to the base code I also made sure that we can have floating point numbers (0.xx) and that parrying is also checked.
The codes are as optimized as possible..
Enjoy.

PS: The code only unequips the items. You can't use troop_remove_item in multiplayer (because if you use it then it will remove the items from every player of the same troop. Same thing applies to singleplayer.) so I didn't use it at all. You most likely want to use agent and item slots to determine if the agent can use the item or not.
However, you can use that command on the player.

###########
# Multiplayer & singleplayer
# weapon breaking.
# Chaalaak.
###########

multiplayer_weapon_break = (
    0.4, 0, 0,
[(game_in_multiplayer_mode)],
[
    (set_fixed_point_multiplier, 100),
        (multiplayer_get_my_player, ":player"),
    (player_get_agent_id, ":agent", ":player"),
    (ge, ":agent", 0),
(agent_get_attack_action, ":attack_action", ":agent"),
(agent_get_defend_action, ":defend_action", ":agent"),
(agent_get_wielded_item, ":wielded_item", ":agent", 0),
(ge, ":wielded_item", 0),
(store_random_in_range, ":chance", 0, 100),

#Calculate chance.
(try_begin),
    (eq, ":attack_action", 3), #Completing attack.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 102),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(try_end),
(else_try),
    (eq, ":defend_action", 1), #Parrying.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(try_end),
(try_end),

#Determine weapon breaking based on chance.
(ge, ":chance", 95),
(display_message, "@Your weapon has broken!", 0xCC0000), #Red color.
(multiplayer_send_int_to_server, multiplayer_weapon_break_event, ":wielded_item"),
])


#Meant only for AI.
single_and_multiplayer_weapon_break = (
    1, 0, 0,
[],
[
(this_or_next|multiplayer_is_server),
(neg|game_in_multiplayer_mode),
    (set_fixed_point_multiplier, 100),
(try_for_agents, ":agent"),
    (agent_is_non_player, ":agent"),
(agent_is_alive, ":agent"),
(agent_is_human, ":agent"),
    (agent_get_attack_action, ":attack_action", ":agent"),
    (agent_get_defend_action, ":defend_action", ":agent"),
    (agent_get_wielded_item, ":wielded_item", ":agent", 0),
    (ge, ":wielded_item", 0),
    (store_random_in_range, ":chance", 0, 100),

    #Calculate chance.
    (try_begin),
        (eq, ":attack_action", 3), #Completing attack.
    (item_get_type, ":weapon_type", ":wielded_item"),
    (try_begin),
        (agent_get_horse, ":horse", ":agent"),
    (ge, ":horse", 0),
    (val_mul, ":chance", 110),
    (val_div, ":chance", 100),
    (try_end),
    (try_begin),
        (eq, ":weapon_type", itp_type_polearm), #Polearm.
    (val_mul, ":chance", 105),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
    (val_mul, ":chance", 102),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
    (val_mul, ":chance", 103),
    (val_div, ":chance", 100),
    (try_end),
    (else_try),
        (eq, ":defend_action", 1), #Parrying.
    (item_get_type, ":weapon_type", ":wielded_item"),
    (try_begin),
        (agent_get_horse, ":horse", ":agent"),
    (ge, ":horse", 0),
    (val_mul, ":chance", 110),
    (val_div, ":chance", 100),
    (try_end),
    (try_begin),
        (eq, ":weapon_type", itp_type_polearm), #Polearm.
    (val_mul, ":chance", 10:cool:,
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
    (val_mul, ":chance", 103),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
    (val_mul, ":chance", 105),
    (val_div, ":chance", 100),
    (try_end),
    (try_end),
   
    #Determine weapon breaking based on chance.
    (ge, ":chance", 95),
(try_begin),
    (game_in_multiplayer_mode),
        (multiplayer_send_int_to_server, multiplayer_weapon_break_event_2, ":agent", ":wielded_item"),
(else_try),
    (agent_play_sound, ":agent", "snd_shield_broken"),
    (agent_unequip_item, ":agent", ":weapon"),
(try_end),
(try_end),
])


#Meant only for player.
singleplayer_weapon_break = (
    0.4, 0, 0,
[(neg|game_in_multiplayer_mode)],
[
    (set_fixed_point_multiplier, 100),
(get_player_agent_no, ":agent"),
(agent_get_attack_action, ":attack_action", ":agent"),
(agent_get_defend_action, ":defend_action", ":agent"),
(agent_get_wielded_item, ":wielded_item", ":agent", 0),
(ge, ":wielded_item", 0),
(store_random_in_range, ":chance", 0, 100),

#Calculate chance.
(try_begin),
    (eq, ":attack_action", 3), #Completing attack.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 102),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(try_end),
(else_try),
    (eq, ":defend_action", 1), #Parrying.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(try_end),
(try_end),

#Determine weapon breaking based on chance.
(ge, ":chance", 95),
(display_message, "@Your weapon has broken!", 0xCC0000), #Red color.
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),
    (troop_remove_item, "trp_player", ":weapon"),
])
###########
###########
# Multiplayer & singleplayer
# weapon breaking.
# Chaalaak.
###########


    #Server event.
    (eq, ":event_type", multiplayer_weapon_break_event),
(store_script_param, ":weapon", 3),
(player_get_agent_id, ":agent", ":player_no"),
(ge, ":agent", 0),
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),

    #Server event 2.
    (eq, ":event_type", multiplayer_weapon_break_event_2),
(store_script_param, ":agent", 3),
(store_script_param, ":weapon", 4),
(player_get_agent_id, ":agent", ":player_no"),
(ge, ":agent", 0),
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),

###########
 
The chance is calculated according to weapon type and if the agent is mounted or not.
 
What do you mean under weapon hp stat? Do you want each agents weapon to have their own personal HP bar of some sort?

I'd say it's possible but probably wouldn't be good performance wise.
 
Sorry I Dont have Much experience with coding but I noticed in Morghs mount and blade warband tools when you open the item editor theres a field that says hit points. I think its shield hitpoints but I noticed that weapons have a number is this field even though it isnt used.I thought maybe you could make use of that
 
Nope. This script can destroy your weapon whenever you hit enemy. Adapting this script will result in shields randomly breaking when you hit enemy - it makes no sense.
 
According to the manual, shields are supposed to degrade one condition level (Reinforced -> Thick -> Normal -> Battered -> Cracked) when you break them in combat, and be removed from your inventory when Cracked and then broken.

...But I have never seen a shield degrade in condition level or be removed from my inventory. I'm trying to create a script that will allow me to do this. Do you know how it could be done?
 
I had an weapon breaking script in freelancer at one stage, I was thinking of adding it back to the game this time around... shield idea sounds feasible as your shield 'can' break in combat. I can not see why we couldn't make it remove your shield from the inventory..

:mrgreen: 

This is my old mission_templates script that Caba'drin and my dev team came up with (may need slots).

Code:
 (ti_on_agent_hit, 0, 0,
	    [],
	 [   	    (store_trigger_param_1, ":unused"), 	 
			 (store_trigger_param_2, ":agent"), #Trigger parameter 2 for this trigger is the attacker agent. Parameter 1 is the hit agent.
			 (agent_is_human, ":agent"),
			 (agent_is_alive, ":agent"),
		
		 (agent_get_wielded_item,":breakweapon",":agent",0),
		
		 (is_between,":breakweapon","itm_wooden_stick","itm_awlpike_long"), #change this to set the items you want to break				
						
		 (store_random_in_range,":weaponbreakchance",1,150),
# #--Cabadrin imod Quality Modifier
	   (agent_get_troop_id, ":troop_id", ":agent"),
	    (try_begin),    #only heroes have item modifications
		    (troop_is_hero, ":troop_id"),
		    (try_for_range, ":item_slot", ek_item_0, ek_head),    # heroes have only 4 possible weapons (equipped)
			    (troop_get_inventory_slot, reg8, ":troop_id", ":item_slot"),  #Find Item Slot with same item ID as Equipped Weapon
			    (eq, reg8, ":breakweapon"),
			    (troop_get_inventory_slot_modifier, ":imod", ":troop_id", ":item_slot"),
		    (try_end),
	    (else_try),
		    (assign, ":imod", imodbit_plain),
	    (try_end),
# #Better than Average
# #I know i'm totally wrong with most of them, just go on and edit the values as you want 
	    (try_begin),
		    (eq, imodbit_masterwork, ":imod"),
		    (val_min,":weaponbreakchance",9),#1%
	    (else_try),
		    (eq, imodbit_tempered, ":imod"),
		    (val_min,":weaponbreakchance",8),#2%
	    (else_try),
		    (eq, imodbit_balanced, ":imod"),
		    (val_add,":weaponbreakchance",3),
	    (else_try),
		    (eq, imodbit_heavy, ":imod"),
		    (val_add,":weaponbreakchance",2),
	    (else_try),
		    (eq, imodbit_plain, ":imod"),
		    (val_add,":weaponbreakchance",1),
   # #Worse than Average
	    (else_try),
		    (eq, imodbit_bent, ":imod"),
		    (val_add,":weaponbreakchance",4),
	    (else_try),
		    (eq, imodbit_rusty, ":imod"),
		    (val_add,":weaponbreakchance",7),
	    (else_try),
		    (eq, imodbit_chipped, ":imod"),
		    (val_add,":weaponbreakchance",8),
	    (else_try),
		    (eq, imodbit_cracked, ":imod"),
		    (val_add,":weaponbreakchance",15),
	    (try_end),
	# #--End imod Quality Modifier
			    (try_begin),   
			 (ge,":weaponbreakchance",140),
			 (agent_unequip_item,":agent",":breakweapon"),
			 (str_store_item_name, s0, ":breakweapon"),
			 (try_begin),
				 (get_player_agent_no, ":player_agent"),
				 (eq, ":agent", ":player_agent"),
				 (troop_remove_item, "trp_player", ":breakweapon"),
				 (display_message, "@You broke your {s0}.",0xFF0000),
				 (else_try),
				   (agent_get_troop_id, ":troop", ":agent"),
				 (str_store_troop_name,s1,":troop_id"),
				 (display_message, "@{s1} broke his {s0}.",0x66FF33),
					 (agent_set_animation, ":agent", "anim_strike_chest_front"),
				 (assign, ":weapon_broke", 1),
							 (else_try),
								 (agent_get_horse,":horse",":agent"),
				   (agent_get_troop_id, ":troop", ":agent"),
								 (str_store_troop_name,s1,":troop_id"),
				 (display_message, "@{s1} broke his {s0}.",0x66FF33),
					 (agent_set_animation, ":agent", "anim_strike_chest_front"),#You need to replace this animation with the horseman's strike
				 (assign, ":weapon_broke", 1),
			 (try_end),
		 (try_end),  
	 ]),



 
Here's My Version. I combined everything and made some adjustments, and I documented.

Code:
common_weapon_break =  (
         0.4, 0, 0,
       [
       
       #(player_get_agent_id, ":player_agent", ":player_no"),
#       (get_player_agent_no, ":player_agent"),
     (try_for_agents, ":player_agent"),
      (agent_get_attack_action, ":playeraction", ":player_agent"), #returned values: free = 0, readying_attack = 1, releasing_attack = 2, completing_attack_after_hit = 3, attack_parried = 4, reloading = 5, after_release = 6, cancelling_attack = 7
        (try_begin),
           (this_or_next|eq,":playeraction",3),
           (eq,":playeraction",4),
           (agent_get_wielded_item,":breakweapon",":player_agent",0),   
           (gt,":breakweapon",2),
#--imod, get weapon imod
           (try_for_range, ":i_slot", 0, 10),
               (troop_get_inventory_slot, ":item_id", "trp_player", ":i_slot"),  #Find Item Slot with same item ID as Equipped Weapon
               (eq, ":item_id", ":breakweapon"),
               (troop_get_inventory_slot_modifier, ":imod", "trp_player", ":i_slot"),
           (try_end),
#--End Get imod
           (item_get_type, ":weapontype", ":breakweapon"),
           (agent_get_horse,":playermounted",":player_agent"),
           (store_random_in_range,":weaponbreakchance",1,100),
#First, calculate based off weapon type and mount
          (try_begin),
            (ge,":playermounted",0),
      #      (display_message, "@Mounted."),
            (val_mul,":weaponbreakchance",105), #Scale chance 5% greater   
            (val_div,":weaponbreakchance",100),
            (eq,itp_type_polearm,":weapontype"),
            (val_add,":weaponbreakchance",4), #Total polearm break chance on horseback increase is 7%
          (try_end),
          (try_begin),            
             (eq,itp_type_polearm,":weapontype"), #3% higher break chance polearm
             (neq,"itm_iron_staff",":weapontype"), #reinforced polerams exempt
      #       (display_message, "@Polearm"),
             (val_add,":weaponbreakchance",3),
          (try_end),
          (try_begin),            
             (eq,itp_type_two_handed_wpn,":weapontype"), #Scale chance 2% greater
      #       (display_message, "@2 hander"),
            (val_mul,":weaponbreakchance",102),
            (val_div,":weaponbreakchance",100),
         (try_end),
         (try_begin),            
             (eq,itp_type_one_handed_wpn,":weapontype"),
      #       (display_message, "@1 hander"),
            (val_sub,":weaponbreakchance",1), #1 handed weapons are more durable
         (try_end),
#Now adjust for item modifier
#--imod Quality Modifier
    #Better than Average
        (try_begin), #66% reduction for masterwork weapon
            (eq, imodbit_masterwork, ":imod"),
            (val_div,":weaponbreakchance",3),
        (try_end),
        (try_begin), #50% reduction for tempered
            (eq, imodbit_tempered, ":imod"),
            (val_div,":weaponbreakchance",2),
        (try_end),
        (try_begin), #33% reduction for balanced
            (eq, imodbit_balanced, ":imod"),
            (val_mul,":weaponbreakchance",2),
            (val_div,":weaponbreakchance",3),
        (try_end),
        (try_begin), #20% reduction for heavy
            (eq, imodbit_heavy, ":imod"),
            (val_mul,":weaponbreakchance",4),
            (val_div,":weaponbreakchance",5),
        (try_end),
        (try_begin), #Since the weapon is already 'broken', 10% reduction for bent
            (eq, imodbit_bent, ":imod"),
            (val_mul,":weaponbreakchance",10),
            (val_div,":weaponbreakchance",11),
        (try_end),
    #Worse than Average
        (try_begin),
            (eq, imodbit_rusty, ":imod"), #20% more likely to be broken if rusty
            (val_mul,":weaponbreakchance",5),
            (val_div,":weaponbreakchance",4),
        (try_end),
        (try_begin), #33% more likely to be broken if chipped
            (eq, imodbit_chipped, ":imod"),
            (val_mul,":weaponbreakchance",4),
            (val_div,":weaponbreakchance",3),
        (try_end),
        (try_begin), #66% more likely to be broken if already cracked
            (eq, imodbit_cracked, ":imod"),
            (val_mul,":weaponbreakchance",5),
            (val_div,":weaponbreakchance",3),
        (try_end),
#--End imod Quality Modifier
#Finally, adjust for skills TBD
        (try_begin),   
            (assign, reg8, ":weaponbreakchance"),
      #      (display_message, "@{reg8}"),
            (ge,":weaponbreakchance",97), #3% base break chance
#AI MODIFICATION BEGIN----------------------------------------------------------------------------
            (assign, ":num_agent_items", 0),
            (try_for_range, ":item", "itm_no_item", "itm_items_end"),
              (item_get_type, ":weapontype2", ":item"),
              (this_or_next|eq,itp_type_one_handed_wpn,":weapontype2"),
              (this_or_next|eq,itp_type_two_handed_wpn,":weapontype2"),
              (eq,itp_type_polearm,":weapontype2"),
              (try_begin),
                (agent_has_item_equipped,":player_agent",":item"),
                (val_add, ":num_agent_items", 1),
              (try_end),
            (try_end),
#What happens?
#--imod Weapon Damage
    #Better than Average
        (try_begin),
            (eq, imodbit_masterwork, ":imod"),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", 0),
        (else_try),
            (eq, imodbit_tempered, ":imod"),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", imodbit_bent),
        (else_try),
            (eq, imodbit_balanced, ":imod"),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", imodbit_chipped),
        (else_try),
            (eq, imodbit_heavy, ":imod"),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", imodbit_bent),
    #Worse than Average
        (else_try),
            (eq, imodbit_bent, ":imod"),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", imodbit_cracked),
    #Chipped, Cracked, Rusty weapons break
        (else_try),
            (this_or_next|eq, imodbit_rusty, ":imod"),
            (this_or_next|eq, imodbit_chipped, ":imod"),
            (eq, imodbit_cracked, ":imod"),
#            (ge, ":num_agent_items", 2),
#AI MODIFICATION END------------------------------------------------------------------------------
            (agent_unequip_item,":player_agent",":breakweapon"),
            (agent_get_troop_id,":troop", ":player_agent"),
          #  (troop_remove_item, "trp_player", ":breakweapon"),
            (try_begin),
              (get_player_agent_no,":player"),
              (eq, ":player_agent", ":player"),
              (troop_remove_item, "trp_player", ":breakweapon"),
              (play_sound,"snd_shield_broken",),
              (display_message, "@Your weapon has been broken!"), 
            (try_end),
            (lt,":playermounted",0),
            (agent_set_animation, ":player_agent", "anim_strike_chest_front"),
        (try_end),
#Normal Weapon gets cracked
        (else_try),
            (troop_set_inventory_slot_modifier, "trp_player", ":i_slot", imodbit_cracked),
        (try_end),
      
       (try_end),          
      ], [])

Let me know if I need to make any improvements.

EDIT: I got rid of the 2 or more weapons thing, since AI's weapons probably won't break at first. I plan to add a new "Dueling" skill to my mod, that is why I have a 'TBD' in there.  :cool:
 
I've been using some of these scripts, I like it a lot.  However,  I don't like the AI solution of just not allowing AI to break their last weapon, so I made this trigger:

https://forums.taleworlds.com/index.php/topic,349722.0.html

AI will randomly get Wooden Sticks if they have no weapons.  You could easily change it to be daggers or throwing stones or whatever you'd like.
 
Chaalaak said:
Okay, here's my try at it.

There are three main mission templates: the multiplayer template in which each client runs the code independetly, the multi and singleplayer template in which either the server loops through every agent each 1 second (player not affected performance wise, might cause some lag in crappy servers) or does all that singleplayer, and finally the singleplayer template in which the player itself is checked.

In addition to the base code I also made sure that we can have floating point numbers (0.xx) and that parrying is also checked.
The codes are as optimized as possible..
Enjoy.

PS: The code only unequips the items. You can't use troop_remove_item in multiplayer (because if you use it then it will remove the items from every player of the same troop. Same thing applies to singleplayer.) so I didn't use it at all. You most likely want to use agent and item slots to determine if the agent can use the item or not.
However, you can use that command on the player.

###########
# Multiplayer & singleplayer
# weapon breaking.
# Chaalaak.
###########

multiplayer_weapon_break = (
    0.4, 0, 0,
[(game_in_multiplayer_mode)],
[
    (set_fixed_point_multiplier, 100),
        (multiplayer_get_my_player, ":player"),
    (player_get_agent_id, ":agent", ":player"),
    (ge, ":agent", 0),
(agent_get_attack_action, ":attack_action", ":agent"),
(agent_get_defend_action, ":defend_action", ":agent"),
(agent_get_wielded_item, ":wielded_item", ":agent", 0),
(ge, ":wielded_item", 0),
(store_random_in_range, ":chance", 0, 100),

#Calculate chance.
(try_begin),
    (eq, ":attack_action", 3), #Completing attack.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 102),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(try_end),
(else_try),
    (eq, ":defend_action", 1), #Parrying.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(try_end),
(try_end),

#Determine weapon breaking based on chance.
(ge, ":chance", 95),
(display_message, "@Your weapon has broken!", 0xCC0000), #Red color.
(multiplayer_send_int_to_server, multiplayer_weapon_break_event, ":wielded_item"),
])


#Meant only for AI.
single_and_multiplayer_weapon_break = (
    1, 0, 0,
[],
[
(this_or_next|multiplayer_is_server),
(neg|game_in_multiplayer_mode),
    (set_fixed_point_multiplier, 100),
(try_for_agents, ":agent"),
    (agent_is_non_player, ":agent"),
(agent_is_alive, ":agent"),
(agent_is_human, ":agent"),
    (agent_get_attack_action, ":attack_action", ":agent"),
    (agent_get_defend_action, ":defend_action", ":agent"),
    (agent_get_wielded_item, ":wielded_item", ":agent", 0),
    (ge, ":wielded_item", 0),
    (store_random_in_range, ":chance", 0, 100),

    #Calculate chance.
    (try_begin),
        (eq, ":attack_action", 3), #Completing attack.
    (item_get_type, ":weapon_type", ":wielded_item"),
    (try_begin),
        (agent_get_horse, ":horse", ":agent"),
    (ge, ":horse", 0),
    (val_mul, ":chance", 110),
    (val_div, ":chance", 100),
    (try_end),
    (try_begin),
        (eq, ":weapon_type", itp_type_polearm), #Polearm.
    (val_mul, ":chance", 105),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
    (val_mul, ":chance", 102),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
    (val_mul, ":chance", 103),
    (val_div, ":chance", 100),
    (try_end),
    (else_try),
        (eq, ":defend_action", 1), #Parrying.
    (item_get_type, ":weapon_type", ":wielded_item"),
    (try_begin),
        (agent_get_horse, ":horse", ":agent"),
    (ge, ":horse", 0),
    (val_mul, ":chance", 110),
    (val_div, ":chance", 100),
    (try_end),
    (try_begin),
        (eq, ":weapon_type", itp_type_polearm), #Polearm.
    (val_mul, ":chance", 10:cool:,
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
    (val_mul, ":chance", 103),
    (val_div, ":chance", 100),
    (else_try),
        (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
    (val_mul, ":chance", 105),
    (val_div, ":chance", 100),
    (try_end),
    (try_end),
   
    #Determine weapon breaking based on chance.
    (ge, ":chance", 95),
(try_begin),
    (game_in_multiplayer_mode),
        (multiplayer_send_int_to_server, multiplayer_weapon_break_event_2, ":agent", ":wielded_item"),
(else_try),
    (agent_play_sound, ":agent", "snd_shield_broken"),
    (agent_unequip_item, ":agent", ":weapon"),
(try_end),
(try_end),
])


#Meant only for player.
singleplayer_weapon_break = (
    0.4, 0, 0,
[(neg|game_in_multiplayer_mode)],
[
    (set_fixed_point_multiplier, 100),
(get_player_agent_no, ":agent"),
(agent_get_attack_action, ":attack_action", ":agent"),
(agent_get_defend_action, ":defend_action", ":agent"),
(agent_get_wielded_item, ":wielded_item", ":agent", 0),
(ge, ":wielded_item", 0),
(store_random_in_range, ":chance", 0, 100),

#Calculate chance.
(try_begin),
    (eq, ":attack_action", 3), #Completing attack.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 102),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(try_end),
(else_try),
    (eq, ":defend_action", 1), #Parrying.
(item_get_type, ":weapon_type", ":wielded_item"),
(try_begin),
    (agent_get_horse, ":horse", ":agent"),
(ge, ":horse", 0),
(val_mul, ":chance", 110),
(val_div, ":chance", 100),
(try_end),
(try_begin),
    (eq, ":weapon_type", itp_type_polearm), #Polearm.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_one_handed_wpn), #1-handed.
(val_mul, ":chance", 103),
(val_div, ":chance", 100),
(else_try),
    (eq, ":weapon_type", itp_type_two_handed_wpn), #2-handed.
(val_mul, ":chance", 105),
(val_div, ":chance", 100),
(try_end),
(try_end),

#Determine weapon breaking based on chance.
(ge, ":chance", 95),
(display_message, "@Your weapon has broken!", 0xCC0000), #Red color.
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),
    (troop_remove_item, "trp_player", ":weapon"),
])
###########
###########
# Multiplayer & singleplayer
# weapon breaking.
# Chaalaak.
###########


    #Server event.
    (eq, ":event_type", multiplayer_weapon_break_event),
(store_script_param, ":weapon", 3),
(player_get_agent_id, ":agent", ":player_no"),
(ge, ":agent", 0),
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),

    #Server event 2.
    (eq, ":event_type", multiplayer_weapon_break_event_2),
(store_script_param, ":agent", 3),
(store_script_param, ":weapon", 4),
(player_get_agent_id, ":agent", ":player_no"),
(ge, ":agent", 0),
(agent_play_sound, ":agent", "snd_shield_broken"),
(agent_unequip_item, ":agent", ":weapon"),

###########

I have errors : usage of unassign global variable :. :weapon
 
Back
Top Bottom