OSP Code Combat [WIP] Auto-Firing weapons mod

ithilienranger

Knight at Arms
Best answers
0
This has been updated but it is a work in progress!

Changes:
  • WSE is no longer needed for this mod to work but you should have either version 1.153 or 1.154 of Warband
  • Auto shotguns are no longer supported due to performance
  • No more crosshair - because it looked bad
  • Multiplayer portion not implemented yet
  • Multiple ammo types per weapon type no longer supported
Notes:
  • Firing rate is determined by speed_rating
  • The AI will fire in bursts by default
  • The bullets fire in the direction of the camera which may not be where your agent is facing if on horseback

Code:
[plain]
######################################################################
##################### ADD THESE BELOW OTHER IMPORTS ##################
################# THESE STORE ITEM STATS IN ITEM_SLOTS ###############
######################################################################
from module_items import *
   
def get_item_accuracy():
   item_accuracy = []
   for i_item in xrange(len(items)):
    item_accuracy.append((item_set_slot, i_item, slot_item_accuracy, get_leg_armor(items[i_item][6])))
   return item_accuracy[:]

def get_item_shoot_speed():
   item_shoot_speed = []
   for i_item in xrange(len(items)):
    item_shoot_speed.append((item_set_slot, i_item, slot_item_shoot_speed, get_missile_speed(items[i_item][6])))
   return item_shoot_speed[:]

def get_item_speed_rating():
   item_speed_rating = []
   for i_item in xrange(len(items)):
    item_speed_rating.append((item_set_slot, i_item, slot_item_speed_rating, get_speed_rating(items[i_item][6])))
   return item_speed_rating[:]

########################################################################
##################### FIRES WEAPON BASED ON STATS ######################
########################################################################
   ("fire_auto_weapon",[
      (store_script_param, ":shooter_agent", 1),
      (store_script_param, ":shooter_weapon", 2),
      (store_script_param, ":shooter_ammo", 3),

	  (item_get_slot, ":auto_accuracy", ":shooter_weapon", slot_item_accuracy),
      (store_sub, ":inaccuracy", 100, ":auto_accuracy"),

#################### CALCULATE AGENT INACCURACY #######################
      (agent_get_troop_id, ":shooter_troop", ":shooter_agent"),
      (store_proficiency_level, ":firing_skill", ":shooter_troop", wpt_firearm),
      (store_sub, ":firing_disability", 500, ":firing_skill"),
      (val_div, ":firing_disability", 10),

########################### CALCULATE WANDER ##########################
      (item_get_slot, ":weapon_speed", ":shooter_weapon", slot_item_speed_rating),
      (store_sub, ":weapon_recoil", 150, ":weapon_speed"),
      (assign, ":max_recoil", ":weapon_recoil"),
	  (val_div, ":weapon_recoil", 3),
      (agent_get_slot, ":wander", ":shooter_agent", slot_agent_firearm_wander),
      (val_add, ":wander", ":weapon_recoil"),
      (val_clamp, ":wander", 0, ":max_recoil"),
      (agent_set_slot, ":shooter_agent", slot_agent_firearm_wander, ":wander"),
	  
      (agent_get_animation, ":cur_anim", ":shooter_agent", 0),
############### INCREASE INACCURACY DUE TO MOVEMENT ###################
      (try_begin),
         (is_between, ":cur_anim", "anim_run_forward", "anim_stand_to_crouch"),
         (val_mul, ":wander", 2),
      (try_end),
################# INCREASE INACCURACY DUE TO JUMP #####################
      (try_begin),
         (is_between, ":cur_anim", "anim_jump", "anim_stand_unarmed"),
         (val_mul, ":wander", 3),
      (try_end),

      (val_add, ":wander", ":firing_disability"),
	  (val_div, ":wander", 3),
      (val_add, ":inaccuracy", ":wander"),

############# RAISE POS TO EYE LEVEL & MOVE TO END OF GUN ###############
      (agent_get_look_position, pos1, ":shooter_agent"),
      (position_move_y, pos1, 80, 0),

      (try_begin),
         (agent_get_horse, ":horse", ":shooter_agent"),
         (gt, ":horse", 0),
         (position_move_z, pos1, 240, 0),
      (else_try),
         (position_move_z, pos1, 150, 0),
      (try_end),

#################### SOUNDS AND PARTICLES PLAY HERE #####################
      (item_get_slot, ":sound_id", ":shooter_weapon", slot_item_sound),
      (agent_play_sound, ":shooter_agent", ":sound_id"),
		 
############ GET INITIAL RANDOMIZED BULLET ANGLE ROTATION ###############
      (store_random_in_range, ":y_rotation", 0, 360),
      (position_rotate_y, pos1, ":y_rotation"),
         
#################### SPAWN BULLET WITH INACCURACY #######################
      (store_random_in_range, ":x_inaccuracy", 0, ":inaccuracy"),
      (set_fixed_point_multiplier, 10),
      (position_rotate_x_floating, pos1, ":x_inaccuracy"),

	  (item_get_slot, ":velocity", ":shooter_weapon", slot_item_shoot_speed),
      (set_fixed_point_multiplier, 1),
	  (add_missile, ":shooter_agent", pos1, ":velocity", ":shooter_weapon", 0, ":shooter_ammo", 0),
   ]),
   
   
