Showing with 1,227 additions and 96 deletions.
  1. +2 −0 builtin/init.lua
  2. +13 −0 builtin/local_player_physics/init.lua
  3. +7 −0 doc/lua_api.txt
  4. +62 −0 doc/player_physics_api.txt
  5. +77 −0 games/minimal/mods/physics_script_test/init.lua
  6. +219 −0 games/minimal/mods/physics_script_test/local_player_physics.lua
  7. +8 −2 src/camera.cpp
  8. +7 −0 src/client.cpp
  9. +3 −0 src/client.h
  10. +3 −32 src/environment.cpp
  11. +85 −3 src/localplayer.cpp
  12. +13 −2 src/localplayer.h
  13. +2 −2 src/network/clientopcodes.cpp
  14. +26 −0 src/network/clientpackethandler.cpp
  15. +15 −0 src/network/networkprotocol.h
  16. +1 −1 src/network/serveropcodes.cpp
  17. +9 −0 src/network/serverpackethandler.cpp
  18. +20 −20 src/player.h
  19. +1 −0 src/script/CMakeLists.txt
  20. +43 −0 src/script/common/c_content.cpp
  21. +7 −0 src/script/common/c_content.h
  22. +1 −0 src/script/cpp_api/CMakeLists.txt
  23. +5 −0 src/script/cpp_api/s_base.h
  24. +46 −0 src/script/cpp_api/s_player_physics.cpp
  25. +32 −0 src/script/cpp_api/s_player_physics.h
  26. +40 −14 src/script/cpp_api/s_security.cpp
  27. +3 −0 src/script/cpp_api/s_security.h
  28. +1 −0 src/script/lua_api/CMakeLists.txt
  29. +5 −0 src/script/lua_api/l_base.cpp
  30. +2 −0 src/script/lua_api/l_base.h
  31. +43 −19 src/script/lua_api/l_object.cpp
  32. +6 −0 src/script/lua_api/l_object.h
  33. +42 −0 src/script/lua_api/l_player_physics.cpp
  34. +35 −0 src/script/lua_api/l_player_physics.h
  35. +3 −1 src/script/scripting_game.h
  36. +255 −0 src/script/scripting_player_physics.cpp
  37. +50 −0 src/script/scripting_player_physics.h
  38. +32 −0 src/server.cpp
  39. +3 −0 src/server.h
2 changes: 2 additions & 0 deletions builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ elseif INIT == "mainmenu" then
end
elseif INIT == "async" then
dofile(asyncpath .. "init.lua")
elseif INIT == "local_player_physics" then
dofile(scriptdir .. "local_player_physics" .. DIR_DELIM .. "init.lua")
else
error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT)))
end
Expand Down
13 changes: 13 additions & 0 deletions builtin/local_player_physics/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- minetest/builtin/local_player_physics/init.lua

local scriptpath = core.get_builtin_path()..DIR_DELIM
local commonpath = scriptpath.."common"..DIR_DELIM
dofile(commonpath.."vector.lua")

