added inventory for the cars

master
Alexsandro Percy 2022-03-21 13:33:55 -03:00
parent bcf97bafc0
commit f38a33685c
14 changed files with 265 additions and 18 deletions

View File

@ -36,6 +36,7 @@ minetest.register_craftitem("automobiles_buggy:buggy", {
itemstack:take_item()
ent.object:set_acceleration({x=0,y=-automobiles_lib.gravity,z=0})
automobiles_lib.setText(ent, "buggy")
automobiles_lib.create_inventory(ent, buggy.trunk_slots, owner)
end
end

View File

@ -289,6 +289,8 @@ minetest.register_entity("automobiles_buggy:buggy", {
_last_ground_check = 0,
_last_light_move = 0,
_last_engine_sound_update = 0,
_inv = nil,
_inv_id = "",
get_staticdata = function(self) -- unloaded/unloads ... is now saved
return minetest.serialize({
@ -304,9 +306,14 @@ minetest.register_entity("automobiles_buggy:buggy", {
stored_rag = self._show_rag,
stored_pitch = self._pitch,
stored_light_old_pos = self._light_old_pos,
stored_inv_id = self._inv_id,
})
end,
on_deactivate = function(self)
automobiles_lib.save_inventory(self)
end,
on_activate = function(self, staticdata, dtime_s)
if staticdata ~= "" and staticdata ~= nil then
local data = minetest.deserialize(staticdata) or {}
@ -323,6 +330,7 @@ minetest.register_entity("automobiles_buggy:buggy", {
self._show_rag = data.stored_rag
self._pitch = data.stored_pitch
self._light_old_pos = data.stored_light_old_pos
self._inv_id = data.stored_inv_id
automobiles_lib.setText(self, "Buggy")
end
@ -399,6 +407,14 @@ minetest.register_entity("automobiles_buggy:buggy", {
self.object:set_armor_groups({immortal=1})
local inv = minetest.get_inventory({type = "detached", name = self._inv_id})
-- if the game was closed the inventories have to be made anew, instead of just reattached
if not inv then
automobiles_lib.create_inventory(self, buggy.trunk_slots)
else
self.inv = inv
end
mobkit.actfunc(self, staticdata, dtime_s)
end,
@ -713,11 +729,15 @@ minetest.register_entity("automobiles_buggy:buggy", {
buggy.driver_formspec(name)
else
if name == self.owner then
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "buggy_engine"},
{object = self.object, gain = 4, pitch = 1, max_hear_distance = 10, loop = true,})
if clicker:get_player_control().sneak == true then
automobiles_lib.show_vehicle_trunk_formspec(self, clicker, 8)
else
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "buggy_engine"},
{object = self.object, gain = 4, pitch = 1, max_hear_distance = 10, loop = true,})
end
else
--minetest.chat_send_all("clicou")
--a passenger

View File

@ -40,6 +40,7 @@ function buggy.destroy(self, puncher)
if self.lights then self.lights:remove() end
if self.r_lights then self.r_lights:remove() end
automobiles_lib.destroy_inventory(self)
self.object:remove()
pos.y=pos.y+2

View File

@ -8,6 +8,7 @@ buggy.gravity = automobiles_lib.gravity
buggy.max_speed = 15
buggy.max_acc_factor = 5
buggy.max_fuel = 10
buggy.trunk_slots = 8
BUGGY_GAUGE_FUEL_POSITION = {x=0,y=4.65,z=15.17}

View File

@ -27,10 +27,12 @@ minetest.register_craftitem("automobiles_coupe:coupe", {
local owner = placer:get_player_name()
if ent then
ent.owner = owner
car:set_yaw(placer:get_look_horizontal())
itemstack:take_item()
--minetest.chat_send_all("owner: " .. ent.owner)
car:set_yaw(placer:get_look_horizontal())
itemstack:take_item()
ent.object:set_acceleration({x=0,y=-automobiles_lib.gravity,z=0})
automobiles_lib.setText(ent, "coupe")
automobiles_lib.setText(ent, "Coupe")
automobiles_lib.create_inventory(ent, coupe.trunk_slots, owner)
end
end

View File

@ -286,6 +286,8 @@ minetest.register_entity("automobiles_coupe:coupe", {
_last_light_move = 0,
_last_engine_sound_update = 0,
_turn_light_timer = 0,
_inv = nil,
_inv_id = "",
get_staticdata = function(self) -- unloaded/unloads ... is now saved
return minetest.serialize({
@ -301,9 +303,14 @@ minetest.register_entity("automobiles_coupe:coupe", {
stored_rag = self._show_rag,
stored_pitch = self._pitch,
stored_light_old_pos = self._light_old_pos,
stored_inv_id = self._inv_id,
})
end,
on_deactivate = function(self)
automobiles_lib.save_inventory(self)
end,
on_activate = function(self, staticdata, dtime_s)
if staticdata ~= "" and staticdata ~= nil then
local data = minetest.deserialize(staticdata) or {}
@ -320,6 +327,7 @@ minetest.register_entity("automobiles_coupe:coupe", {
self._show_rag = data.stored_rag
self._pitch = data.stored_pitch
self._light_old_pos = data.stored_light_old_pos
self._inv_id = data.stored_inv_id
automobiles_lib.setText(self, "Coupe")
end
@ -407,6 +415,15 @@ minetest.register_entity("automobiles_coupe:coupe", {
self.object:set_armor_groups({immortal=1})
local inv = minetest.get_inventory({type = "detached", name = self._inv_id})
-- if the game was closed the inventories have to be made anew, instead of just reattached
if not inv then
automobiles_lib.create_inventory(self, coupe.trunk_slots)
else
self.inv = inv
end
mobkit.actfunc(self, staticdata, dtime_s)
end,
@ -740,11 +757,15 @@ minetest.register_entity("automobiles_coupe:coupe", {
coupe.driver_formspec(name)
else
if name == self.owner then
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "automobiles_engine"},
{object = self.object, gain = 4, pitch = 1, max_hear_distance = 10, loop = true,})
if clicker:get_player_control().sneak == true then
automobiles_lib.show_vehicle_trunk_formspec(self, clicker, 8)
else
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "automobiles_engine"},
{object = self.object, gain = 4, pitch = 1, max_hear_distance = 10, loop = true,})
end
else
--minetest.chat_send_all("clicou")
--a passenger

View File

@ -42,6 +42,7 @@ function coupe.destroy(self, puncher)
if self.turn_l_light then self.turn_l_light:remove() end
if self.turn_r_light then self.turn_r_light:remove() end
automobiles_lib.destroy_inventory(self)
self.object:remove()
pos.y=pos.y+2

View File

@ -8,6 +8,7 @@ coupe.gravity = automobiles_lib.gravity
coupe.max_speed = 22
coupe.max_acc_factor = 8
coupe.max_fuel = 10
coupe.trunk_slots = 8
COUPE_GAUGE_FUEL_POSITION = {x=0,y=6.2,z=15.8}

View File

@ -395,6 +395,7 @@ dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "ground_detection.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "painter.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "inventory_management.lua")
-- engine
minetest.register_craftitem("automobiles_lib:engine",{

View File

@ -0,0 +1,175 @@
local storage = minetest.get_mod_storage()
automobiles_lib.modname = minetest.get_current_modname()
local function get_formspec_by_size(self, size)
local background = default.gui_bg .. default.gui_bg_img .. default.gui_slots
local default_inventory_formspecs = {
["4"]="size[8,6]".. background ..
"list[detached:" .. self._inv_id .. ";main;2,0;4,1;]" ..
"list[current_player;main;0,2;8,4;]" ..
"listring[]",
["6"]="size[8,6]".. background ..
"list[detached:" .. self._inv_id .. ";main;1,0;6,1;]"..
"list[current_player;main;0,2;8,4;]" ..
"listring[]",
["8"]="size[8,6]".. background ..
"list[detached:" .. self._inv_id .. ";main;0,0;8,1;]"..
"list[current_player;main;0,2;8,4;]" ..
"listring[]",
["12"]="size[8,7]".. background ..
"list[detached:" .. self._inv_id .. ";main;1,0;6,2;]"..
"list[current_player;main;0,3;8,4;]" ..
"listring[]",
["16"]="size[8,7]".. background ..
"list[detached:" .. self._inv_id .. ";main;0,0;8,2;]"..
"list[current_player;main;0,3;8,4;]" ..
"listring[]",
["24"]="size[8,8]".. background ..
"list[context:" .. self._inv_id .. ";main;0,0;8,3;]"..
"list[current_player;main;0,4;8,4;]" ..
"listring[]",
["32"]="size[8,9]".. background ..
"list[detached:" .. self._inv_id .. ";main;0,0.3;8,4;]"..
"list[current_player;main;0,4.85;8,1;]"..
"list[current_player;main;0,6.08;8,3;8]"..
"listring[context;main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85),
["50"]="size[10,10]".. background ..
"list[detached:" .. self._inv_id .. ";main;0,0;10,5;]"..
"list[current_player;main;1,6;8,4;]" ..
"listring[]",
}
local formspec = default_inventory_formspecs[tostring(size)]
return formspec
end
local function inventory_id(maker_name)
local id= automobiles_lib.modname .. "_" .. maker_name .. "_"
for i=0,5 do
id=id..(math.random(0,9))
end
return id
end
function automobiles_lib.load_inventory(self)
if self._inv then
local inv_content = minetest.deserialize(storage:get_string(self._inv_id))
if inv_content then
self._inv:set_list("main", inv_content)
end
end
end
function automobiles_lib.save_inventory(self)
if self._inv then
local inv_content = self._inv:get_list("main")
if inv_content then
for k, v in pairs(inv_content) do
inv_content[k] = v:to_string()
end
local inv_content = minetest.serialize(inv_content)
storage:set_string(self._inv_id, inv_content)
end
end
end
function automobiles_lib.remove_inventory(self)
local inventory = automobiles_lib.get_inventory(self)
if inventory:is_empty("main") then
return minetest.remove_detached_inventory(self._inv_id)
else
local inv_content = inventory:get_list("main")
if inv_content then
local pos = self.object:get_pos()
for k, v in pairs(inv_content) do
for i = 0,v:get_count()-1,1
do
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},v:get_name())
end
end
end
return minetest.remove_detached_inventory(self._inv_id)
end
return false
end
function automobiles_lib.destroy_inventory(self)
automobiles_lib.remove_inventory(self)
storage:set_string(self._inv_id, nil)
end
--show inventory form to user
function automobiles_lib.show_vehicle_trunk_formspec(self, player, size)
local form = get_formspec_by_size(self, size)
minetest.show_formspec(player:get_player_name(), automobiles_lib.modname .. ":inventory",
form
)
end
function automobiles_lib.create_inventory(self, size, owner)
owner = owner or ""
if owner == "" then owner = self.owner end
if owner ~= nil and owner ~= "" then
if self._inv_id == "" then
self._inv_id = inventory_id(owner)
end
local vehicle_inv = minetest.create_detached_inventory(self._inv_id, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count -- allow moving
end,
allow_put = function(inv, listname, index, stack, player)
return stack:get_count() -- allow putting
end,
allow_take = function(inv, listname, index, stack, player)
return stack:get_count() -- allow taking
end,
on_put = function(inv, toList, toIndex, stack, player)
automobiles_lib.save_inventory(self)
end,
on_take = function(inv, toList, toIndex, stack, player)
automobiles_lib.save_inventory(self)
end,
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
automobiles_lib.save_inventory(self)
end,
})
if size >= 8 then
if vehicle_inv:set_size("main", size) then
vehicle_inv:set_width("main", 8)
end
else
vehicle_inv:set_size("main", size)
end
self._inv = vehicle_inv
automobiles_lib.load_inventory(self)
end
end
function automobiles_lib.get_inventory(self)
if self._inv then
return minetest.get_inventory({type="detached", name=self._inv_id})
end
return nil
end
function automobiles_lib.list_inventory(self)
local inventory = automobiles_lib.get_inventory(self)
if inventory then
local list = inventory.get_list("main")
minetest.chat_send_all(dump(list))
end
end

View File

@ -8,6 +8,7 @@ roadster.gravity = automobiles_lib.gravity
roadster.max_speed = 12
roadster.max_acc_factor = 5
roadster.max_fuel = 10
roadster.trunk_slots = 8
ROADSTER_GAUGE_FUEL_POSITION = {x=0,y=8.04,z=17.84}

View File

@ -36,6 +36,7 @@ minetest.register_craftitem("automobiles_roadster:roadster", {
itemstack:take_item()
ent.object:set_acceleration({x=0,y=-automobiles_lib.gravity,z=0})
automobiles_lib.setText(ent, "Roadster")
automobiles_lib.create_inventory(ent, roadster.trunk_slots, owner)
end
end

View File

@ -271,6 +271,8 @@ minetest.register_entity("automobiles_roadster:roadster", {
_last_ground_check = 0,
_last_light_move = 0,
_last_engine_sound_update = 0,
_inv = nil,
_inv_id = "",
get_staticdata = function(self) -- unloaded/unloads ... is now saved
return minetest.serialize({
@ -286,9 +288,14 @@ minetest.register_entity("automobiles_roadster:roadster", {
stored_rag = self._show_rag,
stored_pitch = self._pitch,
stored_light_old_pos = self._light_old_pos,
stored_inv_id = self._inv_id,
})
end,
on_deactivate = function(self)
automobiles_lib.save_inventory(self)
end,
on_activate = function(self, staticdata, dtime_s)
if staticdata ~= "" and staticdata ~= nil then
local data = minetest.deserialize(staticdata) or {}
@ -305,6 +312,7 @@ minetest.register_entity("automobiles_roadster:roadster", {
self._show_rag = data.stored_rag
self._pitch = data.stored_pitch
self._light_old_pos = data.stored_light_old_pos
self._inv_id = data.stored_inv_id
automobiles_lib.setText(self, "Roadster")
end
@ -381,6 +389,14 @@ minetest.register_entity("automobiles_roadster:roadster", {
self.object:set_armor_groups({immortal=1})
local inv = minetest.get_inventory({type = "detached", name = self._inv_id})
-- if the game was closed the inventories have to be made anew, instead of just reattached
if not inv then
automobiles_lib.create_inventory(self, roadster.trunk_slots)
else
self.inv = inv
end
mobkit.actfunc(self, staticdata, dtime_s)
end,
@ -686,11 +702,15 @@ minetest.register_entity("automobiles_roadster:roadster", {
roadster.driver_formspec(name)
else
if name == self.owner then
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "roadster_engine"},
{object = self.object, gain = 0.5, pitch = 0.6, max_hear_distance = 10, loop = true,})
if clicker:get_player_control().sneak == true then
automobiles_lib.show_vehicle_trunk_formspec(self, clicker, 12)
else
--is the owner, okay, lets attach
automobiles_lib.attach_driver(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "roadster_engine"},
{object = self.object, gain = 0.5, pitch = 0.6, max_hear_distance = 10, loop = true,})
end
else
--minetest.chat_send_all("clicou")
--a passenger

View File

@ -40,6 +40,7 @@ function roadster.destroy(self, puncher)
if self.fuel_gauge then self.fuel_gauge:remove() end
if self.lights then self.lights:remove() end
automobiles_lib.destroy_inventory(self)
self.object:remove()
pos.y=pos.y+2