Wagon iterator, lookup by id, and use them in code
This commit is contained in:
parent
96f4ac7f6c
commit
6d3c5a5f38
|
@ -142,15 +142,10 @@ minetest.register_on_joinplayer(function(player)
|
|||
local pname = player:get_player_name()
|
||||
local id=advtrains.player_to_train_mapping[pname]
|
||||
if id then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id then
|
||||
local wdata = advtrains.wagons[wagon.id]
|
||||
if wdata and wdata.train_id == id then
|
||||
for _, wagon in advtrains.wagon_entity_pairs_in_train(id) do
|
||||
wagon:reattach_all()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
|
@ -160,13 +155,11 @@ minetest.register_on_dieplayer(function(player)
|
|||
if id then
|
||||
local train=advtrains.trains[id]
|
||||
if not train then advtrains.player_to_train_mapping[pname]=nil return end
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
|
||||
for _, wagon in advtrains.wagon_entity_pairs_in_train(id) do
|
||||
--when player dies, detach him from the train
|
||||
--call get_off_plr on every wagon since we don't know which one he's on.
|
||||
wagon:get_off_plr(pname)
|
||||
end
|
||||
end
|
||||
-- just in case no wagon felt responsible for this player: clear train mapping
|
||||
advtrains.player_to_train_mapping[pname] = nil
|
||||
end
|
||||
|
|
|
@ -1103,15 +1103,14 @@ function wagon:handle_bordcom_fields(pname, formname, fields)
|
|||
for i, tpid in ipairs(train.trainparts) do
|
||||
if fields["dcpl_"..i] then
|
||||
advtrains.safe_decouple_wagon(tpid, pname)
|
||||
elseif fields["wgprp"..i] then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id==tpid and data.owner==pname then
|
||||
elseif fields["wgprp"..i] and data.owner==pname then
|
||||
local wagon = advtrains.get_wagon_entity(tpid)
|
||||
if wagon then
|
||||
wagon:show_wagon_properties(pname)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
--check cpl_eid_front and _back of train
|
||||
local couple_front = checkcouple(train.cpl_front)
|
||||
local couple_back = checkcouple(train.cpl_back)
|
||||
|
@ -1154,8 +1153,8 @@ end
|
|||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local uid=string.match(formname, "^advtrains_geton_(.+)$")
|
||||
if uid then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id==uid then
|
||||
local wagon = advtrains.get_wagon_entity(uid)
|
||||
if wagon then
|
||||
local data = advtrains.wagons[wagon.id]
|
||||
if fields.inv then
|
||||
if wagon.has_inventory and wagon.get_inventory_formspec then
|
||||
|
@ -1171,12 +1170,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
uid=string.match(formname, "^advtrains_seating_(.+)$")
|
||||
if uid then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id==uid then
|
||||
local wagon = advtrains.get_wagon_entity(uid)
|
||||
if wagon then
|
||||
local pname=player:get_player_name()
|
||||
local no=wagon:get_seatno(pname)
|
||||
if no then
|
||||
|
@ -1185,13 +1185,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
uid=string.match(formname, "^advtrains_prop_(.+)$")
|
||||
if uid then
|
||||
local pname=player:get_player_name()
|
||||
local data = advtrains.wagons[uid]
|
||||
if pname~=data.owner and not minetest.check_player_privs(pname, {train_admin = true}) then
|
||||
if not data then
|
||||
return true
|
||||
elseif pname~=data.owner and not minetest.check_player_privs(pname, {train_admin = true}) then
|
||||
return true
|
||||
end
|
||||
if fields.save or not fields.quit then
|
||||
|
@ -1213,29 +1216,32 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
wagon.show_wagon_properties({id=uid}, pname)
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
uid=string.match(formname, "^advtrains_bordcom_(.+)$")
|
||||
if uid then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id==uid then
|
||||
local wagon = advtrains.get_wagon_entity(uid)
|
||||
if wagon then
|
||||
wagon:handle_bordcom_fields(player:get_player_name(), formname, fields)
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
uid=string.match(formname, "^advtrains_inv_(.+)$")
|
||||
if uid then
|
||||
local pname=player:get_player_name()
|
||||
local data = advtrains.wagons[uid]
|
||||
if fields.prop and data.owner==pname then
|
||||
for _,wagon in pairs(minetest.luaentities) do
|
||||
if wagon.is_wagon and wagon.initialized and wagon.id==uid and data.owner==pname then
|
||||
local wagon = advtrains.get_wagon_entity(uid)
|
||||
if wagon then
|
||||
wagon:show_wagon_properties(pname)
|
||||
--wagon:handle_bordcom_fields(player:get_player_name(), formname, fields)
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end)
|
||||
|
||||
function wagon:seating_from_key_helper(pname, fields, no)
|
||||
local data = advtrains.wagons[self.id]
|
||||
local sgr=self.seats[no].group
|
||||
|
@ -1507,3 +1513,27 @@ function advtrains.get_wagon_at_index(train_id, w_index)
|
|||
-- nothing found, dist must be further back
|
||||
return nil
|
||||
end
|
||||
|
||||
function advtrains.get_wagon_entity(wagon_id)
|
||||
if not advtrains.wagons[wagon_id] then return end
|
||||
local object = advtrains.wagon_objects[wagon_id]
|
||||
if object then
|
||||
return object:get_luaentity()
|
||||
end
|
||||
end
|
||||
|
||||
function advtrains.next_wagon_entity_in_train(train, i)
|
||||
local wagon_id = train.trainparts[i + 1]
|
||||
if wagon_id then
|
||||
local wagon = advtrains.get_wagon_entity(wagon_id)
|
||||
if wagon then
|
||||
return i + 1, wagon
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function advtrains.wagon_entity_pairs_in_train(train_id)
|
||||
local train = advtrains.trains[train_id]
|
||||
if not train then return function() end end
|
||||
return advtrains.next_wagon_entity_in_train, train, 0
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue