-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoord.py
183 lines (151 loc) · 9.14 KB
/
coord.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
import json
import os
import subprocess
from random import randint
import math
import users.lib
import traceback
fightInProgress = False
turn = 1
whoPlays = -1
class Coordinator():
def __init__(self, pId1, pId2):
# VARIABLES #
self.history = []
with open('users/user' + str(pId1) + '/stat.dat') as file:
user1Stat = json.loads(file.read())
with open('users/user' + str(pId2) + '/stat.dat') as file:
user2Stat = json.loads(file.read())
self.game = {'id': -1, 'maxTurns': 32, 'path':'', 'turn': 1, 'whoPlays': -1, 'width': 16, 'height': 16}
self.players = [{'pseudo': user1Stat['pseudo'], 'color': 'blue', 'x': 1, 'y': 1, 'currentWeapon' : -1, 'maxMp': user1Stat['maxMp'], 'mp': user1Stat['maxMp'], 'id': 0, 'maxTp': user1Stat['maxTp'], 'tp': user1Stat['maxTp'], 'hp': user1Stat['maxHp'], 'maxHp': user1Stat['maxHp']},
{'pseudo': user2Stat['pseudo'], 'color': 'red', 'x': self.game['width'] - 2, 'y': self.game['height']-2, 'currentWeapon' : -1, 'maxMp': user2Stat['maxMp'], 'mp': user2Stat['maxMp'], 'id': 1, 'maxTp': user2Stat['maxTp'], 'tp': user2Stat['maxTp'], 'hp': user2Stat['maxHp'], 'maxHp': user2Stat['maxHp']}]
self.history.append(json.dumps(self.players))
exec("from users.user{0} import ai{1} as u1".format(pId1, pId1), globals())
self.players[0]['ai'] = u1
exec("from users.user{0} import ai{1} as u2".format(pId2, pId2), globals())
self.players[1]['ai'] = u2
self.globals = {}
with open('globals.dat', 'r') as file:
self.globals = file.read().split('\n')
self.history.append(self.globals[0])
self.spells = json.loads(self.globals[1])
self.weapons = json.loads(self.globals[0])
# GAME TREE #
self.game['id'] = 0
while os.path.isfile('Fights/' + str(self.game['id']) + '.dat'):
self.game['id'] += 1
# GENERATING MAP #
self.map = [[-1 for i in range(self.game['width'])] for o in range(self.game['height'])]
for player in self.players:
self.map[player['y']][player['x']] = player['id']
placedObstacles = 0
obstaclesToPlace = 20
while placedObstacles < obstaclesToPlace:
y = randint(0, len(self.map)-1)
x = randint(0, len(self.map[y])-1)
if (self.map[y][x] == -1):
placedObstacles += 1
self.map[y][x] = -2
placedLavaHole = 0
lavaHoleToPlace = 20
while placedLavaHole < lavaHoleToPlace:
y = randint(0, len(self.map)-1)
x = randint(0, len(self.map[y])-1)
if (self.map[y][x] == -1):
placedLavaHole += 1
self.map[y][x] = -3
self.history.append(json.dumps(self.map))
# LAUNCH GAME #
self.processGame()
def processGame(self):
countAlivePlayers = len(self.players)
for t in range(self.game['maxTurns']):
self.game['turn'] = t
print('\nGenerating turn ' + str(self.game['turn']) + ' / ' + str(self.game['maxTurns']-1))
self.history.append('[TURN] ' + str(self.game['turn']))
for i in range(len(self.players)):
self.game['whoPlays'] = i
self.history.append('[WHOPLAYS] ' + str(self.game['whoPlays']))
# LAUNCH
users.lib.MAP_DAT = self.map.copy()
users.lib.GAME_DAT = self.game.copy()
users.lib.PLAYERS_DAT = self.players.copy()
self.players[self.game['whoPlays']]['ai'].lib.MAP_DAT = self.map.copy()
self.players[self.game['whoPlays']]['ai'].lib.GAME_DAT = self.game.copy()
self.players[self.game['whoPlays']]['ai'].lib.PLAYERS_DAT = self.players.copy()
self.players[self.game['whoPlays']]['ai'].lib.actions = []
self.players[self.game['whoPlays']]['ai'].lib.WEAPONS = self.weapons.copy()
try:
self.players[self.game['whoPlays']]['ai'].main()
except:
print(self.players[self.game['whoPlays']]['pseudo'] + ': IA exit with non 0 statement')
print(traceback.print_exc())
result = self.players[self.game['whoPlays']]['ai'].lib.actions
for action in result:
if len(action) and action[0] == '[MOVE]':
x = int(action[1])
y = int(action[2])
if (abs(self.players[self.game['whoPlays']]['y'] - y) + abs(self.players[self.game['whoPlays']]['x'] - x) == 1
and x >= 0 and y >= 0 and y < len(self.map) and x < len(self.map[y])
and self.map[y][x] == -1
and self.players[self.game['whoPlays']]['mp'] >= 1):
self.map[self.players[self.game['whoPlays']]['y']][self.players[self.game['whoPlays']]['x']] = -1
self.players[self.game['whoPlays']]['x'] = x
self.players[self.game['whoPlays']]['y'] = y
self.map[y][x] = self.game['whoPlays']
self.players[self.game['whoPlays']]['mp'] -= 1
self.history.append(' '.join([str(a) for a in action]))
users.lib.MAP_DAT = self.map.copy()
elif len(action) and action[0] == '[MARK]':
self.history.append(' '.join([str(a) for a in action]))
elif len(action) and action[0] == '[ATTACK]' and self.players[self.game['whoPlays']]['currentWeapon'] != -1:
x = int(action[1])
y = int(action[2])
currentWeapon = self.players[self.game['whoPlays']]['currentWeapon']
distance = math.sqrt((self.players[self.game['whoPlays']]['x'] - x)**2 + (self.players[self.game['whoPlays']]['y'] - y)**2)
maxRange = self.weapons[currentWeapon]['maxRange']
cost = self.weapons[currentWeapon]['cost']
damage = self.weapons[currentWeapon]['damage']
if distance <= maxRange and self.players[self.game['whoPlays']]['tp'] >= cost: # 5 is max range of the weapon, 4 is the cost of attack
pos = [self.players[self.game['whoPlays']]['x'], self.players[self.game['whoPlays']]['y']]
pos2 = [x, y]
los = users.lib.getLineOfSight(pos, pos2)
if los:
for i in range(len(self.players)):
if self.players[i]['x'] == x and self.players[i]['y'] == y:
self.players[i]['hp'] -= damage
self.players[self.game['whoPlays']]['tp'] -= cost
self.history.append(' '.join([str(a) for a in action]))
if (self.players[i]['hp'] <= 0):
self.history.append('[DEATH] ' + str(self.players[i]['id']))
countAlivePlayers -= 1
users.lib.PLAYERS_DAT = self.players.copy()
elif len(action) and action[0] == '[SET_WEAPON]':
if self.players[self.game['whoPlays']]['tp'] >= 1 and self.players[self.game['whoPlays']]['currentWeapon'] != action[1]:
self.players[self.game['whoPlays']]['currentWeapon'] = action[1]
self.players[self.game['whoPlays']]['tp'] -= 1
self.history.append(' '.join([str(a) for a in action]))
elif len(action) and action[0] == '[HEAL]':
if self.players[self.game['whoPlays']]['tp'] >= 4:
self.players[self.game['whoPlays']]['hp'] = min(self.players[self.game['whoPlays']]['hp'] + 5, self.players[self.game['whoPlays']]['maxHp'])
self.players[self.game['whoPlays']]['tp'] -= 4
self.history.append(' '.join([str(a) for a in action]))
if countAlivePlayers <= 1:
break
# Stats
self.players[self.game['whoPlays']]['mp'] = self.players[self.game['whoPlays']]['maxMp']
self.players[self.game['whoPlays']]['tp'] = self.players[self.game['whoPlays']]['maxTp']
if countAlivePlayers <= 1:
break
if countAlivePlayers <= 1:
break
print("End of the game")
self.history.append('[END]')
# Save replay:
with open('Fights/' + str(self.game['id']) + '.dat', 'w') as file:
file.write('\n'.join(self.history))
subprocess.run(['python', 'player.py', 'Fights/' + str(self.game['id']) + '.dat'], stdout=subprocess.PIPE)# Play replay
print('Generation d\'un combat')
user1 = int(input('ID premier utilisateur: '))
user2 = int(input('ID second utilisateur: '))
Coordinator(user1, user2)