-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.lua
322 lines (311 loc) · 6.06 KB
/
console.lua
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
-- vim:et
require "chat"
local function history(sz)
local object={}
object.size=sz or 1000
object.len=0
object.offp=nil
object.offv=0
function object:push(str)
if self.len>=self.size then
self:del()
end
local t={}
t.val=str
self.len=self.len+1
if not self.head then
self.head=t
self.tail=t
return false
end
t.link=self.head
t.link.prev=t
self.head=t
return true
end
function object:del()
if self.tail then
local t=self.tail
self.len=self.len-1
if t.prev then
self.tail=t.prev
self.tail.link=nil
else
self.head=nil
self.tail=nil
self.offp=nil
self.offv=0
self.len=0
end
end
end
function object:set_off(n,s)
if not n or n<=0 then
self.offp=nil
self.offv=0
return
end
if s>=self.len then
self.offp=nil
self.offv=0
return
end
if n>self.len-s then
n=self.len-s
end
local i=self.head
self.offv=n
while i and n>0 do
i=i.link
n=n-1
end
self.offp=i
end
function object:iter()
local i=self.offp or self.head
return function()
if i then
local v=i.val
i=i.link
return v
end
return nil
end
end
function object:clear()
self.head=nil
self.tail=nil
self.offp=nil
self.offv=0
self.len=0
end
return object
end
local histq=history()
local readline=Readline:new(100)
console={
input=false;
}
local function split(str)
local a={}
local l=str:len()
local n=0
local p=1
local q=str:find(" ",1,true)
while q do
a[#a+1]=str:sub(p,q-1)
q=q+n
while str:sub(q,q)==" " do
q=q+1
end
if str:sub(q,q)=="\"" then
p=q+1
q=str:find("\"",p,true)
n=1
else
p=q
q=str:find(" ",p,true)
n=0
end
end
if p<=l then
a[#a+1]=str:sub(p,l)
end
return a
end
function console.cmd(str)
arg=split(str)
if arg[1]=="/graph" then
if #arg<2 then
histq:push("graph: not enough arguments")
return
end
a=str_split(arg[2],"x")
if a.n<2 then
histq:push("graph: bad arguments")
return
end
local x,y=tonumber(a[1]),tonumber(a[2])
if not x or not y then
histq:push("graph: bad arguments")
return
end
if x<640 or y<480 then
histq:push("graph: minimal supported resolution is 640x480")
return
end
conf.graph_width=x
conf.graph_height=y
save_conf()
set_graph()
init_graph()
init_gui()
return
end
if arg[1]=="/set" then
if #arg<2 then
histq:push("set: not enough arguments")
return
end
if arg[2]=="chat_timeout" then
if #arg<3 then
histq:push(string.format("chat_timeout=%s",conf.chat_timeout))
return
end
conf.chat_timeout=tonumber(arg[3])
save_conf()
return
end
end
if arg[1]=="/replay" then
if ME then
histq:push("replay: game in progress")
return
end
if #arg<2 then
histq:push("replay: not enough arguments")
return
end
local fn=string.format("replays/%s",arg[2])
if not love.filesystem.exists(fn) then
histq:push("replay: file not found")
return
end
replay=love.filesystem.lines(fn)
rep_init()
return
end
if arg[1]=="/list" then
files=love.filesystem.enumerate("replays")
for _,fn in pairs(files) do
histq:push(fn)
end
return
end
if arg[1]=="/ally" or arg[1]=="/enemy" then
if #arg<2 then
histq:push("not enough arguments")
return
end
local b=arg[1]=="/ally" and 1 or 0
local idx
if arg[2]:sub(1,1)=="#" then
idx=tonumber(arg[2]:sub(2))
else
for i,p in pairs(players) do
if arg[2]==p.name then
idx=i
break
end
end
end
if not idx or not players[idx] then
histq:push("unknown player")
return
end
if players[idx]==ME then
histq:push("cannot set relation to myself")
return
end
net_send("ALLY:%d:%d:%d",ME.idx,idx,b)
return
end
histq:push("unknown command")
end
function console.enter()
if readline.str:len()>readline.sz or readline.str:len()<1 then
readline:clr()
return
end
if readline.str:match("[%a%d_\\ :;\"\'%,%.%<%>%(%)%[%]%{%}%/%?%!%@%#%$%%%^%&%*%-%+%=]*")~=readline.str then
readline:clr()
return
end
if readline.str:sub(1,1)=="/" then
console.cmd(readline.str)
readline:done()
return
end
chat.send(readline.str)
readline:done()
return
end
function console.keypressed(key,ch)
local step=floor((eye.cy-25)/15/2)
if key=="escape" then
readline:clr()
return true
end
if key=="return" then
console.enter()
return true
end
if key=="`" or key=="f1" then
readline:clr()
return false
end
if key=="pageup" then
histq:set_off(histq.offv+step,step)
return true
end
if key=="pagedown" then
histq:set_off(histq.offv-step,step)
return true
end
readline:key(key,ch)
return true
end
local con_off=0
function console.draw()
if con_off>0 then
graph.setColor(0,0,96,192)
graph.setLine(1,"rough")
graph.rectangle("fill",0,0,eye.sx-1,con_off)
graph.setColor(64,64,192)
graph.line(0,con_off,eye.sx-1,con_off)
local y=con_off-20
readline:draw(5,y,"> ")
y=y-20
graph.setColor(255,255,255)
for m in histq:iter() do
if y<5 then
break
end
graph.print(m,5,y)
y=y-15
end
return
end
chat.draw()
end
local cr_dt=0
local my_dt=0
function console.update(dt)
local offv=eye.cy/0.3
cr_dt=cr_dt+dt
if cr_dt>=0.5 then
cr_dt=cr_dt-0.5
readline.cr=not readline.cr
end
if console.input then
if con_off<eye.cy then
con_off=floor(con_off+offv*dt)
end
if con_off>eye.cy then
con_off=eye.cy
end
else
if con_off>0 then
con_off=floor(con_off-offv*dt)
if con_off<0 then
con_off=0
histq:set_off()
end
end
end
chat.update(dt)
end
function console.msg(str)
histq:push(str)
chat.msg(str)
end