(try_begin),#respawn
(main_hero_fallen), #when dead
(key_is_down, "$g_hp_bar_key"),
(eq, "$g_battle_result", -1),#death already triggered
(get_player_agent_no, ":player_agent"),
(assign, "$g_battle_result", 0),#same as in beginning
(assign, "$g_battle_won", 0), #this battle isn't over
(agent_get_position, pos3, ":player_agent"),
(set_spawn_position, pos3),
(agent_get_team, ":player_team", ":player_agent"),
# (assign, "$dmod_move_camera", -1),
(mission_cam_set_mode, 0),#end camera
(try_begin), #recently died
(eq, "$dmod_current_agent", -1),
(troop_set_inventory_slot, "$g_player_troop", ek_horse, -1),#remove your horse
(troop_equip_items, "$g_player_troop"),
(set_spawn_position, pos63), #this pos was stored on death
(spawn_agent, "$g_player_troop"),
(assign, ":player_agent", reg0),
(try_begin),
(position_get_distance_to_terrain, ":distance", pos63),
(lt, ":distance", 500), #5 meters?
(init_position, pos62),
(position_set_z_to_ground_level, pos63), #ground
(position_copy_rotation, pos63, pos62), #clear rotation
(set_spawn_position, pos63),
(spawn_scene_prop, "spr_grave_a"), #marker - might also cover body
(try_end),
(play_sound, "snd_unhide"),#will probably get annoying
(else_try),
(agent_is_alive, "$dmod_current_agent"),
(agent_get_troop_id, ":troop", "$dmod_current_agent"),
#we get a bunch of parameters
(agent_get_wielded_item, ":item", "$dmod_current_agent", 0),
(try_begin),#melee
(call_script, "script_cf_is_melee_weapon_for_tutorial", ":item"),
(assign, ":ammo", -1),
(else_try),#is ranged
(agent_get_ammo, ":amount", "$dmod_current_agent", 1),#ammo amount
(gt, ":amount", 0),
#get ammo type
(assign, ":end", "itm_pilgrim_disguise"),
(try_for_range, ":ammo_type", "itm_arrows", ":end"),
(agent_has_item_equipped, "$dmod_current_agent", ":ammo_type"), #ammo counts as equipped?
(assign, ":ammo", ":ammo_type"),
(assign, ":end", "itm_arrows"),
(try_end),
(try_end),
(agent_get_position, pos3, "$dmod_current_agent"),
(agent_get_look_position, pos4, "$dmod_current_agent"),
(store_agent_hit_points, ":hp", "$dmod_current_agent"),
(agent_get_horse, ":horse", "$dmod_current_agent"),
(try_begin), #dismounted
(lt, ":horse", 0), #invalid agent
(troop_get_inventory_slot, ":horse_item", ":troop", ek_horse),
(troop_set_inventory_slot, ":troop", ek_horse, -1), #hopefully this won't **** up
(else_try), #mounted
(agent_get_item_id, ":horse_item", ":horse"),
(try_end),
(set_spawn_position, pos3),#distinguish from player hero's corpse
(set_player_troop, ":troop"),
(spawn_agent, ":troop"),
(assign, ":player_agent", reg0),
#parse inventory (thanks to agent_un/equip_item),
(troop_get_inventory_capacity, ":cap", ":troop"),
(try_for_range, ":slot", 0, ":cap"),
(troop_get_inventory_slot, ":equipment", ":troop", ":slot"),
(gt, ":equipment", 0),
(try_begin),
(agent_has_item_equipped, "$dmod_current_agent", ":equipment"),
(neg|agent_has_item_equipped, ":player_agent", ":equipment"),
(agent_equip_item, ":player_agent", ":equipment"),
(try_end),
(try_end),
(try_begin), #dismounted
(lt, ":horse", 0), #invalid agent
(troop_set_inventory_slot, ":troop", ek_horse, ":horse_item"), #restore mount?
(try_end), #mounted
(agent_fade_out, "$dmod_current_agent"),#doppleganger culled off
(party_remove_members, "p_ally_casualties", ":troop", 1), #not actually dead (yet) until you get killed again
#apply to player
(agent_set_look_target_position, ":player_agent", pos4),
(agent_set_wielded_item, ":player_agent", ":item"),
(agent_set_hit_points, ":player_agent", ":hp"),
(try_begin),
(gt, ":amount", 0),
(gt, ":ammo", -1),
(agent_set_ammo, ":player_agent", ":ammo", ":amount"),
(try_end),
(play_sound, "snd_hide"),#will probably get annoying
(agent_set_team, ":player_agent", ":player_team"),#otherwise backstabbed
(try_end),
(try_end),