#########################################################################
################ SCRIPT TO SET ITEM STATS TO ITEM_SLOTS #################
#########################################################################
      ("init_item_accuracy", get_item_accuracy()),
      ("init_item_shoot_speed", get_item_shoot_speed()),
      ("init_item_speed_rating", get_item_speed_rating()),
[/plain]
Code:
[plain]
####################################################################
####################### INITIALIZE AUTOFIRE ########################
####################################################################
common_init_auto_fire = (
    ti_after_mission_start, 0, ti_once, [], [
         (this_or_next|multiplayer_is_server),
         (neg|game_in_multiplayer_mode),

######### NEEDED TO PREVENT UNINITIALIZE SLOTS FOR CLIENTS #########
############# ADD EACH SCRIPT CALL FROM AUTO ITEM HERE #############
         (try_begin),
            (multiplayer_is_dedicated_server),
            (call_script, "script_set_auto_weapon_stats", "itm_m_249", "snd_pistol_shot"),
            (call_script, "script_set_auto_weapon_stats", "itm_AK_47", "snd_pistol_shot"),
            (call_script, "script_set_auto_weapon_stats", "itm_UZI", "snd_pistol_shot"),
         (try_end),
		 
############### NEEDED TO PREVENT UNINITIALIZE SLOTS ###############
      (call_script, "script_init_item_accuracy"),
      (call_script, "script_init_item_shoot_speed"),
	  (call_script, "script_init_item_speed_rating"),
    ])

common_auto_fire_held = (
   0.1, 0.5, 0, [ # adjust the delay to match the time it takes to play the ready animation
	  (game_key_is_down, gk_attack),
   ],[
      (get_player_agent_no, ":shooter_agent"),
	  (agent_set_slot, ":shooter_agent", slot_agent_autofire_ready, 1),
    ])

common_auto_fire_clicked = (
   0.1, 0, 0, [
      (get_player_agent_no, ":shooter_agent"),
      (agent_get_animation, ":shooter_stance", ":shooter_agent", 1),
      (this_or_next|eq, ":shooter_stance", "anim_reload_musket"),
      (this_or_next|eq, ":shooter_stance", "anim_reload_pistol"),
	  (neg|game_key_is_down, gk_attack),
   ],[
      (get_player_agent_no, ":shooter_agent"),
	  (agent_set_slot, ":shooter_agent", slot_agent_autofire_ready, 0),
    ])

