CRAW now runs on Windows 7 too - the problem was that Windows 7 has moved some functi...
[craw.git] / script / chicken.py
blob6ab3d93ec79917e0d7e253dcac0178638e0b35bc
1 import craw, utility, configuration, time
3 class chicken_handler_class:
4 def __init__(self):
5 self.damage_handlers = []
6 self.hostility_handlers = []
7 self.last_maximal_life = None
9 def life_check(self, bytes):
10 if len(bytes) < 3:
11 return
13 configuration.chicken_data = craw.get_life()
14 if configuration.chicken_data == None:
15 return
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]:
25 return
27 new_life = utility.read_bytes(bytes, 1, 2) & 0x7fff
28 if new_life >= current_life:
29 return
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
37 return
39 #print time.time()
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))
44 if new_life <= 0:
45 print 'I am dead.'
46 return
48 in_town = utility.town_check()
50 if in_town == None:
51 return
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'
55 craw.leave_game()
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:
65 return
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()
71 if my_id == None:
72 return
74 if player_id_1 != my_id:
75 return
77 if bytes[9] < 0x08:
78 return
80 for hostility_handler in self.hostility_handlers:
81 hostility_handler()
83 in_town = utility.town_check()
85 if in_town == None:
86 return
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'
92 else:
93 print 'Leaving the game because player %s has declared hostility against us' % player_name
94 craw.leave_game()
96 def process_bytes(self, bytes):
97 self.life_check(bytes)
98 self.hostility_check(bytes)