fix control max speed, added new test vehicle

This commit is contained in:
Alexsandro Percy 2022-09-11 09:29:25 -03:00
parent 942f2bf6f8
commit b7ee0f99ca
33 changed files with 1196 additions and 22 deletions

View File

@ -419,11 +419,11 @@ minetest.register_entity("automobiles_buggy:buggy", {
self.inv = inv
end
mobkit.actfunc(self, staticdata, dtime_s)
automobiles_lib.actfunc(self, staticdata, dtime_s)
end,
on_step = function(self, dtime)
mobkit.stepfunc(self, dtime)
automobiles_lib.stepfunc(self, dtime)
--[[sound play control]]--
self._last_time_collision_snd = self._last_time_collision_snd + dtime
if self._last_time_collision_snd > 1 then self._last_time_collision_snd = 1 end

View File

@ -2,4 +2,4 @@ name=automobiles_buggy
title=Buggy
description=A buggy
author=apercy
depends=biofuel,automobiles_lib,mobkit
depends=biofuel,automobiles_lib

View File

@ -427,11 +427,11 @@ minetest.register_entity("automobiles_coupe:coupe", {
end
mobkit.actfunc(self, staticdata, dtime_s)
automobiles_lib.actfunc(self, staticdata, dtime_s)
end,
on_step = function(self, dtime)
mobkit.stepfunc(self, dtime)
automobiles_lib.stepfunc(self, dtime)
--[[sound play control]]--
self._last_time_collision_snd = self._last_time_collision_snd + dtime
if self._last_time_collision_snd > 1 then self._last_time_collision_snd = 1 end

View File

@ -1,4 +1,4 @@
name=automobiles_coupe
description=A coupe
author=apercy
depends=biofuel,automobiles_lib,mobkit
depends=biofuel,automobiles_lib

View File

@ -1,10 +1,9 @@
Coupe
This mod adds library for automobiles
licence of code
LGPL3
except painter.lua, licenced as MIT
MIT
physics_lib.lua - MIT by TheTermos
licence of the media:
licence CC0

View File

@ -35,7 +35,7 @@ function automobiles_lib.control(self, dtime, hull_direction, longit_speed, long
local acc = 0
if self._energy > 0 then
if longit_speed < roadster.max_speed and ctrl.up then
if longit_speed < max_speed and ctrl.up then
--get acceleration factor
acc = automobiles_lib.check_road_is_ok(self.object, max_acc_factor)
--minetest.chat_send_all('engineacc: '.. engineacc)

View File

@ -39,28 +39,28 @@ function automobiles_lib.physics(self)
-- buoyancy
local surface = nil
local surfnodename = nil
local spos = mobkit.get_stand_pos(self)
local spos = automobiles_lib.get_stand_pos(self)
spos.y = spos.y+0.01
-- get surface height
local snodepos = mobkit.get_node_pos(spos)
local surfnode = mobkit.nodeatpos(spos)
local snodepos = automobiles_lib.get_node_pos(spos)
local surfnode = automobiles_lib.nodeatpos(spos)
while surfnode and (surfnode.drawtype == 'liquid' or surfnode.drawtype == 'flowingliquid') do
surfnodename = surfnode.name
surface = snodepos.y +0.5
if surface > spos.y+self.height then break end
snodepos.y = snodepos.y+1
surfnode = mobkit.nodeatpos(snodepos)
surfnode = automobiles_lib.nodeatpos(snodepos)
end
self.isinliquid = surfnodename
if surface then -- standing in liquid
-- self.isinliquid = true
local submergence = min(surface-spos.y,self.height)/self.height
-- local balance = self.buoyancy*self.height
local buoyacc = mobkit.gravity*(self.buoyancy-submergence)
mobkit.set_acceleration(self.object,
local buoyacc = (automobiles_lib.gravity*-1)*(self.buoyancy-submergence)
automobiles_lib.set_acceleration(self.object,
{x=-vel.x*self.water_drag,y=buoyacc-vel.y*abs(vel.y)*0.4,z=-vel.z*self.water_drag})
else
self.object:set_acceleration({x=0,y=mobkit.gravity,z=0})
self.object:set_acceleration({x=0,y=-automobiles_lib.gravity,z=0})
end
end

View File

@ -403,6 +403,7 @@ function automobiles_lib.paint(self, colstr)
end
end
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "physics_lib.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua")

View File

@ -1,2 +1,2 @@
name = automobiles_lib
depends=biofuel,mobkit,player_api
depends=biofuel,player_api

177
automobiles_lib/physics_lib.lua Executable file
View File

@ -0,0 +1,177 @@
local abs = math.abs
local pi = math.pi
local floor = math.floor
local ceil = math.ceil
local random = math.random
local sqrt = math.sqrt
local max = math.max
local min = math.min
local tan = math.tan
local pow = math.pow
local sign = function(x)
return (x<0) and -1 or 1
end
function automobiles_lib.rot_to_dir(rot) -- keep rot within <-pi/2,pi/2>
local dir = minetest.yaw_to_dir(rot.y)
dir.y = dir.y+tan(rot.x)*vector.length(dir)
return vector.normalize(dir)
end
function automobiles_lib.dir_to_rot(v,rot)
rot = rot or {x=0,y=0,z=0}
return {x = (v.x==0 and v.y==0 and v.z==0) and rot.x or math.atan2(v.y,vector.length({x=v.x,y=0,z=v.z})),
y = (v.x==0 and v.z==0) and rot.y or minetest.dir_to_yaw(v),
z=rot.z}
end
function automobiles_lib.pos_shift(pos,vec) -- vec components can be omitted e.g. vec={y=1}
vec.x=vec.x or 0
vec.y=vec.y or 0
vec.z=vec.z or 0
return {x=pos.x+vec.x,
y=pos.y+vec.y,
z=pos.z+vec.z}
end
function automobiles_lib.get_stand_pos(thing) -- thing can be luaentity or objectref.
local pos = {}
local colbox = {}
if type(thing) == 'table' then
pos = thing.object:get_pos()
colbox = thing.object:get_properties().collisionbox
elseif type(thing) == 'userdata' then
pos = thing:get_pos()
colbox = thing:get_properties().collisionbox
else
return false
end
return automobiles_lib.pos_shift(pos,{y=colbox[2]+0.01}), pos
end
function automobiles_lib.get_node_pos(pos)
return {
x=floor(pos.x+0.5),
y=floor(pos.y+0.5),
z=floor(pos.z+0.5),
}
end
function automobiles_lib.nodeatpos(pos)
local node = minetest.get_node_or_nil(pos)
if node then return minetest.registered_nodes[node.name] end
end
function automobiles_lib.minmax(v,m)
return min(abs(v),m)*sign(v)
end
function automobiles_lib.set_acceleration(thing,vec,limit)
limit = limit or 100
if type(thing) == 'table' then thing=thing.object end
vec.x=automobiles_lib.minmax(vec.x,limit)
vec.y=automobiles_lib.minmax(vec.y,limit)
vec.z=automobiles_lib.minmax(vec.z,limit)
thing:set_acceleration(vec)
end
function automobiles_lib.actfunc(self, staticdata, dtime_s)
self.logic = self.logic or self.brainfunc
self.physics = self.physics or automobiles_lib.physics
self.lqueue = {}
self.hqueue = {}
self.nearby_objects = {}
self.nearby_players = {}
self.pos_history = {}
self.path_dir = 1
self.time_total = 0
self.water_drag = self.water_drag or 1
local sdata = minetest.deserialize(staticdata)
if sdata then
for k,v in pairs(sdata) do
self[k] = v
end
end
if self.textures==nil then
local prop_tex = self.object:get_properties().textures
if prop_tex then self.textures=prop_tex end
end
if not self.memory then -- this is the initial activation
self.memory = {}
-- texture variation
if #self.textures > 1 then self.texture_no = random(#self.textures) end
end
if self.timeout and ((self.timeout>0 and dtime_s > self.timeout and next(self.memory)==nil) or
(self.timeout<0 and dtime_s > abs(self.timeout))) then
self.object:remove()
end
-- apply texture
if self.textures and self.texture_no then
local props = {}
props.textures = {self.textures[self.texture_no]}
self.object:set_properties(props)
end
--hp
self.max_hp = self.max_hp or 10
self.hp = self.hp or self.max_hp
--armor
if type(self.armor_groups) ~= 'table' then
self.armor_groups={}
end
self.armor_groups.immortal = 1
self.object:set_armor_groups(self.armor_groups)
self.buoyancy = self.buoyancy or 0
self.oxygen = self.oxygen or self.lung_capacity
self.lastvelocity = {x=0,y=0,z=0}
end
function automobiles_lib.get_box_height(self)
if type(self) == 'table' then self = self.object end
local colbox = self:get_properties().collisionbox
local height = 0.1
if colbox then height = colbox[5]-colbox[2] end
return height > 0 and height or 0.1
end
function automobiles_lib.stepfunc(self,dtime,colinfo)
self.dtime = min(dtime,0.2)
self.colinfo = colinfo
self.height = automobiles_lib.get_box_height(self)
-- physics comes first
local vel = self.object:get_velocity()
if colinfo then
self.isonground = colinfo.touching_ground
else
if self.lastvelocity.y==0 and vel.y==0 then
self.isonground = true
else
self.isonground = false
end
end
self:physics()
if self.logic then
if self.view_range then self:sensefunc() end
self:logic()
execute_queues(self)
end
self.lastvelocity = self.object:get_velocity()
self.time_total=self.time_total+self.dtime
end

View File

@ -0,0 +1,7 @@
Motorcycle
This mod adds a motorcycle to minetest
licence see the files

26
automobiles_motorcycle/init.lua Executable file
View File

@ -0,0 +1,26 @@
--
-- constants
--
motorcycle={}
motorcycle.LONGIT_DRAG_FACTOR = 0.14*0.14
motorcycle.LATER_DRAG_FACTOR = 25.0
motorcycle.gravity = automobiles_lib.gravity
motorcycle.max_speed = 20
motorcycle.max_acc_factor = 8
motorcycle.max_fuel = 5
motorcycle.trunk_slots = 0
motorcycle.front_wheel_xpos = 0
motorcycle.rear_wheel_xpos = 0
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
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_motorcycle") .. DIR_DELIM .. "motorcycle_player.lua")
dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "motorcycle_utilities.lua")
dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "motorcycle_entities.lua")
dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "motorcycle_forms.lua")
dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "motorcycle_crafts.lua")

View File

@ -0,0 +1,5 @@
name=automobiles_motorcycle
title=Buggy
description=A motorcycle
author=apercy
depends=biofuel,automobiles_lib

View File

@ -0,0 +1,75 @@
--
-- items
--
-- body
minetest.register_craftitem("automobiles_motorcycle:body",{
description = "Motorcycle body",
inventory_image = "automobiles_motorcycle_body.png",
})
-- wheel
minetest.register_craftitem("automobiles_motorcycle:wheel",{
description = "Motorcycle wheel",
inventory_image = "automobiles_motorcycle_wheel_icon.png",
})
-- motorcycle
minetest.register_craftitem("automobiles_motorcycle:motorcycle", {
description = "Motorcycle",
inventory_image = "automobiles_motorcycle.png",
liquids_pointable = false,
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pointed_pos = pointed_thing.above
--pointed_pos.y=pointed_pos.y+0.2
local car = minetest.add_entity(pointed_pos, "automobiles_motorcycle:motorcycle")
if car and placer then
local ent = car:get_luaentity()
local owner = placer:get_player_name()
if ent then
ent.owner = 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, "motorcycle")
automobiles_lib.create_inventory(ent, motorcycle.trunk_slots, owner)
end
end
return itemstack
end,
})
--
-- crafting
--
if minetest.get_modpath("default") then
minetest.register_craft({
output = "automobiles_motorcycle:motorcycle",
recipe = {
{"automobiles_motorcycle:wheel", "automobiles_lib:body", "automobiles_motorcycle:wheel"},
}
})
minetest.register_craft({
output = "automobiles_motorcycle:body",
recipe = {
{"default:glass" ,"","default:steel_ingot"},
{"default:steel_ingot","","default:steel_ingot"},
{"default:steel_ingot","automobiles_lib:engine", "default:steel_ingot"},
}
})
minetest.register_craft({
output = "automobiles_motorcycle:wheel",
recipe = {
{"default:steel_ingot", "default:tin_ingot", "default:steel_ingot"},
{"default:steel_ingot","default:steelblock", "default:steel_ingot"},
{"default:steel_ingot", "default:tin_ingot", "default:steel_ingot"},
}
})
end

