Still more things I want to add to it, but it works for now.
Should show up in existing cars (once they do something for the first time, for now)
This commit is contained in:
cheapie 2024-04-12 20:48:19 -05:00
parent dab488531a
commit 23826516ca
9 changed files with 108 additions and 2 deletions

25
car.lua
View File

@ -118,7 +118,7 @@ local pieces = {
tiles = {
"celevator_cabinet_sides.png",
"celevator_cabinet_sides.png",
"celevator_car_wallpaper.png",
"celevator_car_wallpaper_2x.png^celevator_cop.png",
"celevator_cabinet_sides.png",
},
},
@ -344,6 +344,19 @@ for _,def in ipairs(pieces) do
def.drawtype = "nodebox"
def.description = "Car "..def._position
def.light_source = 9
def.on_receive_fields = function(pos,_,fields,player)
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)))
if not carinfo then return end
local event = {
type = "cop",
fields = fields,
player = player:get_player_name(),
}
celevator.controller.run(carinfo.controllerpos,event)
end
minetest.register_node("celevator:car_"..def._position,def)
end
@ -391,3 +404,13 @@ minetest.register_abm({
entity:set_pos(vector.add(pos,vector.multiply(fdir,0.44)))
end,
})
function celevator.car.updatecop(pos)
local copmeta = minetest.get_meta(pos)
local carid = copmeta:get_int("carid")
if carid == 0 then return end
local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid)))
if not carinfo then return end
local controllermeta = minetest.get_meta(carinfo.controllerpos)
copmeta:set_string("formspec",controllermeta:get_string("copformspec"))
end

View File

@ -62,6 +62,8 @@ local function ondestruct(pos)
end
celevator.controller.equeue[minetest.hash_node_position(pos)] = nil
celevator.storage:set_string("controller_equeue",minetest.serialize(celevator.controller.equeue))
local carid = minetest.get_meta(pos):get_int("carid")
if carid ~= 0 then celevator.storage:set_string(string.format("car%d",carid),"") end
end
local function onrotate(controllerpos,node,user,mode,new_param2)
@ -490,6 +492,10 @@ function celevator.controller.finish(pos,mem,changedinterrupts)
celevator.lantern.setlight(lantern.pos,"down",newlanterns[lantern.landing] == "down")
end
end
meta:set_string("copformspec",mem.copformspec)
if mem.copformspec ~= oldmem.copformspec and drivetype then
minetest.after(0.25,celevator.drives[drivetype].updatecopformspec,drivepos)
end
meta:set_string("mem",minetest.serialize(mem))
if node.name == "celevator:controller_open" then meta:set_string("formspec",mem.formspec or "") end
meta:set_string("formspec_hidden",mem.formspec or "")

View File

