-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
474 lines (466 loc) · 13.4 KB
/
init.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
--@name Printer farm
--@server
--@include ./client.lua
--@clientmain ./client.lua
--@include ./shared.lua
local pf = dofile('./shared.lua')
function pf.tprint(target, ...)
net.start(pf.ID_NET)
net.writeUInt(pf.NET_PRINT, pf.NET_BITS)
for i=1, select('#', ...) do
local v = select(i, ...)
if v == nil then
break
elseif type(v) == 'Color' then
net.writeBit(1)
net.writeUInt(v[1], 8)
net.writeUInt(v[2], 8)
net.writeUInt(v[3], 8)
elseif isstring(v) then
net.writeBit(0)
net.writeString(v)
else
error(string.format("bad argument #%d to '%s' (Color or string expected, got %s)", i+1, debug.getinfo(1, 'n').name, type(v)), 2)
end
end
net.send(target)
end
function pf.tprintf(target, ...)
return pf.tprint(target, string.format(...))
end
function pf.print(...)
return pf.tprint(nil, ...)
end
pf.command_prefix = string.format("$%d ", chip():entIndex())
pf.commands = {}
pf.command_help = {}
hook.add('PlayerSay', pf.ID_HOOK, function(sender, message, is_team)
if string.sub(message, 1, #pf.command_prefix) == pf.command_prefix then
message = string.sub(message, #pf.command_prefix+1)
else
return
end
local first_space = string.find(message, ' ', nil, true)
local command = string.lower(first_space == nil and message or string.sub(message, 1, first_space-1))
local command_func = pf.commands[command]
if command_func ~= nil then
local parameters = first_space == nil and "" or string.sub(message, first_space+1)
local success, retval = command_func(sender, command, parameters, is_team)
if not success then
if type(retval) == 'table' then
retval = rawget(retval, 'message')
end
retval = tostring(retval)
pf.tprint(sender, retval)
end
return ""
end
pf.tprintf(sender, "Unknown command %q.", command)
return ""
end)
pf.commands.help = function(sender, command, parameters, is_team)
if parameters ~= '' then
local help = pf.command_help[parameters]
if help == nil then
return false, "No such command %q."
end
pf.tprintf(sender, "Help for %q: %s", parameters, help)
return true
end
local commands_list = {}
for k in pairs(pf.commands) do
table.insert(commands_list, k)
end
table.sort(commands_list)
commands_list = table.concat(commands_list, ", ")
pf.tprintf(sender, "Available commands: %s", commands_list)
return true
end
pf.command_help.help = "Get documentation for command, or list all commands if none specified."
pf.commands.l = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
local func, err = loadstring('local pf, me, this, there = ...\n'..parameters, "pf_l")
if func ~= nil and type(func) ~= 'function' then
func, err = nil, func
end
if func == nil then
if type(err) == 'table' then
err = rawget(err, 'message')
end
err = tostring(err)
return false, "Compilation error: "..err
end
local trace = sender:getEyeTrace()
local success
success, err = pcall(func, pf, sender, trace.Entity, trace.HitPos)
if not success then
if type(err) == 'table' then
err = rawget(err, 'message')
end
err = tostring(err)
return false, "Runtime error: "..err
end
return true
end
pf.command_help.l = "Run Lua code."
pf.aabbs = {}
if game.getMap() == 'rp_construct_b5' then
table.insert(pf.aabbs, {Vector(-1352, -2560, 48), Vector(-864, -2368, 176)})
table.insert(pf.aabbs, {Vector(-1104, -2368, 48), Vector(-864, -2080, 176)})
end
pf.commands.aabb_list = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
local aabbs = pf.aabbs
if #aabbs == 0 then
pf.tprint(sender, "No AABBs.")
return true
end
for i=1, #aabbs do
local aabb = aabbs[i]
local mins, maxs = aabb[1], aabb[2]
pf.tprintf(sender, "%d: Vector(%s, %s, %s), Vector(%s, %s, %s)", i, mins[1], mins[2], mins[3], maxs[1], maxs[2], maxs[3])
end
return true
end
pf.command_help.aabb_list = "List AABBs."
pf.commands.aabb_clear = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
pf.aabbs = {}
pf.tprint(sender, "Cleared.")
return true
end
pf.command_help.aabb_clear = "Clear list of AABBs."
local function parse_vector(parameter)
if string.sub(parameter, -1, -1) == ')' then
if string.sub(parameter, 1, 7) == 'Vector(' then
parameter = string.sub(parameter, 8, -2)
elseif string.sub(parameter, 1, 1) == '(' then
parameter = string.sub(parameter, 2, -2)
end
end
local first_comma = string.find(parameter, ',', 1, true)
if first_comma == nil then
return nil
end
local second_comma = string.find(parameter, ',', first_comma+1, true)
if second_comma == nil then
return nil
end
local x = tonumber(string.sub(parameter, 1, first_comma-1))
local y = tonumber(string.sub(parameter, first_comma+1, second_comma-1))
local z = tonumber(string.sub(parameter, second_comma+1))
if x == nil or y == nil or z == nil then
return nil
end
return Vector(x, y, z)
end
pf.commands.aabb_add = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
local first_space = string.find(parameters, ' ', 1, true)
if first_space == nil then
return false, "Malformed parameters."
end
local mins = parse_vector(string.sub(parameters, 1, first_space-1))
local maxs = parse_vector(string.sub(parameters, first_space+1))
if mins == nil or maxs == nil then
return false, "Malformed parameters."
end
local minx, miny, minz = math.min(mins[1], maxs[1]), math.min(mins[2], maxs[2]), math.min(mins[3], maxs[3])
local maxx, maxy, maxz = math.max(mins[1], maxs[1]), math.max(mins[2], maxs[2]), math.max(mins[3], maxs[3])
mins, maxs = Vector(minx, miny, minz), Vector(maxx, maxy, maxz)
table.insert(pf.aabbs, {mins, maxs})
pf.tprint(sender, "Added.")
return true
end
pf.command_help.aabb_add = "Add AABB to list."
pf.commands.aabb_remove = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
local i = tonumber(parameters)
if i == nil then
return false, "Malformed parameters."
end
local aabbs = pf.aabbs
if aabbs[i] == nil then
return false, "No such AABB."
end
table.remove(aabbs, i)
pf.tprint(sender, "Removed.")
return true
end
pf.command_help.aabb_remove = "Remove AABB from list."
hook.add('ClientInitialized', pf.ID_HOOK, function(ply)
if ply ~= owner() then
return
end
pf.tprintf(ply, "Run \"%shelp\" for commands.", pf.command_prefix)
end)
function pf.is_entity_in_aabb(printer)
local pos = printer:obbCenterW()
local posx, posy, posz = pos[1], pos[2], pos[3]
local aabbs = pf.aabbs
for i=1, #aabbs do
local aabb = aabbs[i]
local mins, maxs = aabb[1], aabb[2]
local minx, miny, minz = mins[1], mins[2], mins[3]
local maxx, maxy, maxz = maxs[1], maxs[2], maxs[3]
if (
posx >= minx and posx < maxx
and posy >= miny and posy < maxy
and posz >= minz and posz < maxz
) then
return true, i
end
end
return false
end
function pf.extinguish_update()
local extinguishing = pf.extinguishing
net.start(pf.ID_NET)
net.writeUInt(extinguishing, pf.NET_BITS)
if extinguishing == pf.NET_EXTINGUISHING_PRE then
net.writeEntity(pf.extinguishee)
end
net.send(pf.extinguisher)
end
pf.seat_pos = Vector(0, 0, 0)
pf.seat = prop.createSeat(pf.seat_pos, Angle(), 'models/hunter/plates/plate.mdl', true)
pf.seat:setNoDraw(false)
pf.seat:setSolid(false)
pf.seat:setColor(Color(0, 0, 0, 0))
pf.seat:setDrawShadow(false)
function pf.extinguish_teleport(sender, pos, target)
if not pcall(sender.setPos, sender, pos) then
local seat = pf.seat
local eyeangles_old = sender:getEyeAngles()
seat:setPos(pos)
seat:use()
seat:ejectDriver()
seat:setPos(pf.seat_pos)
pcall(sender.setEyeAngles, sender, eyeangles_old)
end
end
function pf.extinguish(extinguishee, extinguisher)
if extinguisher == nil then
extinguisher = owner()
end
pf.extinguisher = extinguisher
pf.extinguishee = extinguishee
pf.extinguishing = pf.NET_EXTINGUISHING_PRE
pf.extinguish_update()
timer.simple(2, function()
if not isValid(extinguisher) or not isValid(extinguishee) then
return
end
pf.extinguishing = pf.NET_EXTINGUISHING_TELEPORT
pf.extinguish_update()
local pos_old = extinguisher:getPos()
pf.extinguish_teleport(extinguisher, extinguishee:getPos())
timer.simple(1, function()
if not isValid(extinguisher) or not isValid(extinguishee) then
return
end
pf.extinguishing = pf.NET_EXTINGUISHING_TELEPORTPOST
pf.extinguish_update()
timer.simple(0.5, function()
if not isValid(extinguisher) or not isValid(extinguishee) then
return
end
pf.extinguishing = pf.NET_EXTINGUISHING_POCKET
pf.extinguish_update()
timer.simple(1, function()
if not isValid(extinguisher) then
return
end
pf.extinguishing = pf.NET_EXTINGUISHING_UNPOCKET
pf.extinguish_update()
timer.simple(1, function()
if not isValid(extinguisher) then
return
end
pf.extinguish_teleport(extinguisher, pos_old)
timer.simple(0.5, function()
pf.extinguishing = pf.NET_EXTINGUISHING_NULL
pf.extinguish_update()
end)
end)
end)
end)
end)
end)
end
pf.extinguish_queue = {}
function pf.extinguish_tick()
if pf.extinguishing ~= pf.NET_EXTINGUISHING_NULL then
return
end
local queue = pf.extinguish_queue
for i=#queue, 1, -1 do
local printer, extinguisher = queue[i]
if type(printer) ~= 'Entity' then
printer, extinguisher = printer[1], printer[2]
end
if isValid(printer) then
pf.extinguish(printer, extinguisher)
queue[i] = nil
break
else
pf.printf("Invalid printer %q in queue at %d.", tostring(printer), i)
queue[i] = nil
end
end
end
pf.commands.extinguish = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized."
end
local target = tonumber(parameters)
if target == nil then
return false, "Malformed parameters."
end
target = entity(target)
if not isValid(target) then
return false, "Invalid entity."
end
table.insert(pf.extinguish_queue, {target, sender})
return true
end
pf.command_help.extinguish = "Manually trigger an extinguishing of a printer."
hook.add('moneyPrinterCatchFire', pf.ID_HOOK, function(printer)
if not pf.is_entity_in_aabb(printer) then
return
end
if pf.VERBOSE then
pf.tprintf(owner(), "moneyPrinterCatchFire: %s", tostring(printer))
end
table.insert(pf.extinguish_queue, printer)
end)
wire.adjustPorts({User='entity'}, {Use='number'})
function pf.collect(printer)
local ports = wire.ports
local user = ports.User
if not isValid(user) then
return
end
local pos_old = user:getPos()
local angles_old = user:getAngles()
local frozen_old = user:isFrozen()
user:setFrozen(true)
user:setPos(printer:obbCenterW())
user:setAngles(Angle())
ports.Use = 1
ports.Use = 0
user:setPos(pos_old)
user:setAngles(angles_old)
user:setFrozen(frozen_old)
end
pf.collect_queue = {}
pf.start_time = timer.curtime()
pf.total_money_collected = 0
hook.add('moneyPrinterPrinted', pf.ID_HOOK, function(printer, bag)
if not pf.is_entity_in_aabb(printer) then
return
end
pf.tprintf(owner(), "moneyPrinterPrinted: %s, %s", tostring(printer), tostring(bag))
end)
hook.add('moneyPrinterPrintMoney', pf.ID_HOOK, function(printer, amount)
if not pf.is_entity_in_aabb(printer) then
return
end
if pf.VERBOSE then
pf.tprintf(owner(), "moneyPrinterPrintMoney: %s, %s", tostring(printer), darkrp.formatMoney(amount))
end
pf.total_money_collected = pf.total_money_collected+amount
table.insert(pf.collect_queue, printer)
end)
pf.every_tick = true
pf.is_on_fire = {}
pf.collect_order = {}
function pf.collect_tick()
local collect = pf.collect
if pf.every_tick then
local is_entity_in_aabb = pf.is_entity_in_aabb
local is_on_fire = pf.is_on_fire
local extinguish_queue = pf.extinguish_queue
local collect_order = pf.collect_order
local coi = pf.collect_order_i
if coi == nil then
coi = 1
else
coi = coi+1
end
local coj = #collect_order
if coi >= coj and coj ~= 0 then
coi = (coi-1)%coj+1
end
local coe = collect_order[coi]
pf.collect_order_i = coi
for i=1, #collect_order do
local printer = collect_order[i]
if isValid(printer) then
if printer:isOnFire() and not is_on_fire[printer] then
is_on_fire[printer] = true
table.insert(extinguish_queue, printer)
end
if printer == coe then
collect(printer)
end
end
end
for printer in pairs(is_on_fire) do
if not printer:isValid() or not printer:isOnFire() then
is_on_fire[printer] = nil
end
end
return
end
local queue = pf.collect_queue
local isValid = isValid
for i=#queue, 1, -1 do
local printer = queue[i]
if isValid(printer) then
collect(printer)
queue[i] = nil
else
queue[i] = nil
end
end
end
timer.create(pf.ID_TIMER, 1, 0, function()
if not pf.every_tick then
return
end
local is_entity_in_aabb = pf.is_entity_in_aabb
local collect_order = {}
local printers = find.byClass('scriptis_printer')
for i=1, #printers do
local printer = printers[i]
if is_entity_in_aabb(printer) then
table.insert(collect_order, printer)
end
end
pf.collect_order = collect_order
end)
pf.commands.stats = function(sender, command, parameters, is_team)
if sender ~= owner() then
return false, "Not authorized"
end
local now = timer.curtime()
local uptime = now-pf.start_time
pf.tprintf(sender, "Collected a total of %s over %s.", darkrp.formatMoney(pf.total_money_collected), string.niceTime(uptime))
return true
end
pf.command_help.stats = "Display total collected money and uptime."
hook.add('tick', pf.ID_HOOK, function()
pf.extinguish_tick()
pf.collect_tick()
end)