####################################################################
################## CHECK IF BULLET CAN BE FIRED ####################
####################################################################
common_auto_fire = (
   0.1, 0, 0, [
      (this_or_next|multiplayer_is_server),
      (neg|game_in_multiplayer_mode),
   ],[
      (try_for_agents, ":shooter_agent"),
         (agent_is_alive, ":shooter_agent"),
         (try_begin),
############# CHECK IF AGENT WIELDS AN AUTOFIRE WEAPON #############
            (agent_get_wielded_item, ":cur_weapon", ":shooter_agent", 0),
            (is_between, ":cur_weapon", "itm_uzi", "itm_torch"),

########### CHECK IF AGENT IS IN READY WEAPON ANIMATION ############
            (agent_get_animation, ":shooter_stance", ":shooter_agent", 1),
            (this_or_next|eq, ":shooter_stance", "anim_ready_pistol"),
            (eq, ":shooter_stance", "anim_ready_musket"),
			
			(assign, ":ready_flag", 0),
			(try_begin), #Limits the AI to burst firing
			   (agent_is_non_player, ":shooter_agent"),
			   (agent_get_combat_state, ":cur_state", ":shooter_agent"),
			   (eq, ":cur_state", 3),
			   (assign, ":ready_flag", 1),
			(else_try), #Check if a player has held down the attack button
			   (neg|agent_is_non_player, ":shooter_agent"),
			   (agent_slot_eq, ":shooter_agent", slot_agent_autofire_ready, 1),
			   (assign, ":ready_flag", 1),
            (try_end),
			(eq, ":ready_flag", 1),

################## FIND WEAPON AND SET AMMO TYPE ##################
            (try_for_range, ":cur_slot", 0, 4),
               (agent_get_item_slot, ":cur_item", ":shooter_agent", ":cur_slot"),
               (eq, ":cur_item", ":cur_weapon"),
               (assign, ":weapon_slot", ":cur_slot"),
            (try_end),

            (assign, ":ammo_item", "itm_cartridges"),
############################ REDUCE AMMO ###########################
            (try_begin),
               (agent_get_ammo_for_slot, ":ammo", ":shooter_agent", ":weapon_slot"),
               (gt, ":ammo", 0),
               (val_sub, ":ammo", 1),
               (agent_set_ammo, ":shooter_agent", ":cur_weapon", ":ammo"),

####################### SET FIRING ANIMATION #######################
               (try_begin),
                  (eq, ":cur_weapon", "itm_uzi"),
                  (agent_set_animation, ":shooter_agent", "anim_release_pistol", 1),
               (else_try),
                  (agent_set_animation, ":shooter_agent", "anim_release_musket", 1),
               (try_end),
			   
               (call_script, "script_fire_auto_weapon", ":shooter_agent", ":cur_weapon", ":ammo_item"),
            (try_end),
         (else_try),
################## REDUCES RECOIL FOR ALL AGENTS ###################
            (agent_get_slot, ":wander", ":shooter_agent", slot_agent_firearm_wander),
            (val_sub, ":wander", 20),
            (val_min, ":wander", 0),
            (agent_set_slot, ":shooter_agent", slot_agent_firearm_wander, ":wander"),
         (try_end),
      (try_end),
   ])
[/plain]
#autofire begin
slot_agent_firearm_wander = 30
slot_agent_autofire_ready = 31
#autofire end

#autofire begin
slot_item_accuracy = 62
slot_item_shoot_speed = 63
slot_item_speed_rating = 64
slot_item_sound = 65
#autofire end
Code:
[plain]
########################################################################
##########            ITEM EXAMPLE FOR AUTOMATIC GUNS         ##########
########################################################################
["uzi", "UZI", [("flintlock_pistol",0)], itp_type_pistol |itp_merchandise|itp_primary|itp_cant_use_on_horseback, itcf_shoot_pistol|itcf_carry_revolver_right|itcf_reload_pistol, 67 , weight(3.5)|spd_rtng(100) | shoot_speed(160) | thrust_damage(40, pierce)|max_ammo(30)|accuracy(60),imodbits_none,
   [(ti_on_init_item, [(item_set_slot, "itm_uzi", slot_item_sound, "snd_pistol_shot"),])]],

