CRAW now runs on Windows 7 too - the problem was that Windows 7 has moved some functi...
[craw.git] / script / utility.py
blobd15293a62fef7d59d6d74ff83ce1c1469ff93354
1 import craw, os, sys, utility
3 def get_flag(flags, offset):
4 return ((flags >> offset) & 1) != 0
6 def draw_box(coordinate, colour):
7 x, y = coordinate
9 square_size = 3
11 coordinates = [
12 (- 1, 1),
13 (1, 1),
14 (1, -1),
15 (-1, -1),
16 (-1, 1)
19 for i in range(0, len(coordinates) - 1):
20 start_x, start_y = coordinates[i]
21 end_x, end_y = coordinates[i + 1]
23 start_x *= square_size
24 start_y *= square_size
26 end_x *= square_size
27 end_y *= square_size
29 craw.draw_line(x + start_x, y + start_y, x + end_x, y + end_y, colour)
31 def draw_automap_text(string, coordinate):
32 craw.draw_text(string, coordinate[0] - 5, coordinate[1] - 2, True)
34 def read_bytes(bytes, offset, size):
35 output = 0
36 for i in range(1, size + 1):
37 output <<= 8
38 output |= bytes[offset + size - i]
39 return output
41 def pack_number(number, size):
42 output = ''
43 for i in range(0, size):
44 output += chr(number & 0xff)
45 number >>= 8
46 return output
48 def get_packet_string(packet):
49 output = ''
51 if len(packet) == 0:
52 return output
54 if type(packet[0]) != str:
55 packet = map(chr, packet)
57 first = True
58 for byte in packet:
59 if first:
60 first = False
61 else:
62 output += ' '
63 output += '%02x' % ord(byte)
64 return output
66 def town_check():
67 level = craw.get_player_level()
68 if level == None:
69 return None
70 return level in [0x01, 0x28, 0x4b, 0x67, 0x6d]
72 def get_configuration_directory():
73 return os.path.join(sys.path[-1], '..', 'configuration')
75 def read_name(bytes, offset):
76 output = ''
77 i = 0
78 while i < 16:
79 byte = bytes[offset]
80 if byte == 0:
81 return output
82 output += chr(byte)
83 i += 1
84 offset += 1
85 return output
87 def get_character_class_string(character_class):
88 strings = [
89 'Amazon',
90 'Sorceress',
91 'Necromancer',
92 'Paladin',
93 'Barbarian',
94 'Druid',
95 'Assassin'
98 return strings[character_class]
100 def get_character_string(character):
101 return 'Level %d %s' % (character.level, get_character_class_string(character.character_class))
103 def get_player_data_by_name(name):
104 players = craw.get_players()
105 for player in players:
106 if player.name == name:
107 return player
108 return None
110 def get_my_name():
111 players = craw.get_players()
112 if len(players) == 0:
113 return None
114 player = players[0]
115 return player.name
117 def get_player_by_name(name):
118 players = craw.get_players()
119 for player in players:
120 if player.name == name:
121 return player
122 return None
124 def get_player_by_id(player_id):
125 players = craw.get_players()
126 for player in players:
127 if player.id == player_id:
128 return player
129 return None
131 def distance(coordinate1, coordinate2):
132 return ((coordinate1[0] - coordinate2[0]) ** 2 + (coordinate1[1] - coordinate2[1]) ** 2) ** 0.5
134 def get_my_player():
135 players = craw.get_players()
136 if players == None or len(players) == 0:
137 return None
138 return players[0]
140 mercenary_map = {
141 0x010f: 1,
142 0x0152: 2,
143 0x0167: 3,
144 0x0231: 5
147 def is_mercenary(id):
148 return id in mercenary_map
150 def same_party(player1, player2):
151 not_in_party = 0x0000ffff
152 return not_in_party not in [player1.party, player2.party] and player1.party == player2.party
154 def get_party_players():
155 my_player = get_my_player()
156 if my_player == None:
157 return None
159 players = filter(lambda x: x.id != my_player.id and same_party(my_player, x), craw.get_players())
160 return players
162 def get_d2_distance(player1, player2):
163 dx = abs(player1.x - player2.x)
164 dy = abs(player1.y - player2.y)
165 return (dx + dy + max(dx, dy)) / 2.0
167 def get_euclidean_distance(player1, player2):
168 dx = player1.x - player2.x
169 dy = player1.y - player2.y
171 return (dx**2 + dy**2)**0.5
173 def sort_units(left, right):
174 difference = left[0] - right[0]
175 if difference == 0:
176 return 0
177 return 1 if difference > 0 else -1
179 def get_my_unit():
180 player = get_my_player()
181 if player == None:
182 return None
183 return craw.get_unit(player.id, 0)