forked from mtcontrib/maidroid
drop self.thread in on_stop and add getvelocity, getacceleration and jump instructions
M maidroid_core/cores/ocr.lua
This commit is contained in:
parent
13013b7aa2
commit
9628c8efe2
@ -4,20 +4,29 @@
|
|||||||
------------------------------------------------------------
|
------------------------------------------------------------
|
||||||
|
|
||||||
local maidroid_instruction_set = {
|
local maidroid_instruction_set = {
|
||||||
|
-- popular (similars in lua_api) information gathering functions
|
||||||
getpos = function(_, thread)
|
getpos = function(_, thread)
|
||||||
local pos = thread.droid.object:getpos()
|
local pos = thread.droid.object:getpos()
|
||||||
return true, {pos.x, pos.y, pos.z}
|
return true, {pos.x, pos.y, pos.z}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
beep = function(_, thread)
|
getvelocity = function()
|
||||||
minetest.sound_play("maidroid_beep", {pos = thread.droid.object:getpos()})
|
local vel = self.vel
|
||||||
return true
|
return true, {vel.x, vel.y, vel.z}
|
||||||
|
end,
|
||||||
|
|
||||||
|
getacceleration = function(_, thread)
|
||||||
|
local acc = thread.droid.object:getacceleration()
|
||||||
|
return true, {acc.x, acc.y, acc.z}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
getyaw = function(_, thread)
|
getyaw = function(_, thread)
|
||||||
return true, thread.droid.object:getyaw()
|
return true, thread.droid.object:getyaw()
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
-- other info functions
|
||||||
|
|
||||||
|
-- popular actions for changing sth
|
||||||
setyaw = function(params, thread)
|
setyaw = function(params, thread)
|
||||||
if #params ~= 1 then
|
if #params ~= 1 then
|
||||||
return false, "wrong number of arguments"
|
return false, "wrong number of arguments"
|
||||||
@ -29,6 +38,48 @@ local maidroid_instruction_set = {
|
|||||||
thread.droid.object:setyaw(p)
|
thread.droid.object:setyaw(p)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
-- other actions
|
||||||
|
jump = function(params, thread)
|
||||||
|
-- test if it can jump
|
||||||
|
local droid = thread.droid
|
||||||
|
if droid.vel.y ~= 0
|
||||||
|
or droid.vel_prev.y ~= 0 then
|
||||||
|
return true, false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- get the strength of the jump
|
||||||
|
local h = tonumber(params[1])
|
||||||
|
if not h
|
||||||
|
or h <= 0
|
||||||
|
or h > 2 then
|
||||||
|
h = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
-- play sound
|
||||||
|
local p = droid.object:getpos()
|
||||||
|
p.y = p.y - 1
|
||||||
|
local node_under = minetest.get_node(p).name
|
||||||
|
local def = minetest.registered_nodes[node_under]
|
||||||
|
if def
|
||||||
|
and def.sounds then
|
||||||
|
local snd = def.sounds.footstep or def.sounds.dig
|
||||||
|
if snd then
|
||||||
|
p.y = p.y + .5
|
||||||
|
minetest.sound_play(snd.name, {pos = p, gain = snd.gain})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- perform jump
|
||||||
|
droid.vel.y = math.sqrt(-2 * h * droid.object:getacceleration().y)
|
||||||
|
droid.object:setvelocity(droid.vel)
|
||||||
|
return true, true
|
||||||
|
end,
|
||||||
|
|
||||||
|
beep = function(_, thread)
|
||||||
|
minetest.sound_play("maidroid_beep", {pos = thread.droid.object:getpos()})
|
||||||
|
return true
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -61,6 +112,8 @@ end
|
|||||||
local function on_start(self)
|
local function on_start(self)
|
||||||
self.object:setacceleration{x = 0, y = -10, z = 0}
|
self.object:setacceleration{x = 0, y = -10, z = 0}
|
||||||
self.object:setvelocity{x = 0, y = 0, z = 0}
|
self.object:setvelocity{x = 0, y = 0, z = 0}
|
||||||
|
self.vel = {x = 0, y = 0, z = 0}
|
||||||
|
self.vel_prev = self.vel
|
||||||
|
|
||||||
local parsed_code = pdisc.parse(get_code(self) or dummycode)
|
local parsed_code = pdisc.parse(get_code(self) or dummycode)
|
||||||
self.thread = pdisc.create_thread(function(thread)
|
self.thread = pdisc.create_thread(function(thread)
|
||||||
@ -77,9 +130,13 @@ end
|
|||||||
|
|
||||||
local function on_step(self)
|
local function on_step(self)
|
||||||
local thread = self.thread
|
local thread = self.thread
|
||||||
if thread.stopped then
|
if not thread.stopped then
|
||||||
thread:try_rebirth()
|
return
|
||||||
end
|
end
|
||||||
|
self.vel_prev = self.vel
|
||||||
|
self.vel = self.object:getvelocity()
|
||||||
|
|
||||||
|
thread:try_rebirth()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_resume(self)
|
local function on_resume(self)
|
||||||
@ -92,6 +149,7 @@ end
|
|||||||
|
|
||||||
local function on_stop(self)
|
local function on_stop(self)
|
||||||
self.thread:exit()
|
self.thread:exit()
|
||||||
|
self.thread = nil
|
||||||
|
|
||||||
self.object:setvelocity{x = 0, y = 0, z = 0}
|
self.object:setvelocity{x = 0, y = 0, z = 0}
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user