This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from jav/pybotwar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
440 lines (366 loc) · 12.7 KB
/
settings.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright 2009-2014 Lee Harr
#
# This file is part of pybotwar.
# http://pybotwar.googlecode.com/
#
# Pybotwar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pybotwar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pybotwar. If not, see <http://www.gnu.org/licenses/>.
import os
from PyQt4 import QtCore, QtGui, uic
import stats
import conf
uidir = 'data/ui'
ss = dict(
# Robot subprocess
subprocess = dict(subproc_python = 's+',
subproc_main = 's+',
init_timeout = 'f',
tick_timeout = 'f',
),
# Files
files = dict(base_dir = 's+',
more_robot_dirs = 'sa+',
logdir = 's+',
template = 's+X',
lineups = 's+',
dbfile = 's+',
),
# Physics
physics = dict(pybox2d_version = 's+X',
## robot
maxforce = 'f+',
maxtorque = 'f+',
robot_density = 'f+',
robot_linearDamping = 'f+',
robot_angularDamping = 'f+',
robot_friction = 'f+',
robot_restitution = 'f+',
## cannon
cannon_reload_ticks = 'i+',
cannon_maxheat = 'i+',
cannon_heating_per_shot = 'i+',
cannon_cooling_per_tick = 'f+',
overheat_fire_reload_penalty = 'i',
unloaded_fire_reload_penalty = 'i',
## turret
turret_density = 'f+',
turret_maxMotorTorque = 'f+',
turret_maxMotorSpeed = 'f+',
## bullet
bulletspeed = 'f+',
bullet_density = 'f+',
),
# Rules
rules = dict(
## game end
maxtime = 'i',
maxhealth = 'i',
remove_dead_robots = 'b+',
## damage
direct_hit_damage = 'i',
explosion_radii = 'fa+3',
explosion_damage = 'ia+3',
collision_damage_start = 'i',
collision_damage_factor = 'f',
## cannon
cannon_reload_ticks = 'i',
cannon_maxheat = 'i',
cannon_heating_per_shot = 'i',
cannon_cooling_per_tick = 'f',
overheat_fire_reload_penalty = 'i',
unloaded_fire_reload_penalty = 'i',
),
)
def setup_qt_settings():
QtCore.QCoreApplication.setOrganizationName('pybotwar.googlecode.com')
QtCore.QCoreApplication.setOrganizationDomain('pybotwar.googlecode.com')
QtCore.QCoreApplication.setApplicationName('pybotwar')
settings = QtCore.QSettings()
settings.sync()
setup_sections()
def setup_sections():
for section, items in ss.items():
setup_section(section, items)
def setup_section(section, items):
'''read settings from QSettings and store in conf module'''
for name, kind in items.items():
setup_one(section, name, kind)
def read_value(setting, kind):
ok = True
if kind.startswith('s'):
v = str(setting.toString())
elif kind.startswith('i'):
v, ok = setting.toInt()
elif kind.startswith('f'):
v, ok = setting.toFloat()
elif kind.startswith('b'):
v = setting.toBool()
#print 'R', name, kind, v, ok
return v, ok
def read_array(k, name, kind):
'read an array-type from QSettings and return it as a list'
settings = QtCore.QSettings()
ok = True
v = []
for n in range(settings.beginReadArray(k)):
settings.setArrayIndex(n)
setting = settings.value(name)
vi, ok = read_value(setting, kind)
if not ok:
#print '++', name, n, vi
break
v.append(vi)
settings.endArray()
ns = kind[-1]
if ns.isdigit():
n = int(ns)
v = v[:n]
return v
def setup_one(section, name, kind):
settings = QtCore.QSettings()
k = '%s/%s' % (section, name)
ok = True
if 'a' in kind:
v = read_array(k, name, kind)
else:
setting = settings.value(k)
v, ok = read_value(setting, kind)
if '+' in kind and not v:
ok = False
if not ok:
#print '!!', name,
v = getattr(conf, name)
#print 'loading', v
if 'a' not in kind:
settings.setValue(k, v)
else:
settings.beginWriteArray(k)
for n in range(len(v)):
settings.setArrayIndex(n)
vi = v[n]
settings.setValue(name, vi)
settings.endArray()
else:
#print 'ok %s = %s' % (name, v)
setattr(conf, name, v)
def save_robots(robots):
settings = QtCore.QSettings()
settings.beginWriteArray('files/robots')
for i, val in enumerate(robots):
settings.setArrayIndex(i)
settings.setValue('robot', val)
settings.endArray()
def load_robots():
settings = QtCore.QSettings()
robots = []
for n in range(settings.beginReadArray('files/robots')):
settings.setArrayIndex(n)
setting = settings.value('robot')
v = str(setting.toString())
robots.append(v)
if robots:
conf.robots = robots
class Settings(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
uifile = 'settings.ui'
uipath = os.path.join(uidir, uifile)
SClass, _ = uic.loadUiType(uipath)
self.ui = SClass()
self.ui.setupUi(self)
self.load_current()
def load_current(self):
'read settings from QSettings and store in settings form'
settings = QtCore.QSettings()
self.settings = settings
for section, items in ss.items():
self.load_section(section, items)
def load_section(self, section, items):
for name, kind in items.items():
self.load_one(section, name, kind)
def load_var(self, var, kind, val):
if 'X' in kind:
return
o = getattr(self.ui, var)
if 'i' in kind:
o.setValue(val)
elif 'b' in kind:
o.setChecked(val)
elif 'f' in kind:
if val == int(val):
o.setText(str(int(val)))
else:
sval = '%.3f' % val
o.setText(sval)
else:
o.setText(val)
def load_one(self, section, name, kind):
settings = self.settings
k = '%s/%s' % (section, name)
#print 'O', k,
if 'a' not in kind:
setting = settings.value(k)
val, ok = read_value(setting, kind)
if not ok:
val = getattr(conf, var)
#print val
#else:
#print 'ok'
self.load_var(name, kind, val)
else:
v = read_array(k, name, kind)
#print k, v
for n, val in enumerate(v):
var = name + str(n+1)
self.load_var(var, kind, val)
def browse(self):
d = QtGui.QFileDialog.getExistingDirectory(
self,
'Set Robot dir',
self.robotdir.text())
self.robotdir.setText(d)
def accept(self):
self.set_current()
QtGui.QDialog.accept(self)
def set_current(self):
for section, items in ss.items():
self.set_section(section, items)
def set_section(self, section, items):
for name, kind in items.items():
self.set_one(section, name, kind)
def get_from_form(self, var, kind):
if 'X' in kind:
return None
o = getattr(self.ui, var)
if 'i' in kind:
retval = o.value()
elif 'b' in kind:
retval = o.isChecked()
elif 'f' in kind:
retval = float(str(o.text()))
else:
retval = str(o.text())
return retval
def set_one(self, section, name, kind):
if 'X' in kind:
return
settings = self.settings
k = '%s/%s' % (section, name)
#print 'S', k
if 'a' not in kind:
oldval = getattr(conf, name)
val = self.get_from_form(name, kind)
settings.setValue(k, val)
setattr(conf, name, val)
if val != oldval:
special = '_changed_%s' % name
if hasattr(self, special):
getattr(self, special)(val, oldval)
else:
matching = []
for e in dir(self.ui):
if e.endswith('_browse'):
continue
if e.startswith(name):
matching.append(e)
vals = []
settings.beginWriteArray(k)
for i, e in enumerate(matching):
settings.setArrayIndex(i)
val = self.get_from_form(e, kind)
settings.setValue(name, val)
vals.append(val)
#print 'A', e, val
settings.endArray()
setattr(conf, name, vals)
def browse_for_directory(self):
button = str(self.sender().objectName())
#print 'browsing directory', button
base = button[:-7] # remove _browse
txt = str(getattr(self.ui, base).text())
if base == 'base_dir':
if os.path.isabs(conf.base_dir):
curdir, _ = os.path.split(conf.base_dir)
else:
curdir = conf.base_dir
else:
curdir = conf.base_dir
#print 'CD', os.getcwd(), curdir, os.path.exists(curdir)
curdir = os.path.abspath(curdir)
dirpath = QtGui.QFileDialog.getExistingDirectory(
self,
'Choose folder',
curdir)
dirpath = str(dirpath)
base_dir_abs = os.path.abspath(conf.base_dir)
common = os.path.commonprefix((dirpath, base_dir_abs))
#print 'C', common
if common == base_dir_abs:
dirpath = os.path.relpath(dirpath, base_dir_abs)
#print 'CC', dirpath
getattr(self.ui, base).setText(dirpath)
def browse_for_file(self):
button = str(self.sender().objectName())
#print 'browsing file', button
base = button[:-7] # remove _browse
txt = str(getattr(self.ui, base).text())
d, f = os.path.split(txt)
if os.path.isabs(txt):
fdir, fname = d, f
else:
fdir = os.path.join(conf.base_dir, d)
fdir = os.path.abspath(fdir)
fpath = QtGui.QFileDialog.getOpenFileName(
self,
'Choose file',
fdir)
fpath = str(fpath)
base_dir_abs = os.path.abspath(conf.base_dir)
common = os.path.commonprefix((fpath, base_dir_abs))
#print 'C', common
if common == base_dir_abs:
fpath = os.path.relpath(fpath, base_dir_abs)
#print 'CC', fpath
getattr(self.ui, base).setText(fpath)
def _changed_base_dir(self, val, oldval):
if not os.path.exists(val):
os.mkdir(val)
stats.dbclose(restart=True)
if not stats.dbcheck():
QtGui.QMessageBox.warning(self, 'DB Version Mismatch', 'Database version mismatch.')
stats.dbopen()
import shutil
#print 'ONV', oldval, conf.template, val
scriptdir = os.path.dirname(os.path.abspath(__file__))
robotsdir = os.path.join(scriptdir, 'robots')
template = os.path.join(robotsdir, conf.template)
shutil.copy(template, val)
examples = os.path.join(robotsdir, 'examples')
shutil.copytree(examples, os.path.join(val, 'examples'))
def _changed_dbfile(self, val, oldval):
stats.dbclose(restart=True)
if not stats.dbcheck():
QtGui.QMessageBox.warning(self, 'DB Version Mismatch', 'Database version mismatch.\nUpgrade database with -D switch.\n\nReverted to previous value.')
conf.dbfile = oldval
self.settings.setValue('files/dbfile', oldval)
stats.dbclose(restart=True)
stats.dbcheck()
stats.dbopen()
def reject(self):
QtGui.QDialog.reject(self)
def reset_to_defaults(self):
settings = QtCore.QSettings()
settings.clear()
reload(conf)
setup_sections()
self.load_current()