Merge pull request #39 from zaoqi-clone/zaoqi-patch-2

Refactor api.lua and fix some minor bugs
This commit is contained in:
D00Med 2019-12-16 08:10:46 +10:00 committed by GitHub
commit b8783635ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

180
api.lua
View File

@ -153,17 +153,65 @@ function vehicles.object_drive(entity, dtime, def)
local shoot_anim2 = def.shoot_anim2 or nil
--variables
local ctrl = entity.driver:get_player_control()
local velo = entity.object:getvelocity()
local dir = entity.driver:get_look_dir();
local vec_backward = {x=-dir.x*speed/4,y=velo.y+1*-2,z=-dir.z*speed/4}
local vec_stop = {x=velo.x*decell,y=velo.y+1*-2,z=velo.z*decell}
local yaw = entity.driver:get_look_yaw();
local pos = entity.object:getpos()
local node = minetest.get_node(pos).name
local node_under = minetest.get_node({x=pos.x, y=pos.y+2, z=pos.z})
local accell = 1
--lava explode
if node == "default:lava_source" or node == "default:lava_flowing" then
if entity.driver then
vehicles.object_detach(entity, entity.driver, {x=1, y=0, z=1})
end
vehicles.explodinate(entity, 5)
entity.object:remove()
return
end
--respond to controls
--check for water
local function is_water(node)
return node == "default:river_water_source" or node == "default:water_source" or node == "default:river_water_flowing" or node == "default:water_flowing"
end
entity.on_water = is_water(node)
entity.in_water = is_water(minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name) or is_water(node_under.name)
local function is_watercraft_and_in_water()
entity.object:setvelocity({x=velo.x*0.9, y=velo.y+1, z=velo.z*0.9})
end
local function is_watercraft_and_not_on_water()
entity.object:setvelocity({x=velo.x*decell,y=velo.y-1,z=velo.z*decell})
end
local function not_watercraft_and_on_or_in_water()
entity.object:setvelocity({x=velo.x*0.9, y=-1, z=velo.z*0.9})
end
if not entity.driver then
--apply water effects
if is_watercraft and entity.in_water then
is_watercraft_and_in_water()
elseif is_watercraft and entity.on_water == false then
is_watercraft_and_not_on_water()
elseif (entity.on_water or entity.in_water) and not is_watercraft then
not_watercraft_and_on_or_in_water()
else
--stop
entity.object:setvelocity(vec_stop)
--animation
if moving_anim ~= nil and entity.moving and not hovering then
entity.object:set_animation(stand_anim, 20, 0)
entity.moving = false
end
end
else
--variables
local ctrl = entity.driver:get_player_control()
local dir = entity.driver:get_look_dir()
local vec_backward = {x=-dir.x*speed/4,y=velo.y+1*-2,z=-dir.z*speed/4}
local yaw = entity.driver:get_look_yaw()
--dummy variables
local vec_rise = {}
local vec_forward_simple = {}
@ -178,8 +226,8 @@ function vehicles.object_drive(entity, dtime, def)
vec_forward_simple = {x=dir.x*speed,y=velo.y+1*-2,z=dir.z*speed}
end
if shoots then
local pname = entity.driver:get_player_name();
inv = minetest.get_inventory({type="player", name=pname});
local pname = entity.driver:get_player_name()
inv = minetest.get_inventory({type="player", name=pname})
end
--timer
@ -292,30 +340,13 @@ function vehicles.object_drive(entity, dtime, def)
dir.z = math.cos(entity_yaw)
end
--lava explode
if node == "default:lava_source" or node == "default:lava_flowing" then
if entity.driver then
vehicles.object_detach(entity, entity.driver, {x=1, y=0, z=1})
end
vehicles.explodinate(entity, 5)
entity.object:remove()
return
end
--respond to controls
--check for water
local function is_water(node)
return node == "default:river_water_source" or node == "default:water_source" or node == "default:river_water_flowing" or node == "default:water_flowing"
end
entity.on_water = is_water(node)
entity.in_water = is_water(minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name) or is_water(node_under.name)
--apply water effects
if is_watercraft and entity.in_water then
entity.object:setvelocity({x=velo.x*0.9, y=velo.y+1, z=velo.z*0.9})
is_watercraft_and_in_water()
elseif is_watercraft and entity.on_water == false then
entity.object:setvelocity({x=velo.x*decell,y=velo.y-1,z=velo.z*decell})
is_watercraft_and_not_on_water()
elseif (entity.on_water or entity.in_water) and not is_watercraft then
entity.object:setvelocity({x=velo.x*0.9, y=-1, z=velo.z*0.9})
not_watercraft_and_on_or_in_water()
--brakes
elseif ctrl.jump and brakes and not ctrl.up then
@ -604,32 +635,14 @@ function vehicles.object_drive(entity, dtime, def)
end
--simplified in an attempt to reduce lag
function vehicles.object_drive_simple(entity, dtime, speed, decell)
local ctrl = entity.driver:get_player_control()
local velo = entity.object:getvelocity()
local dir = entity.driver:get_look_dir();
local vec_forward = {x=dir.x*speed,y=velo.y+1*-2,z=dir.z*speed}
local vec_backward = {x=-dir.x*speed,y=velo.y+1*-2,z=-dir.z*speed}
local vec_stop = {x=velo.x*decell,y=velo.y+1*-2,z=velo.z*decell}
local yaw = entity.driver:get_look_yaw();
entity.object:setyaw(yaw+math.pi+math.pi/2)
if ctrl.up then
entity.object:setvelocity(vec_forward)
elseif ctrl.down then
entity.object:setvelocity(vec_backward)
elseif not ctrl.down or ctrl.up then
entity.object:setvelocity(vec_stop)
end
end
function vehicles.object_glide(entity, dtime, speed, decell, gravity, moving_anim, stand_anim)
local ctrl = entity.driver:get_player_control()
local dir = entity.driver:get_look_dir();
local velo = entity.object:getvelocity();
local dir = entity.driver:get_look_dir()
local velo = entity.object:getvelocity()
local vec_glide = {x=dir.x*speed*decell, y=velo.y, z=dir.z*speed*decell}
local yaw = entity.driver:get_look_yaw();
local yaw = entity.driver:get_look_yaw()
if not ctrl.sneak then
entity.object:setyaw(yaw+math.pi+math.pi/2)
entity.object:setvelocity(vec_glide)
@ -637,7 +650,7 @@ function vehicles.object_glide(entity, dtime, speed, decell, gravity, moving_ani
end
if ctrl.sneak then
local vec = {x=0,y=gravity*15,z=0}
local yaw = entity.driver:get_look_yaw();
local yaw = entity.driver:get_look_yaw()
entity.object:setyaw(yaw+math.pi+math.pi/2)
entity.object:setvelocity(vec)
end
@ -667,13 +680,14 @@ minetest.register_craftitem(vehicle.."_spawner", {
liquids_pointable = is_boat,
wield_scale = {x = 1.5, y = 1.5, z = 1},
on_place = function(item, placer, pointed_thing)
local dir = placer:get_look_dir();
local playerpos = placer:getpos();
local dir = placer:get_look_dir()
local playerpos = placer:getpos()
local creative_mode = creative and creative.is_enabled_for and creative.is_enabled_for(placer:get_player_name())
if pointed_thing.type == "node" and not is_boat then
local obj = minetest.env:add_entity(pointed_thing.above, vehicle)
local object = obj:get_luaentity()
object.owner = placer
if not minetest.setting_getbool("creative_mode") then
if not creative_mode then
item:take_item()
return item
end
@ -682,7 +696,7 @@ minetest.register_craftitem(vehicle.."_spawner", {
obj:setvelocity({x=0, y=-1, z=0})
local object = obj:get_luaentity()
object.owner = placer
if not minetest.setting_getbool("creative_mode") then
if not creative_mode then
item:take_item()
return item
end
@ -749,65 +763,15 @@ function vehicles.on_punch(self, puncher)
end
end
function vehicles.object_no_drive(entity, dtime, def)
--definition
local decell = def.decell or 0
local gravity = def.gravity or 1
local is_watercraft = def.is_watercraft or false
local stand_anim = def.stand_anim
--variables
local velo = entity.object:getvelocity()
local pos = entity.object:getpos()
local node = minetest.get_node(pos).name
--timer dependant variables
local vec_stop = {x=velo.x*decell,y=velo.y-gravity,z=velo.z*decell}
if node == "default:lava_source" or node == "default:lava_flowing" then
if entity.driver then
vehicles.object_detach(entity, entity.driver, {x=1, y=0, z=1})
end
vehicles.explodinate(entity, 5)
entity.object:remove()
return
end
--respond to controls
--check for water
local function is_water(node)
return node == "default:river_water_source" or node == "default:water_source" or node == "default:river_water_flowing" or node == "default:water_flowing"
end
entity.on_water = is_water(node) or is_water({x=pos.x, y=pos.y-0.6, z=pos.z})
entity.in_water = is_water(minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name) or is_water(minetest.get_node({x=pos.x, y=pos.y+2, z=pos.z}).name)
--apply water effects
if is_watercraft and entity.in_water then
entity.object:setvelocity({x=velo.x*0.9, y=velo.y+1, z=velo.z*0.9})
elseif is_watercraft and entity.on_water == false then
entity.object:setvelocity({x=velo.x*decell,y=velo.y-1,z=velo.z*decell})
elseif entity.on_water and not is_watercraft then
entity.object:setvelocity({x=velo.x*0.9, y=-1, z=velo.z*0.9})
else
--stop
entity.object:setvelocity(vec_stop)
--animation
if moving_anim ~= nil and entity.moving and not hovering then
entity.object:set_animation(stand_anim, 20, 0)
entity.moving = false
end
end
end
function vehicles.on_step(self, dtime, def, have, no)
if self.driver then
function vehicles.on_step(self, dtime, def, have_driver, no_driver)
vehicles.object_drive(self, dtime, def)
if have ~= nil then
have()
if self.driver then
if have_driver ~= nil then
have_driver()
end
else
vehicles.object_no_drive(self, dtime, def)
if no ~= nil then
no()
if no_driver ~= nil then
no_driver()
end
end
return false