/
pinfo.py
136 lines (135 loc) · 3.71 KB
/
pinfo.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
import common, maze, character
from maze import ENTRANCE, COFFERS, WEST, SOUTH, EAST, NORTH
class pinfo:
def __init__(self,conn,addr,ctr=None,maz=None):
self.ctr = ctr
self.maz = maz
self.conn = conn
self.addr = addr
self.exploring = False
self.helpstr = 'Commands currently available:\nload\tshow\thelp\n'
def loadMaze(self,mname):
self.maz = maze.maze(maze.mfname(mname))
def process(self,line):
args = line.split()
if args[0] == 'load':
try:
self.loadMaze(args[1])
except:
self.maz = None
elif args[0] == 'show':
if self.maz == None:
return
chars = []
if self.exploring:
xmin = max(0,self.ctr.x-5)
xmax = min(self.maz.width,self.ctr.x+6)
ymin = max(0,self.ctr.y-5)
ymax = min(self.maz.height,self.ctr.y+6)
else:
xmin = 0
xmax = self.maz.width
ymin = 0
ymax = self.maz.height
for i in range(2*self.maz.width+1):
chars.append([])
for j in range(2*self.maz.height+1):
chars[i].append(' ')
for i in range(self.maz.width):
for j in range(self.maz.height):
if not self.exploring or self.visible[i][j]:
chars[2*i][2*j]='@'
chars[2*i+2][2*j]='@'
chars[2*i][2*j+2]='@'
chars[2*i+2][2*j+2]='@'
c = self.maz.cell(i,j)
if c & ENTRANCE:
chars[2*i+1][2*j+1]='I'
if c & COFFERS:
chars[2*i+1][2*j+1]='C'
if self.exploring:
if i == self.ctr.x and j == self.ctr.y:
chars[2*i+1][2*j+1]='*'
if c & WEST:
chars[2*i][2*j+1]='@'
if c & SOUTH:
chars[2*i+1][2*j+2]='@'
if c & EAST:
chars[2*i+2][2*j+1]='@'
if c & NORTH:
chars[2*i+1][2*j]='@'
outstr = ''
if self.exploring:
for y in range(2*(ymin-(self.ctr.y-5))):
outstr+='\n'
for y in range(2*ymin,2*ymax+1):
if self.exploring:
for x in range(2*(xmin-(self.ctr.x-5))):
outstr += ' '
for x in range(2*xmin,2*xmax+1):
outstr += chars[x][y]
outstr+='\n'
if self.exploring:
for y in range(2*((self.ctr.y+6)-ymax)):
outstr+='\n'
self.conn.sendall(outstr)
if self.exploring:
descr = ''
if self.ctr.x == self.maz.ix and self.ctr.y == self.maz.iy:
descr += 'There is an exit here.\n'
if self.ctr.x == self.maz.cx and self.ctr.y == self.maz.cy:
descr += 'The coffers are located here\n'
self.conn.sendall(descr)
elif args[0] == 'help':
self.conn.sendall(self.helpstr)
elif args[0] == 'explore':
if self.maz==None:
return
self.exploring = True
if self.ctr == None:
self.ctr = character.character()
self.ctr.x = self.maz.ix
self.ctr.y = self.maz.iy
self.visible=[]
for i in range(self.maz.width):
self.visible.append([])
for j in range(self.maz.height):
self.visible[i].append(False)
self.visible[self.maz.ix][self.maz.iy]=True
elif args[0] == 'n':
if not self.exploring:
return
if self.maz.cell(self.ctr.x,self.ctr.y) & NORTH:
return
self.ctr.y-=1
self.visible[self.ctr.x][self.ctr.y]=True
elif args[0] == 'e':
if not self.exploring:
return
if self.maz.cell(self.ctr.x,self.ctr.y) & EAST:
return
self.ctr.x+=1
self.visible[self.ctr.x][self.ctr.y]=True
elif args[0] == 's':
if not self.exploring:
return
if self.maz.cell(self.ctr.x,self.ctr.y) & SOUTH:
return
self.ctr.y+=1
self.visible[self.ctr.x][self.ctr.y]=True
elif args[0] == 'w':
if not self.exploring:
return
if self.maz.cell(self.ctr.x,self.ctr.y) & WEST:
return
self.ctr.x-=1
self.visible[self.ctr.x][self.ctr.y]=True
elif args[0] == 'n':
if not self.exploring:
return
if self.maz.cell(self.ctr.x,self.ctr.y) & NORTH:
return
self.ctr.y-=1
self.visible[self.ctr.x][self.ctr.y]=True
else:
self.conn.sendall('What?\n')