cars unified using one common step method (except delorean) and added initial work on beetle

This commit is contained in:
Alexsandro Percy 2023-12-26 20:08:17 -03:00
parent da92a02a3a
commit 2575abbe3f
36 changed files with 865 additions and 1278 deletions

10
automobiles_beetle/README.md Executable file
View File

@ -0,0 +1,10 @@
## Beetle
This mod adds a beetle automobile to Minetest.
## Licenses
- Code: see [LICENSE](/LICENSE)
- Media: Model made by myself; licence CC0
## Screenshot
![beetle](/automobiles_beetle/screenshot.jpg)

View File

@ -0,0 +1,64 @@
local S = minetest.get_translator(minetest.get_current_modname())
--
-- items
--
-- body
minetest.register_craftitem("automobiles_beetle:beetle_body",{
description = S("Beetle Body"),
inventory_image = "automobiles_beetle_body.png",
})
-- beetle
minetest.register_craftitem("automobiles_beetle:beetle", {
description = S("Beetle"),
inventory_image = "automobiles_beetle.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_beetle:beetle")
if car and placer then
local ent = car:get_luaentity()
local owner = placer:get_player_name()
if ent then
ent.owner = owner
--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, "Beetle")
automobiles_lib.create_inventory(ent, ent._trunk_slots, owner)
end
end
return itemstack
end,
})
--
-- crafting
--
if minetest.get_modpath("default") then
minetest.register_craft({
output = "automobiles_beetle:beetle",
recipe = {
{"automobiles_lib:wheel", "automobiles_lib:engine", "automobiles_lib:wheel"},
{"automobiles_lib:wheel","automobiles_beetle:beetle_body", "automobiles_lib:wheel"},
}
})
minetest.register_craft({
output = "automobiles_beetle:beetle_body",
recipe = {
{"default:glass" ,"default:steel_ingot","default:steel_ingot"},
{"default:steel_ingot","default:steel_ingot","default:steel_ingot"},
{"default:steelblock","default:steelblock", "default:steelblock"},
}
})
end

428
automobiles_beetle/entities.lua Executable file
View File

@ -0,0 +1,428 @@
-- destroy the beetle
function auto_beetle.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.front_suspension then self.front_suspension:remove() end
if self.lf_wheel then self.lf_wheel:remove() end
if self.rf_wheel then self.rf_wheel:remove() end
if self.rear_suspension then self.rear_suspension:remove() end
if self.lr_wheel then self.lr_wheel:remove() end
if self.rr_wheel then self.rr_wheel:remove() end
if self.fuel_gauge then self.fuel_gauge:remove() end
if self.lights then self.lights:remove() end
if self.r_lights then self.r_lights:remove() end
if self.reverse_lights then self.reverse_lights:remove() end
if self.turn_l_light then self.turn_l_light:remove() end
if self.turn_r_light then self.turn_r_light:remove() end
if self.back_seat then self.back_seat:remove() end
automobiles_lib.seats_destroy(self)
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_lib:engine')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_lib:wheel')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_lib:wheel')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_lib:wheel')
minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'automobiles_lib:wheel')
end
--
-- entity
--
minetest.register_entity('automobiles_beetle:wheel',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "beetle_anim_wheel.b3d",
backface_culling = false,
textures = {"automobiles_black.png", "automobiles_metal.png", "beetle_wheel.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_beetle:front_suspension',{
initial_properties = {
physical = true,
collide_with_objects=true,
collisionbox = {-0.5, 0, -0.5, 0.5, 1, 0.5},
pointable=false,
visual = "mesh",
mesh = "automobiles_buggy_f_suspension.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,
--[[on_step = function(self, dtime, moveresult)
minetest.chat_send_all(dump(moveresult))
end,]]--
})
minetest.register_entity('automobiles_beetle:rear_suspension',{
initial_properties = {
physical = true,
collide_with_objects=true,
pointable=false,
visual = "mesh",
mesh = "automobiles_buggy_r_suspension.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_beetle:f_lights',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "beetle_f_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_beetle:r_lights',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "beetle_pos_lights.b3d",
textures = {"automobiles_rear_lights_off.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_beetle:turn_left_light',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "beetle_turn_l_light.b3d",
textures = {"automobiles_turn.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_beetle:turn_right_light',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "beetle_turn_r_light.b3d",
textures = {"automobiles_turn.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_beetle:reverse_lights',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
glow = 0,
visual = "mesh",
mesh = "beetle_reverse_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,
})
function auto_beetle.paint(self, colstr)
local l_textures = self.initial_properties.textures
self._color = colstr
--paint details
local target_texture = "beetle_painting.png"
local accessorie_texture = "beetle_paint_interior.png"
for _, texture in ipairs(l_textures) do
local indx = texture:find(target_texture)
if indx then
l_textures[_] = "("..target_texture.."^[multiply:"..colstr..")^("..target_texture.."^[multiply:#BBBBBB^[mask:beetle_details.png)" --here changes the main color
end
local indx = texture:find(accessorie_texture)
if indx then
l_textures[_] = accessorie_texture.."^[multiply:".. colstr --here changes the main color
end
end
self.object:set_properties({textures=l_textures})
end
function auto_beetle.set_paint(self, puncher, itmstck)
local is_admin = false
is_admin = minetest.check_player_privs(puncher, {server=true})
if not (self.owner == puncher:get_player_name() or is_admin == true) then
return
end
local item_name = ""
if itmstck then item_name = itmstck:get_name() end
if item_name == "bike:painter" then
--painting with bike painter
local meta = itmstck:get_meta()
local colstr = meta:get_string("paint_color")
auto_beetle.paint(self, colstr)
return true
else
--painting with dyes
local split = string.split(item_name, ":")
local color, indx, _
if split[1] then _,indx = split[1]:find('dye') end
if indx then
for clr,_ in pairs(automobiles_lib.colors) do
local _,x = split[2]:find(clr)
if x then color = clr end
end
--lets paint!!!!
--local color = item_name:sub(indx+1)
local colstr = automobiles_lib.colors[color]
--minetest.chat_send_all(color ..' '.. dump(colstr))
if colstr then
auto_beetle.paint(self, colstr)
itmstck:set_count(itmstck:get_count()-1)
puncher:set_wielded_item(itmstck)
return true
end
-- end painting
end
end
return false
end
auto_beetle.car_properties1 = {
initial_properties = {
physical = true,
collide_with_objects = true,
collisionbox = {-0.1, -0.2, -0.1, 0.1, 1.8, 0.1},
selectionbox = {-2.0, 0.0, -2.0, 2.0, 2, 2.0},
stepheight = 0.65 + automobiles_lib.extra_stepheight,
visual = "mesh",
mesh = "beetle_body.b3d",
--use_texture_alpha = true,
backface_culling = false,
textures = {
"automobiles_black.png", --bancos
"automobiles_black.png", --banco traseiro
"beetle_painting.png", --pintura carroceria
"automobiles_black.png", --chassis
"automobiles_metal.png", "automobiles_black.png", --escapamento
"automobiles_metal.png", --detalhes metal
"automobiles_black.png", "automobiles_metal.png", --estribos
"beetle_glasses.png", --vidros
"beetle_interior.png", --assoalho e forros
"beetle_paint_interior.png", --painel
"automobiles_metal.png", --para-choques
"beetle_painting.png", --para-lamas
"automobiles_metal.png", "automobiles_black.png", --volante
"beetle_painting.png", --portas
"automobiles_black.png", --forro portas
"automobiles_metal.png", --metais portas
"beetle_glasses.png", --vidros portas
},
},
textures = {},
driver_name = nil,
sound_handle = nil,
owner = "",
static_save = true,
infotext = "A very nice Beetle!",
hp = 50,
buoyancy = 2,
physics = automobiles_lib.physics,
lastvelocity = vector.new(),
time_total = 0,
_passenger = nil,
_color = "#FFFFFF",
_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_rag = true,
_show_lights = false,
_light_old_pos = nil,
_last_ground_check = 0,
_last_light_move = 0,
_last_engine_sound_update = 0,
_turn_light_timer = 0,
_inv = nil,
_inv_id = "",
_change_color = automobiles_lib.paint,
_intensity = 4,
_car_gravity = -automobiles_lib.gravity,
_is_flying = 0,
_trunk_slots = 12,
_engine_sound = "beetle_engine",
_max_fuel = 10,
_vehicle_name = "Beetle",
_drive_wheel_pos = {x=-4.0, y=7.00, z=21},
_drive_wheel_angle = 12,
_seat_pos = {{x=-4.0,y=2,z=13.8},{x=4.0,y=2,z=13.8}, {x=-4.0,y=2,z=7},{x=4.0,y=2,z=7}},
_front_suspension_ent = 'automobiles_beetle:front_suspension',
_front_suspension_pos = {x=0,y=1.8,z=27.6},
_front_wheel_ent = 'automobiles_beetle:wheel',
_front_wheel_xpos = 8.5,
_front_wheel_frames = {x = 1, y = 24},
_rear_suspension_ent = 'automobiles_beetle:rear_suspension',
_rear_suspension_pos = {x=0,y=1.8,z=0},
_rear_wheel_ent = 'automobiles_beetle:wheel',
_rear_wheel_xpos = 8.5,
_rear_wheel_frames = {x = 1, y = 24},
_wheel_compensation = 0.9,
_fuel_gauge_pos = {x=-4.47,y=8.50,z=20.5},
_front_lights = 'automobiles_beetle:f_lights',
_rear_lights = 'automobiles_beetle:r_lights',
_reverse_lights = 'automobiles_beetle:reverse_lights',
_turn_left_lights = 'automobiles_beetle:turn_left_light',
_turn_right_lights = 'automobiles_beetle:turn_right_light',
_change_color = auto_beetle.paint,
_painting_function = auto_beetle.set_paint,
_painting_load = auto_beetle.paint,
_transmission_state = 1,
_LONGIT_DRAG_FACTOR = 0.12*0.12,
_LATER_DRAG_FACTOR = 6.0,
_max_acc_factor = 5,
_max_speed = 14,
_min_later_speed = 2,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
automobiles_lib.save_inventory(self)
end,
on_activate = automobiles_lib.on_activate,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,
}
minetest.register_entity("automobiles_beetle:beetle", auto_beetle.car_properties1)

14
automobiles_beetle/init.lua Executable file
View File

@ -0,0 +1,14 @@
--
-- constants
--
auto_beetle={}
auto_beetle.gravity = automobiles_lib.gravity
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.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 .. "control.lua")
dofile(minetest.get_modpath("automobiles_beetle") .. DIR_DELIM .. "entities.lua")
dofile(minetest.get_modpath("automobiles_beetle") .. DIR_DELIM .. "crafts.lua")

5
automobiles_beetle/mod.conf Executable file
View File

@ -0,0 +1,5 @@
name=automobiles_beetle
title=Beetle
description=The old classic Beetle for Minetest
author=apercy
depends=automobiles_lib

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

@ -167,28 +167,6 @@ initial_properties = {
})
minetest.register_entity('automobiles_buggy:steering',{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "automobiles_buggy_drive_wheel.b3d",
textures = {"automobiles_metal.png", "automobiles_black.png", "automobiles_metal.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_buggy:buggy", {
initial_properties = {
physical = true,
@ -215,6 +193,7 @@ minetest.register_entity("automobiles_buggy:buggy", {
"automobiles_metal.png", --front protection
"automobiles_metal.png", --driver protection
"automobiles_black.png", -- engine details
"automobiles_metal.png", "automobiles_black.png", --volante
},
},
textures = {},
@ -259,9 +238,8 @@ minetest.register_entity("automobiles_buggy:buggy", {
_destroy_function = buggy.destroy,
_vehicle_name = "Buggy",
_drive_wheel_pos = {x=-4.26,y=6.01,z=14.18},
_drive_wheel_pos = {x=-4.26,y=6.01,z=16},
_drive_wheel_angle = 15,
_steering_ent = 'automobiles_buggy:steering',
_rag_extended_ent = 'automobiles_buggy:rag',
_seat_pos = {{x=-4.25,y=0.48,z=9.5},{x=4.25,y=0.48,z=9.5}},
@ -280,6 +258,12 @@ minetest.register_entity("automobiles_buggy:buggy", {
_front_lights = 'automobiles_buggy:f_lights',
_rear_lights = 'automobiles_buggy:r_lights',
_LONGIT_DRAG_FACTOR = 0.12*0.12,
_LATER_DRAG_FACTOR = 6.0,
_max_acc_factor = 5,
_max_speed = 15,
_min_later_speed = 2.5,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
@ -288,229 +272,7 @@ minetest.register_entity("automobiles_buggy:buggy", {
on_activate = automobiles_lib.on_activate,
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) *
(buggy.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*buggy.LATER_DRAG_FACTOR*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
if self._show_rag == true then
self.rag:set_properties({is_visible=true})
else
self.rag:set_properties({is_visible=false})
end
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.r_lights:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=15})
end
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_buggy_lights.png"}, glow=15})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights.png"}, glow=10})
end
automobiles_lib.put_light(self)
else
--self.lights:set_properties({is_visible=false})
self.lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
if is_breaking == false then
self.r_lights: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()
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
--control
local steering_angle_max = 30
local steering_speed = 40
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, buggy.max_acc_factor, buggy.max_speed, steering_angle_max, steering_speed)
else
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.lf_wheel:set_animation_frame_speed(longit_speed * (10 - angle_factor))
self.rf_wheel:set_animation_frame_speed(-longit_speed * (10 + angle_factor))
self.lr_wheel:set_animation_frame_speed(longit_speed * (10 - angle_factor))
self.rr_wheel:set_animation_frame_speed(-longit_speed * (10 + angle_factor))
--whell turn
self.steering:set_attach(self.steering_axis,'',{x=0,y=0,z=0},{x=0,y=0,z=self._steering_angle*2})
self.lf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=(-self._steering_angle+angle_factor)+180,z=0})
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
----------------------------
-- end energy consumption --
accel.y = -automobiles_lib.gravity
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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)
if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,

View File

@ -2,11 +2,7 @@
-- constants
--
buggy={}
buggy.LONGIT_DRAG_FACTOR = 0.14*0.14
buggy.LATER_DRAG_FACTOR = 25.0
buggy.gravity = automobiles_lib.gravity
buggy.max_speed = 15
buggy.max_acc_factor = 5
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua")

View File

@ -347,6 +347,12 @@ catrelle.car_properties1 = {
_turn_left_lights = 'automobiles_catrelle:turn_left_light',
_turn_right_lights = 'automobiles_catrelle:turn_right_light',
_LONGIT_DRAG_FACTOR = 0.12*0.12,
_LATER_DRAG_FACTOR = 10.0,
_max_acc_factor = 5,
_max_speed = 14,
_min_later_speed = 3,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
@ -355,268 +361,7 @@ catrelle.car_properties1 = {
on_activate = automobiles_lib.on_activate,
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) *
(catrelle.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*catrelle.LATER_DRAG_FACTOR*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
local curr_pos = self.object:get_pos()
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.r_lights:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=15})
end
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_catrelle_lights.png"}, glow=15})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights.png"}, glow=10})
end
automobiles_lib.put_light(self)
else
--self.lights:set_properties({is_visible=false})
self.lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
end
automobiles_lib.remove_light(self)
end
end
-- impacts and control
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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
--control
local steering_angle_max = 30
local steering_speed = 40
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, catrelle.max_acc_factor, catrelle.max_speed, steering_angle_max, steering_speed)
else
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
--whell turn
self.lf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=(-self._steering_angle+angle_factor)+180,z=0})
self.lr_wheel:set_attach(self.rear_suspension,'',{x=-self._rear_wheel_xpos,y=0,z=0},{x=0,y=0,z=0})
self.rr_wheel:set_attach(self.rear_suspension,'',{x=self._rear_wheel_xpos,y=0,z=0},{x=0,y=180,z=0})
--check if the tyres is touching the pavement
local noded = automobiles_lib.nodeatpos(automobiles_lib.pos_shift(curr_pos,{y=-0.5}))
if (noded and noded.drawtype ~= 'airlike') then
if noded.drawtype ~= 'liquid' then
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("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
self.lf_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rf_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
self.lr_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rr_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
end
end
--drive wheel turn
self.object:set_bone_position("drive_wheel", {x=-0, y=0, z=0}, {x=0, y=0, z=-self._steering_angle*2})
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
--turn light
self._turn_light_timer = self._turn_light_timer + dtime
if math.abs(self._steering_angle) > 15 and self._turn_light_timer >= 1 then
self._turn_light_timer = 0
--set turn light
--minetest.chat_send_all(self._steering_angle)
if self._steering_angle < 0 then
--minetest.chat_send_all("direita")
self.turn_r_light:set_properties({textures={"automobiles_turn_on.png"}, glow=20})
end
if self._steering_angle > 0 then
--minetest.chat_send_all("esquerda")
self.turn_l_light:set_properties({textures={"automobiles_turn_on.png"}, glow=20})
end
end
if self._turn_light_timer > 0.5 then
self.turn_l_light:set_properties({textures={"automobiles_turn.png"}, glow=0})
self.turn_r_light:set_properties({textures={"automobiles_turn.png"}, glow=0})
end
if self._turn_light_timer > 1 then
self._turn_light_timer = 1
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
self._is_flying = 0
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
----------------------------
-- end energy consumption --
--gravity works
if not self._is_flying or self._is_flying == 0 then
accel.y = -automobiles_lib.gravity
else
local time_correction = (self.dtime/catrelle.ideal_step)
local y_accel = self._car_gravity*time_correction
accel.y = y_accel --sets the anti gravity
end
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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, 3.0)
end
local newpitch = self._pitch --velocity.y * math.rad(6)
local newroll = 0
self.object:set_rotation({x=newpitch,y=newyaw,z=newroll})
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,

