-
Notifications
You must be signed in to change notification settings - Fork 11
/
language.py
212 lines (161 loc) · 4.82 KB
/
language.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from globals import *
import alife
import logging
import random
import os
def prettify_string_array(array, max_length):
"""Returns a human readable string from an array of strings."""
_string = ''
_i = 0
for entry in array:
if len(_string) > max_length:
_string += ', and %s more.' % (_i+1)
break
if _i == 0:
_string += entry
elif 0<_i<len(array)-1:
_string += ', %s' % entry
elif _i == len(array)-1:
_string += ' and %s.' % entry
_i += 1
return _string
def get_name(life):
return ' '.join(life['name'])
def get_real_direction(direction, short=False):
if abs(direction)<22 or abs(direction-360)<22:
if short:
return 'e'
return 'east'
elif abs(direction-45)<22:
if short:
return 'ne'
return 'northeast'
elif abs(direction-90)<22:
if short:
return 'n'
return 'north'
elif abs(direction-135)<22:
if short:
return 'nw'
return 'northwest'
elif abs(direction-180)<22:
if short:
return 'w'
return 'west'
elif abs(direction-225)<22:
if short:
return 'sw'
return 'southwest'
elif abs(direction-270)<22:
if short:
return 's'
return 'south'
elif abs(direction-315)<22:
if short:
return 'se'
return 'southeast'
else:
if short:
return 'e'
return 'east'
def get_real_distance(distance):
"""Returns the real-life representation of a distance."""
if SETTINGS['distance unit'] == 'Yards':
return distance*YARDS
else:
return distance*METERS
def get_real_distance_string(distance, round_up=False):
_distance = get_real_distance(distance)
_mods = ''
if round_up:
_distance = int(round(_distance))
if not _distance == 1:
_mods = 's'
if SETTINGS['distance unit'] == 'Yards':
return '%s yd%s' % (_distance, _mods)
return '%s m%s' % (_distance, _mods)
def get_name_ownership(life, pronoun=False):
if pronoun:
if life['type'] == 'humanoid':
return 'his'
else:
return 'its'
return '%s\'s' % ' '.join(life['name'])
def get_introduction(life, posession=False):
if 'player' in life:
if posession:
return 'Your'
return 'You'
if life['type'] == 'humanoid':
if posession:
return '%s\'s' % get_name(life)
else:
return get_name(life)
else:
#TODO: Check limb conditions
if posession:
return 'The %s\'s' % life['species']
else:
return 'The %s' % life['species']
def _load_strings(a, directory, filenames):
for filename in [f for f in filenames if f.count('.txt')]:
_map_name = filename.strip('.txt')
TEXT_MAP[_map_name] = []
with open(os.path.join(directory, filename), 'r') as e:
TEXT_MAP[_map_name].extend([line.strip() for line in e.readlines()])
def load_strings():
#TODO: Use better walk, like one in profiles.py
try:
os.path.walk(TEXT_DIR, _load_strings, None)
load_dialog()
except Exception, e:
raise Exception(e)
def load_dialog():
with open(os.path.join(TEXT_DIR, 'dialog.txt')) as f:
for line in f.readlines():
line = line.rstrip()
if not line or line.startswith('#'):
continue
try:
_gist, _requirements, _text, _result = line.split(':')
except:
raise Exception('Error in dialog (wrong number of arguments): %s' % line)
_dialog = {'gist': _gist,
'requirements': _requirements.split(','),
'text': _text,
'result': _result}
if _gist in DIALOG_TOPICS:
DIALOG_TOPICS[_gist].append(_dialog)
else:
DIALOG_TOPICS[_gist] = [_dialog]
logging.debug('Loaded dialog.')
def generate_place_name():
if not TEXT_MAP['places']:
return 'Zoolandia %s' % WORLD_INFO['ticks']
return TEXT_MAP['places'].pop(random.randint(0, len(TEXT_MAP['places'])-1))
def generate_scheme_title():
return TEXT_MAP['nouns'][random.randint(0, len(TEXT_MAP['nouns'])-1)]
def generate_first_and_last_name_from_species(species):
_map_first_names = '%s_first_names' % species
_map_last_names = '%s_last_names' % species
if not TEXT_MAP[_map_first_names] or not TEXT_MAP[_map_last_names]:
return ('Wayne', 'Brady')
_first_name = TEXT_MAP[_map_first_names].pop(random.randint(0, len(TEXT_MAP[_map_first_names])-1))
_last_name = TEXT_MAP[_map_last_names].pop(random.randint(0, len(TEXT_MAP[_map_last_names])-1))
return (_first_name, _last_name)
def format_injury(injury):
if injury['lodged_item']:
return 'a %s lodged in the %s' % (ITEMS[injury['lodged_item']]['name'], injury['limb'])
elif injury['artery_ruptured']:
return 'a ruptured artery in the %s' % injury['limb']
elif injury['cut']:
return 'a cut to the %s' % injury['limb']
return 'nothing in particular.'
def generate_memory_phrase(memory):
_details = [key for key in memory.keys() if not key == 'text']
_memory_age = WORLD_INFO['ticks']-memory['time_created']
_topic = memory['text']
if _topic == 'friendly':
return '%s seems like a good guy.' % (' '.join(LIFE[memory['target']]['name']))
else:
print 'DIDNT HAVE A PHRASE FOR',_topic