aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2016-11-03 11:27:17 +0100
committerorwell96 <mono96.mml@gmail.com>2016-11-03 11:27:17 +0100
commit245f07d32324183ae2f300435c07e4cb7520ebc2 (patch)
treefbc2ddb63b2c5f08de9a581f6fa17b0b3e8dfe80
parent31b579bc5c5e23eca78605ba082e7627334a468e (diff)
downloadadvtrains-245f07d32324183ae2f300435c07e4cb7520ebc2.tar.gz
advtrains-245f07d32324183ae2f300435c07e4cb7520ebc2.tar.bz2
advtrains-245f07d32324183ae2f300435c07e4cb7520ebc2.zip
Add a box wagon
Alongside with coding wagon inventories, and several bugfixes
-rw-r--r--advtrains.zipbin1326241 -> 1349516 bytes
-rw-r--r--crafting.lua8
-rw-r--r--helpers.lua25
-rw-r--r--textures/advtrains_wagon_box.pngbin0 -> 22503 bytes
-rw-r--r--trainlogic.lua1
-rw-r--r--wagons.lua105
6 files changed, 117 insertions, 22 deletions
diff --git a/advtrains.zip b/advtrains.zip
index fc977e9..dee85ee 100644
--- a/advtrains.zip
+++ b/advtrains.zip
Binary files differ
diff --git a/crafting.lua b/crafting.lua
index bd24534..b0bb8bc 100644
--- a/crafting.lua
+++ b/crafting.lua
@@ -85,6 +85,14 @@ minetest.register_craft({
},
})
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 = {
{'default:steelblock', 'default:steelblock', 'default:steelblock'},
diff --git a/helpers.lua b/helpers.lua
index ef3752a..6a8175f 100644
--- a/helpers.lua
+++ b/helpers.lua
@@ -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 \ No newline at end of file
+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
diff --git a/textures/advtrains_wagon_box.png b/textures/advtrains_wagon_box.png
new file mode 100644
index 0000000..8bfbe06
--- /dev/null
+++ b/textures/advtrains_wagon_box.png
Binary files differ
diff --git a/trainlogic.lua b/trainlogic.lua
index a372b19..5db366e 100644
--- a/trainlogic.lua
+++ b/trainlogic.lua
@@ -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
diff --git a/wagons.lua b/wagons.lua
index 239b122..c4e5f21 100644
--- a/wagons.lua
+++ b/wagons.lua
@@ -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)