@ -439,6 +439,23 @@ elseif event.iid == "opentimeout" then
fault("opentimeout",true)
elseif event.iid == "closetimeout" then
fault("closetimeout",true)
elseif event.type == "cop" then
local fields = event.fields
if mem.carstate == "normal" then
for k,v in pairs(fields) do
if string.sub(k,1,7) == "carcall" then
local landing = tonumber(string.sub(k,8,-1))
if v and landing and landing >= 1 and landing <= #mem.params.floorheights then
mem.carcalls[landing] = true
end
end
end
if fields.close and mem.doorstate == "open" then
interrupt(0,"close")
elseif fields.open and mem.doorstate == "closed" and not (mem.carmotion or juststarted) then
open()
end
end
end
local oldstate = mem.carstate
@ -612,7 +629,7 @@ elseif mem.screenstate == "oobe_floortable" or mem.screenstate == "floortable" t
fs(minetest.formspec_escape(string.format("%d - Height: %d - PI: %s",i,mem.params.floorheights[i],mem.params.floornames[i]))..(i==1 and "" or ","))
end
fs(";"..tostring(#mem.params.floornames-mem.editingfloor+1)..";false]")
fs("button[8,2;2,1;add;New Floor]")
if #mem.params.floornames < 100 then fs("button[8,2;2,1;add;New Floor]") end
fs("button[8,3.5;2,1;edit;Edit Floor]")
if #mem.params.floornames > 2 then fs("button[8,5;2,1;remove;Remove Floor]") end
if mem.editingfloor < #mem.params.floornames then fs("button[8,6.5;2,1;moveup;Move Up]") end
@ -775,4 +792,31 @@ if mem.carstate == "normal" and (mem.doorstate == "open" or mem.doorstate == "op
mem.lanterns[getpos()] = mem.direction
end
mem.copformspec = "formspec_version[7]"
local floorcount = #mem.params.floornames
local copcols = math.floor((floorcount-1)/10)+1
local coprows = math.floor((floorcount-1)/copcols)+1
local litimg = "celevator_copbutton_lit.png"
local unlitimg = "celevator_copbutton_unlit.png"
mem.copformspec = mem.copformspec..string.format("size[%f,%f]",copcols*1.25+2.5,coprows*1.25+4)
for i=1,floorcount,1 do
local row = math.floor((i-1)/copcols)+1
local col = ((i-1)%copcols)+1
local yp = (coprows-row+1)*1.25
local xp = col*1.25
local tex = mem.carcalls[i] and litimg or unlitimg
mem.copformspec = mem.copformspec..string.format("image_button[%f,%f;1,1;%s;carcall%d;%d;false;false;%s]",xp,yp,tex,i,i,litimg)
end
local doxp = (copcols == 1) and 0.5 or 1.25
mem.copformspec = mem.copformspec..string.format("image_button[%f,%f;1,1;%s;open;%s;false;false;%s]",doxp,coprows*1.25+1.5,unlitimg,minetest.formspec_escape("<|>"),litimg)
local dcxp = 3.75
if copcols == 1 then
dcxp = 2
elseif copcols == 2 then
dcxp = 2.5
end
mem.copformspec = mem.copformspec..string.format("image_button[%f,%f;1,1;%s;close;%s;false;false;%s]",dcxp,coprows*1.25+1.5,unlitimg,minetest.formspec_escape(">|<"),litimg)
return pos,mem,changedinterrupts

View File

@ -364,6 +364,7 @@ function celevator.drives.entity.step(dtime)
celevator.car.spawncar(vector.round(vector.add(origin,vector.new(0,apos,0))),minetest.dir_to_yaw(minetest.fourdir_to_dir(carparam2)),carid)
end
apos = math.floor(apos+0.5)
minetest.after(0.25,celevator.drives.entity.updatecopformspec,pos)
elseif dremain < 0.2 then
vel = 0.2
elseif dremain < 2*maxvel and dremain < dmoved then
@ -404,6 +405,7 @@ function celevator.drives.entity.step(dtime)
local carparam2 = meta:get_int("carparam2")
celevator.car.spawncar(vector.round(vector.add(origin,vector.new(0,apos,0))),minetest.dir_to_yaw(minetest.fourdir_to_dir(carparam2)),carid)
apos = math.floor(apos+0.5)
minetest.after(0.25,celevator.drives.entity.updatecopformspec,pos)
elseif dremain < 0.2 then
vel = 0.2
elseif dremain < 2*maxvel and dremain < dmoved then
@ -785,3 +787,30 @@ function celevator.drives.entity.sheavetonode(carid)
param2 = minetest.dir_to_fourdir(dir),
})
end
function celevator.drives.entity.updatecopformspec(drivepos)
local entitydrives_running = minetest.deserialize(celevator.storage:get_string("entitydrives_running")) or {}
if entitydrives_running[minetest.hash_node_position(drivepos)] then return end
local drivemeta = minetest.get_meta(drivepos)
local carid = drivemeta:get_int("carid")
if carid == 0 then return end
local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid)))
if not carinfo then return end
local formspec = minetest.get_meta(carinfo.controllerpos):get_string("copformspec")
local origin = minetest.string_to_pos(drivemeta:get_string("origin"))
if not origin then
minetest.log("error","[celevator] [entity drive] Invalid origin for drive at "..minetest.pos_to_string(drivepos))
drivemeta:set_string("fault","badorigin")
return
end
local apos = tonumber(drivemeta:get_string("apos")) or 0
local carpos = vector.add(origin,vector.new(0,apos,0))
local carnodes = celevator.drives.entity.gathercar(carpos,minetest.dir_to_yaw(minetest.fourdir_to_dir(minetest.get_node(carpos).param2)))
for hash in pairs(carnodes) do
local piecepos = minetest.get_position_from_hash(hash)
local piece = minetest.get_node(piecepos)
if piece.name == "celevator:car_010" then
minetest.get_meta(piecepos):set_string("formspec",formspec)
end
end
end

View File

@ -230,3 +230,7 @@ function celevator.drives.null.resetfault(pos)
--This drive has no possible faults at this time (drive communication faults are generated by the controller), but the controller expects to be able to call this
minetest.get_meta(pos):set_string("fault","")
end
function celevator.drives.null.updatecopformspec()
--No car means no COP
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
textures/celevator_cop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 683 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 B