-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
363 lines (328 loc) · 9.71 KB
/
client.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
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
function requireDirectory( dir )
dir = dir or ""
local entities = love.filesystem.getDirectoryItems(dir)
for k, ents in ipairs(entities) do
trim = string.gsub( ents, ".lua", "")
require(dir .. "/" .. trim)
end
end
local sock = require("sock")
inspect = require("inspect")
sha = require("sha")
utf8 = require("utf8")
moonshine = require("moonshine")
tobool = require("toboolean")
function string.explode(str, div)
assert(type(str) == "string" and type(div) == "string", "invalid arguments")
local o = {}
while true do
local pos1,pos2 = str:find(div)
if not pos1 then
o[#o+1] = str
break
end
o[#o+1],str = str:sub(1,pos1-1),str:sub(pos2+1)
end
return o
end
SG = {}
SG.atomicTable = {}
SG.fonts = {
title = love.graphics.newFont("/fonts/spixel.ttf", 24),
label = love.graphics.newFont("/fonts/chill.ttf", 16),
}
SG.loggedIn = false
require("draw")
require("tiles")
require("sounds")
require("controls")
function love.load()
local serverdata = string.explode( love.filesystem.read("serverinfo.txt"), "\n" )
client = sock.newClient(serverdata[1], tonumber(serverdata[2]))
love.mouse.setVisible(false)
-- Called when a connection is made to the server
client:on("connect", function(data)
print("You've successfully connected. Sending credentials, attempting to log in as " .. serverdata[3] .. "...")
client:send("login", {serverdata[3], serverdata[4]})
end)
-- Called when the client disconnects from the server
client:on("disconnect", function(data)
print("You've lost connection.")
end)
-- Custom callback, called whenever you send the event from the server
client:on("hello", function(msg)
print(msg)
end)
client:on("loginSuccess", function(msg)
SG.loggedIn = true
end)
client:on("loginFailure", function(msg)
SG.loggedIn = msg
end)
client:on("sendobject", function(obj)
SG.atomicTable[obj.id] = obj
end)
client:on("deleteobject", function(obj)
SG.atomicTable[obj.id] = nil
end)
client:on("sendobjects", function(objs)
for k, obj in pairs( objs ) do
SG.atomicTable[obj.id] = obj
end
end)
client:on("sendparticle", function(data)
SG:addEmitter( SG.particles[data.particleName], data.x, data.y, data.post )
end)
updates = 0
client:on("updateobject", function(data)
if( not data.id ) then
return
end
if( not SG.atomicTable[data.id] ) then
SG.atomicTable[data.id] = {}
end
for k, v in pairs( data ) do
SG.atomicTable[data.id][k] = v;
end
end)
client:on("updateawareness", function(data)
if( not client.mob ) then
return
end
local snipped = 0
local total = 0
for k, v in pairs( SG.atomicTable ) do
if( not client.mob.awareTable or not client.mob.awareTable[v.id] ) then
SG.atomicTable[k] = nil
snipped = snipped + 1
end
total = total + 1
end
end)
client:on("sendmap", function(recmap)
map = recmap
--updateTilesetBatch()
end)
client:on("verifyMob", function(id)
for k, obj in pairs( SG.atomicTable ) do
if( obj.id == id ) then
client.mob = obj;
client.mobid = id;
end
end
end)
client:on("hearSound", function(data)
-- don't use music or really big files for this
TEsound.play("sound/" .. data.sound, "static", data.tags or {}, data.volume or 1, data.pitch or 1)
end)
client:connect()
love.audio.setEffect("dreamy", {type = "reverb"})
--music = love.audio.newSource("sound/music/whitewaking.mp3", "stream")
--music:setEffect("dreamy")
--music:play()
end
function love.update(dt)
if love.keyboard.isDown("up") then
if( love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") ) then
client:send("command", "up?")
elseif( love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") ) then
client:send("command", "up+")
elseif( love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") ) then
client:send("command", "up-")
else
client:send("command", "up")
end
--moveMap(0, -0.2 * tileSize * dt)
adjustMap()
end
if love.keyboard.isDown("down") then
if( love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") ) then
client:send("command", "down?")
elseif( love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") ) then
client:send("command", "down+")
elseif( love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") ) then
client:send("command", "down-")
else
client:send("command", "down")
end
--moveMap(0, 0.2 * tileSize * dt)
adjustMap()
end
if love.keyboard.isDown("left") then
if( love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") ) then
client:send("command", "left?")
elseif( love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") ) then
client:send("command", "left+")
elseif( love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") ) then
client:send("command", "left-")
else
client:send("command", "left")
end
--moveMap(-0.2 * tileSize * dt, 0)
adjustMap()
end
if love.keyboard.isDown("right") then
if( love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") ) then
client:send("command", "right?")
elseif( love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") ) then
client:send("command", "right+")
elseif( love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") ) then
client:send("command", "right-")
else
client:send("command", "right")
end
--moveMap(0.2 * tileSize * dt, 0)
adjustMap()
end
for k, v in pairs( SG.emittersPre ) do
v[1]:update(dt)
if( love.timer.getTime() >= v[4] and v[1]:getCount() <= 0 ) then
SG.emittersPre[k] = nil
end
end
for k, v in pairs( SG.emittersPost ) do
v[1]:update(dt)
if( love.timer.getTime() >= v[4] and v[1]:getCount() <= 0 ) then
SG.emittersPost[k] = nil
end
end
client:update()
TEsound.cleanup()
if( not mouseHide ) then
mouseHide = love.timer.getTime();
mouseX = 0;
mouseY = 0;
end
if( love.timer.getTime() >= mouseHide ) then
--love.mouse.setVisible(false)
end
end
function love.keypressed(key)
if( not SG.keybinds[key] ) then
return;
end
local control = SG.keybinds[key]
if( control == "chat" ) then
if( chatting ) then
chatting = false
client:send("chat", chatstring)
else
chatting = true
end
chatstring = ""
love.keyboard.setKeyRepeat(chatting)
elseif( control == "chatbackspace" ) then
local byteoffset = utf8.offset(chatstring, -1)
if byteoffset then
chatstring = string.sub(chatstring, 1, byteoffset - 1)
TEsound.play("sound/prespeech.wav", "static", {}, 1, 0.5)
end
elseif( not chatting ) then
if( control == "use" ) then
if( not inventoryOpen and not inventorySlot and ( not client.mob.hilite_id or client.mob.hilite_id == 0 ) ) then
TEsound.play("sound/cantuse.wav", "static", {}, 1, 1)
else
if( inventorySlot ) then
client:send("useInventory", inventorySlot)
else
client:send("command", "use")
end
end
elseif( control == "inventory" ) then
if( inventoryOpen ) then
TEsound.play("sound/inv_close.wav", "static", {}, 1, 1)
else
TEsound.play("sound/inv_open.wav", "static", {}, 1, 1)
end
inventoryOpen = not inventoryOpen
inventorySlot = nil
elseif( control == "drop" ) then
if( inventoryOpen and inventorySlot ) then
if( client.mob.inventory[inventorySlot] and client.mob.inventory[inventorySlot].id ) then
client:send("drop", client.mob.inventory[inventorySlot].id)
else
TEsound.play("sound/cantuse.wav", "static", {}, 1, 1)
end
else
if( client.mob.inventory.wielded and client.mob.inventory.wielded.id ) then
client:send("drop", client.mob.inventory.wielded.id)
elseif( client.mob.inventory.offhand and client.mob.inventory.offhand.id ) then
client:send("drop", client.mob.inventory.offhand.id)
else
TEsound.play("sound/cantuse.wav", "static", {}, 1, 1)
end
end
elseif( inventoryOpen ) then
local invslots = {
["invslotwielded"] = "wielded",
["invslotoffhand"] = "offhand",
["invslotheadwear"] = "headwear",
["invslottorsowear"] = "torsowear",
["invslotlegwear"] = "legwear",
["invslotneckwear"] = "neckwear",
["invslotvessel"] = "vessel",
["invslottrinket"] = "trinket",
};
if( invslots[control] ) then
if( inventorySlot == invslots[control] ) then
inventorySlot = nil;
TEsound.play("sound/inv_emptyslot.wav", "static", {}, 2, 1)
else
inventorySlot = invslots[control]
if( not client.mob.inventory[inventorySlot] or not client.mob.inventory[inventorySlot].id ) then
TEsound.play("sound/inv_emptyslot.wav", "static", {}, 1, 1)
end
end
end
end
end
end
function love.textinput(key)
if( not client.mob ) then
return;
end
local control = SG.keybinds[key]
if( chatting ) then
if( utf8.len(chatstring) >= 80 ) then
TEsound.play("sound/prespeech.wav", "static", {}, 1, 0.2)
else
chatstring = chatstring .. key
TEsound.play("sound/prespeech.wav", "static", {}, 1, 0.75)
end
end
end
function love.mousemoved(x, y)
mouseHide = love.timer.getTime() + 3;
local x, y = love.mouse.getPosition();
local nx, ny = mapX + math.floor( x / 40 ), mapY + math.floor( y / 40 );
local shouldUpdate = false;
if( nx ~= mouseX ) then
mouseX = nx;
shouldUpdate = true;
end
if( ny ~= mouseY ) then
mouseY = ny;
shouldUpdate = true;
end
if( shouldUpdate ) then
interestedEye = false;
for k, v in pairs( SG.atomicTable ) do
if( v.x == mouseX and v.y == mouseY ) then
interestedEye = true;
break;
end
end
end
--love.mouse.setVisible(true)
end
function love.mousepressed(x, y)
for k, v in pairs( SG.atomicTable ) do
if( v.x == mouseX and v.y == mouseY ) then
mouseHide = love.timer.getTime() + 2.5;
describingWhen = nil;
describingObject = v;
recognizedCoord = { v.x, v.y };
break;
end
end
end