minetest-mm/mods/vehicle_mash/framework.lua

204 lines
6.3 KiB
Lua
Raw Normal View History

2020-10-25 19:05:08 +01:00
function vehicle_mash.register_vehicle(name, def)
minetest.register_entity(name, {
terrain_type = def.terrain_type,
collisionbox = def.collisionbox,
can_fly = def.can_fly,
2021-03-25 16:46:51 +01:00
can_go_down = def.can_go_down, -- Applies only when `can_fly` is enabled
can_go_up = def.can_go_up, -- Applies only when `can_fly` is enabled
2020-10-25 19:05:08 +01:00
player_rotation = def.player_rotation,
driver_attach_at = def.driver_attach_at,
driver_eye_offset = def.driver_eye_offset,
driver_detach_pos_offset = def.driver_detach_pos_offset,
number_of_passengers = def.number_of_passengers,
2024-12-19 12:55:40 +01:00
passengers = def.passengers,
2020-10-25 19:05:08 +01:00
2021-02-15 16:43:50 +01:00
enable_crash = def.enable_crash,
2020-10-25 19:05:08 +01:00
visual = def.visual,
mesh = def.mesh,
textures = def.textures,
tiles = def.tiles,
visual_size = def.visual_size,
stepheight = def.stepheight,
2021-03-25 16:46:51 +01:00
2020-10-25 19:05:08 +01:00
max_speed_forward = def.max_speed_forward,
max_speed_reverse = def.max_speed_reverse,
2021-03-25 16:46:51 +01:00
max_speed_upwards = def.max_speed_upwards, -- Applies only when `can_fly` is enabled
max_speed_downwards = def.max_speed_downwards, -- Applies only when `can_fly` is enabled
2020-10-25 19:05:08 +01:00
accel = def.accel,
braking = def.braking,
turn_spd = def.turn_speed,
drop_on_destroy = def.drop_on_destroy or {},
driver = nil,
passenge = nil,
v = 0,
v2 = 0,
mouselook = false,
physical = true,
removed = false,
2024-12-19 12:55:40 +01:00
offset = vector.new(0,0,0),
2020-10-25 19:05:08 +01:00
owner = "",
2024-12-19 12:55:40 +01:00
hp_min = def.hp_min,
hp_max = def.hp_max,
armor = def.armor,
rpm_values = {{16, 16, .5}, {10, 10, .4}, {0, 5, .3}},
2020-10-25 19:05:08 +01:00
on_rightclick = function(self, clicker)
if not clicker or not clicker:is_player() then
return
end
-- if there is already a driver
if self.driver then
-- if clicker is driver detach passengers and driver
if clicker == self.driver then
2024-12-19 12:55:40 +01:00
for i = 1, self.number_of_passengers do -- Detaches all passengers with the new Library Mount API.
local passenger = self.passengers[i]
if passenger.player then
lib_mount.detach(passenger.player, passenger.eye_offset)
end
2020-10-25 19:05:08 +01:00
end
-- detach driver
lib_mount.detach(self.driver, self.offset)
-- if clicker is not the driver
else
-- if clicker is passenger
2024-12-19 12:55:40 +01:00
-- detach passengers with the new Library Mount API
for i = 1, self.number_of_passengers do
local passenger = self.passengers[i]
if clicker == passenger.player then
lib_mount.detach(passenger.player, passenger.eye_offset)
break
else
-- Attach passengers with the new Library Mount API
if not passenger.player then -- If there's no current passenger, attach if possible.
lib_mount.attach(self, clicker, true, i)
break -- We've already attached the player, no need to continue.
end
2020-10-25 19:05:08 +01:00
end
end
end
-- if there is no driver
else
-- attach driver
if self.owner == clicker:get_player_name() then
lib_mount.attach(self, clicker, false, 0)
end
end
end,
on_activate = function(self, staticdata, dtime_s)
2024-12-19 12:55:40 +01:00
if def.armor then
self.object:set_armor_groups({fleshy = def.armor}) -- Set armor groups to vehicle
else
self.object:set_armor_groups({fleshy = 0, immortal = 1}) -- Else, make vehicle immortal
end
if def.hp_min and def.hp_max then
self.object:set_hp(math.random(def.hp_min, def.hp_max), "Set HP to vehicle")
end
2020-10-25 19:05:08 +01:00
local tmp = minetest.deserialize(staticdata)
if tmp then
for _,stat in pairs(tmp) do
if _ == "owner" then print(stat) end
self[_] = stat
end
end
print("owner: ", self.owner)
self.v2 = self.v
end,
get_staticdata = function(self)
local tmp = {}
for _,stat in pairs(self) do
local t = type(stat)
if t ~= 'function' and t ~= 'nil' and t ~= 'userdata' then
tmp[_] = self[_]
end
end
return core.serialize(tmp)
end,
2024-12-19 12:55:40 +01:00
on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
2020-10-25 19:05:08 +01:00
if not puncher or not puncher:is_player() or self.removed or self.driver then
return
end
local punchername = puncher:get_player_name()
if self.owner == punchername or minetest.get_player_privs(punchername).protection_bypass then
self.removed = true
-- delay remove to ensure player is detached
minetest.after(0.1, function()
self.object:remove()
end)
puncher:get_inventory():add_item("main", self.name)
end
end,
2024-12-19 12:55:40 +01:00
on_step = function(self, dtime, moveresult, ...)
2021-02-15 16:43:50 +01:00
-- Automatically set `enable_crash` to true if there's no value found
if def.enable_crash == nil then
def.enable_crash = true
end
2024-12-19 12:55:40 +01:00
lib_mount.drive(self, dtime, false, nil, nil, 0, def.can_fly, def.can_go_down, def.can_go_up, def.enable_crash, moveresult)
end,
on_death = function(self, killer)
if self.driver then
lib_mount.detach(self.driver, self.offset)
end
for i = 1, self.number_of_passengers do -- Detaches all passengers with the new Library Mount API.
local passenger = self.passengers[i]
if passenger.player then
lib_mount.detach(passenger.player, passenger.eye_offset)
passenger.player:set_eye_offset(vector.new(0,0,0), vector.new(0,0,0))
end
end
2020-10-25 19:05:08 +01:00
end
})
local can_float = false
if def.terrain_type == 2 or def.terrain_type == 3 then
can_float = true
end
minetest.register_craftitem(name, {
description = def.description,
inventory_image = def.inventory_image,
wield_image = def.wield_image,
wield_scale = def.wield_scale,
liquids_pointable = can_float,
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local ent
if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "liquid") == 0 then
if def.terrain_type == 0 or def.terrain_type == 1 or def.terrain_type == 3 then
pointed_thing.above.y = pointed_thing.above.y + def.onplace_position_adj
ent = minetest.add_entity(pointed_thing.above, name)
else
return
end
else
if def.terrain_type == 2 or def.terrain_type == 3 then
pointed_thing.under.y = pointed_thing.under.y + 0.5
ent = minetest.add_entity(pointed_thing.under, name)
else
return
end
end
if ent:get_luaentity().player_rotation.y == 90 then
ent:set_yaw(placer:get_look_horizontal())
else
ent:set_yaw(placer:get_look_horizontal() - math.pi/2)
end
ent:get_luaentity().owner = placer:get_player_name()
itemstack:take_item()
return itemstack
end
})
if def.recipe then
minetest.register_craft({
output = name,
recipe = def.recipe
})
end
end