aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_localplayer.cpp
blob: 2efb976c7ed0fc78ebf05d0d56aa6b5273429b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "collision.h"
#include "mapblock.h"
#include "map.h"

collisionMoveResult collisionMoveSimple(Map *map, f32 pos_max_d,
		const core::aabbox3d<f32> &box_0,
		f32 dtime, v3f &pos_f, v3f &speed_f)
{
	collisionMoveResult result;

	v3f oldpos_f = pos_f;
	v3s16 oldpos_i = floatToInt(oldpos_f, BS);

	/*
		Calculate new position
	*/
	pos_f += speed_f * dtime;

	/*
		Collision detection
	*/
	
	// position in nodes
	v3s16 pos_i = floatToInt(pos_f, BS);
	
	/*
		Collision uncertainty radius
		Make it a bit larger than the maximum distance of movement
	*/
	f32 d = pos_max_d * 1.1;
	// A fairly large value in here makes moving smoother
	//f32 d = 0.15*BS;

	// This should always apply, otherwise there are glitches
	assert(d > pos_max_d);
	
	/*
		Calculate collision box
	*/
	core::aabbox3d<f32> box = box_0;
	box.MaxEdge += pos_f;
	box.MinEdge += pos_f;
	core::aabbox3d<f32> oldbox = box_0;
	oldbox.MaxEdge += oldpos_f;
	oldbox.MinEdge += oldpos_f;

	/*
		If the object lies on a walkable node, this is set to true.
	*/
	result.touching_ground = false;
	
	/*
		Go through every node around the object
		TODO: Calculate the range of nodes that need to be checked
	*/
	for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
	for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
	for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
	{
		try{
			// Object collides into walkable nodes
			if(content_walkable(map->getNode(v3s16(x,y,z)).d) == false)
				continue;
		}
		catch(InvalidPositionException &e)
		{
			// Doing nothing here will block the object from
			// walking over map borders
		}

		core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
		
		/*
			See if the object is touching ground.

			Object touches ground if object's minimum Y is near node's
			maximum Y and object's X-Z-area overlaps with the node's
			X-Z-area.

			Use 0.15*BS so that it is easier to get on a node.
		*/
		if(
				//fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < d
				fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS
				&& nodebox.MaxEdge.X-d > box.MinEdge.X
				&& nodebox.MinEdge.X+d < box.MaxEdge.X
				&& nodebox.MaxEdge.Z-d > box.MinEdge.Z
				&& nodebox.MinEdge.Z+d < box.MaxEdge.Z
		){
			result.touching_ground = true;
		}
		
		// If object doesn't intersect with node, ignore node.
		if(box.intersectsWithBox(nodebox) == false)
			continue;
		
		/*
			Go through every axis
		*/
		v3f dirs[3] = {
			v3f(0,0,1), // back-front
			v3f(0,1,0), // top-bottom
			v3f(1,0,0), // right-left
		};
		for(u16 i=0; i<3; i++)
		{
			/*
				Calculate values along the axis
			*/
			f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
			f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
			f32 objectmax = box.MaxEdge.dotProduct(dirs[i]);
			f32 objectmin = box.MinEdge.dotProduct(dirs[i]);
			f32 objectmax_old = oldbox.MaxEdge.dotProduct(dirs[i]);
			f32 objectmin_old = oldbox.MinEdge.dotProduct(dirs[i]);
			
			/*
				Check collision for the axis.
				Collision happens when object is going through a surface.
			*/
			bool negative_axis_collides =
				(nodemax > objectmin && nodemax <= objectmin_old + d
					&& speed_f.dotProduct(dirs[i]) < 0);
			bool positive_axis_collides =
				(nodemin < objectmax && nodemin >= objectmax_old - d
					&& speed_f.dotProduct(dirs[i]) > 0);
			bool main_axis_collides =
					negative_axis_collides || positive_axis_collides;
			
			/*
				Check overlap of object and node in other axes
			*/
			bool other_axes_overlap = true;
			for(u16 j=0; j<3; j++)
			{
				if(j == i)
					continue;
				f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
				f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
				f32 objectmax = box.MaxEdge.dotProduct(dirs[j]);
				f32 objectmin = box.MinEdge.dotProduct(dirs[j]);
				if(!(nodemax - d > objectmin && nodemin + d < objectmax))
				{
					other_axes_overlap = false;
					break;
				}
			}
			
			/*
				If this is a collision, revert the pos_f in the main
				direction.
			*/
			if(other_axes_overlap && main_axis_collides)
			{
				speed_f -= speed_f.dotProduct(dirs[i]) * dirs[i];
				pos_f -= pos_f.dotProduct(dirs[i]) * dirs[i];
				pos_f += oldpos_f.dotProduct(dirs[i]) * dirs[i];
			}
		
		}
	} // xyz
	
	return result;
}


lass="hl kwd">getobject(L, 1); lua_pushinteger(L, player->getWieldIndex()); return 1; } // get_wielded_item(self) int LuaLocalPlayer::l_get_wielded_item(lua_State *L) { LocalPlayer *player = getobject(L, 1); ItemStack selected_item; player->getWieldedItem(&selected_item, nullptr); LuaItemStack::create(L, selected_item); return 1; } int LuaLocalPlayer::l_is_attached(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->getParent() != nullptr); return 1; } int LuaLocalPlayer::l_is_touching_ground(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->touching_ground); return 1; } int LuaLocalPlayer::l_is_in_liquid(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->in_liquid); return 1; } int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->in_liquid_stable); return 1; } int LuaLocalPlayer::l_get_move_resistance(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushinteger(L, player->move_resistance); return 1; } int LuaLocalPlayer::l_is_climbing(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->is_climbing); return 1; } int LuaLocalPlayer::l_swimming_vertical(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushboolean(L, player->swimming_vertical); return 1; } // get_physics_override(self) int LuaLocalPlayer::l_get_physics_override(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->physics_override_speed); lua_setfield(L, -2, "speed"); lua_pushnumber(L, player->physics_override_jump); lua_setfield(L, -2, "jump"); lua_pushnumber(L, player->physics_override_gravity); lua_setfield(L, -2, "gravity"); lua_pushboolean(L, player->physics_override_sneak); lua_setfield(L, -2, "sneak"); lua_pushboolean(L, player->physics_override_sneak_glitch); lua_setfield(L, -2, "sneak_glitch"); lua_pushboolean(L, player->physics_override_new_move); lua_setfield(L, -2, "new_move"); return 1; } int LuaLocalPlayer::l_get_last_pos(lua_State *L) { LocalPlayer *player = getobject(L, 1); push_v3f(L, player->last_position / BS); return 1; } int LuaLocalPlayer::l_get_last_velocity(lua_State *L) { LocalPlayer *player = getobject(L, 1); push_v3f(L, player->last_speed); return 1; } int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD); return 1; } int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD); return 1; } // get_control(self) int LuaLocalPlayer::l_get_control(lua_State *L) { LocalPlayer *player = getobject(L, 1); const PlayerControl &c = player->getPlayerControl(); auto set = [L] (const char *name, bool value) { lua_pushboolean(L, value); lua_setfield(L, -2, name); }; lua_createtable(L, 0, 12); set("jump", c.jump); set("aux1", c.aux1); set("sneak", c.sneak); set("zoom", c.zoom); set("dig", c.dig); set("place", c.place); // Player movement in polar coordinates and non-binary speed lua_pushnumber(L, c.movement_speed); lua_setfield(L, -2, "movement_speed"); lua_pushnumber(L, c.movement_direction); lua_setfield(L, -2, "movement_direction"); // Provide direction keys to ensure compatibility set("up", c.direction_keys & (1 << 0)); set("down", c.direction_keys & (1 << 1)); set("left", c.direction_keys & (1 << 2)); set("right", c.direction_keys & (1 << 3)); return 1; } // get_breath(self) int LuaLocalPlayer::l_get_breath(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_pushinteger(L, player->getBreath()); return 1; } // get_pos(self) int LuaLocalPlayer::l_get_pos(lua_State *L) { LocalPlayer *player = getobject(L, 1); push_v3f(L, player->getPosition() / BS); return 1; } // get_movement_acceleration(self) int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->movement_acceleration_default); lua_setfield(L, -2, "default"); lua_pushnumber(L, player->movement_acceleration_air); lua_setfield(L, -2, "air"); lua_pushnumber(L, player->movement_acceleration_fast); lua_setfield(L, -2, "fast"); return 1; } // get_movement_speed(self) int LuaLocalPlayer::l_get_movement_speed(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->movement_speed_walk); lua_setfield(L, -2, "walk"); lua_pushnumber(L, player->movement_speed_crouch); lua_setfield(L, -2, "crouch"); lua_pushnumber(L, player->movement_speed_fast); lua_setfield(L, -2, "fast"); lua_pushnumber(L, player->movement_speed_climb); lua_setfield(L, -2, "climb"); lua_pushnumber(L, player->movement_speed_jump); lua_setfield(L, -2, "jump"); return 1; } // get_movement(self) int LuaLocalPlayer::l_get_movement(lua_State *L) { LocalPlayer *player = getobject(L, 1); lua_newtable(L); lua_pushnumber(L, player->movement_liquid_fluidity); lua_setfield(L, -2, "liquid_fluidity"); lua_pushnumber(L, player->movement_liquid_fluidity_smooth); lua_setfield(L, -2, "liquid_fluidity_smooth"); lua_pushnumber(L, player->movement_liquid_sink); lua_setfield(L, -2, "liquid_sink"); lua_pushnumber(L, player->movement_gravity); lua_setfield(L, -2, "gravity"); return 1; } // get_armor_groups(self) int LuaLocalPlayer::l_get_armor_groups(lua_State *L) { LocalPlayer *player = getobject(L, 1); push_groups(L, player->getCAO()->getGroups()); return 1; } // hud_add(self, form) int LuaLocalPlayer::l_hud_add(lua_State *L) { LocalPlayer *player = getobject(L, 1); HudElement *elem = new HudElement; read_hud_element(L, elem); u32 id = player->addHud(elem); if (id == U32_MAX) { delete elem; return 0; } lua_pushnumber(L, id); return 1; } // hud_remove(self, id) int LuaLocalPlayer::l_hud_remove(lua_State *L) { LocalPlayer *player = getobject(L, 1); u32 id = luaL_checkinteger(L, 2); HudElement *element = player->removeHud(id); if (!element) lua_pushboolean(L, false); else lua_pushboolean(L, true); delete element; return 1; } // hud_change(self, id, stat, data) int LuaLocalPlayer::l_hud_change(lua_State *L) { LocalPlayer *player = getobject(L, 1); u32 id = luaL_checkinteger(L, 2); HudElement *element = player->getHud(id); if (!element) return 0; HudElementStat stat; void *unused; bool ok = read_hud_change(L, stat, element, &unused); lua_pushboolean(L, ok); return 1; } // hud_get(self, id) int LuaLocalPlayer::l_hud_get(lua_State *L) { LocalPlayer *player = getobject(L, 1); u32 id = luaL_checkinteger(L, -1); HudElement *e = player->getHud(id); if (!e) { lua_pushnil(L); return 1; } push_hud_element(L, e); return 1; } LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg) { luaL_checktype(L, narg, LUA_TUSERDATA); void *ud = luaL_checkudata(L, narg, className); if (!ud) luaL_typerror(L, narg, className); return *(LuaLocalPlayer **)ud; } LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref) { return ref->m_localplayer; } LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg) { LuaLocalPlayer *ref = checkobject(L, narg); assert(ref); LocalPlayer *player = getobject(ref); assert(player); return player; } int LuaLocalPlayer::gc_object(lua_State *L) { LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1)); delete o; return 0; } void LuaLocalPlayer::Register(lua_State *L) { lua_newtable(L); int methodtable = lua_gettop(L); luaL_newmetatable(L, className); int metatable = lua_gettop(L); lua_pushliteral(L, "__metatable"); lua_pushvalue(L, methodtable); lua_settable(L, metatable); // hide metatable from lua getmetatable() lua_pushliteral(L, "__index"); lua_pushvalue(L, methodtable); lua_settable(L, metatable); lua_pushliteral(L, "__gc"); lua_pushcfunction(L, gc_object); lua_settable(L, metatable); lua_pop(L, 1); // Drop metatable luaL_register(L, nullptr, methods); // fill methodtable lua_pop(L, 1); // Drop methodtable } const char LuaLocalPlayer::className[] = "LocalPlayer"; const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, get_velocity), luamethod(LuaLocalPlayer, get_hp), luamethod(LuaLocalPlayer, get_name), luamethod(LuaLocalPlayer, get_wield_index), luamethod(LuaLocalPlayer, get_wielded_item), luamethod(LuaLocalPlayer, is_attached), luamethod(LuaLocalPlayer, is_touching_ground), luamethod(LuaLocalPlayer, is_in_liquid), luamethod(LuaLocalPlayer, is_in_liquid_stable), luamethod(LuaLocalPlayer, is_climbing), luamethod(LuaLocalPlayer, swimming_vertical), luamethod(LuaLocalPlayer, get_physics_override), // TODO: figure our if these are useful in any way luamethod(LuaLocalPlayer, get_last_pos), luamethod(LuaLocalPlayer, get_last_velocity), luamethod(LuaLocalPlayer, get_last_look_horizontal), luamethod(LuaLocalPlayer, get_last_look_vertical), // luamethod(LuaLocalPlayer, get_control), luamethod(LuaLocalPlayer, get_breath), luamethod(LuaLocalPlayer, get_pos), luamethod(LuaLocalPlayer, get_movement_acceleration), luamethod(LuaLocalPlayer, get_movement_speed), luamethod(LuaLocalPlayer, get_movement), luamethod(LuaLocalPlayer, get_armor_groups), luamethod(LuaLocalPlayer, hud_add), luamethod(LuaLocalPlayer, hud_remove), luamethod(LuaLocalPlayer, hud_change), luamethod(LuaLocalPlayer, hud_get), luamethod(LuaLocalPlayer, get_move_resistance), {0, 0} };