/
warconomy_sv.py
executable file
·495 lines (443 loc) · 15.7 KB
/
warconomy_sv.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python
from __future__ import with_statement
import os, pickle, random, sys
from twisted.internet import reactor
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Factory
nats = []
nats_by_name = {}
last_input = None
forces = []
stances = {}
tock = 0
last_tick_results = ''
STANCE_EXTERMINATE = 0
STANCE_WAR = 1
STANCE_BRINK = 2
STANCE_HOSTILE = 3
STANCE_UNFRIENDLY = 4
STANCE_WARY = 5
STANCE_NEUTRAL = 6
STANCE_FRIENDLY = 7
STANCE_MINOR_ALLY = 8
STANCE_CLOSE_MAJOR_ALLY = 9
STANCE_BRETHREN = 10
stance_codes = ['EXT','WAR','BNK','HOS','UNF','WRY','NEU','FRI','MIN','MAJ','BRE']
class State: pass
SAVE_FILENAME = 'savedgame'
class Nation:
name = ''
money = 1000
def __init__(self, name, adj, money, pop, soldiers, troop_skill):
self.name = name
self.adj = adj
self.money = money
self.pop = pop
self.soldiers = soldiers
self.troop_skill = troop_skill # represents sum effects of: tech, morale, training, initiative, fitness, leadership
self.conquered = None
self.human = False
self.turn_done = False
def __str__(self):
return self.name
class Force:
def __init__(self, owner, soldiers, target_nat):
self.owner = owner
self.soldiers = soldiers
self.target_nat = target_nat
def mutual_stance(n1_name, n2_name, stance):
global stances
#print '%s, %s, %s' % (n1_name, n2_name, stance)
n1 = nats_by_name[n1_name]
n2 = nats_by_name[n2_name]
#print '%s, %s' % (hex(id(n1)), hex(id(n2)))
stances[n1][n2] = stance
stances[n2][n1] = stance
def abbrev_num(qty):
#print 'qty %s' % qty
factor = 1
abbrev_amount = None
while True:
abbrev_amount = float(qty) / float(factor)
if abbrev_amount >= 1000:
factor *= 1000
else:
break
units = {1:'', 1000:'k', 1000000:'m', 1000000000:'b', 1000000000000:'t'}
unit = None
if factor in units:
unit = units[factor]
else:
unit = '?'
amt = '%.1f' % abbrev_amount
if amt.endswith('.0'):
amt = amt[:-2]
return '%s%s' % (amt, unit)
def nat_with_name(name):
for k in nats_by_name.keys():
if name.lower() == k.lower():
return nats_by_name[k]
return None
def nats_with_name_starting_with(prefix):
ns = []
for k in nats_by_name.keys():
if k.lower().startswith(prefix.lower()):
nat = nats_by_name[k]
ns.append(nat)
return ns
def nat_with_name_starting_with(prefix):
ns = nats_with_name_starting_with(prefix)
if len(ns) == 1:
return ns[0]
else:
return None
def all_human_turns_done():
for n in nats:
if n.human and not n.turn_done: return False
return True
def cmd_done(pov_nat_id, args):
global last_tick_results
resp = ''
if nats[pov_nat_id].turn_done:
print 'player already was done so ignoring command'
return resp
nats[pov_nat_id].turn_done = True
if all_human_turns_done():
resp = tick(resp)
last_tick_results = resp
resp += '-' * 40 + '\n'
return resp
cmd_d = cmd_done
def cmd_last_tick_results(pov_nat_id, args):
return last_tick_results
cmd_l = cmd_last_tick_results
def cmd_raise_soldiers(pov_nat_id, args):
you = pov_nat_id
resp = ''
new_soldiers = int(args[1])
cost = new_soldiers * 100000
nat = nats[you]
if cost <= nat.money:
nat.money -= cost
nat.soldiers += new_soldiers
resp += '%s raises %s more soldiers costing $%s\n' % (nat, abbrev_num(new_soldiers), abbrev_num(cost))
else:
resp += '%s cannot afford the $%s cost to raise %s more soldiers\n' % (nat, abbrev_num(cost), abbrev_num(new_soldiers))
return resp
cmd_s = cmd_raise_soldiers
def cmd_invade(pov_nat_id, args):
you = pov_nat_id
resp = ''
name = args[1]
defender = nat_with_name_starting_with(name)
if not defender:
resp += 'that is not a unique country: %s\n' % name
return resp
attacker = nats[you]
if attacker is defender:
resp += '%s cannot invade itself\n' % attacker.name
return resp
force_size = attacker.soldiers / 2
attacker.soldiers -= force_size
f = get_force_there_already(attacker,defender)
if not f:
resp += '%s invades %s with %s soldiers\n' % (attacker, defender, abbrev_num(force_size))
f = Force(attacker, force_size, defender)
forces.append(f)
else:
resp += '%s sends %s additional soldiers to %s\n' % (attacker, abbrev_num(force_size), defender)
f.soldiers += force_size
mutual_stance(attacker.name, defender.name, STANCE_WAR)
return resp
cmd_inv = cmd_invade
def cmd_withdraw(pov_nat_id, args):
you = pov_nat_id
resp = ''
name = args[1]
target_nat = nat_with_name_starting_with(name)
if not target_nat:
resp += 'that is not a unique country: %s\n' % name
return resp
force_owner = nats[you]
if force_owner == target_nat:
resp += 'you cannot withraw forces from yourself\n'
return resp
f = get_force_there_already(force_owner,target_nat)
if not f:
resp += '%s has no force in %s\n' % (force_owner.name, target_nat.name)
return resp
min_req_occ_soldiers = get_min_req_occupying_force_size(target_nat)
# assumes we have only one force there
if f.soldiers > min_req_occ_soldiers:
wd_amount = f.soldiers - min_req_occ_soldiers
resp += '%s withdraws all but min required occupation force (of %s soldiers) from %s (%s soldiers)\n' % (force_owner.name, abbrev_num(min_req_occ_soldiers), target_nat.name, abbrev_num(wd_amount))
else:
wd_amount = f.soldiers
resp += '%s withdraws all forces from %s (%s %s soldiers)\n' % (force_owner.name, target_nat.name, force_owner.adj, abbrev_num(wd_amount))
f.soldiers -= wd_amount
force_owner.soldiers += wd_amount
if f.soldiers == 0:
forces.remove(f)
return resp
cmd_wd = cmd_withdraw
def calc_kills(shooting_soldiers, skill):
kills = int( shooting_soldiers * (float(skill) / 10.0) * random.random() )
return kills
def get_force_there_already(owner, where):
for f in forces:
if f.owner == owner and f.target_nat == where:
return f
return None
def ai_orders():
resp = ''
for nat in nats:
if nat.human: continue
resp += '%s thinks...\n' % nat
nat.turn_done = True
return resp
def tick(resp):
global tock
resp += ai_orders()
tock += 1
resp += 'Time advances to %i...\n' % tock
resp = conflicts(resp)
resp = looting(resp)
resp = rebellion(resp)
reset_turn_dones()
save()
return resp
def conflicts(resp):
print 'conflicts'
for f in forces:
print 'eval %s force in %s' % (f.owner.adj, f.target_nat)
attacker = f.owner
defender = f.target_nat
if defender.conquered:
continue
if not defender.conquered and f.soldiers == 0:
continue
atk_kills = calc_kills(f.soldiers, attacker.troop_skill)
def_kills = calc_kills(defender.soldiers, defender.troop_skill)
if atk_kills > defender.soldiers:
atk_kills = defender.soldiers
if def_kills > f.soldiers:
def_kills = f.soldiers
defender.soldiers -= atk_kills
f.soldiers -= def_kills
resp += 'In the war in %s the losses are %s %s soldiers and %s %s soldiers\n' % (defender.name, abbrev_num(atk_kills), defender.adj, abbrev_num(def_kills), attacker.adj)
if defender.soldiers <= 0:
defender.soldiers = 0
defender.conquered = attacker
resp += '%s conquered by %s\n' % (defender.name, attacker.name)
return resp
def looting(resp):
for nat in nats:
if not nat.conquered: continue
looted_money = nat.money / 4
nat.money -= looted_money
nat.conquered.money += looted_money
resp += '%s loots $%s from conquered %s\n' % (nat.conquered, abbrev_num(looted_money), nat)
return resp
def get_total_occupying_soldiers(nat):
total = 0
for f in forces:
if f.target_nat == nat:
total += f.soldiers
return total
def get_min_req_occupying_force_size(nat):
return nat.pop / 100
def rebellion(resp):
print 'rebellion'
for nat in nats:
if not nat.conquered: continue
occ_soldiers = get_total_occupying_soldiers(nat)
min_req_occ_soldiers = get_min_req_occupying_force_size(nat)
if occ_soldiers < min_req_occ_soldiers:
nat.conquered = None
new_rebels = nat.pop / 100000
nat.soldiers += new_rebels
resp += '%s rebels (gains %s fighters) because not enough occupying soldiers (was %s, needed %s)\n' % (nat, new_rebels, occ_soldiers, min_req_occ_soldiers)
return resp
shortcuts = {'.' : 'again'}
def cmd_ui(pov_nat_id, args):
return render_ui(pov_nat_id)
def render_ui(pov_nat_id):
you = pov_nat_id
resp = ''
resp += 'Turn %i\n' % tock
fmt = '%9s %1s %1s %6s %9s %7s %6s %6s %2s %6s'
resp += fmt % ('country','p','d','stance','owner','money','pop.','troops','ts','occreq') + '\n'
for nat in nats:
name = '%s' % nat.name
if nat is nats[you]:
name = '*%s*' % name
ptype = nat.human and 'h' or 'c'
done = nat.turn_done and 'd' or '-'
stance = '%s/%s' % (stances[nat][nats[you]], stances[nats[you]][nat])
money = '$%s' % abbrev_num(nat.money)
owner = nat.conquered and nat.conquered.name or ''
pop = abbrev_num(nat.pop)
soldiers = abbrev_num(nat.soldiers)
occ_req = abbrev_num(get_min_req_occupying_force_size(nat))
resp += fmt % (name, ptype, done, stance, owner, money, pop, soldiers, nat.troop_skill, occ_req)
invaders = ''
for f in forces:
if f.target_nat == nat:
invaders += ' (%s %s sol.)' % (abbrev_num(f.soldiers), f.owner.adj)
resp += invaders + '\n'
return resp
def handle_input(exe_nat_id, input):
global last_input
resp = ''
#input = sys.stdin.readline().strip()
resp += '\n'
args = input.split()
if len(args) == 0:
resp += 'no input\n'
return
cmd = args[0]
if cmd in shortcuts:
cmd = shortcuts[cmd]
if cmd == 'again':
if last_input is not None:
args = last_input.split()
cmd = args[0]
else:
last_input = input
cmd_fn_name = 'cmd_%s' % cmd
if cmd_fn_name in globals():
fn = globals()[cmd_fn_name]
print 'dispatching to: %s (%s)' % (fn, args)
resp += fn(exe_nat_id,args)
resp += '-' * 40 + '\n'
if cmd_fn_name not in ('cmd_ui','cmd_done','cmd_d'):
save()
if cmd_fn_name not in ('cmd_ui'):
resp += render_ui(exe_nat_id)
return resp
class Server(LineReceiver):
cl_nat_id = None
auth = False
def connectionMade(self):
print 'server conn made'
def connectionLost(self, reason):
print 'server conn lost: %s' % reason.getErrorMessage()
def connectionFailed(self, reason):
print 'server conn failed: %s' % reason.getErrorMessage()
def lineReceived(self, line):
print "server line recvd: '%s'" % line
resp = ''
if not self.auth:
if line.startswith('login '):
nat_id = int(line.split()[1])
if not nats[nat_id].human:
print 'login attempt for a non-human nation %i so disconnecting him' % nat_id
self.transport.loseConnection()
return
auth_valid = True #TODO truly check given password/key
if not auth_valid:
print 'login attempt gave invalid auth so disconnecting him'
self.transport.loseConnection()
return
self.cl_nat_id = nat_id
self.auth = True
else:
resp = handle_input(self.cl_nat_id,line)
#print "server responding: '%s'" % resp
self.sendLine(resp)
def save():
print 'saving'
state = State()
state.tock = tock
state.last_tick_results = last_tick_results
state.nats = nats
state.nats_by_name = nats_by_name
state.last_input = last_input
state.forces = forces
state.stances = stances
with open(SAVE_FILENAME,'w') as f:
pickle.dump(state,f)
def restore():
fpath = SAVE_FILENAME
if not os.path.isfile(fpath):
return
global nats, nats_by_name, you, last_input, forces, stances
with open(fpath,'r') as f:
state = pickle.load(f)
tock = state.tock
last_tick_results = state.last_tick_results
nats = state.nats
nats_by_name = state.nats_by_name
last_input = state.last_input
forces = state.forces
stances = state.stances
def init(hums):
global nats, nats_by_name, stances
# pop from Wikipedia, for 2009
# soldiers from 'Total Troops' column of web page: http://en.wikipedia.org/wiki/List_of_countries_by_size_of_armed_forces
# key: country adj money pop soldiers ts
nats = [
Nation('USA', 'US', 2000000000000, 306963000, 3385400, 5),
Nation('France', 'Fre', 100000000000, 65073482, 779450, 3),
Nation('Germany', 'Ger', 250000000000, 82060000, 606362, 4),
Nation('Mexico', 'Mexn', 10000000000, 111211789, 517770, 1),
Nation('China', 'Chin', 1000000000000, 1338612968, 7024000, 3),
Nation('Russia', 'Rus', 100000000000, 142008838, 3796100, 4),
Nation('Iran', 'Iran.', 10000000000, 70495782, 1695000, 2),
Nation('Japan', 'Jap', 300000000000, 127433494, 296550, 3),
Nation('India', 'Ind', 100000000000, 1100000000, 3773300, 3),
Nation('Brazil', 'Braz.', 10000000000, 191241714, 1687600, 1),
Nation('UK', 'UK', 200000000000, 61612300, 421830, 5),
Nation('N. Korea', 'NKor', 1000000000, 22666000, 5995000, 2),
]
for nat in nats:
nats_by_name[nat.name] = nat
for hum in hums:
nats[hum].human = True
reset_turn_dones()
stances = {}
for n1 in nats:
stances[n1] = {}
#print 'stances[n1] = %s, where n1 is %s' % (stances[n1], n1)
for n2 in nats:
stance = STANCE_NEUTRAL
if n2 == n1:
stance = STANCE_BRETHREN
stances[n1][n2] = stance
#print 'stances[n1][n2] = %s' % stances[n1][n2]
mutual_stance('USA', 'UK', STANCE_CLOSE_MAJOR_ALLY)
mutual_stance('USA', 'Japan', STANCE_CLOSE_MAJOR_ALLY)
mutual_stance('USA', 'Germany', STANCE_MINOR_ALLY)
mutual_stance('USA', 'France', STANCE_MINOR_ALLY)
mutual_stance('USA', 'Mexico', STANCE_FRIENDLY)
mutual_stance('USA', 'India', STANCE_FRIENDLY)
mutual_stance('USA', 'Brazil', STANCE_FRIENDLY)
mutual_stance('USA', 'China', STANCE_WARY)
mutual_stance('USA', 'Russia', STANCE_WARY)
mutual_stance('USA', 'Iran', STANCE_UNFRIENDLY)
mutual_stance('USA', 'N. Korea', STANCE_UNFRIENDLY)
def reset_turn_dones():
for nat in nats:
nat.turn_done = False
def main():
print 'Warconomy server'
port = 9876
hums = [0,] # by default one human, controlling nation 0
if len(sys.argv) > 1:
args = sys.argv[1:]
for arg in args:
if arg.startswith('hum='):
hums = arg.split('=')[1].split(',')
hums = map(int,hums)
print 'hums: %s' % hums
if arg.startswith('port='):
port = int(arg.split('=')[1])
init(hums)
restore()
factory = Factory()
factory.protocol = Server
print 'listening on port %i' % port
reactor.listenTCP(port, factory)
reactor.run()
if __name__ == '__main__':
main()