View File

@ -2,12 +2,7 @@
-- constants
--
catrelle={}
catrelle.LONGIT_DRAG_FACTOR = 0.12*0.12
catrelle.LATER_DRAG_FACTOR = 8.0
catrelle.gravity = automobiles_lib.gravity
catrelle.max_speed = 14
catrelle.max_acc_factor = 5
catrelle.ideal_step = 0.2
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua")

View File

@ -285,6 +285,12 @@ minetest.register_entity("automobiles_coupe:coupe", {
_turn_left_lights = 'automobiles_coupe:turn_left_light',
_turn_right_lights = 'automobiles_coupe:turn_right_light',
_LONGIT_DRAG_FACTOR = 0.12*0.12,
_LATER_DRAG_FACTOR = 18.0,
_max_acc_factor = 8,
_max_speed = 22,
_min_later_speed = 2,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
@ -293,248 +299,7 @@ minetest.register_entity("automobiles_coupe:coupe", {
on_activate = automobiles_lib.on_activate,
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) *
(coupe.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*coupe.LATER_DRAG_FACTOR*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
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.r_lights:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=15})
end
if ctrl.sneak then
self.reverse_lights:set_properties({textures={"automobiles_white.png"}, glow=15})
else
self.reverse_lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
end
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_coupe_lights.png"}, glow=15})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights.png"}, glow=10})
end
automobiles_lib.put_light(self)
else
--self.lights:set_properties({is_visible=false})
self.lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
if is_breaking == false then
self.r_lights: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()
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
--control
local steering_angle_max = 30
local steering_speed = 40
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, coupe.max_acc_factor, coupe.max_speed, steering_angle_max, steering_speed)
else
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.lf_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rf_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
self.lr_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rr_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
--whell turn
self.steering:set_attach(self.steering_axis,'',{x=0,y=0,z=0},{x=0,y=0,z=self._steering_angle*2})
self.lf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=(-self._steering_angle+angle_factor)+180,z=0})
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
--turn light
self._turn_light_timer = self._turn_light_timer + dtime
if math.abs(self._steering_angle) > 15 and self._turn_light_timer >= 1 then
self._turn_light_timer = 0
--set turn light
if self._steering_angle < 0 then
self.turn_r_light:set_properties({textures={"automobiles_turn_on.png"}, glow=20})
end
if self._steering_angle > 0 then
self.turn_l_light:set_properties({textures={"automobiles_turn_on.png"}, glow=20})
end
end
if self._turn_light_timer > 0.5 then
self.turn_l_light:set_properties({textures={"automobiles_turn.png"}, glow=0})
self.turn_r_light:set_properties({textures={"automobiles_turn.png"}, glow=0})
end
if self._turn_light_timer > 1 then
self._turn_light_timer = 1
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
----------------------------
-- end energy consumption --
accel.y = -automobiles_lib.gravity
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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)
if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,