["ak_47", "AK-47", [("crossbow_a",0)], itp_type_musket |itp_merchandise|itp_primary|itp_two_handed|itp_cant_use_on_horseback, itcf_shoot_musket|itcf_carry_crossbow_back|itcf_reload_musket, 67 , weight(4.3)|spd_rtng(75) | shoot_speed(160) | thrust_damage(50, pierce)|max_ammo(50)|accuracy(100),imodbits_none,
   [(ti_on_init_item, [(item_set_slot, "itm_ak_47", slot_item_sound, "snd_pistol_shot"),])]],

["m_249", "M-249", [("crossbow_a",0)], itp_type_musket |itp_merchandise|itp_primary|itp_two_handed|itp_cant_use_on_horseback, itcf_shoot_musket|itcf_carry_crossbow_back|itcf_reload_musket, 67 , weight(8)|spd_rtng(50) | shoot_speed(160) | thrust_damage(40, pierce)|max_ammo(100)|accuracy(60),imodbits_none,
   [(ti_on_init_item, [(item_set_slot, "itm_m_249", slot_item_sound, "snd_pistol_shot"),])]],
[/plain]

Here is the link to the old version download: http://www.mbrepository.com/file.php?id=2464

Running Credits:
ithilienranger - majority of coding & testing
cmpxchg8b - coding assistance
All other modding experts who have helped me with questions.

Special thanks to Swyter for motivating me to make this  :lol: and for mr.master for his patience and support :cool:.
 

ithilienranger

Knight at Arms
Best answers
0
Ok, you win. :roll: I will try to add bullet drop, but I am keep having a CTD when I run it.

Also, I have the aiming reticle/crosshairs close to finished. I just need a way to figure out how to adjust its position based on screen resolution.
 

ithilienranger

Knight at Arms
Best answers
0
I have the gun recoil working now and I have been trying to add a new aiming reticule. However, I want to know what everyone else thinks about that so I added a new poll.

Thanks for the positive comments.
 

uio0000

Sergeant
WB
Best answers
0
TitoWasFat said:
Did we not already have this?
this is certainly not something we've seen before
first of all this is dumn AI firing automatic!
second it's actually shooting projectiles,instead of some "shots" coded by modders which just gives you a damage and a graphic effect
 

ithilienranger

Knight at Arms
Best answers
0
uio0000 said:
TitoWasFat said:
Did we not already have this?
this is certainly not something we've seen before
first of all this is dumn AI firing automatic!
second it's actually shooting projectiles,instead of some "shots" coded by modders which just gives you a damage and a graphic effect
What do you mean be dumn?

If you mean dumb I can see that since the AI don't actually know that they are autofiring. I have it set that when an agent is in the ready weapon animation they autofire. However, I really need to code something to allow the AI to conserve ammo and to avoid shooting their own men.
 

ithilienranger

Knight at Arms
Best answers
0
I have been too busy to work on it recently because of school. All my classes are piling up projects. :mad:

I am close to a new video featuring realistic damage, the new crosshairs/reticle, item based stats, and possibly auto-shotguns.

After that I won't be able to do much until the next update for the module system. Then I can fix the ammo consumption, fix the animations, and reduce some lag if my trigger intervals get added.
 

ithilienranger

Knight at Arms
Best answers
0
Slow, I am trying to figure out why the game lags a ton when I use nonstandard models. Also, I was trying to use the flying_missile model for the bullets, but then hit detection didn't work.
 

mr.master

Count
Best answers
0
Hmm. Well, you dont necessary need to use bullets D: Atleasy people complained me as bullets showed in my mod.
 

ithilienranger

Knight at Arms
Best answers
0
I guess I need to give away my secret weapon then. Actually, Daegoth on the bug reporter gave it away, but after I had the idea. I am using a prop attached to an agent as a hitbox. Then I use the prop_intersects_prop function to determine a hit. That is why I need a prop for the bullet, but I can make them invisible.

Also, the prop that I am using is the straw dummy model. However, I had Swyter make me a better hitbox model to use with fewer faces and vertexes to reduce lag. On the contrary, the new model increases lag.
 

mr.master

Count
Best answers
0
Ah, now I see :razz: Well, as you said, make them invisible then. Looks probably much neater then :smile: Well hopefully you can solve the issues : o Your the only hope in autofire code currently :razz: