summaryrefslogtreecommitdiff
path: root/mesecons.lua
blob: 3a70dc7a036c0ac791207bb8004feb74f9dc47a1 (plain)
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
local iorules = {
	vector.new(-1,0,0),
	vector.new(1,0,0),
	vector.new(0,0,-1),
	vector.new(0,0,1),
}

local function getpos(mem,searchpos,pioffset)
	local carpos = 0
	if not searchpos then searchpos = mem.drive.status.apos end
	if pioffset then searchpos = searchpos+0.5 end
	for k,v in ipairs(mem.params.floorheights) do
		carpos = carpos+v
		if carpos > searchpos then
			return k
		end
	end
end

local outputoptions = {
	{
		id = "normal",
		desc = "Normal Operation",
		func = function(mem)
			return (mem.carstate == "normal")
		end,
		needsfloor = false,
	},
	{
		id = "fault",
		desc = "Fault",
		func = function(mem)
			return (mem.carstate == "fault")
		end,
		needsfloor = false,
	},
	{
		id = "estop",
		desc = "Emergency Stop",
		func = function(mem)
			return (mem.carstate == "stop")
		end,
		needsfloor = false,
	},
	{
		id = "inspect",
		desc = "Inspection (Any)",
		func = function(mem)
			return (mem.carstate == "mrinspect" or mem.carstate == "inspconflict" or mem.carstate == "carinspect")
		end,
		needsfloor = false,
	},
	{
		id = "fire",
		desc = "Fire Service",
		func = function(mem)
			return (mem.carstate == "fs1" or mem.carstate == "fs2" or mem.carstate == "fs2hold")
		end,
		needsfloor = false,
	},
	{
		id = "fire1",
		desc = "Fire Service Phase 1",
		func = function(mem)
			return (mem.carstate == "fs1")
		end,
		needsfloor = false,
	},
	{
		id = "fire2",
		desc = "Fire Service Phase 2",
		func = function(mem)
			return (mem.carstate == "fs2" or mem.carstate == "fs2hold")
		end,
		needsfloor = false,
	},
	{
		id = "independent",
		desc = "Independent Service",
		func = function(mem)
			return (mem.carstate == "indep")
		end,
		needsfloor = false,
	},
	{
		id = "opening",
		desc = "Doors Opening",
		func = function(mem)
			return (mem.doorstate == "opening")
		end,
		needsfloor = false,
	},
	{
		id = "open",
		desc = "Doors Open",
		func = function(mem)
			return (mem.doorstate == "open")
		end,
		needsfloor = false,
	},
	{
		id = "closing",
		desc = "Doors Closing",
		func = function(mem)
			return (mem.doorstate == "closing")
		end,
		needsfloor = false,
	},
	{
		id = "closed",
		desc = "Doors Closed",
		func = function(mem)
			return (mem.doorstate == "closed")
		end,
		needsfloor = false,
	},
	{
		id = "moveup",
		desc = "Moving Up",
		func = function(mem)
			return (mem.drive.status and mem.drive.status.vel > 0)
		end,
		needsfloor = false,
	},
	{
		id = "movedown",
		desc = "Moving Down",
		func = function(mem)
			return (mem.drive.status and mem.drive.status.vel < 0)
		end,
		needsfloor = false,
	},
	{
		id = "moving",
		desc = "Moving (Any Direction)",
		func = function(mem)
			return (mem.drive.status and mem.drive.status.vel ~= 0)
		end,
		needsfloor = false,
	},
	{
		id = "collectorup",
		desc = "Collecting Up Calls",
		func = function(mem)
			return (mem.carstate == "normal" and mem.direction == "up")
		end,
		needsfloor = false,
	},
	{
		id = "collectordown",
		desc = "Collecting Down Calls",
		func = function(mem)
			return (mem.carstate == "normal" and mem.direction == "down")
		end,
		needsfloor = false,
	},
	{
		id = "lightsw",
		desc = "Car Light Switch",
		func = function(mem)
			return mem.lightsw
		end,
		needsfloor = false,
	},
	{
		id = "fansw",
		desc = "Car Fan Switch",
		func = function(mem)
			return mem.fansw
		end,
		needsfloor = false,
	},
	{
		id = "upcall",
		desc = "Up Call Exists at Landing:",
		func = function(mem,floor)
			return mem.upcalls[floor]
		end,
		needsfloor = true,
	},
	{
		id = "downcall",
		desc = "Down Call Exists at Landing:",
		func = function(mem,floor)
			return mem.dncalls[floor]
		end,
		needsfloor = true,
	},
	{
		id = "carcall",
		desc = "Car Call Exists at Landing:",
		func = function(mem,floor)
			return mem.carcalls[floor]
		end,
		needsfloor = true,
	},
	{
		id = "apos",
		desc = "Car at Landing:",
		func = function(mem,floor)
			if mem.drive.status then
				return (getpos(mem,nil,true) == floor)
			end
		end,
		needsfloor = true,
	},
	{
		id = "dpos",
		desc = "Moving to Landing:",
		func = function(mem,floor)
			if mem.drive.status then
				return (getpos(mem,mem.drive.status.dpos) == floor)
			end
		end,
		needsfloor = true,
	},
}

