-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.py
158 lines (124 loc) · 4.99 KB
/
output.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
from termcolor import colored;
from utils import Utils;
import sys;
import copy;
from gamemap import GameMap;
from GameTimeline import GameTimeline
class Output(object):
def log(self, msg): pass;
def logAction(self, msg, x, y): pass;
def dump(self): pass
def write(self): pass
def writeln(self): pass
def clear(self): pass
def color(self, sColor = None): pass
def drawMap(self, mapToDraw, attackers, defenders, trapColor = "red",
treasureColor = "yellow", terrainColor = "green"): pass
def saveToFile(self, sFilename):
pass
class ConsoleOutput(Output):
def __init__(self):
self.lastMessages = [];
self.msg = "";
self.nextColor = None;
self.maxMessages = 10;
self.actions = [];
self.timeline = GameTimeline();
def log(self, msg):
msg = msg.strip();
if (len(msg) == 0):
return;
if (self.nextColor == None):
self.msg += "\n" + msg;
self.lastMessages += [msg];
else:
self.msg += colored(msg, self.nextColor);
self.lastMessages += [colored(msg, self.nextColor)];
if (len(self.lastMessages) > self.maxMessages):
self.lastMessages = self.lastMessages[-self.maxMessages:];
def logAction(self, msg, x, y):
self.actions.append({"text": msg, "pos": {"x": x, "y": y}})
def dump(self):
# DEBUG LINES
# print "Messages %d"%(len(self.lastMessages));
# Keep 5 last messages
if (len(self.lastMessages) > self.maxMessages):
self.lastMessages = self.lastMessages[-self.maxMessages:];
print "\n".join(self.lastMessages);
#self.msg = "";
def write(self):
print Utils.padWithSpaces(self.msg, 79)[0:79],;
def writeln(self):
print Utils.padWithSpaces(self.msg, 79)[0:79];
def clear(self):
#self.msg = "";
self.lastMessages = [];
def color(self, sColor = None):
self.nextColor = sColor;
def drawMap(self, mapToDraw, attackers, defenders, trapColor = "red", treasureColor = "yellow", terrainColor = "green"):
# Clear screen
Utils.cls();
viewState = {
"treasures": [],
"traps": [],
"allies": [],
"enemies": []
}
mapInstance = copy.deepcopy(mapToDraw);
# Init squares
mapInstance.squares = [[ colored('...', terrainColor) for iCnt in range(0, mapInstance.xSize) ] for iCnt in range(0, mapInstance.ySize)];
# Add traps
for iCnt in range(0, mapInstance.xSize):
for iCnt2 in range(0, mapInstance.ySize):
squareTreasures = mapInstance.getTreasures(iCnt, iCnt2);
squareTraps = mapInstance.getTraps(iCnt, iCnt2);
squareFoes = mapInstance.getFoes(iCnt, iCnt2);
# If treasure in square
if (len(squareTreasures)) > 0:
# Show the first
mapInstance.squares[iCnt][iCnt2] = colored(str(squareTreasures[0]), treasureColor);
viewState["treasures"].append({"id": type(squareTreasures[0]).__name__, "pos": {"x": iCnt, "y": iCnt2}})
else:
if len(squareFoes) > 0:
# Show the first
mapInstance.squares[iCnt][iCnt2] = colored(str(squareFoes[0]), trapColor);
viewState["enemies"].append({"id": type(squareFoes[0]).__name__, "pos": {"x": iCnt, "y": iCnt2}})
else:
# If trap in square
if (len(squareTraps)) > 0:
# Show the first
mapInstance.squares[iCnt][iCnt2] = colored(str(squareTraps[0]), trapColor);
viewState["traps"].append({"id": type(squareTraps[0]).__name__, "pos": {"x": iCnt, "y": iCnt2}})
# Render soldiers
for cItem in attackers:
mapInstance.squares[cItem.x][cItem.y] = colored(str(cItem), cItem.color);
viewState["allies"].append({"id": type(cItem).__name__, "pos": {"x": cItem.x, "y": cItem.y}})
for cItem in defenders:
mapInstance.squares[cItem.x][cItem.y] = colored(str(cItem), cItem.color);
viewState["enemies"].append({"id": type(cItem).__name__, "pos": {"x": cItem.x, "y": cItem.y}})
# Show map
for curRow in range(mapInstance.ySize):
for curCol in range(mapInstance.xSize):
sys.stdout.write(mapInstance.squares[curCol][curRow]);
sys.stdout.write("\n");
# Get rid of deep map copy
del mapInstance;
# Append state - action pair for the View engine
for action in self.actions:
self.timeline.appendStateActionPair(viewState, action)
# self.timeline.appendStateActionPair(viewState, {"text": "Something happened", "pos": {"x": 4, "y": 5}})
self.actions[:] = []
def saveToFile(self, sFilename):
fOut = open(sFilename, 'w')
fOut.write(self.msg)
fOut.close()
self.timeline.export()
if __name__ == "__main__":
o = Output();
m = GameMap(10, 10);
import soldiers;
s = soldiers.SoldierClass();
s.x = 3; s.y = 0;
s2 = soldiers.SoldierClass();
s2.x = 3; s2.y = 1;
o.drawMap(m, [s], [s2], "green", "red");