function core.set_local_player_physics(def)
core.log("core.set_local_player_physics")
core.registered_local_player_physics_apply_control = def and def.apply_control
core.registered_local_player_physics_move = def and def.move
core.registered_local_player_physics_camera_up_vector = def and def.camera_up_vector
core.registered_local_player_physics_on_message = def and def.on_message
end
7 changes: 7 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,12 @@ This is basically a reference to a C++ `ServerActiveObject`
* `sneak`: whether player can sneak (default: `true`)
* `sneak_glitch`: whether player can use the sneak glitch (default: `true`)
* `get_physics_override()`: returns the table given to set_physics_override
* `set_physics_script(script_content, handlers)`
* `script_content`: A Lua script. See player_physics_api.txt.
* `handlers´: `{on_message=function(message)}`
* `message´: string from client-side send_local_player_physics_message()
* `send_physics_script_message(message)`
* `message´: string (Hint: use minetest.serialize() for tables)
* `hud_add(hud definition)`: add a HUD element described by HUD def, returns ID
number on success
* `hud_remove(id)`: remove the HUD element of the specified id
Expand Down Expand Up @@ -4137,3 +4143,4 @@ The Biome API is still in an experimental phase and subject to change.
-- ^ HTTP status code
data = "response"
}

62 changes: 62 additions & 0 deletions doc/player_physics_api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Minetest Lua Modding API Reference 0.4.14: Player Physics API
=============================================================

* `minetest.set_local_player_physics(handlers)`
handlers = {
apply_control = function(dtime, control, player_params) -> player_params or nil,
move = function(dtime, control, player_params, pos_max_d) -> player_params or nil, -- Called more often if velocity is high
camera_up_vector = function() -> vector or nil,
get_collision_box = function(), -- TODO
get_eye_offset = function(), -- TODO
on_message = function(message),
* message: string from server-side player:send_physics_script_message()
})

* `minetest.collision_move_simple(pos_max_d, box, stepheight, dtime, pos,
speed, accel)` -> result
* `pos_max_d`: maximum movement distance
* `box`: collision box {minx, miny, minz, maxx, maxy, maxz}
* `stepheight`: Maximum node height difference to step over
* `dtime`: Time step length in seconds
* `pos`: Position vector {x=, y=, z=}
* `speed`: Speed vector
* `accel`: Acceleration vector
* `result`: {pos, speed}

* `minetest.send_local_player_physics_message(message)`
* message: string (Hint: use minetest.serialize() for tables)

### control format

{
right = false,
aux1 = false,
sneak = false,
down = false,
left = false,
up = false,
jump = false
LMB = false,
RMB = false,
yaw = 3.5694076447901,
pitch = 1.5620696805349,
sidew_move_joystick_axis = 0,
forw_move_joystick_axis = 0,
}

### player_params format

{
position = {
y = -626.39599609375,
x = -3668.8452148438,
z = 4305.4926757812
},
velocity = {
y = -87.501235961914,
x = 0.62141418457031,
z = -1.3628302812576
},
yaw = 606.75519234034,
pitch = 604.74785916978
}
77 changes: 77 additions & 0 deletions games/minimal/mods/physics_script_test/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local airplane_players = {}
local mod_base_path = core.get_modpath(core.get_current_modname())

local function read_file_content(fpath)
local f, errmsg = io.open(fpath, 'rb')
if f then
local content = f:read "*a"
f:close()
return content
else
minetest.log("error", "failed to open "..fpath)
end
end

local function activate_airplane_mode(player)
print("Calling player:set_physics_script")

local state = {
player = player,
}

airplane_players[player:get_player_name()] = state

state.hud_id_engine_text = player:hud_add({
hud_elem_type = "text",
position = {x=0.05, y=0.85},
alignment = {x=1, y=0},
number = 0xffffff,
text = "Engine: OFF (press space to toggle)",
})

state.hud_id_speed_text = player:hud_add({
hud_elem_type = "text",
position = {x=0.05, y=0.9},
alignment = {x=1, y=0},
number = 0xffffff,
text = "Speed: 0",
})

local script = read_file_content(mod_base_path.."/local_player_physics.lua")

player:set_physics_script(script, {
on_message = function(serialized_message)
local message = minetest.deserialize(serialized_message)
if message.name == "throttle_status" then
if message.status then
player:hud_change(state.hud_id_engine_text, "text", "Engine: ON")
else
player:hud_change(state.hud_id_engine_text, "text", "Engine: OFF")
end
else
print("player:set_physics_script on_message unknown: "..dump(message))
end
end,
})
end

local dt_accu = 0.0
minetest.register_globalstep(function(dtime)
dt_accu = dt_accu + dtime
if dt_accu < 1.0 then
return
end
dt_accu = 0.0
for name, state in pairs(airplane_players) do
state.player:hud_change(state.hud_id_speed_text, "text", "Speed: "..
(math.floor(vector.length(state.player:get_player_velocity())*10)/10).." m/s")
--state.player:send_physics_script_message("test message")
end
end)

minetest.register_on_joinplayer(function(player)
-- TODO: Activate by using an item or something maybe
minetest.after(2, function()
activate_airplane_mode(player)
end)
end)
Loading