local inputoptions = {
	{
		id = "carcall",
		desc = "Car Call at Landing:",
		func_on = function(controllerpos,floor)
			celevator.controller.run(controllerpos,{
				type = "remotemsg",
				channel = "carcall",
				msg = floor,
			})
		end,
		needsfloor = true,
	},
	{
		id = "upcall",
		desc = "Up Call (simplex car) at Landing:",
		func_on = function(controllerpos,floor)
			celevator.controller.run(controllerpos,{
				type = "remotemsg",
				channel = "upcall",
				msg = floor,
			})
		end,
		needsfloor = true,
	},
	{
		id = "downcall",
		desc = "Down Call (simplex car) at Landing:",
		func_on = function(controllerpos,floor)
			celevator.controller.run(controllerpos,{
				type = "remotemsg",
				channel = "dncall",
				msg = floor,
			})
		end,
		needsfloor = true,
	},
	{
		id = "swingupcall",
		desc = "Up Call (swing) at Landing:",
		func_on = function(controllerpos,floor)
			celevator.controller.run(controllerpos,{
				type = "remotemsg",
				channel = "swingupcall",
				msg = floor,
			})
		end,
		needsfloor = true,
	},
	{
		id = "swingdowncall",
		desc = "Down Call (swing) at Landing:",
		func_on = function(controllerpos,floor)
			celevator.controller.run(controllerpos,{
				type = "remotemsg",
				channel = "swingdncall",
				msg = floor,
			})
		end,
		needsfloor = true,
	},
	{
		id = "fs1off",
		desc = "Deactivate Fire Service Phase 1",
		func_on = function(controllerpos)
			celevator.controller.run(controllerpos,{
				type = "fs1switch",
				state = false,
			})
		end,
		needsfloor = false,
	},
	{
		id = "fs1on",
		desc = "Activate Fire Service Phase 1",
		func_on = function(controllerpos)
			celevator.controller.run(controllerpos,{
				type = "fs1switch",
				state = true,
			})
		end,
		needsfloor = false,
	},
}

local function updateoutputform(pos)
	local meta = minetest.get_meta(pos)
	local fs = "formspec_version[7]size[8,6.5]"
	fs = fs.."dropdown[1,0.5;6,1;signal;"
	local selected = 1
	local currentid = meta:get_string("signal")
	for k,v in ipairs(outputoptions) do
		fs = fs..minetest.formspec_escape(v.desc)..","
		if v.id == currentid then selected = k end
	end
	fs = string.sub(fs,1,-2)
	fs = fs..";"..selected..";false]"
	fs = fs.."field[0.5,2.5;3,1;carid;Car ID;${carid}]"
	fs = fs.."field[4.5,2.5;3,1;floor;Landing Number;${floor}]"
	fs = fs.."label[1.5,4;Not all signal options require a landing number.]"
	fs = fs.."button_exit[2.5,5;3,1;save;Save]"
	meta:set_string("formspec",fs)
end

