forked from theonlydude/RandomMetroidSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
222 lines (191 loc) · 7.4 KB
/
utils.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
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python
import os, json, random
from parameters import Knows, Settings, Controller, isKnows, isSettings, isButton
from parameters import easy, medium, hard, harder, hardcore, mania
from smbool import SMBool
# gauss random in [0, r] range
# the higher the slope, the less probable extreme values are.
def randGaussBounds(r, slope=5):
r = float(r)
n = int(round(random.gauss(r/2, r/slope), 0))
if n < 0:
n = 0
if n > r:
n = int(r)
return n
# from a relative weight dictionary, gives a normalized range dictionary
# example :
# { 'a' : 10, 'b' : 17, 'c' : 3 } => {'c': 0.1, 'a':0.4333333, 'b':1 }
def getRangeDict(weightDict):
total = float(sum(weightDict.values()))
rangeDict = {}
current = 0.0
for k in sorted(weightDict, key=weightDict.get):
w = float(weightDict[k]) / total
current += w
rangeDict[k] = current
return rangeDict
def chooseFromRange(rangeDict):
r = random.random()
val = None
for v in sorted(rangeDict, key=rangeDict.get):
val = v
if r < rangeDict[v]:
return v
return val
class PresetLoader(object):
@staticmethod
def factory(params):
# can be a json, a python file or a dict with the parameters
if type(params) in [str, unicode]:
ext = os.path.splitext(params)
if ext[1].lower() == '.json':
return PresetLoaderJson(params)
else:
raise Exception("PresetLoader: wrong parameters file type: {}".format(ext[1]))
elif type(params) is dict:
return PresetLoaderDict(params)
else:
raise Exception("wrong parameters input, is neither a string nor a json file name: {}::{}".format(params, type(params)))
def __init__(self):
if 'Knows' not in self.params:
self.params['Knows'] = {}
if 'Settings' not in self.params:
self.params['Settings'] = {}
if 'Controller' not in self.params:
self.params['Controller'] = {}
self.params['score'] = self.computeScore()
def load(self):
# update the parameters in the parameters classes: Knows, Settings
# Knows
for param in self.params['Knows']:
if isKnows(param) and hasattr(Knows, param):
setattr(Knows, param, SMBool(self.params['Knows'][param][0],
self.params['Knows'][param][1],
['{}'.format(param)]))
# Settings
## hard rooms
for hardRoom in ['X-Ray', 'Gauntlet']:
if hardRoom in self.params['Settings']:
Settings.hardRooms[hardRoom] = Settings.hardRoomsPresets[hardRoom][self.params['Settings'][hardRoom]]
## bosses
for boss in ['Kraid', 'Phantoon', 'Draygon', 'Ridley', 'MotherBrain']:
if boss in self.params['Settings']:
Settings.bossesDifficulty[boss] = Settings.bossesDifficultyPresets[boss][self.params['Settings'][boss]]
## hellruns
for hellRun in ['Ice', 'MainUpperNorfair', 'LowerNorfair']:
if hellRun in self.params['Settings']:
Settings.hellRuns[hellRun] = Settings.hellRunPresets[hellRun][self.params['Settings'][hellRun]]
# Controller
for button in self.params['Controller']:
if isButton(button):
setattr(Controller, button, self.params['Controller'][button])
def dump(self, fileName):
with open(fileName, 'w') as jsonFile:
json.dump(self.params, jsonFile)
def printToScreen(self):
print("self.params: {}".format(self.params))
print("loaded knows: ")
for knows in Knows.__dict__:
if isKnows(knows):
print("{}: {}".format(knows, Knows.__dict__[knows]))
print("loaded settings:")
for setting in Settings.__dict__:
if isSettings(setting):
print("{}: {}".format(setting, Settings.__dict__[setting]))
print("loaded controller:")
for button in Controller.__dict__:
if isButton(button):
print("{}: {}".format(button, Controller.__dict__[button]))
print("loaded score: {}".format(self.params['score']))
def computeScore(self):
# the more techniques you know and the smaller the difficulty of the techniques, the higher the score
diff2score = {
easy: 6,
medium: 5,
hard: 4,
harder: 3,
hardcore: 2,
mania: 1
}
boss2score = {
"He's annoying": 1,
'A lot of trouble': 1,
"I'm scared!": 1,
"It can get ugly": 1,
'Default': 2,
'Quick Kill': 3,
'Used to it': 3,
'Is this really the last boss?': 3,
'No problemo': 4,
'Piece of cake': 4,
'Nice cutscene bro': 4
}
hellrun2score = {
'No thanks': 0,
'Solution': 0,
'Gimme energy': 2,
'Default': 4,
'Bring the heat': 6,
'I run RBO': 8
}
hellrunLN2score = {
'Default': 0,
'Solution': 0,
'Bring the heat': 6,
'I run RBO': 12
}
xray2score = {
'Aarghh': 0,
'Solution': 0,
"I don't like spikes": 1,
'Default': 2,
"I don't mind spikes": 3,
'D-Boost master': 4
}
gauntlet2score = {
'Aarghh': 0,
"I don't like acid": 1,
'Default': 2
}
score = 0
# knows
for know in Knows.__dict__:
if isKnows(know):
if know in self.params['Knows']:
if self.params['Knows'][know][0] == True:
score += diff2score[self.params['Knows'][know][1]]
else:
# if old preset with not all the knows, use default values for the know
if Knows.__dict__[know][0] == True:
score += diff2score[Knows.__dict__[know][1]]
# hard rooms
hardRoom = 'X-Ray'
if hardRoom in self.params['Settings']:
score += xray2score[self.params['Settings'][hardRoom]]
hardRoom = 'Gauntlet'
if hardRoom in self.params['Settings']:
score += gauntlet2score[self.params['Settings'][hardRoom]]
# bosses
for boss in ['Kraid', 'Phantoon', 'Draygon', 'Ridley', 'MotherBrain']:
if boss in self.params['Settings']:
score += boss2score[self.params['Settings'][boss]]
# hellruns
for hellRun in ['Ice', 'MainUpperNorfair']:
if hellRun in self.params['Settings']:
score += hellrun2score[self.params['Settings'][hellRun]]
hellRun = 'LowerNorfair'
if hellRun in self.params['Settings']:
score += hellrunLN2score[self.params['Settings'][hellRun]]
return score
class PresetLoaderJson(PresetLoader):
# when called from the test suite
def __init__(self, jsonFileName):
with open(jsonFileName) as jsonFile:
self.params = json.load(jsonFile)
super(PresetLoaderJson, self).__init__()
class PresetLoaderDict(PresetLoader):
# when called from the website
def __init__(self, params):
self.params = params
super(PresetLoaderDict, self).__init__()