-
Notifications
You must be signed in to change notification settings - Fork 0
/
Architect.lua
581 lines (532 loc) · 15.8 KB
/
Architect.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
-- architect/Architect.lua
-- /lua require('architect.Architect'); Architect('mickkay'):start()
local module = ...
local log
local MAY_SET_BLOCK_EVENT = "architect.MaySetBlockEvent"
require "architect.Spell"
clipboard = require "architect.clipboard"
construction = require "architect.construction"
-- Declare a new class "Architect"
declare("Architect")
-- This declares a convenient 'constructor' delegating to Architect.new()
-- Use it like this: Architect('mickkay')
local mt = {
__call = function(tbl,player)
return Architect.new(player)
end;
}
setmetatable(Architect,mt)
-- Some constants
local SYNC = "SYNC"
local ASYNC = "ASYNC"
local tools = {
OFF = "", BAR = "bar", FLOOR = "floor", WALL = "wall",
COPY = "copy", PASTE = "paste", REPLACE = "replace", FILL = "fill",
DELETE = "delete", CUT = "cut"
}
-- The constructor
function Architect.new(player)
player = player or spell.owner
if type(player)=="string" then
local p = Entities.find("@a[name="..player.."]")[1]
if p == nil then
error("Can't find player '%s'",player)
else
player = p
end
end
if not instanceOf(Player,player) then
error("Expected Player instance, but got: %s", type(player))
end
local self = {
playerName = player.name ,
player = player ,
tool = "OFF" ,
mode = ASYNC ,
facing = player.facing ,
castCount = 0 ,
options = {
BAR = { length=4 } ,
FLOOR = { maxsel=16*16*16 } ,
WALL = { maxsel=16*16*16 } ,
COPY = { maxsel=16*16*16 } ,
PASTE = { } ,
REPLACE = { maxsel=16*16*16 } ,
FILL = { maxsel=16*16*16 } ,
DELETE = { maxsel=16*16*16, radius=8 },
CUT = { maxsel=16*16*16}
}
}
setmetatable(self,Architect)
return self
end
-- Starts the spell loop
function Architect:start()
spell:singleton(module.."-"..self.playerName)
self:whisper("Welcome Architect "..self.playerName..".")
self:whisper("Select your tool by typing its name into the chat.")
self:whisper("Available tools are "..self:getToolNames()..".")
self:whisper("Show your current tool by typing TOOL.")
self:whisper("Current tool is "..self.tool..".")
Events.on("ChatEvent"):call(
function(event)
if event.player.name==self.playerName then
if event.name=="ChatEvent" then
local message = event.message
if message=="TOOL" then
event.canceled = true
self:tellTool()
elseif tools[message] then
self.tool = message
event.canceled = true
self:tellTool()
else
if message:match("BAR %d+") then
self.tool = "BAR"
local val = tonumber(message:match("%d+"))
self.options[self.tool].length = val
event.canceled = true
self:tellTool()
elseif message:match("FLOOR %d+") then
self.tool = "FLOOR"
local val = tonumber(message:match("%d+"))
self.options[self.tool].maxsel = val
event.canceled = true
self:tellTool()
elseif message:match("COPY %d+") then
self.tool = "COPY"
local val = tonumber(message:match("%d+"))
self.options[self.tool].maxsel = val
event.canceled = true
self:tellTool()
elseif message:match("CUT %d+") then
self.tool = "CUT"
local val = tonumber(message:match("%d+"))
self.options[self.tool].maxsel = val
event.canceled = true
self:tellTool()
elseif message:match("REPLACE %d+") then
self.tool = "REPLACE"
local val = tonumber(message:match("%d+"))
self.options[self.tool].maxsel = val
event.canceled = true
self:tellTool()
elseif message:match("FILL %d+") then
self.tool = "FILL"
local val = tonumber(message:match("%d+"))
self.options[self.tool].maxsel = val
event.canceled = true
self:tellTool()
elseif message:match("DELETE %d+") then
self.tool = "DELETE"
local val = tonumber(message:match("%d+"))
self.options[self.tool].radius = val
event.canceled = true
self:tellTool()
end
end
end
end
end
)
local queue = Events.collect("RightClickBlockEvent")
while true do
local event = queue:next()
if event.player.name==self.playerName then
--print("Click",self.tool,event.pos)
if self.tool~="OFF" and event.player.mainhand ~= nil then
self:handle(event)
end
end
end
end
function Architect:tellTool()
self:whisper("Current Tool is "..self.tool)
end
-- Handles the given event
function Architect:handle(event)
if self.player.gamemode ~= "survival" and self.player.gamemode ~= "creative" then
self:whisper("You must be in 'creative' or 'survial' mode to be an architect, sorry!")
return
end
if self.mode==ASYNC then
-- handle the event in another spell
self.castCount = self.castCount+1
local channel = module.."-"..self.playerName.."-"..self.castCount
local cmd = "lua require('%s'); Architect('%s'):worker('%s')"
spell:execute(cmd, module, self.playerName, channel)
sleep(1) -- wait 1 tick to ensure that the receiver has time to get connected
Events.fire(channel, {
tool = self.tool ,
options = self.options[self.tool] ,
event = event ,
facing = event.player.facing
})
elseif self.mode==SYNC then
-- handle the event in this spell
local fName = tools[self.tool]
local func = Architect[fName]
local options = self.options[self.tool]
func(self,event,options)
else
error("Unknown mode %s",self.mode)
end
end
-- Send a message to the architect
function Architect:whisper(message)
spell:execute('/tellraw %s [{"text":"Architect: ","color":"gold"},{"text":"%s","color":"green"}]',self.playerName, message)
end
-- Returns a string with all tool names
function Architect:getToolNames()
local result = ""
for k,v in pairs(tools) do
if result~="" then
result = result..", "
end
result = result..k
end
return result
end
-- Handles the next event transmitted on the given channel
function Architect:worker(channel)
self.mode = SYNC
local queue = Events.collect(channel)
local event = queue:next(20)
if event then
self.tool = event.data.tool
self.facing = event.data.facing
self.options[self.tool] = event.data.options
self:handle(event.data.event)
else
error("Can't do my work. Timeout occured!")
end
end
-- Creates a bar of blocks
function Architect:bar(event,options)
local n = options.length
spell.pos = event.pos
spell:move(event.face)
local b = spell.block
for i=1,n-1 do
spell:move(event.face)
--spell.block = b
self:trySetBlock(b)
end
end
local SAME_X = {}
function SAME_X.neighbors(pos)
return {
Vec3(pos.x, pos.y+1, pos.z),
Vec3(pos.x, pos.y-1, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1)
}
end
local SAME_Y = {}
function SAME_Y.neighbors(pos)
return {
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1)
}
end
local SAME_Z = {}
function SAME_Z.neighbors(pos)
return {
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y+1, pos.z),
Vec3(pos.x, pos.y-1, pos.z)
}
end
local ANY_DIRECTION = {}
function ANY_DIRECTION.neighbors(pos)
return {
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y+1, pos.z),
Vec3(pos.x, pos.y-1, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1)
}
end
local function SAME_OR_ABOVE_Y(y)
return {
neighbors = function(pos)
local result = {
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1),
Vec3(pos.x, pos.y+1, pos.z)
}
if pos.y>y then
table.insert(result,Vec3(pos.x, pos.y-1, pos.z))
end
return result
end
}
end
local function SAME_OR_BELOW_Y(y)
return {
neighbors = function(pos)
local result = {
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1),
Vec3(pos.x, pos.y-1, pos.z)
}
if pos.y<y then
table.insert(result,Vec3(pos.x, pos.y+1, pos.z))
end
return result
end
}
end
local function WITHIN_RADIUS(center,r)
local rsqr=r*r
return {
neighbors = function(pos)
local result = {}
for _,v in pairs({
Vec3(pos.x+1, pos.y, pos.z),
Vec3(pos.x-1, pos.y, pos.z),
Vec3(pos.x, pos.y, pos.z+1),
Vec3(pos.x, pos.y, pos.z-1),
Vec3(pos.x, pos.y+1, pos.z),
Vec3(pos.x, pos.y-1, pos.z)
}) do
if (v-center):sqrMagnitude() <= rsqr then
table.insert(result,v)
end
end
return result
end
}
end
local NOT_SOLID = {}
function NOT_SOLID.matches(pos)
spell.pos = pos
return not spell.block.material.solid
end
local SOLID = {}
function SOLID.matches(pos)
spell.pos = pos
return spell.block.material.solid
end
local ALL_BUT_AIR = {}
function ALL_BUT_AIR.matches(pos)
spell.pos = pos
return spell.block.name~="air"
end
local function EQUAL(block)
return {
matches = function(pos)
spell.pos = pos
-- TODO also compare data
return spell.block.name==block.name
end
}
end
-- Fills an area with a floor
function Architect:floor(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
spell:move(event.face)
local block = spell.block
local posL = self:selectblocks(spell.pos, maxsel, SAME_Y, NOT_SOLID)
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = block
self:trySetBlock(block)
end
end
local areaByFace = {
south=SAME_X, west=SAME_Z, north=SAME_X, east=SAME_Z
}
-- Fills an area with a wall
function Architect:wall(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
spell:move(event.face)
--print("wall",maxsel, spell.pos)
local area = areaByFace[event.face]
if not area then
error("Can't select blocks when face is '%s'", event.face)
end
local block = spell.block
local posL = self:selectblocks(spell.pos, maxsel, area, NOT_SOLID)
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = block
self:trySetBlock(block)
end
end
-- Copies an area into the clipboard
function Architect:copy(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
spell:move(event.face)
--spell.block = Blocks.get("air")
self:trySetBlock(Blocks.get("air"))
spell.pos = event.pos
--print("copy",maxsel, spell.pos)
--local vecS = self:selectblocks(spell.pos, maxsel, SAME_OR_ABOVE_Y(spell.pos.y), SOLID)
local vecS = self:selectblocks(spell.pos, maxsel, SAME_OR_ABOVE_Y(spell.pos.y), ALL_BUT_AIR)
local origin = event.pos
local blocks = {}
for i,vec in pairs(vecS) do
spell.pos = vec
blocks[i] = {
pos = vec-origin ,
state = spell.block:copy()
}
end
local snapshot = {
pivot = Vec3(0,0,0) ,
facing = self.facing ,
blocks = blocks
}
clipboard.putClip(snapshot,self.playerName.."-clipboard")
end
local rot={south=0, west=90, north=180, east=270}
-- Pastes the contents of the clipboard
function Architect:paste(event,options)
spell.pos = event.pos
spell.rotationYaw = rot[self.facing]
spell:move(event.face)
spell.block = Blocks.get("air")
--print("paste",maxsel, spell.pos)
local clip = clipboard.getClip(self.playerName.."-clipboard")
local func = function(blk ) self:trySetBlock(blk) end
construction.paste(clip, func)
end
-- Replaces all connected blocks equal to the clicked one with a copy of the created one
function Architect:replace(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
local oldBlock = spell.block
spell:move(event.face)
local newBlock = spell.block:copy();
spell.block = Blocks.get("air")
--print("replace",maxsel, spell.pos)
local posL = self:selectblocks(event.pos, maxsel, ANY_DIRECTION, EQUAL(oldBlock))
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = newBlock
self:trySetBlock(newBlock)
end
end
-- Fills an area complete
function Architect:fill(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
spell:move(event.face)
local block = spell.block
local posL = self:selectblocks(spell.pos, maxsel, SAME_OR_BELOW_Y(spell.pos.y), NOT_SOLID)
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = block
self:trySetBlock(block)
end
end
-- Deletes all connected blocks equal to the clicked one in a given radius
function Architect:delete(event,options)
local maxsel = options.maxsel
local radius = options.radius
spell.pos = event.pos
local oldBlock = spell.block
spell:move(event.face)
local newBlock = Blocks.get("air")
spell.block = Blocks.get("air")
--print("replace",maxsel, spell.pos)
local posL = self:selectblocks(event.pos, maxsel, WITHIN_RADIUS(event.pos,radius), EQUAL(oldBlock))
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = newBlock
self:trySetBlock(newBlock)
end
end
-- Cuts an area from the world and copies it into the clipboard
function Architect:cut(event,options)
local maxsel = options.maxsel
spell.pos = event.pos
spell:move(event.face)
local newBlock = Blocks.get("air")
--spell.block = Blocks.get("air")
self:trySetBlock(Blocks.get("air"))
spell.pos = event.pos
--print("cut",maxsel, spell.pos)
--local vecS = self:selectblocks(spell.pos, maxsel, SAME_OR_ABOVE_Y(spell.pos.y), SOLID)
local posL = self:selectblocks(spell.pos, maxsel, SAME_OR_ABOVE_Y(spell.pos.y), ALL_BUT_AIR)
local origin = event.pos
local blocks = {}
for i,pos in pairs(posL) do
spell.pos = pos
blocks[i] = {
pos = pos-origin ,
state = spell.block:copy()
}
end
local snapshot = {
pivot = Vec3(0,0,0) ,
facing = self.facing ,
blocks = blocks
}
for i,pos in pairs(posL) do
spell.pos = pos
--spell.block = newBlock
self:trySetBlock(newBlock)
end
clipboard.putClip(snapshot,self.playerName.."-clipboard")
end
-- Selects a list of block positions
function Architect:selectblocks(start, maxsel, area, matcher)
local original = spell.pos
local result = {}
local selected = 0
local done = {}
local todo = {}
table.insert(todo,start)
while next(todo) do
local pos = table.remove(todo,1)
local pkey = pos:tostring()
if not done[pkey] then
table.insert(result,pos)
done[pkey] = true
selected = selected+1
if selected > maxsel then
error("Can't select more than %s blocks!", maxsel)
end
local neighbors = area.neighbors(pos)
for i,npos in pairs(neighbors) do
local nkey = npos:tostring()
if not done[nkey] then
if matcher.matches(npos) then
table.insert(todo,npos)
end
end
end
end
end
spell.pos = original
return result
end
function Architect:trySetBlock(block)
--spell.block = block
local data = {pos = spell.pos, player = self.player, result = true, block = block}
Events.fire(MAY_SET_BLOCK_EVENT,data)
if data.result then
spell.block = block
end
end
-- Logs the given message into the chat
function log(message, ...)
local n = select('#', ...)
if n>0 then
message = string.format(message, ...)
end
spell:execute("say %s", message)
end