local function handleoutputfields(pos,_,fields,player)
	local meta = minetest.get_meta(pos)
	if not fields.save then return end
	local name = player:get_player_name()
	if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
		if player:is_player() then
			minetest.record_protection_violation(pos,name)
		end
		return
	end
	if not tonumber(fields.carid) then return end
	meta:set_int("carid",fields.carid)
	local carid = tonumber(fields.carid)
	local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) or {}
	if not carinfo.controllerpos then return end
	if not celevator.controller.iscontroller(carinfo.controllerpos) then return end
	if minetest.is_protected(carinfo.controllerpos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
		if player:is_player() then
			minetest.chat_send_player(name,"Can't connect to a controller you don't have access to.")
			minetest.record_protection_violation(carinfo.controllerpos,name)
		end
		return
	end
	local floor = tonumber(fields.floor)
	if floor then meta:set_int("floor",floor) end
	local def
	for _,v in ipairs(outputoptions) do
		if v.desc == fields.signal then
			def = v
		end
	end
	if not def then return end
	if def.needsfloor and not floor then return end
	meta:set_string("signal",def.id)
	updateoutputform(pos)
	local infotext = "Car: "..carid.." - "..def.desc..(def.needsfloor and " "..floor or "")
	meta:set_string("infotext",infotext)
end

minetest.register_node("celevator:mesecons_output_off",{
	description = "Elevator Mesecons Output",
	tiles = {
		"celevator_meseconsoutput_top_off.png",
		"celevator_cabinet_sides.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,-0.5,0.5,-0.47,0.5},
			{-0.438,-0.47,-0.438,0.438,-0.42,0.438},
		},
	},
	mesecons = {
		receptor = {
			state = mesecon.state.off,
			rules = iorules,
		},
	},
	after_place_node = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_int("floor",1)
		updateoutputform(pos)
	end,
	on_receive_fields = handleoutputfields,
})

minetest.register_node("celevator:mesecons_output_on",{
	description = "Elevator Mesecons Output (on state - you hacker you!)",
	tiles = {
		"celevator_meseconsoutput_top_on.png",
		"celevator_cabinet_sides.png",
	},
	drop = "celevator:mesecons_output_off",
	groups = {
		dig_immediate = 2,
		not_in_creative_inventory = 1,
	},
	paramtype = "light",
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,-0.5,0.5,-0.47,0.5},
			{-0.438,-0.47,-0.438,0.438,-0.42,0.438},
		},
	},
	mesecons = {
		receptor = {
			state = mesecon.state.on,
			rules = iorules,
		},
	},
	after_place_node = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_int("floor",1)
		updateoutputform(pos)
	end,
	on_receive_fields = handleoutputfields,
})

minetest.register_abm({
	label = "Update mesecons output",
	nodenames = {"celevator:mesecons_output_off","celevator:mesecons_output_on",},
	interval = 1,
	chance = 1,
	action = function(pos,node)
		local meta = minetest.get_meta(pos)
		local oldstate = (node.name == "celevator:mesecons_output_on")
		local carid = meta:get_int("carid")
		if carid == 0 then return end
		local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) or {}
		if not carinfo.controllerpos then return end
		if not celevator.controller.iscontroller(carinfo.controllerpos) then return end
		local floor = meta:get_int("floor")
		local mem = minetest.deserialize(minetest.get_meta(carinfo.controllerpos):get_string("mem")) or {}
		local signal = meta:get_string("signal")
		local def
		for _,v in ipairs(outputoptions) do
			if v.id == signal then
				def = v
				break
			end
		end
		if not def then return end
		local newstate = def.func(mem,floor)
		if newstate ~= oldstate then
			node.name = (newstate and "celevator:mesecons_output_on" or "celevator:mesecons_output_off")
			minetest.swap_node(pos,node)
			if newstate then
				mesecon.receptor_on(pos,iorules)
			else
				mesecon.receptor_off(pos,iorules)
			end
		end
	end,
})

local function updateinputform(pos)
	local meta = minetest.get_meta(pos)
	local fs = "formspec_version[7]size[8,6.5]"
	fs = fs.."dropdown[1,0.5;6,1;signal;"
	local selected = 1
	local currentid = meta:get_string("signal")
	for k,v in ipairs(inputoptions) do
		fs = fs..minetest.formspec_escape(v.desc)..","
		if v.id == currentid then selected = k end
	end
	fs = string.sub(fs,1,-2)
	fs = fs..";"..selected..";false]"
	fs = fs.."field[0.5,2.5;3,1;carid;Car ID;${carid}]"
	fs = fs.."field[4.5,2.5;3,1;floor;Landing Number;${floor}]"
	fs = fs.."label[1.5,4;Not all signal options require a landing number.]"
	fs = fs.."button_exit[2.5,5;3,1;save;Save]"
	meta:set_string("formspec",fs)
