1 import craw
, utility
, configuration
, time
3 class chicken_handler_class
:
5 self
.damage_handlers
= []
6 self
.hostility_handlers
= []
7 self
.last_maximal_life
= None
9 def life_check(self
, bytes
):
13 configuration
.chicken_data
= craw
.get_life()
14 if configuration
.chicken_data
== None:
17 current_life
, maximal_life
= configuration
.chicken_data
19 if maximal_life
> 0 and maximal_life
< self
.last_maximal_life
and self
.last_maximal_life
!= None:
20 craw
.print_text('Warning: Maximal life decreased to %d' % maximal_life
)
22 self
.last_maximal_life
= maximal_life
24 if bytes
[0] not in [0x18, 0x95]:
27 new_life
= utility
.read_bytes(bytes
, 1, 2) & 0x7fff
28 if new_life
>= current_life
:
31 damage
= current_life
- new_life
32 ratio
= float(new_life
) / maximal_life
33 percent
= '%.2f%%' % (ratio
* 100)
35 if new_life
== maximal_life
:
36 #hack for a strange bug
40 print '%d damage, %d/%d left (%s)' % (damage
, new_life
, maximal_life
, percent
)
41 for damage_handler
in self
.damage_handlers
:
42 damage_handler(damage
, (new_life
, maximal_life
))
48 in_town
= utility
.town_check()
53 if not in_town
and configuration
.chicken
and ratio
<= configuration
.chicken_ratio
:
54 print 'Leaving the game because the chicken life ratio has been reached'
57 def add_damage_handler(self
, handler
):
58 self
.damage_handlers
.append(handler
)
60 def add_hostility_handler(self
, handler
):
61 self
.hostility_handlers
.append(handler
)
63 def hostility_check(self
, bytes
):
64 if len(bytes
) < 10 or bytes
[0] != 0x8c:
67 player_id_1
= utility
.read_bytes(bytes
, 1, 4)
68 player_id_2
= utility
.read_bytes(bytes
, 5, 4)
70 my_id
= craw
.get_player_id()
74 if player_id_1
!= my_id
:
80 for hostility_handler
in self
.hostility_handlers
:
83 in_town
= utility
.town_check()
88 if not in_town
and configuration
.chicken_on_hostile
:
89 player_name
= craw
.get_name_by_id(player_id_2
)
90 if player_name
== None:
91 print 'Leaving the game because an unknown player has declared hostility against us'
93 print 'Leaving the game because player %s has declared hostility against us' % player_name
96 def process_bytes(self
, bytes
):
97 self
.life_check(bytes
)
98 self
.hostility_check(bytes
)