View File

@ -0,0 +1,597 @@
--
-- entity
--
minetest.register_entity('automobiles_motorcycle:lights',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "automobiles_motorcycle_lights.b3d",
textures = {"automobiles_grey.png",},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity('automobiles_motorcycle:r_lights',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "automobiles_motorcycle_r_lights.b3d",
textures = {"automobiles_red.png",},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity('automobiles_motorcycle:pivot_mesh',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "automobiles_pivot_mesh.b3d",
textures = {"automobiles_black.png",},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity('automobiles_motorcycle:pointer',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "automobiles_pointer.b3d",
visual_size = {x = 0.8, y = 0.8, z = 0.8},
textures = {"automobiles_white.png"},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity("automobiles_motorcycle:motorcycle", {
initial_properties = {
physical = true,
collide_with_objects = true,
collisionbox = {-0.1, -0.43, -0.1, 0.1, 1, 0.1},
selectionbox = {-1, 1, -1, 1, -1, 1},
stepheight = 0.7,
visual = "mesh",
mesh = "automobiles_motorcycle_body.b3d",
--use_texture_alpha = true,
backface_culling = false,
textures = {
"automobiles_black.png", --bancos
"automobiles_black.png", --chassis
"automobiles_black.png", --suspensao traseira
"automobiles_metal.png", --metais
"automobiles_painting.png", --paralamas
"automobiles_black.png", --preto
"automobiles_motorcycle_wheel.png", --rodas
"automobiles_metal.png", --escapamento
"automobiles_black.png", --saida escape
"automobiles_metal.png", --motor
"automobiles_painting.png", --paralamas 2
"automobiles_painting.png", --carenagens
"automobiles_blue.png", --frontlights
"automobiles_red.png", --ref
},
},
textures = {},
driver_name = nil,
sound_handle = nil,
owner = "",
static_save = true,
infotext = "A very nice motorcycle!",
hp = 50,
buoyancy = 2,
physics = automobiles_lib.physics,
lastvelocity = vector.new(),
time_total = 0,
_passenger = nil,
_color = "#AAAAAA",
_steering_angle = 0,
_engine_running = false,
_last_checkpoint = "",
_total_laps = -1,
_race_id = "",
_energy = 1,
_last_time_collision_snd = 0,
_last_time_drift_snd = 0,
_last_time_command = 0,
_roll = math.rad(0),
_pitch = 0,
_longit_speed = 0,
_show_lights = false,
_light_old_pos = nil,
_last_ground_check = 0,
_last_light_move = 0,
_last_engine_sound_update = 0,
_inv = nil,
_inv_id = "",
_change_color = automobiles_lib.paint,
get_staticdata = function(self) -- unloaded/unloads ... is now saved
return minetest.serialize({
stored_owner = self.owner,
stored_hp = self.hp,
stored_color = self._color,
stored_steering = self._steering_angle,
stored_energy = self._energy,
--race data
stored_last_checkpoint = self._last_checkpoint,
stored_total_laps = self._total_laps,
stored_race_id = self._race_id,
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 {}
self.owner = data.stored_owner
self.hp = data.stored_hp
self._color = data.stored_color
self._steering_angle = data.stored_steering
self._energy = data.stored_energy
--minetest.debug("loaded: ", self.energy)
--race data
self._last_checkpoint = data.stored_last_checkpoint
self._total_laps = data.stored_total_laps
self._race_id = data.stored_race_id
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, "Motorcycle")
end
self.object:set_animation({x = 1, y = 41}, 0, 0, true)
automobiles_lib.paint(self, self._color)
local pos = self.object:get_pos()
local lights = minetest.add_entity(pos,'automobiles_motorcycle:lights')
lights:set_attach(self.object,'',{x=0,y=0,z=0},{x=0,y=0,z=0})
self.lights = lights
self.lights:set_properties({is_visible=true})
local rlights = minetest.add_entity(pos,'automobiles_motorcycle:r_lights')
rlights:set_attach(self.object,'',{x=0,y=0,z=0},{x=0,y=0,z=0})
self.rlights = rlights
self.rlights:set_properties({is_visible=true})
local driver_seat=minetest.add_entity(pos,'automobiles_motorcycle:pivot_mesh')
driver_seat:set_attach(self.object,'',{x=0.0,y=-1.1,z=5.5},{x=0,y=0,z=0})
self.driver_seat = driver_seat
local passenger_seat=minetest.add_entity(pos,'automobiles_motorcycle:pivot_mesh')
passenger_seat:set_attach(self.object,'',{x=0.0,y=-0.3,z=0.09},{x=0,y=0,z=0})
self.passenger_seat = passenger_seat
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, motorcycle.trunk_slots)
else
self.inv = inv
end
self.object:set_bone_position("guidao", {x=0, y=0, z=17.5}, {x=30, y=180, z=0})
self.lights:set_bone_position("guidao", {x=0, y=0, z=17.5}, {x=30, y=180, z=0})
automobiles_lib.actfunc(self, staticdata, dtime_s)
end,
on_step = function(self, dtime)
automobiles_lib.stepfunc(self, dtime)
--[[sound play control]]--
self._last_time_collision_snd = self._last_time_collision_snd + dtime
if self._last_time_collision_snd > 1 then self._last_time_collision_snd = 1 end
self._last_time_drift_snd = self._last_time_drift_snd + dtime
if self._last_time_drift_snd > 1 then self._last_time_drift_snd = 1 end
--[[end sound control]]--
local rotation = self.object:get_rotation()
local yaw = rotation.y
local newyaw=yaw
local pitch = rotation.x
local hull_direction = minetest.yaw_to_dir(yaw)
local nhdir = {x=hull_direction.z,y=0,z=-hull_direction.x} -- lateral unit vector
local velocity = self.object:get_velocity()
local longit_speed = automobiles_lib.dot(velocity,hull_direction)
local fuel_weight_factor = (5 - self._energy)/5000
local longit_drag = vector.multiply(hull_direction,(longit_speed*longit_speed) *
(motorcycle.LONGIT_DRAG_FACTOR - fuel_weight_factor) * -1 * automobiles_lib.sign(longit_speed))
local later_speed = automobiles_lib.dot(velocity,nhdir)
local later_drag = vector.multiply(nhdir,later_speed*
later_speed*motorcycle.LATER_DRAG_FACTOR*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local player = nil
local is_attached = false
if self.driver_name then
player = minetest.get_player_by_name(self.driver_name)
if player then
local player_attach = player:get_attach()
if player_attach then
if self.driver_seat then
if player_attach == self.driver_seat then is_attached = true end
end
end
end
end
local is_breaking = false
if is_attached then
local ctrl = player:get_player_control()
if ctrl.aux1 then
--sets the engine running - but sets a delay also, cause keypress
if self._last_time_command > 0.8 then
self._last_time_command = 0
minetest.sound_play({name = "automobiles_horn"},
{object = self.object, gain = 0.6, pitch = 1.0, max_hear_distance = 32, loop = false,})
end
end
if ctrl.down then
is_breaking = true
self.lights:set_properties({textures={"automobiles_motorcycle_lights.png",}, glow=32})
self.rlights:set_properties({textures={"automobiles_motorcycle_rear_lights_full.png",}, glow=32})
end
else
--desligado
self.lights:set_properties({textures={"automobiles_grey.png",}, glow=0})
self.rlights:set_properties({textures={"automobiles_rear_lights_off.png",}, glow=0})
end
self._last_light_move = self._last_light_move + dtime
if self._last_light_move > 0.15 then
self._last_light_move = 0
if self._show_lights == true then
--self.lights:set_properties({is_visible=true})
self.lights:set_properties({textures={"automobiles_motorcycle_lights.png",}, glow=32})
self.rlights:set_properties({textures={"automobiles_motorcycle_rear_lights_full.png",}, glow=32})
if is_breaking == false then
self.lights:set_properties({textures={"automobiles_motorcycle_lights.png",}, glow=32})
self.rlights:set_properties({textures={"automobiles_motorcycle_rear_lights_full.png",}, glow=10})
end
automobiles_lib.put_light(self)
else
if is_breaking == false then
--desligado
self.lights:set_properties({textures={"automobiles_grey.png",}, glow=0})
self.rlights:set_properties({textures={"automobiles_rear_lights_off.png",}, glow=0})
end
automobiles_lib.remove_light(self)
end
end
local curr_pos = self.object:get_pos()
local steering_angle_max = 30
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
self._show_lights = true
if self.driver_mesh then self.driver_mesh:set_properties({is_visible=true}) end
local impact = automobiles_lib.get_hipotenuse_value(velocity, self.lastvelocity)
if impact > 1 then
--self.damage = self.damage + impact --sum the impact value directly to damage meter
if self._last_time_collision_snd > 0.3 then
self._last_time_collision_snd = 0
minetest.sound_play("collision", {
to_player = self.driver_name,
--pos = curr_pos,
--max_hear_distance = 5,
gain = 1.0,
fade = 0.0,
pitch = 1.0,
})
end
--[[if self.damage > 100 then --if acumulated damage is greater than 100, adieu
automobiles_lib.destroy(self)
end]]--
end
local min_later_speed = 0.9
if (later_speed > min_later_speed or later_speed < -min_later_speed) and
self._last_time_drift_snd > 0.6 then
self._last_time_drift_snd = 0
minetest.sound_play("drifting", {
to_player = self.driver_name,
pos = curr_pos,
max_hear_distance = 5,
gain = 1.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
--control
local steering_speed = 80
if math.abs(longit_speed) > 3 then
local mid_speed = (steering_speed/2)
steering_speed = mid_speed + mid_speed / math.abs(longit_speed*0.25)
end
accel, stop = automobiles_lib.control(self, dtime, hull_direction, longit_speed, longit_drag, later_drag, accel, motorcycle.max_acc_factor, motorcycle.max_speed, steering_angle_max, steering_speed)
else
if self.driver_mesh then self.driver_mesh:set_properties({is_visible=false}) end
self._show_lights = false
if self.sound_handle ~= nil then
minetest.sound_stop(self.sound_handle)
self.sound_handle = nil
end
end
local angle_factor = self._steering_angle / 10
self.object:set_animation_frame_speed(longit_speed * (10 - angle_factor))
--whell turn
self.object:set_bone_position("eixo_direcao", {x=0, y=0, z=0}, {x=0, y=-self._steering_angle-angle_factor, z=0})
self.lights:set_bone_position("eixo_direcao", {x=0, y=0, z=0}, {x=0, y=-self._steering_angle-angle_factor, z=0})
if player then
-- -30 direita -> steering_angle_max
-- +30 esquerda
local arm_range = 2
local range = steering_angle_max * 2
local armZ = -((self._steering_angle+steering_angle_max) * arm_range) / 60
--player:set_bone_position("Arm_Left", {x=3.0, y=5, z=-arm_range-armZ}, {x=240-(self._steering_angle/2), y=0, z=0})
--player:set_bone_position("Arm_Right", {x=-3.0, y=5, z=armZ}, {x=240+(self._steering_angle/2), y=0, z=0})
if self.driver_mesh then
self.driver_mesh:set_bone_position("Arm_Left", {x=3.0, y=5, z=-armZ-2}, {x=60-(self._steering_angle/2), y=0, z=0})
self.driver_mesh:set_bone_position("Arm_Right", {x=-3.0, y=5, z=armZ}, {x=60+(self._steering_angle/2), y=0, z=0})
end
end
if math.abs(self._steering_angle)>5 then
local turn_rate = math.rad(40)
newyaw = yaw + dtime*(1 - 1 / (math.abs(longit_speed) + 1)) *
self._steering_angle / 30 * turn_rate * automobiles_lib.sign(longit_speed)
end
--[[
accell correction
under some circunstances the acceleration exceeds the max value accepted by set_acceleration and
the game crashes with an overflow, so limiting the max acceleration in each axis prevents the crash
]]--
local max_factor = 25
local acc_adjusted = 10
if accel.x > max_factor then accel.x = acc_adjusted end
if accel.x < -max_factor then accel.x = -acc_adjusted end
if accel.z > max_factor then accel.z = acc_adjusted end
if accel.z < -max_factor then accel.z = -acc_adjusted end
-- end correction
-- calculate energy consumption --
----------------------------------
if self._energy > 0 then
local zero_reference = vector.new()
local acceleration = automobiles_lib.get_hipotenuse_value(accel, zero_reference)
--minetest.chat_send_all(acceleration)
local consumed_power = acceleration/40000
self._energy = self._energy - consumed_power;
end
if self._energy <= 0 then
self._engine_running = false
if self.sound_handle then minetest.sound_stop(self.sound_handle) end
--minetest.chat_send_player(self.driver_name, "Out of fuel")
else
self._last_engine_sound_update = self._last_engine_sound_update + dtime
if self._last_engine_sound_update > 0.300 then
self._last_engine_sound_update = 0
motorcycle.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
----------------------------
-- end energy consumption --
accel.y = -automobiles_lib.gravity
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:set_acceleration(accel)
else
if stop == true then
self.object:set_acceleration({x=0,y=0,z=0})
self.object:set_velocity({x=0,y=0,z=0})
end
end
self._last_ground_check = self._last_ground_check + dtime
if self._last_ground_check > 0.18 then
self._last_ground_check = 0
automobiles_lib.ground_get_distances(self, 0.372, 2.3)
end
local newpitch = self._pitch --velocity.y * math.rad(6)
local turn_effect_speed = longit_speed
if turn_effect_speed > 10 then turn_effect_speed = 10 end
local newroll = (-self._steering_angle/100)*(turn_effect_speed/10)
if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=newroll}) end
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_punch = function(self, puncher, ttime, toolcaps, dir, damage)
if not puncher or not puncher:is_player() then
return
end
local name = puncher:get_player_name()
--[[if self.owner and self.owner ~= name and self.owner ~= "" then return end]]--
if self.owner == nil then
self.owner = name
end
if self.driver_name and self.driver_name ~= name then
-- do not allow other players to remove the object while there is a driver
return
end
local is_attached = false
if puncher:get_attach() == self.driver_seat then is_attached = true end
local itmstck=puncher:get_wielded_item()
local item_name = ""
if itmstck then item_name = itmstck:get_name() end
--refuel procedure
--[[
refuel works it car is stopped and engine is off
]]--
local velocity = self.object:get_velocity()
local speed = automobiles_lib.get_hipotenuse_value(vector.new(), velocity)
if math.abs(speed) <= 0.1 then
if automobiles_lib.loadFuel(self, puncher:get_player_name(), false, motorcycle.max_fuel) then return end
end
-- end refuel
if is_attached == false then
-- deal with painting or destroying
if itmstck then
--race status restart
if item_name == "checkpoints:status_restarter" and self._engine_running == false then
--restart race current status
self._last_checkpoint = ""
self._total_laps = -1
self._race_id = ""
return
end
if automobiles_lib.set_paint(self, puncher, itmstck) == false then
local is_admin = false
is_admin = minetest.check_player_privs(puncher, {server=true})
--minetest.chat_send_all('owner '.. self.owner ..' - name '.. name)
if not self.driver and (self.owner == name or is_admin == true) and toolcaps and
toolcaps.damage_groups and toolcaps.damage_groups.fleshy then
self.hp = self.hp - 10
minetest.sound_play("collision", {
object = self.object,
max_hear_distance = 5,
gain = 1.0,
fade = 0.0,
pitch = 1.0,
})
end
end
end
if self.hp <= 0 then
motorcycle.destroy(self)
end
end
end,
on_rightclick = function(self, clicker)
if not clicker or not clicker:is_player() then
return
end
local name = clicker:get_player_name()
--[[if self.owner and self.owner ~= name and self.owner ~= "" then return end]]--
if self.owner == "" then
self.owner = name
end
if name == self.driver_name then
motorcycle.driver_formspec(name)
else
if name == self.owner then
if clicker:get_player_control().aux1 == true then
automobiles_lib.show_vehicle_trunk_formspec(self, clicker, motorcycle.trunk_slots)
else
--is the owner, okay, lets attach
motorcycle.attach_driver_stand(self, clicker)
-- sound
self.sound_handle = minetest.sound_play({name = "motorcycle_engine"},
{object = self.object, gain = 4, pitch = 1, max_hear_distance = 10, loop = true,})
end
else
--minetest.chat_send_all("clicou")
--a passenger
if self._passenger == nil then
--there is no passenger, so lets attach
if self.driver_name then
motorcycle.attach_pax_stand(self, clicker)
end
else
--there is a passeger
if self._passenger == name then
--if you are the psenger, so deattach
motorcycle.dettach_pax_stand(self, clicker)
end
end
end
end
end,
})

View File

@ -0,0 +1,46 @@
--------------
-- Manual --
--------------
function motorcycle.getCarFromPlayer(player)
local seat = player:get_attach()
if seat then
local car = seat:get_attach()
return car
end
return nil
end
function motorcycle.driver_formspec(name)
local basic_form = table.concat({
"formspec_version[3]",
"size[6,6]",
}, "")
basic_form = basic_form.."button[1,1.0;4,1;go_out;Go Offboard]"
minetest.show_formspec(name, "motorcycle:driver_main", basic_form)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "motorcycle:driver_main" then
local name = player:get_player_name()
local car_obj = motorcycle.getCarFromPlayer(player)
if car_obj then
local ent = car_obj:get_luaentity()
if ent then
if fields.go_out then
if ent._passenger then --any pax?
local pax_obj = minetest.get_player_by_name(ent._passenger)
motorcycle.dettach_pax_stand(ent, pax_obj)
end
motorcycle.dettach_driver_stand(ent, player)
end
end
end
minetest.close_formspec(name, "motorcycle:driver_main")
end
end)

View File

@ -0,0 +1,175 @@
minetest.register_entity('automobiles_motorcycle:player_mesh',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "character.b3d",
textures = {"character.png"},
is_visible = false,
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
-- attach player
function motorcycle.attach_driver_stand(self, player)
local name = player:get_player_name()
self.driver_name = name
self.driver_properties = player:get_properties()
self.driver_properties.selectionbox = nil
self.driver_properties.pointable = false
self.driver_properties.show_on_minimap = false
self.driver_properties.static_save = nil
self.driver_properties.makes_footstep_sound = nil
--minetest.chat_send_all(dump(self.driver_properties))
-- attach the driver
player:set_attach(self.driver_seat, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
player_api.player_attached[name] = true
-- makes it "invisible"
player:set_properties({mesh = "automobiles_pivot_mesh.b3d"})
--create the dummy mesh
local pos = player:get_pos()
local driver_mesh=minetest.add_entity(pos,'automobiles_motorcycle:player_mesh')
driver_mesh:set_attach(player,'',{x=0.0,y=-0.0,z=0.0},{x=0,y=0,z=0})
self.driver_mesh = driver_mesh
self.driver_mesh:set_properties({is_visible=false})
--position the dummy arms and legs
self.driver_mesh:set_properties(self.driver_properties)
self.driver_mesh:set_bone_position("Leg_Left", {x=1.1, y=0, z=0}, {x=12, y=0, z=10})
self.driver_mesh:set_bone_position("Leg_Right", {x=-1.1, y=0, z=0}, {x=12, y=0, z=-10})
self.driver_mesh:set_properties({
is_visible=true,
})
--[[player:set_bone_position("Leg_Left", {x=1.1, y=0, z=0}, {x=180+12, y=0, z=10})
player:set_bone_position("Leg_Right", {x=-1.1, y=0, z=0}, {x=180+12, y=0, z=-10})
player:set_bone_position("Arm_Left", {x=3.0, y=5, z=-1}, {x=180+70, y=0, z=0})
player:set_bone_position("Arm_Right", {x=-3.0, y=5, z=-1}, {x=180+70, y=0, z=0})]]--
end
function motorcycle.dettach_driver_stand(self, player)
local name = self.driver_name
--self._engine_running = false
-- driver clicked the object => driver gets off the vehicle
self.driver_name = nil
if self._engine_running then
self._engine_running = false
end
-- sound and animation
if self.sound_handle then
minetest.sound_stop(self.sound_handle)
self.sound_handle = nil
end
-- detach the player
if player then
--automobiles_lib.remove_hud(player)
player:set_detach()
player_api.player_attached[name] = nil
player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
player_api.set_animation(player, "stand")
if self.driver_properties then
player:set_properties({mesh = self.driver_properties.mesh})
self.driver_properties = nil
end
--player:set_properties({visual_size = {x=1, y=1}})
if driver_mesh then
self.driver_mesh:set_properties({is_visible=false})
self.driver_mesh:remove()
end
end
self.driver = nil
end
-- attach passenger
function motorcycle.attach_pax_stand(self, player)
local onside = onside or false
local name = player:get_player_name()
self.pax_properties = player:get_properties()
self.pax_properties.selectionbox = nil
self.pax_properties.pointable = false
self.pax_properties.show_on_minimap = false
self.pax_properties.static_save = nil
self.pax_properties.makes_footstep_sound = nil
if self._passenger == nil then
self._passenger = name
-- attach the driver
player:set_attach(self.passenger_seat, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
player_api.player_attached[name] = true
-- makes it "invisible"
player:set_properties({mesh = "automobiles_pivot_mesh.b3d"})
--create the dummy mesh
local pos = player:get_pos()
local pax_mesh=minetest.add_entity(pos,'automobiles_motorcycle:player_mesh')
pax_mesh:set_attach(player,'',{x=0.0,y=-0.0,z=0.0},{x=0,y=0,z=0})
self.pax_mesh = pax_mesh
self.pax_mesh:set_properties({is_visible=false})
--position the dummy arms and legs
self.pax_mesh:set_properties(self.pax_properties)
self.pax_mesh:set_bone_position("Leg_Left", {x=1.1, y=0, z=0}, {x=12, y=0, z=10})
self.pax_mesh:set_bone_position("Leg_Right", {x=-1.1, y=0, z=0}, {x=12, y=0, z=-10})
self.pax_mesh:set_properties({
is_visible=true,
})
end
end
function motorcycle.dettach_pax_stand(self, player)
local name = player:get_player_name() --self._passenger
-- passenger clicked the object => driver gets off the vehicle
if self._passenger == name then
self._passenger = nil
end
-- detach the player
if player then
--player:set_properties({physical=true})
player:set_detach()
player_api.player_attached[name] = nil
player_api.set_animation(player, "stand")
player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
--remove_physics_override(player, {speed=1,gravity=1,jump=1})
if self.pax_properties then
player:set_properties({mesh = self.pax_properties.mesh})
self.pax_properties = nil
end
--player:set_properties({visual_size = {x=1, y=1}})
if self.pax_mesh then
self.pax_mesh:set_properties({is_visible=false})
self.pax_mesh:remove()
end
end
end

View File

@ -0,0 +1,66 @@
--dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "buggy_global_definitions.lua")
--dofile(minetest.get_modpath("automobiles_motorcycle") .. DIR_DELIM .. "buggy_hud.lua")
-- destroy the buggy
function motorcycle.destroy(self, puncher)
automobiles_lib.remove_light(self)
if self.sound_handle then
minetest.sound_stop(self.sound_handle)
self.sound_handle = nil
end
if self.driver_name then
-- detach the driver first (puncher must be driver)
if puncher then
puncher:set_detach()
puncher:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
if minetest.global_exists("player_api") then
player_api.player_attached[self.driver_name] = nil
-- player should stand again
player_api.set_animation(puncher, "stand")
end
end
self.driver_name = nil
end
local pos = self.object:get_pos()
if self.driver_seat then self.driver_seat:remove() end
if self.passenger_seat then self.passenger_seat:remove() end
if self.lights then self.lights:remove() end
if self.rlights then self.rlights:remove() end
if self.driver_mesh then self.driver_mesh:remove() end
if self.pax_mesh then self.pax_mesh:remove() end
automobiles_lib.destroy_inventory(self)
self.object:remove()
pos.y=pos.y+2
--minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_motorcycle:buggy')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_lib:engine')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_motorcycle:wheel')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_motorcycle:wheel')
end
function motorcycle.engine_set_sound_and_animation(self, _longit_speed)
--minetest.chat_send_all('test1 ' .. dump(self._engine_running) )
if self.sound_handle then
if (math.abs(self._longit_speed) > math.abs(_longit_speed) + 0.08) or (math.abs(self._longit_speed) + 0.08 < math.abs(_longit_speed)) then
--minetest.chat_send_all('test2')
motorcycle.engineSoundPlay(self)
end
end
end
function motorcycle.engineSoundPlay(self)
--sound
if self.sound_handle then minetest.sound_stop(self.sound_handle) end
if self.object then
self.sound_handle = minetest.sound_play({name = "buggy_engine"},
{object = self.object, gain = 8,
pitch = 1 + ((self._longit_speed/10)/2),
max_hear_distance = 10,
loop = true,})
end
end

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -2,4 +2,4 @@ name=automobiles_roadster
title=Roadster
description=A roadster automobile
author=apercy
depends=biofuel,automobiles_lib,mobkit
depends=biofuel,automobiles_lib

View File

@ -400,11 +400,11 @@ minetest.register_entity("automobiles_roadster:roadster", {
self.inv = inv
end
mobkit.actfunc(self, staticdata, dtime_s)
automobiles_lib.actfunc(self, staticdata, dtime_s)
end,
on_step = function(self, dtime)
mobkit.stepfunc(self, dtime)
automobiles_lib.stepfunc(self, dtime)
--[[sound play control]]--
self._last_time_collision_snd = self._last_time_collision_snd + dtime
if self._last_time_collision_snd > 1 then self._last_time_collision_snd = 1 end