View File

@ -2,11 +2,7 @@
-- constants
--
coupe={}
coupe.LONGIT_DRAG_FACTOR = 0.12*0.12
coupe.LATER_DRAG_FACTOR = 18.0
coupe.gravity = automobiles_lib.gravity
coupe.max_speed = 22
coupe.max_acc_factor = 8
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua")

View File

@ -353,3 +353,304 @@ function automobiles_lib.on_activate(self, staticdata, dtime_s)
automobiles_lib.actfunc(self, staticdata, dtime_s)
end
function automobiles_lib.on_step(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 > 2.0 then self._last_time_drift_snd = 2.0 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) *
(self._LONGIT_DRAG_FACTOR - fuel_weight_factor) * -1 * automobiles_lib.sign(longit_speed))
local later_speed = automobiles_lib.dot(velocity,nhdir)
local dynamic_later_drag = self._LATER_DRAG_FACTOR
if longit_speed > 2 then dynamic_later_drag = 2.0 end
if longit_speed > 8 then dynamic_later_drag = 0.5 end
local later_drag = vector.multiply(nhdir,later_speed*
later_speed*dynamic_later_drag*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
local curr_pos = self.object:get_pos()
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
if self.r_lights then self.r_lights:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=15}) end
end
if self.reverse_lights then
if ctrl.sneak then
self.reverse_lights:set_properties({textures={"automobiles_white.png"}, glow=15})
else
self.reverse_lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
end
end
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.lights then
if self._show_lights == true then
--self.lights:set_properties({is_visible=true})
self.lights:set_properties({textures={"automobiles_front_lights.png"}, glow=15})
if is_breaking == false then
if self.r_lights then self.r_lights:set_properties({textures={"automobiles_rear_lights.png"}, glow=10}) end
end
automobiles_lib.put_light(self)
else
--self.lights:set_properties({is_visible=false})
self.lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
if is_breaking == false then
if self.r_lights then self.r_lights:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0}) end
end
automobiles_lib.remove_light(self)
end
end
end
-- impacts and control
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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
--control
local steering_angle_max = 40
local steering_speed = 40
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
--adjust engine parameter (transmission emulation)
local acc_factor = self._max_acc_factor
local transmission_state = automobiles_lib.get_transmission_state(longit_speed, self._max_speed)
local target_acc_factor = acc_factor
if transmission_state == 1 then
target_acc_factor = (self._max_acc_factor/3)
end
if transmission_state == 2 then
target_acc_factor = (self._max_acc_factor/2)
end
self._transmission_state = transmission_state
--minetest.chat_send_all(transmission_state)
--control
accel, stop = automobiles_lib.control(self, dtime, hull_direction, longit_speed, longit_drag, later_drag, accel, target_acc_factor, self._max_speed, steering_angle_max, steering_speed)
else
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
--whell turn
if self.lf_wheel and self.rf_wheel and self.lr_wheel and self.rr_wheel then
self.lf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=(-self._steering_angle+angle_factor)+180,z=0})
self.lr_wheel:set_attach(self.rear_suspension,'',{x=-self._rear_wheel_xpos,y=0,z=0},{x=0,y=0,z=0})
self.rr_wheel:set_attach(self.rear_suspension,'',{x=self._rear_wheel_xpos,y=0,z=0},{x=0,y=180,z=0})
end
--check if the tyres is touching the pavement
local noded = automobiles_lib.nodeatpos(automobiles_lib.pos_shift(curr_pos,{y=self.initial_properties.collisionbox[2]-0.5}))
if (noded and noded.drawtype ~= 'airlike') then
if noded.drawtype ~= 'liquid' then
local min_later_speed = self._min_later_speed or 3
if (later_speed > min_later_speed or later_speed < -min_later_speed) and
self._last_time_drift_snd >= 2.0 then
self._last_time_drift_snd = 0
minetest.sound_play("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
local wheel_compensation = longit_speed * (self._wheel_compensation or 1)
if self.lf_wheel then self.lf_wheel:set_animation_frame_speed( wheel_compensation * (12 - angle_factor)) end
if self.rf_wheel then self.rf_wheel:set_animation_frame_speed(-wheel_compensation * (12 + angle_factor)) end
if self.lr_wheel then self.lr_wheel:set_animation_frame_speed( wheel_compensation * (12 - angle_factor)) end
if self.rr_wheel then self.rr_wheel:set_animation_frame_speed(-wheel_compensation * (12 + angle_factor)) end
end
end
--drive wheel turn
if self._steering_ent then
self.steering:set_attach(self.steering_axis,'',{x=0,y=0,z=0},{x=0,y=0,z=self._steering_angle*2})
else
self.object:set_bone_position("drive_wheel", {x=-0, y=0, z=0}, {x=0, y=0, z=-self._steering_angle*2})
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
--turn light
if self.turn_l_light and self.turn_r_light then
self._turn_light_timer = self._turn_light_timer + dtime
if math.abs(self._steering_angle) > 15 and self._turn_light_timer >= 1 then
self._turn_light_timer = 0
--set turn light
--minetest.chat_send_all(self._steering_angle)
if self._steering_angle < 0 then
--minetest.chat_send_all("direita")
self.turn_r_light:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=20})
end
if self._steering_angle > 0 then
--minetest.chat_send_all("esquerda")
self.turn_l_light:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=20})
end
end
if self._turn_light_timer > 0.5 then
self.turn_l_light:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
self.turn_r_light:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
end
if self._turn_light_timer > 1 then
self._turn_light_timer = 1
end
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
self._is_flying = 0
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
if self.fuel_gauge then
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
end
----------------------------
-- end energy consumption --
--gravity works
if not self._is_flying or self._is_flying == 0 then
accel.y = -automobiles_lib.gravity
else
local time_correction = (self.dtime/automobiles_lib.ideal_step)
local y_accel = self._car_gravity*time_correction
accel.y = y_accel --sets the anti gravity
end
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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.7)
end
local newpitch = self._pitch --velocity.y * math.rad(6)
local newroll = 0
self.object:set_rotation({x=newpitch,y=newyaw,z=newroll})
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end

