<s> from lampost . context . resource import m_requires <EOL> from lampost . gameops . action import action_handler , ActionError <EOL> from lampost . lpflavor . fight import Fight <EOL> from lampost . lpflavor . attributes import need_refresh , POOL_KEYS <EOL> from lampost . lpflavor . combat import calc_consider <EOL> from lampost . model . entity import Entity <EOL> m_requires ( __name__ , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _post_init ( ) : <EOL> register ( '<STR_LIT>' , update_settings ) <EOL> def update_settings ( game_settings ) : <EOL> EntityLP . _refresh_interval = game_settings . get ( '<STR_LIT>' , <NUM_LIT:12> ) <EOL> EntityLP . _refresher [ '<STR_LIT>' ] = game_settings . get ( '<STR_LIT>' , <NUM_LIT:8> ) <EOL> EntityLP . _refresher [ '<STR_LIT>' ] = game_settings . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> EntityLP . _refresher [ '<STR_LIT>' ] = game_settings . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> EntityLP . _refresher [ '<STR_LIT:action>' ] = game_settings . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> class EntityLP ( Entity ) : <EOL> health = <NUM_LIT:0> <EOL> stamina = <NUM_LIT:0> <EOL> mental = <NUM_LIT:0> <EOL> action = <NUM_LIT:0> <EOL> auto_fight = True <EOL> weapon = None <EOL> last_opponent = None <EOL> _refresh_pulse = None <EOL> _current_action = None <EOL> _action_target = None <EOL> _next_command = None <EOL> _action_pulse = None <EOL> _refresher = { } <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> self . effects = set ( ) <EOL> self . defenses = set ( ) <EOL> self . equip_slots = { } <EOL> self . fight = Fight ( self ) <EOL> def baptise ( self ) : <EOL> super ( ) . baptise ( ) <EOL> for article in self . inven : <EOL> if article . current_slot : <EOL> self . _do_equip ( article , article . current_slot ) <EOL> self . fight . update_skills ( ) <EOL> def add_skill ( self , skill ) : <EOL> if skill . template_key in self . skills : <EOL> raise ActionError ( "<STR_LIT>" ) <EOL> self . skills [ skill . template_key ] = skill <EOL> self . _apply_skill ( skill ) <EOL> def _apply_skill ( self , skill ) : <EOL> if skill . auto_start : <EOL> skill . invoke ( self ) <EOL> else : <EOL> self . enhance_soul ( skill ) <EOL> try : <EOL> self . fight . update_skills ( ) <EOL> except AttributeError : <EOL> pass <EOL> def remove_skill ( self , skill_id ) : <EOL> try : <EOL> skill = self . skills . pop ( skill_id ) <EOL> if skill . auto_start : <EOL> skill . revoke ( self ) <EOL> else : <EOL> self . diminish_soul ( skill ) <EOL> self . fight . update_skills ( ) <EOL> except KeyError : <EOL> raise ActionError ( '<STR_LIT>' . format ( self . name ) ) <EOL> def check_costs ( self , costs ) : <EOL> for pool , cost in costs . items ( ) : <EOL> if getattr ( self , pool , <NUM_LIT:0> ) < cost : <EOL> raise ActionError ( "<STR_LIT>" ) <EOL> def apply_costs ( self , costs ) : <EOL> self . check_costs ( costs ) <EOL> for pool , cost in costs . items ( ) : <EOL> setattr ( self , pool , getattr ( self , pool ) - cost ) <EOL> self . check_status ( ) <EOL> def filter_actions ( self , matches ) : <EOL> if not self . _current_action : <EOL> return matches <EOL> return [ match for match in matches if not getattr ( match . action , '<STR_LIT>' , None ) ] <EOL> @ action_handler <EOL> def start_action ( self , action , act_args ) : <EOL> if hasattr ( action , '<STR_LIT>' ) : <EOL> if self . dead : <EOL> raise ActionError ( "<STR_LIT>" ) <EOL> action . prepare_action ( ** act_args ) <EOL> priority = - len ( self . followers ) <EOL> prep_time = getattr ( action , '<STR_LIT>' , None ) <EOL> if prep_time : <EOL> self . _current_action = action , act_args , register_once ( self . _finish_action , prep_time , priority = priority ) <EOL> self . _action_target = act_args . get ( '<STR_LIT:target>' , None ) <EOL> else : <EOL> super ( ) . process_action ( action , act_args ) <EOL> self . check_follow ( action , act_args ) <EOL> def handle_parse_error ( self , error , command ) : <EOL> if self . _current_action : <EOL> if self . _next_command : <EOL> self . display_line ( "<STR_LIT>" ) <EOL> else : <EOL> self . _next_command = command <EOL> else : <EOL> super ( ) . handle_parse_error ( error , command ) <EOL> @ action_handler <EOL> def _finish_action ( self ) : <EOL> action , action_args , action_pulse = self . _current_action <EOL> del self . _current_action <EOL> if self . _action_target : <EOL> del self . _action_target <EOL> super ( ) . process_action ( action , action_args ) <EOL> if self . _next_command : <EOL> self . parse ( self . _next_command ) <EOL> del self . _next_command <EOL> self . check_fight ( ) <EOL> def entity_leave_env ( self , entity , exit_action ) : <EOL> super ( ) . entity_leave_env ( entity , exit_action ) <EOL> if self . _current_action and self . _action_target == entity : <EOL> self . _cancel_actions ( ) <EOL> def start_refresh ( self ) : <EOL> if not self . _refresh_pulse and need_refresh ( self ) : <EOL> self . _refresh_pulse = register_p ( self . _refresh , pulses = self . _refresh_interval ) <EOL> def _refresh ( self ) : <EOL> if self . dead or not need_refresh ( self ) : <EOL> unregister ( self . _refresh_pulse ) <EOL> del self . _refresh_pulse <EOL> return <EOL> for pool_id , base_pool_id in POOL_KEYS : <EOL> new_value = getattr ( self , pool_id ) + self . _refresher [ pool_id ] <EOL> setattr ( self , pool_id , min ( new_value , getattr ( self , base_pool_id ) ) ) <EOL> self . status_change ( ) <EOL> self . check_fight ( ) <EOL> @ property <EOL> def weapon_type ( self ) : <EOL> if self . weapon : <EOL> return self . weapon . weapon_type <EOL> def attacked ( self , source , attack ) : <EOL> for defense in self . defenses : <EOL> defense . apply ( self , attack ) <EOL> if attack . adj_damage <= <NUM_LIT:0> or attack . adj_accuracy <= <NUM_LIT:0> : <EOL> if defense . success_map : <EOL> self . broadcast ( target = source , ** defense . success_map ) <EOL> else : <EOL> source . broadcast ( verb = attack . verb , target = self , ** attack . fail_map ) <EOL> return <EOL> source . broadcast ( verb = attack . verb , target = self , ** attack . success_map ) <EOL> current_pool = getattr ( self , attack . damage_pool ) <EOL> setattr ( self , attack . damage_pool , current_pool - attack . adj_damage ) <EOL> combat_log ( source , <EOL> lambda : '<STR_LIT>' . join ( [ '<STR_LIT>' , attack . damage_pool , '<STR_LIT>' , <EOL> str ( current_pool ) , '<STR_LIT>' , str ( current_pool - attack . adj_damage ) ] ) , <EOL> self ) <EOL> self . check_status ( ) <EOL> def start_combat ( self , source ) : <EOL> self . last_opponent = source <EOL> self . fight . add ( source ) <EOL> def check_fight ( self ) : <EOL> if self . auto_fight and not self . _current_action and not self . _next_command : <EOL> self . fight . select_action ( ) <EOL> def end_combat ( self , source , victory ) : <EOL> self . fight . end ( source , victory ) <EOL> if self . last_opponent == source : <EOL> del self . last_opponent <EOL> self . status_change ( ) <EOL> def equip_article ( self , article ) : <EOL> if article . art_type == '<STR_LIT>' and self . weapon : <EOL> self . remove_article ( self . weapon ) <EOL> equip_slot = self . _find_slot ( article . equip_slot ) <EOL> if self . _slot_filled ( equip_slot ) : <EOL> self . _remove_by_slot ( equip_slot ) <EOL> if self . _slot_filled ( equip_slot ) : <EOL> raise ActionError ( '<STR_LIT>' ) <EOL> self . _do_equip ( article , equip_slot ) <EOL> def remove_article ( self , article ) : <EOL> if article . equip_slot == '<STR_LIT>' : <EOL> del self . equip_slots [ '<STR_LIT>' ] <EOL> del self . equip_slots [ '<STR_LIT>' ] <EOL> else : <EOL> del self . equip_slots [ article . current_slot ] <EOL> article . current_slot = None <EOL> if article . art_type == '<STR_LIT>' : <EOL> self . weapon = None <EOL> article . on_removed ( self ) <EOL> def considered ( self , ** _ ) : <EOL> return calc_consider ( self ) <EOL> def _do_equip ( self , article , equip_slot ) : <EOL> self . equip_slots [ equip_slot ] = article <EOL> article . current_slot = equip_slot <EOL> article . on_equipped ( self ) <EOL> if article . art_type == '<STR_LIT>' : <EOL> self . weapon = article <EOL> def _find_slot ( self , equip_slot ) : <EOL> if equip_slot == '<STR_LIT>' : <EOL> if self . _slot_filled ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> elif equip_slot == '<STR_LIT>' : <EOL> if self . _slot_filled ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> elif equip_slot == '<STR_LIT>' : <EOL> if self . _find_slot ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> return equip_slot <EOL> def _slot_filled ( self , equip_slot ) : <EOL> if equip_slot == '<STR_LIT>' : <EOL> if self . equip_slots . get ( '<STR_LIT>' ) or self . equip_slots . get ( '<STR_LIT>' ) : <EOL> return None <EOL> return self . equip_slots . get ( equip_slot ) <EOL> def _remove_by_slot ( self , equip_slot ) : <EOL> if equip_slot == '<STR_LIT>' : <EOL> self . _remove_by_slot ( '<STR_LIT>' ) <EOL> self . _remove_by_slot ( '<STR_LIT>' ) <EOL> return <EOL> article = self . equip_slots . get ( equip_slot ) <EOL> if article : <EOL> self . remove_article ( article ) <EOL> def check_status ( self ) : <EOL> if self . health <= <NUM_LIT:0> : <EOL> self . _cancel_actions ( ) <EOL> self . fight . end_all ( ) <EOL> self . die ( ) <EOL> else : <EOL> self . start_refresh ( ) <EOL> self . status_change ( ) <EOL> try : <EOL> self . last_opponent . status_change ( ) <EOL> except AttributeError : <EOL> pass <EOL> def _cancel_actions ( self ) : <EOL> if self . _current_action : <EOL> unregister ( self . _current_action [ <NUM_LIT:2> ] ) <EOL> del self . _current_action <EOL> try : <EOL> del self . _action_target <EOL> except AttributeError : <EOL> pass <EOL> if self . _next_command : <EOL> del self . _next_command <EOL> def die ( self ) : <EOL> self . _death_effects ( ) <EOL> super ( ) . die ( ) <EOL> def _death_effects ( self ) : <EOL> self . status = '<STR_LIT>' <EOL> self . action = <NUM_LIT:0> <EOL> self . health = <NUM_LIT:0> <EOL> self . stamina = <NUM_LIT:0> <EOL> self . mental = <NUM_LIT:0> <EOL> def status_change ( self ) : <EOL> self . pulse_stamp = current_pulse ( ) <EOL> def on_detach ( self ) : <EOL> self . _cancel_actions ( ) <EOL> if self . _refresh_pulse : <EOL> del self . _refresh_pulse <EOL> @ property <EOL> def display_status ( self ) : <EOL> display_status = super ( ) . display_status <EOL> for pool_id , base_pool_id in POOL_KEYS : <EOL> display_status [ pool_id ] = getattr ( self , pool_id ) <EOL> display_status [ base_pool_id ] = getattr ( self , base_pool_id ) <EOL> return display_status <EOL> def combat_status ( self ) : <EOL> return '<STR_LIT>' . join ( [ '<STR_LIT>' , '<STR_LIT>' . join ( [ "<STR_LIT>" . format ( pool_id , getattr ( self , pool_id ) ) <EOL> for pool_id , _ in POOL_KEYS ] ) ] ) </s>