end

local function handleinputfields(pos,_,fields,player)
	local meta = minetest.get_meta(pos)
	if not fields.save then return end
	local name = player:get_player_name()
	if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
		if player:is_player() then
			minetest.record_protection_violation(pos,name)
		end
		return
	end
	if not tonumber(fields.carid) then return end
	meta:set_int("carid",fields.carid)
	local carid = tonumber(fields.carid)
	local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) or {}
	if not carinfo.controllerpos then return end
	if not celevator.controller.iscontroller(carinfo.controllerpos) then return end
	if minetest.is_protected(carinfo.controllerpos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
		if player:is_player() then
			minetest.chat_send_player(name,"Can't connect to a controller you don't have access to.")
			minetest.record_protection_violation(carinfo.controllerpos,name)
		end
		return
	end
	local floor = tonumber(fields.floor)
	if floor then meta:set_int("floor",floor) end
	local def
	for _,v in ipairs(inputoptions) do
		if v.desc == fields.signal then
			def = v
		end
	end
	if not def then return end
	if def.needsfloor and not floor then return end
	meta:set_string("signal",def.id)
	updateinputform(pos)
	local infotext = "Car: "..carid.." - "..def.desc..(def.needsfloor and " "..floor or "")
	meta:set_string("infotext",infotext)
end

local function handleinput(pos,on)
	local meta = minetest.get_meta(pos)
	local carid = meta:get_int("carid")
	if carid == 0 then return end
	local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) or {}
	if not carinfo.controllerpos then return end
	if not celevator.controller.iscontroller(carinfo.controllerpos) then return end
	local floor = meta:get_int("floor")
	local signal = meta:get_string("signal")
	local def
	for _,v in ipairs(inputoptions) do
		if v.id == signal then
			def = v
			break
		end
	end
	if not def then return end
	if on then
		if def.func_on then def.func_on(carinfo.controllerpos,floor) end
	else
		if def.func_off then def.func_off(carinfo.controllerpos,floor) end
	end
end

minetest.register_node("celevator:mesecons_input_off",{
	description = "Elevator Mesecons Input",
	tiles = {
		"celevator_meseconsinput_top_off.png",
		"celevator_cabinet_sides.png",
	},
	groups = {
		dig_immediate = 2,
	},
	paramtype = "light",
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,-0.5,0.5,-0.47,0.5},
			{-0.438,-0.47,-0.438,0.438,-0.42,0.438},
		},
	},
	mesecons = {
		effector = {
			rules = iorules,
			action_on = function(pos,node)
				node.name = "celevator:mesecons_input_on"
				minetest.swap_node(pos,node)
				handleinput(pos,true)
			end,
		},
	},
	after_place_node = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_int("floor",1)
		updateinputform(pos)
	end,
	on_receive_fields = handleinputfields,
})

minetest.register_node("celevator:mesecons_input_on",{
	description = "Elevator Mesecons Input (on state - you hacker you!)",
	tiles = {
		"celevator_meseconsinput_top_on.png",
		"celevator_cabinet_sides.png",
	},
	drop = "celevator:mesecons_input_off",
	groups = {
		dig_immediate = 2,
		not_in_creative_inventory = 1,
	},
	paramtype = "light",
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			{-0.5,-0.5,-0.5,0.5,-0.47,0.5},
			{-0.438,-0.47,-0.438,0.438,-0.42,0.438},
		},
	},
	mesecons = {
		effector = {
			rules = iorules,
			action_off = function(pos,node)
				node.name = "celevator:mesecons_input_off"
				minetest.swap_node(pos,node)
				handleinput(pos,false)
			end,
		},
	},
	after_place_node = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_int("floor",1)
		updateinputform(pos)
	end,
	on_receive_fields = handleinputfields,
})