-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdogma.py
453 lines (355 loc) · 13.7 KB
/
dogma.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
441
442
443
444
445
446
447
448
449
450
451
452
453
from ctypes import *
# TODO: cross-platform support
libdogma = cdll.LoadLibrary("libdogma.so")
libdogma.dogma_init()
OK = 0
NOT_FOUND = 1
NOT_APPLICABLE = 2
class DogmaException(Exception):
pass
class NotFoundException(DogmaException):
pass
class NotApplicableException(DogmaException):
pass
# datatypes
array_t = pointer # to void
key_t = c_int # XXX check this, it's a Word_t
typeid_t = c_uint32
attributeid_t = c_uint16
effectid_t = c_int32
# XXX watch out enum
class LocationType(object):
CHAR = 1
IMPLANT = 2
SKILL = 3
SHIP = 4
MODULE = 5
CHARGE = 6
DRONE = 7
class LocationUnion(Union):
_fields_ = [("implant_index", key_t),
("module_index", key_t),
("skill_typeid", typeid_t),
("drone_typeid", typeid_t)]
class Location(Structure):
_fields_ = [("type", c_int),
("union", LocationUnion)]
@property
def implant_index(self):
return self.union.implant_index
@property
def module_index(self):
return self.union.module_index
@property
def skill_typeid(self):
return self.union.skill_typeid
@property
def drone_typeid(self):
return self.union.drone_typeid
# XXX watch out enum
class State(object):
UNPLUGGED = 0
OFFLINE = 1
ONLINE = 17
ACTIVE = 31
OVERLOADED = 63
state_t = c_int
# dogma-extra.h datatypes
AFFECTOR_PENALIZED = (1 << 0)
AFFECTOR_SINGLETON = (1 << 1)
class SimpleAffector(Structure):
_fields_ = [("id", typeid_t),
("destid", attributeid_t),
("value", c_double),
("operator", c_char),
("order", c_uint8),
("flags", c_uint8)]
class FinalizableSizeablePointer(POINTER(c_void_p)):
def __init__(self, *args, **kw):
super(FinalizablePointer, self).__init__(*args, **kw)
self.finalizer = None
self.size = None
def __finalize__(self):
if self.finalizer:
self.finalizer(self)
super(FinalizablePointer, self).__finalize__()
def __len__(self):
if self.size is not None:
return self.size
return NotImplemented
class SimpleCapacitorUnion(Union):
_fields_ = [("stable_fraction", c_double),
("depletion_time", c_double)]
class SimpleCapacitor(Structure):
_fields_ = [("context", POINTER(c_void_p)),
("capacity", c_double),
("delta", c_double),
("stable", c_bool),
("union", SimpleCapacitorUnion)]
@property
def stable_fraction(self):
return self.union.stable_fraction
@property
def depletion_time(self):
return self.union.depletion_time
def _(x): return x
def accept_or_cast(typ, val):
if isinstance(typ, type) and isinstance(val, typ):
return val
else:
return typ(val)
def sig(*types):
def wrap(f):
def fprime(*values):
casted = [accept_or_cast(typ, val) for typ, val in zip(types, values)]
return f(*casted)
return fprime
return wrap
def chk(ret):
if ret == OK:
return
elif ret == NOT_FOUND:
raise NotFoundException
elif ret == NOT_APPLICABLE:
raise NotApplicableException
raise AssertionError("impossible return value %r" % ret)
context_t = POINTER(c_void_p)
class Context(object):
def __init__(self):
self._as_parameter_ = context_t()
self.targetees_by_location = dict()
self.targeters = set()
chk(libdogma.dogma_init_context(byref(self._as_parameter_)))
def __finalize__(self):
chk(libdogma.dogma_free_context(self))
@sig(_, typeid_t)
def add_implant(self, implant):
slot = key_t()
chk(libdogma.dogma_add_implant(self, implant, byref(slot)))
return slot.value
@sig(_, key_t)
def remove_implant(self, slot):
chk(libdogma.dogma_remove_implant(key_t(slot)))
@sig(_, c_uint8)
def set_default_skill_level(self, level):
chk(libdogma.dogma_set_default_skill_level(self, level))
@sig(_, typeid_t, c_uint8)
def set_skill_level(self, skill, level):
chk(libdogma.dogma_set_default_skill_level(self, skill, level))
@sig(_, typeid_t)
def reset_skill_level(self, skill):
chk(libdogma.dogma_reset_default_skill_level(self, skill))
@sig(_, typeid_t)
def set_ship(self, ship):
chk(libdogma.dogma_set_ship(self, ship))
def add_module(self, module, state=None, charge=None):
accept_or_cast(typeid_t, module)
if state:
accept_or_cast(state_t, state)
if charge:
accept_or_cast(typeid_t, charge)
slot = key_t()
if state is None and charge is None:
chk(libdogma.dogma_add_module(self, module, byref(slot)))
elif charge is None:
chk(libdogma.dogma_add_module_s(self, module, byref(slot), state))
elif state is None:
chk(libdogma.dogma_add_module_c(self, module, byref(slot),
charge))
else:
chk(libdogma.dogma_add_module_sc(self, module, byref(slot), state,
charge))
return slot.value
@sig(_, key_t)
def remove_module(self, slot):
chk(libdogma.dogma_remove_module(self, slot))
@sig(_, key_t, state_t)
def set_module_state(self, slot, state):
chk(libdogma.dogma_set_module_state(self, slot, state))
@sig(_, key_t, typeid_t)
def add_charge(self, slot, charge):
chk(libdogma.dogma_add_charge(self, slot, charge))
@sig(_, key_t)
def remove_charge(self, slot):
chk(libdogma.dogma_remove_charge(self, slot))
@sig(_, typeid_t, c_uint)
def add_drone(self, drone, count):
chk(libdogma.dogma_add_drone(self, drone, count))
@sig(_, typeid_t, c_uint)
def remove_drone_partial(self, drone, count):
chk(libdogma.dogma_remove_drone_partial(self, drone, c_uint(count)))
@sig(_, typeid_t)
def remove_drone(self, drone):
chk(libdogma.dogma_remove_drone(self, drone))
@sig(_, Location, effectid_t, c_bool)
def toggle_chance_based_effect(self, location, effect, on):
chk(libdogma.dogma_toggle_chance_based_effect(self, location, effect,
on))
@sig(_, Location, _)
def target(self, location, targetee):
self.targets_by_location[targetee]
targetee.targeters.add(self)
chk(libdogma.dogma_target(self, location, targetee))
@sig(_, Location)
def clear_target(self, location):
targetee = self.targets_by_location(location)
targetee.targeters.remove(self)
chk(libdogma.dogma_clear_target(self, location))
@sig(_, Location, attributeid_t)
def get_location_attribute(self, location, attribute):
result = c_double()
chk(libdogma.dogma_get_location_attribute(self, location, attribute,
byref(value)))
return result.value
@sig(_, attributeid_t)
def get_character_attribute(self, attribute):
result = c_double()
chk(libdogma.dogma_get_character_attribute(self, attribute,
byref(result)))
return result.value
@sig(_, key_t, attributeid_t)
def get_implant_attribute(self, implant, attribute):
result = c_double()
chk(libdogma.dogma_get_implant_attribute(self, implant, attribute,
byref(result)))
return result.value
@sig(_, typeid_t, attributeid_t)
def get_skill_attribute(self, skill, attribute):
result = c_double()
chk(libdogma.dogma_get_skill_attribute(self, skill, attribute,
byref(result)))
return result.value
@sig(_, attributeid_t)
def get_ship_attribute(self, attribute):
result = c_double()
chk(libdogma.dogma_get_ship_attribute(self, attribute, byref(result)))
return result.value
@sig(_, key_t, attributeid_t)
def get_module_attribute(self, module, attribute):
result = c_double()
chk(libdogma.dogma_get_module_attribute(self, module, attribute,
byref(result)))
return result.value
@sig(_, key_t, attributeid_t)
def get_charge_attribute(self, charge, attribute):
result = c_double()
chk(libdogma.dogma_get_charge_attribute(self, charge, attribute,
byref(result)))
return result.value
@sig(_, typeid_t, attributeid_t)
def get_drone_attribute(self, drone, attribute):
result = c_double()
chk(libdogma.dogma_get_drone_attribute(self, drone, attribute,
byref(result)))
return result.value
@sig(_, Location, effectid_t)
def get_chance_based_effect_chance(self, location, effect):
result = c_double()
chk(libdogma.dogma_get_chance_based_effect_chance(self, drone,
attribute,
byref(result)))
return result.value
@sig(_, Location)
def get_affectors(self, location):
affectors = FinalizableSizeablePointer(c_void_p())
affectors.finalizer = lambda ptr: libdogma.dogma_free_affector_list(ptr)
size = c_size_t
chk(libdogma.dogma_get_affectors(self, location, byref(affectors),
byref(size)))
affectors.size = size.value
return affectors
@sig(_, c_bool)
def get_capacitor_all(self, include_reload_time):
capacitor = POINTER(SimpleCapacitor)()
size = c_size_t()
chk(libdogma.dogma_get_capacitor_all(self, include_reload_time,
byref(capacitor), byref(size)))
capacitors_by_context = {}
relevant_contexts = self.targeters.union(set(self.targetees_by_location))
relevant_contexts.add(self)
for i in range(size.value):
found = False
for context in relevant_contexts:
if (addressof(context._as_parameter_.contents) ==
addressof(capacitor[i].context.contents)):
assert not found
found = True
capacitors_by_context[context] = capacitor[i]
print capacitor[i].stable
print capacitor[i].union.stable_fraction
print capacitor[i].union.depletion_time
#break
assert found
libdogma.dogma_free_capacitor_list(capacitor)
return capacitors_by_context
@sig(_, Location, effectid_t)
def get_location_effect_attributes(self, location, effect):
duration = c_double()
trackingspeed = c_double()
discharge = c_double()
range = c_double()
falloff = c_double()
fittingusagechance = c_double()
chk(libdogma.dogma_get_location_effect_attributes(
self, location, effect,
duration, trackingspeed, discharge,
range, falloff, fittingusagechance))
return (duration.value, trackingspeed.value, discharge.value,
range.value, falloff.value, fittingusagechance.value)
class FleetContext(object):
def __init__(self):
self._as_parameter_ = pointer(None)
chk(libdogma.dogma_init_fleet_context(byref(self._as_parameter_)))
def __finalize__(self):
chk(libdogma.dogma_free_fleet_context(self))
@sig(_, Context)
def add_fleet_commander(self, commander):
chk(libdogma.dogma_add_fleet_commander(self, commander))
@sig(_, key_t, Context)
def add_wing_commander(self, wing, commander):
chk(libdogma.dogma_add_wing_commander(self, wing, commander))
@sig(_, key_t, key_t, Context)
def add_squad_commander(self, wing, squad, commander):
chk(libdogma.dogma_add_squad_commander(self, wing, squad, commander))
@sig(_, key_t, key_t, Context)
def add_squad_member(self, wing, squad, member):
chk(libdogma.dogma_add_squad_member(self, wing, squad, member))
@sig(_, Context)
def remove_fleet_member(self, member):
found = c_bool()
chk(libdogma.dogma_remove_fleet_member(self, member, byref(found)))
return found
@sig(_, Context)
def set_fleet_booster(self, booster):
chk(libdogma.dogma_set_fleet_booster(self, booster))
@sig(_, key_t, Context)
def set_wing_booster(self, wing, booster):
chk(libdogma.dogma_set_wing_booster(self, wing, booster))
@sig(_, key_t, key_t, Context)
def set_squad_booster(self, wing, squad, booster):
chk(libdogma.dogma_set_squad_booster(self, wing, squad, booster))
@sig(typeid_t, state_t, effectid_t)
def type_has_effect(typ, state, effect):
result = c_bool()
chk(libdogma.dogma_type_has_effect(typ, state, effect, byref(result)))
return result.value
@sig(typeid_t)
def type_has_active_effects(typ):
result = c_bool()
chk(libdogma.dogma_type_has_active_effects(typ, byref(result)))
return result.value
@sig(typeid_t)
def type_has_overload_effects(typ):
result = c_bool()
chk(libdogma.dogma_type_has_overload_effects(typ, byref(result)))
return result.value
@sig(typeid_t)
def type_has_projectable_effects(typ):
result = c_bool()
chk(libdogma.dogma_type_has_projectable_effects(typ, byref(result)))
return result.value
@sig(typeid_t, attributeid_t)
def type_base_attribute(typ, attribute):
result = c_double()
chk(libdogma.dogma_type_base_attribute(typ, attribute, byref(result)))
return result.value