Add a box wagon

Alongside with coding wagon inventories, and several bugfixes
master
orwell96 2016-11-03 11:27:17 +01:00
parent 31b579bc5c
commit 245f07d323
6 changed files with 117 additions and 22 deletions

Binary file not shown.

View File

@ -84,6 +84,14 @@ minetest.register_craft({
{'default:steelblock', 'default:steelblock', 'default:steelblock'},
},
})
minetest.register_craft({
output = 'advtrains:wagon_default_box',
recipe = {
{'default:steelblock', 'default:steelblock', 'default:steelblock'},
{'default:steelblock', 'default:junglewood', 'default:steelblock'},
{'default:steelblock', 'default:steelblock', 'default:steelblock'},
},
})
minetest.register_craft({
output = 'advtrains:subway_wagon',
recipe = {

View File

@ -223,4 +223,27 @@ function advtrains.pos_median(pos1, pos2)
end
function advtrains.abs_ceil(i)
return math.ceil(math.abs(i))*math.sign(i)
end
end
function advtrains.serialize_inventory(inv)
local ser={}
local liszts=inv:get_lists()
for lisztname, liszt in pairs(liszts) do
ser[lisztname]={}
for idx, item in ipairs(liszt) do
local istring=item:to_string()
if istring~="" then
ser[lisztname][idx]=istring
end
end
end
return minetest.serialize(ser)
end
function advtrains.deserialize_inventory(sers, inv)
local ser=minetest.deserialize(sers)
if ser then
inv:set_lists(ser)
return true
end
return false
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -769,6 +769,7 @@ function advtrains.invalidate_all_paths()
v.restore_add_index=v.index-math.floor(v.index+0.5)
end
v.path=nil
v.path_dist=nil
v.index=nil
v.min_index_on_track=nil
v.max_index_on_track=nil

View File

@ -1,6 +1,6 @@
--atan2 counts angles clockwise, minetest does counterclockwise
--local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end
local print=function() end
local print=function(t) minetest.log("action", t) minetest.chat_send_all(t) end
--local print=function() end
local wagon={
collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
@ -11,21 +11,16 @@ local wagon={
textures = {"black.png"},
is_wagon=true,
wagon_span=1,--how many index units of space does this wagon consume
attach_offset={x=0, y=0, z=0},
view_offset={x=0, y=0, z=0},
has_inventory=false,
}
function wagon:on_rightclick(clicker)
--print("[advtrains] wagon rightclick")
if not self:ensure_init() then return end
if not clicker or not clicker:is_player() then
return
end
if not self.initialized then
print("[advtrains] not initiaalized")
return
end
if clicker:get_player_control().aux1 then
--advtrains.dumppath(self:train().path)
--minetest.chat_send_all("at index "..(self:train().index or "nil"))
@ -68,10 +63,11 @@ function wagon:on_activate(sd_uid, dtime_s)
self.entity_name=self.name
--duplicates?
for _,wagon in pairs(minetest.luaentities) do
for ao_id,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized and wagon.unique_id==self.unique_id and wagon~=self then--i am a duplicate!
print("[advtrains][wagon "..((sd_uid and sd_uid~="" and sd_uid) or "no-id").."] duplicate found, removing")
print("[advtrains][wagon "..((sd_uid and sd_uid~="" and sd_uid) or "no-id").."] duplicate found(ao_id:"..ao_id.."), removing")
self.object:remove()
minetest.after(0.5, function() advtrains.update_trainpart_properties(self.train_id) end)
return
end
end
@ -84,6 +80,11 @@ end
function wagon:get_staticdata()
if not self:ensure_init() then return end
print("[advtrains][wagon "..((self.unique_id and self.unique_id~="" and self.unique_id) or "no-id").."]: saving to wagon_save")
--serialize inventory, if it has one
if self.has_inventory then
local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..self.unique_id})
self.ser_inv=advtrains.serialize_inventory(inv)
end
--save to table before being unloaded
advtrains.wagon_save[self.unique_id]=advtrains.merge_tables(self)
advtrains.wagon_save[self.unique_id].entity_name=self.name
@ -100,6 +101,7 @@ function wagon:init_new_instance(train_id, properties)
self[k]=v
end
end
self:init_shared()
self.initialized=true
print("init_new_instance "..self.unique_id.." ("..self.train_id..")")
return self.unique_id
@ -119,11 +121,37 @@ function wagon:init_from_wagon_save(uid)
self.object:remove()
return
end
self:init_shared()
self.initialized=true
minetest.after(1, function() self:reattach_all() end)
print("init_from_wagon_save "..self.unique_id.." ("..self.train_id..")")
advtrains.update_trainpart_properties(self.train_id)
end
function wagon:init_shared()
if self.has_inventory then
local uid_noptr=self.unique_id..""
--to be used later
local inv=minetest.create_detached_inventory("advtrains_wgn_"..self.unique_id, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count
end,
allow_put = function(inv, listname, index, stack, player)
return stack:get_count()
end,
allow_take = function(inv, listname, index, stack, player)
return stack:get_count()
end
})
if self.ser_inv then
advtrains.deserialize_inventory(self.ser_inv, inv)
end
if self.inventory_list_sizes then
for lst, siz in pairs(self.inventory_list_sizes) do
inv:set_size(lst, siz)
end
end
end
end
function wagon:ensure_init()
if self.initialized then return true end
self.object:setvelocity({x=0,y=0,z=0})
@ -422,7 +450,13 @@ function wagon:get_off(seatno)
end
function wagon:show_get_on_form(pname)
if not self.initialized then return end
local form, comma="size[5,7]label[0.5,0.5;Select seat:]textlist[0.5,1;4,6;seat;", ""
if #self.seats==0 then
if self.has_inventory and self.get_inventory_formspec then
minetest.show_formspec(pname, "advtrains_inv_"..self.unique_id, self:get_inventory_formspec())
end
return
end
local form, comma="size[5,8]label[0.5,0.5;Select seat:]textlist[0.5,1;4,6;seat;", ""
for seatno, seattbl in ipairs(self.seats) do
local addtext, colorcode="", ""
if self.seatp and self.seatp[seatno] then
@ -432,18 +466,28 @@ function wagon:show_get_on_form(pname)
form=form..comma..colorcode..seattbl.name..addtext
comma=","
end
minetest.show_formspec(pname, "advtrains_geton_"..self.unique_id, form..";0,false")
form=form..";0,false]"
if self.has_inventory and self.get_inventory_formspec then
form=form.."button_exit[1,7;3,1;inv;Show Inventory]"
end
minetest.show_formspec(pname, "advtrains_geton_"..self.unique_id, form)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local uid=string.match(formname, "^advtrains_geton_(.+)$")
if uid and fields.seat then
local val=minetest.explode_textlist_event(fields.seat)
if val and val.type=="CHG" then
--get on
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized and wagon.unique_id==uid then
wagon:get_on(player, val.index)
minetest.show_formspec(player:get_player_name(), "none", "")
if uid then
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized and wagon.unique_id==uid then
if fields.inv then
if wagon.has_inventory and wagon.get_inventory_formspec then
minetest.show_formspec(player:get_player_name(), "advtrains_inv_"..uid, wagon:get_inventory_formspec())
end
elseif fields.seat then
local val=minetest.explode_textlist_event(fields.seat)
if val and val.type~="INV" then
--get on
wagon:get_on(player, val.index)
minetest.show_formspec(player:get_player_name(), "none", "")
end
end
end
end
@ -595,6 +639,25 @@ advtrains.register_wagon("wagon_default", "steam",{
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
drops={"default:steelblock 4"},
}, "Passenger Wagon", "advtrains_wagon_inv.png")
advtrains.register_wagon("wagon_box", "steam",{
mesh="wagon.b3d",
textures = {"advtrains_wagon_box.png"},
seats = {},
visual_size = {x=1, y=1},
wagon_span=1.8,
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},
drops={"default:steelblock 4"},
has_inventory = true,
get_inventory_formspec = function(self)
return "size[8,11]"..
"list[detached:advtrains_wgn_"..self.unique_id..";box;0,0;8,6;]"..
"list[current_player;main;0,7;8,4;]"..
"listring[]"
end,
inventory_list_sizes = {
box=8*6,
},
}, "Box Wagon", "advtrains_wagon_inv.png")
advtrains.register_train_type("electric", {"regular", "default"}, 20)