View File

@ -12,6 +12,7 @@ automobiles_lib.fuel = {['biofuel:biofuel'] = 1,['biofuel:bottle_fuel'] = 1,
['airutils:biofuel'] = 1,}
automobiles_lib.gravity = 9.8
automobiles_lib.ideal_step = 0.2
automobiles_lib.is_creative = minetest.settings:get_bool("creative_mode", false)
@ -601,6 +602,7 @@ end
function automobiles_lib.get_transmission_state(curr_speed, max_speed)
local retVal = 1
if curr_speed >= (max_speed/4) then retVal = 2 end
if curr_speed >= (max_speed/2) then retVal = 3 end
return retVal
end
@ -741,6 +743,7 @@ local old_entities = {
"automobiles_roadster:pivot_mesh",
"automobiles_trans_am:pivot_mesh",
"automobiles_trans_am:pointer",
"automobiles_buggy:steering",
}
for _,entity_name in ipairs(old_entities) do
minetest.register_entity(":"..entity_name, {

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

View File

@ -273,17 +273,24 @@ minetest.register_entity("automobiles_roadster:roadster", {
_front_suspension_pos = {x=0,y=0,z=24.22},
_front_wheel_ent = 'automobiles_roadster:wheel',
_front_wheel_xpos = 10.26,
_front_wheel_frames = {x = 2, y = 13},
_front_wheel_frames = {x = 1, y = 24},
_rear_suspension_ent = 'automobiles_roadster:rear_suspension',
_rear_suspension_pos = {x=0,y=0,z=0},
_rear_wheel_ent = 'automobiles_roadster:wheel',
_rear_wheel_xpos = 10.26,
_rear_wheel_frames = {x = 2, y = 13},
_rear_wheel_frames = {x = 1, y = 24},
_wheel_compensation = 0.5,
_fuel_gauge_pos = {x=0,y=8.04,z=17.84},
_gauge_pointer_ent = 'automobiles_roadster:pointer',
_front_lights = 'automobiles_roadster:lights',
_LONGIT_DRAG_FACTOR = 0.16*0.16,
_LATER_DRAG_FACTOR = 20.0,
_max_acc_factor = 5,
_max_speed = 12,
_min_later_speed = 2,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
@ -292,220 +299,7 @@ minetest.register_entity("automobiles_roadster:roadster", {
on_activate = automobiles_lib.on_activate,
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) *
(roadster.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*roadster.LATER_DRAG_FACTOR*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
if self._show_rag == true then
self.object:set_bone_position("windshield", {x=0, z=15.8317, y=15.0394}, {x=145, y=0, z=0})
self.rag_rect:set_properties({is_visible=true})
self.rag:set_properties({is_visible=false})
else
self.object:set_bone_position("windshield", {x=0, z=15.8317, y=15.0394}, {x=0, y=0, z=0})
self.rag_rect:set_properties({is_visible=false})
self.rag:set_properties({is_visible=true})
end
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 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 = "roadster_horn"},
{object = self.object, gain = 0.6, pitch = 1.0, max_hear_distance = 32, loop = false,})
end
end
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
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 and is_attached then
self.lights:set_properties({is_visible=true})
automobiles_lib.put_light(self)
else
self.lights:set_properties({is_visible=false})
automobiles_lib.remove_light(self)
end
end
local curr_pos = self.object:get_pos()
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
--control
local steering_angle_max = 30
local steering_speed = 40
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, roadster.max_acc_factor, roadster.max_speed, steering_angle_max, steering_speed)
else
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.lf_wheel:set_animation_frame_speed(longit_speed * (10 + angle_factor))
self.rf_wheel:set_animation_frame_speed(longit_speed * (10 - angle_factor))
self.lr_wheel:set_animation_frame_speed(longit_speed * (10 - angle_factor))
self.rr_wheel:set_animation_frame_speed(longit_speed * (10 + angle_factor))
--whell turn
self.steering:set_attach(self.steering_axis,'',{x=0,y=0,z=0},{x=0,y=0,z=self._steering_angle*2})
self.lf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle+angle_factor,z=0})
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
----------------------------
-- end energy consumption --
accel.y = -automobiles_lib.gravity
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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.5, 2.422)
end
local newpitch = self._pitch --velocity.y * math.rad(6)
if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,

