OSP Code Combat AI Improvise Stick Weapon

Users who are viewing this thread

Zarthas

Knight at Arms
Hello,

There's some great scripts for weapon-breaking around the forums.  These have to hard-limit weapon breaking for AI so they don't end up unarmed.  I didn't really like that solution, so I made this script to have my AI agents get sticks if they are unarmed.

TLDR; If an AI agent has no weapon, he'll sometimes get a Stick.

The bit about setting the crouching animation isn't working.  Anyone have any advice on that?

Also its been a very long time since I wrote code for Warband.  That last (try_begin) probably isn't necessary.

Cleaned up via Sebastian, thanks:

Code:
      
common_AI_pickup_weapon = (10, 0, 0, [],
[
	(get_player_agent_no, ":player"),
	(try_for_agents, ":agent"),
		(neq, ":agent", ":player"),#Don't auto-give to player. He can find his own!
		(agent_is_active, ":agent"),
		(agent_is_alive, ":agent"),
		(agent_is_human, ":agent"),
		(agent_get_horse, ":check",":agent"),
		(eq,":check",-1),#No horse here.
		(agent_slot_eq, ":agent", slot_agent_is_running_away, 0),#Isn't fleeing the battle.
		(agent_get_wielded_item, ":check", ":agent", 0),#AI usually only resorts to fisticuffs when unarmed.
		(eq, ":check", -1),#No Weapon
		(agent_get_attack_action, ":check", ":agent"),
		(eq, ":check", 0),#Not Attacking
		(agent_get_defend_action, ":check", ":agent"),
		(eq, ":check", 0),#Not Defending
		(store_random_in_range, ":check", 0, 5),#40% chance to get something every check, which is 10 seconds.
		
		(try_begin),
			
			(eq, ":check", 4), #Only found a rock.
			(agent_equip_item, ":agent", itm_stones),
			(agent_set_wielded_item, ":agent", itm_stones),
			(agent_set_ammo, ":agent",itm_stones,1),
			(agent_set_animation, ":agent", "anim_stand_to_crouch", 0),
			
			(else_try),
			
			(gt, ":check", 4), #Found a stick.
			(agent_equip_item, ":agent", itm_wooden_stick),
			(agent_set_wielded_item, ":agent", itm_wooden_stick),
			(agent_set_animation, ":agent", "anim_stand_to_crouch", 0),
			
		(try_end),
	(try_end),
])

edit: Just realized this didn't check for horses, my mod doesn't use them.  Fixed.
 
The bit about setting the crouching animation isn't working.  Anyone have any advice on that?
Increase the animations priority and try again, or use agent_set_crouch_mode instead.

However, if you mind some optimizations...
Execute the checks step by step and reuse the existing variables as often as possible.
Code:
AI_pickup_weapon = (10, 0, 0, [],
[
	(get_player_agent_no, ":player"),
	(try_for_agents, ":agent"),
		(neq, ":agent", ":player"),#Don't auto-give to player. He can find his own!
		(agent_is_active, ":agent"),
		(agent_is_alive, ":agent"),
		(agent_is_human, ":agent"),
		(agent_slot_eq, ":agent", slot_agent_is_running_away, 0),#Isn't fleeing the battle.
		(agent_get_wielded_item, ":check", ":agent", 0),
		(eq, ":check", -1),#No Weapon
		(agent_get_attack_action, ":check", ":agent"),
		(eq, ":check", 0),#Not Attacking
		(agent_get_defend_action, ":check", ":agent"),
		(eq, ":check", 0),#Not Defending
		(store_random_in_range, ":check", 1, 1000),
		(gt, ":check", 200), #80% chance per check
		(agent_equip_item, ":agent", itm_wooden_stick),
		(agent_set_wielded_item, ":agent", itm_wooden_stick),
		(agent_set_animation, ":agent", "anim_stand_to_crouch", 0),
	(try_end),
])
 
Thanks for that Sebastian, it does make more sense.

@JhonBrazil:  Could definitely work for daggers.  Just change "itm_wooden_stick" to "itm_dagger".  Its kind of a niche use, but I think it could be a better solution than just not letting weapons break.
 
Another advice;
The random number generator has some odd behavior with big ranges.
The bigger the range, the less likely high numbers will be picked.
So keep the range as small as possible, the example below will just work fine.
Code:
(store_random_in_range, ":check", 0, 5),
(neq, ":check", 0), #80% chance per check
 
I didn't realize it was that weighted, thanks again.

Posted another tweak that randomly gives either one throwing Stone, or a Stick.

I want to make this as Vanilla-Friendly as possible, so I'll explore using the agent_set_crouch_mode later on, thanks for the tip!

edit:  agent_set_crouch is somewhat....less than advertised.  It looks like its trying to use an animation that doesn't exist, looking into that.
edit again:  So I guess that's really from the WFAS expansion, and the code just exists in Warband. 
 
Back
Top Bottom