View File

@ -356,6 +356,12 @@ minetest.register_entity("automobiles_trans_am:trans_am", {
_turn_left_lights = 'automobiles_trans_am:turn_left_light',
_turn_right_lights = 'automobiles_trans_am:turn_right_light',
_LONGIT_DRAG_FACTOR = 0.12*0.12,
_LATER_DRAG_FACTOR = 6.0,
_max_acc_factor = 12,
_max_speed = 40,
_min_later_speed = 4.5,
get_staticdata = automobiles_lib.get_staticdata,
on_deactivate = function(self)
@ -364,299 +370,7 @@ minetest.register_entity("automobiles_trans_am:trans_am", {
on_activate = automobiles_lib.on_activate,
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 > 2.0 then self._last_time_drift_snd = 2.0 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) *
(trans_am.LONGIT_DRAG_FACTOR - fuel_weight_factor) * -1 * automobiles_lib.sign(longit_speed))
local later_speed = automobiles_lib.dot(velocity,nhdir)
local dynamic_later_drag = trans_am.LATER_DRAG_FACTOR
if longit_speed > 2 then dynamic_later_drag = 2.0 end
if longit_speed > 8 then dynamic_later_drag = 0.5 end
local later_drag = vector.multiply(nhdir,later_speed*
later_speed*dynamic_later_drag*-1*automobiles_lib.sign(later_speed))
local accel = vector.add(longit_drag,later_drag)
local stop = nil
local curr_pos = self.object:get_pos()
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.r_lights:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=15})
end
if ctrl.sneak then
self.reverse_lights:set_properties({textures={"automobiles_white.png"}, glow=15})
else
self.reverse_lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
end
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.lights then
if self._show_lights == true then
--self.lights:set_properties({is_visible=true})
self.lights:set_properties({textures={"automobiles_trans_am_lights.png"}, glow=15})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights.png"}, glow=10})
end
automobiles_lib.put_light(self)
else
--self.lights:set_properties({is_visible=false})
self.lights:set_properties({textures={"automobiles_grey.png"}, glow=0})
if is_breaking == false then
self.r_lights:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
end
automobiles_lib.remove_light(self)
end
end
end
-- impacts and control
self.object:move_to(curr_pos)
if is_attached then --and self.driver_name == self.owner then
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
--control
local steering_angle_max = 40
local steering_speed = 40
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
--adjust engine parameter (transmission emulation)
local acc_factor = trans_am.max_acc_factor
local transmission_state = automobiles_lib.get_transmission_state(longit_speed, trans_am.max_speed)
local target_acc_factor = acc_factor
if transmission_state == 1 then
target_acc_factor = trans_am.max_acc_factor/2
end
self._transmission_state = transmission_state
--minetest.chat_send_all(transmission_state)
--control
accel, stop = automobiles_lib.control(self, dtime, hull_direction, longit_speed, longit_drag, later_drag, accel, target_acc_factor, trans_am.max_speed, steering_angle_max, steering_speed)
else
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
--whell turn
if self.lf_wheel and self.rf_wheel and self.lr_wheel and self.rr_wheel then
self.lf_wheel:set_attach(self.front_suspension,'',{x=-self._front_wheel_xpos,y=0,z=0},{x=0,y=-self._steering_angle-angle_factor,z=0})
self.rf_wheel:set_attach(self.front_suspension,'',{x=self._front_wheel_xpos,y=0,z=0},{x=0,y=(-self._steering_angle+angle_factor)+180,z=0})
self.lr_wheel:set_attach(self.rear_suspension,'',{x=-self._rear_wheel_xpos,y=0,z=0},{x=0,y=0,z=0})
self.rr_wheel:set_attach(self.rear_suspension,'',{x=self._rear_wheel_xpos,y=0,z=0},{x=0,y=180,z=0})
end
--check if the tyres is touching the pavement
local noded = automobiles_lib.nodeatpos(automobiles_lib.pos_shift(curr_pos,{y=-0.5}))
if (noded and noded.drawtype ~= 'airlike') then
if noded.drawtype ~= 'liquid' then
local min_later_speed = 4.5
if (later_speed > min_later_speed or later_speed < -min_later_speed) and
self._last_time_drift_snd >= 2.0 then
self._last_time_drift_snd = 0
minetest.sound_play("automobiles_drifting", {
pos = curr_pos,
max_hear_distance = 20,
gain = 3.0,
fade = 0.0,
pitch = 1.0,
ephemeral = true,
})
end
if self.lf_wheel and self.rf_wheel and self.lr_wheel and self.rr_wheel then
self.lf_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rf_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
self.lr_wheel:set_animation_frame_speed(longit_speed * (12 - angle_factor))
self.rr_wheel:set_animation_frame_speed(-longit_speed * (12 + angle_factor))
end
end
end
--drive wheel turn
self.object:set_bone_position("drive_wheel", {x=-0, y=0, z=0}, {x=0, y=0, z=-self._steering_angle*2})
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
--turn light
if self.turn_l_light and self.turn_r_light then
self._turn_light_timer = self._turn_light_timer + dtime
if math.abs(self._steering_angle) > 15 and self._turn_light_timer >= 1 then
self._turn_light_timer = 0
--set turn light
--minetest.chat_send_all(self._steering_angle)
if self._steering_angle < 0 then
--minetest.chat_send_all("direita")
self.turn_r_light:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=20})
end
if self._steering_angle > 0 then
--minetest.chat_send_all("esquerda")
self.turn_l_light:set_properties({textures={"automobiles_rear_lights_full.png"}, glow=20})
end
end
if self._turn_light_timer > 0.5 then
self.turn_l_light:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
self.turn_r_light:set_properties({textures={"automobiles_rear_lights_off.png"}, glow=0})
end
if self._turn_light_timer > 1 then
self._turn_light_timer = 1
end
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
self._is_flying = 0
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
automobiles_lib.engine_set_sound_and_animation(self, longit_speed)
end
end
local energy_indicator_angle = automobiles_lib.get_gauge_angle(self._energy)
if self.fuel_gauge then
self.fuel_gauge:set_attach(self.object,'',self._fuel_gauge_pos,{x=0,y=0,z=energy_indicator_angle})
end
----------------------------
-- end energy consumption --
--gravity works
if not self._is_flying or self._is_flying == 0 then
accel.y = -automobiles_lib.gravity
else
local time_correction = (self.dtime/trans_am.ideal_step)
local y_accel = self._car_gravity*time_correction
accel.y = y_accel --sets the anti gravity
end
if stop ~= true then
--self.object:set_velocity(velocity)
self.object:add_velocity(vector.multiply(accel,dtime))
--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.7)
end
local newpitch = self._pitch --velocity.y * math.rad(6)
local newroll = 0
self.object:set_rotation({x=newpitch,y=newyaw,z=newroll})
--saves last velocity for collision detection (abrupt stop)
self.lastvelocity = self.object:get_velocity()
self._longit_speed = longit_speed
end,
on_step = automobiles_lib.on_step,
on_punch = automobiles_lib.on_punch,
on_rightclick = automobiles_lib.on_rightclick,

View File

@ -2,12 +2,7 @@
-- constants
--
trans_am={}
trans_am.LONGIT_DRAG_FACTOR = 0.12*0.12
trans_am.LATER_DRAG_FACTOR = 6.0
trans_am.gravity = automobiles_lib.gravity
trans_am.max_speed = 40
trans_am.max_acc_factor = 12
trans_am.ideal_step = 0.2
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua")
dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua")