Skip to content

Commit

Permalink
Implemented basic pickup position corrector
Browse files Browse the repository at this point in the history
Also, two new script functions (getEntityDistance and
moveEntityToEntity) were impemented, which could come in handy for other
features.
NB! Pedestal pick-ups (TR4-5) are temporarily disabled!
  • Loading branch information
Lwmte committed May 2, 2015
1 parent 3d5c6a0 commit 2cf862c
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 69 deletions.
28 changes: 16 additions & 12 deletions scripts/entity/entity_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function keyhole_func(id) -- Key and puzzle holes.

if(getEntityActivity(object_id) == 0) then
setEntityPos(activator_id, getEntityPos(object_id));
moveEntityLocal(activator_id, 0.0, 256.0, 0.0);
moveEntityLocal(activator_id, 0.0, 384.0, 0.0);
switch_activate(object_id, activator_id);

end
Expand All @@ -130,7 +130,7 @@ function switch_func(id) -- Ordinary switches.
end

setEntityPos(activator_id, getEntityPos(object_id)); -- Move activator right next to object.
moveEntityLocal(activator_id, 0.0, 256.0, 0.0); -- Shift activator back to proper distance.
moveEntityLocal(activator_id, 0.0, 384.0, 0.0); -- Shift activator back to proper distance.
switch_activate(object_id, activator_id); -- Make switching routines.
end;

Expand Down Expand Up @@ -190,6 +190,8 @@ function pickup_func(id, item_id) -- VALID

local f1, f2, f3 = getEntityFlags(id); -- f1 - state flags, f2 - type flags, f3 - callback flags
setEntityFlags(id, nil, bit32.bor(f2, ENTITY_TYPE_PICKABLE), nil);
setEntityActivationOffset(id, 0.0, 0.0, 0.0, 512.0);

if(entity_funcs[id] == nil) then
entity_funcs[id] = {};
end
Expand All @@ -198,7 +200,7 @@ function pickup_func(id, item_id) -- VALID
if((item_id == nil) or (object_id == nil)) then
return;
end

local need_set_pos = true;
local curr_anim = getEntityAnim(activator_id);

Expand All @@ -222,20 +224,22 @@ function pickup_func(id, item_id) -- VALID

print("You're trying to pick up object " .. object_id);

local px, py, pz = getEntityPos(object_id);
if(curr_anim == 108) then
pz = pz + 128.0 -- Shift offset for swim pickup.
end;

if(need_set_pos) then
setEntityPos(activator_id, px, py, pz);
end;

addTask(
function()
if(getEntityMoveType(activator_id) == 6) then
if(getEntityDistance(object_id, activator_id) > 128.0) then
moveEntityToEntity(activator_id, object_id, 25.0);
end;
else
if(getEntityDistance(object_id, activator_id) > 32.0) then
moveEntityToEntity(activator_id, object_id, 50.0);
end;;
end;

local a, f, c = getEntityAnim(activator_id);
local ver = getLevelVersion();

-- Standing pickup anim makes action on frame 40 in TR1-3, in TR4-5
-- it was generalized with all rest animations by frame 16.

Expand Down
6 changes: 0 additions & 6 deletions scripts/trigger/trigger_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,6 @@ function setCamTarget(entity_index, timer)
print("CAMERA TARGET: index = " .. entity_index .. ", timer = " .. timer);
end

-- Flips a flipmap, according to activation mask.

function doFlipmap(flipmap_index, trigger_mask)

end


-- Does specified flipeffect.

Expand Down
16 changes: 8 additions & 8 deletions src/anim_state_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ int State_Control_Lara(struct entity_s *ent)
Controls_JoyRumble(200.0, 200);
Entity_SetAnimation(ent, TR_ANIMATION_LARA_SMASH_JUMP, 0);
ent->dir_flag = ENT_MOVE_FORWARD;
Character_UpdateCurrentSpeed(ent, 1);
Entity_UpdateCurrentSpeed(ent, 1);
}
else if(ent->speed.m_floats[2] <= -FREE_FALL_SPEED_2)
{
Expand Down Expand Up @@ -622,7 +622,7 @@ int State_Control_Lara(struct entity_s *ent)
Controls_JoyRumble(200.0, 200);
Entity_SetAnimation(ent, TR_ANIMATION_LARA_SMASH_JUMP, 0);
ent->dir_flag = ENT_MOVE_RIGHT;
Character_UpdateCurrentSpeed(ent, 1);
Entity_UpdateCurrentSpeed(ent, 1);
}
else
{
Expand All @@ -649,7 +649,7 @@ int State_Control_Lara(struct entity_s *ent)
Controls_JoyRumble(200.0, 200);
Entity_SetAnimation(ent, TR_ANIMATION_LARA_SMASH_JUMP, 0);
ent->dir_flag = ENT_MOVE_LEFT;
Character_UpdateCurrentSpeed(ent, 1);
Entity_UpdateCurrentSpeed(ent, 1);
}
else
{
Expand Down Expand Up @@ -833,7 +833,7 @@ int State_Control_Lara(struct entity_s *ent)
Entity_SetAnimation(ent, TR_ANIMATION_LARA_STAY_SOLID, 0);
}
}
Character_UpdateCurrentSpeed(ent, 0);
Entity_UpdateCurrentSpeed(ent, 0);
}
else if(cmd->move[0] == 1) // If we continue running...
{
Expand Down Expand Up @@ -913,7 +913,7 @@ int State_Control_Lara(struct entity_s *ent)
{
Entity_SetAnimation(ent, TR_ANIMATION_LARA_WALL_SMASH_RIGHT, 0);
}
Character_UpdateCurrentSpeed(ent, 0);
Entity_UpdateCurrentSpeed(ent, 0);
}
else if(cmd->sprint == 0)
{
Expand Down Expand Up @@ -1040,7 +1040,7 @@ int State_Control_Lara(struct entity_s *ent)
if(ent->character->height_info.quicksand)
{
ent->current_speed = 8.0;
Character_UpdateCurrentSpeed(ent);
Entity_UpdateCurrentSpeed(ent);
}

if(cmd->move[0] == 1)
Expand Down Expand Up @@ -1120,7 +1120,7 @@ int State_Control_Lara(struct entity_s *ent)
if(ent->character->height_info.quicksand)
{
ent->current_speed = 4.0;
Character_UpdateCurrentSpeed(ent);
Entity_UpdateCurrentSpeed(ent);
}

if(ent->move_type == MOVE_FREE_FALLING)
Expand Down Expand Up @@ -2169,7 +2169,7 @@ int State_Control_Lara(struct entity_s *ent)
{
Entity_SetAnimation(ent, TR_ANIMATION_LARA_SMASH_JUMP, 0);
ent->dir_flag = ENT_MOVE_BACKWARD;
Character_UpdateCurrentSpeed(ent, 1);
Entity_UpdateCurrentSpeed(ent, 1);
}
else if(ent->speed.m_floats[2] <= -FREE_FALL_SPEED_2)
{
Expand Down
32 changes: 0 additions & 32 deletions src/character_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,38 +1336,6 @@ void Character_CheckNextPenetration(struct entity_s *ent, btScalar move[3])
}


void Character_UpdateCurrentSpeed(struct entity_s *ent, int zeroVz)
{
btScalar t, vz;

t = ent->current_speed * ent->character->speed_mult;
vz = (zeroVz)?(0.0):(ent->speed.m_floats[2]);

if(ent->dir_flag & ENT_MOVE_FORWARD)
{
vec3_mul_scalar(ent->speed.m_floats, ent->transform+4, t);
}
else if(ent->dir_flag & ENT_MOVE_BACKWARD)
{
vec3_mul_scalar(ent->speed.m_floats, ent->transform+4,-t);
}
else if(ent->dir_flag & ENT_MOVE_LEFT)
{
vec3_mul_scalar(ent->speed.m_floats, ent->transform+0,-t);
}
else if(ent->dir_flag & ENT_MOVE_RIGHT)
{
vec3_mul_scalar(ent->speed.m_floats, ent->transform+0, t);
}
else
{
vec3_set_zero(ent->speed.m_floats);
}

ent->speed.m_floats[2] = vz;
}


void Character_SetToJump(struct entity_s *ent, btScalar v_vertical, btScalar v_horizontal)
{
btScalar t;
Expand Down
1 change: 0 additions & 1 deletion src/character_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ int Character_GetPenetrationFixVector(struct entity_s *ent, btScalar reaction[3]
void Character_FixPenetrations(struct entity_s *ent, btScalar move[3], btScalar step_up_check);
void Character_CheckNextPenetration(struct entity_s *ent, btScalar move[3]);

void Character_UpdateCurrentSpeed(struct entity_s *ent, int zeroVz = 0);
void Character_UpdateCurrentHeight(struct entity_s *ent);
void Character_UpdatePlatformPreStep(struct entity_s *ent);
void Character_UpdatePlatformPostStep(struct entity_s *ent);
Expand Down
90 changes: 84 additions & 6 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,41 @@ int lua_GetEntityVector(lua_State * lua)
return 3;
}

int lua_GetEntityDistance(lua_State * lua)
{
if(lua_gettop(lua) < 2)
{
Con_Warning(SYSWARN_WRONG_ARGS, "[id1], [id2]");
return 0;
}

int id = lua_tointeger(lua, 1);
entity_p e1 = World_GetEntityByID(&engine_world, id);
if(e1 == NULL)
{
Con_Warning(SYSWARN_NO_ENTITY, id);
return 0;
}
id = lua_tointeger(lua, 2);
entity_p e2 = World_GetEntityByID(&engine_world, id);
if(e2 == NULL)
{
Con_Warning(SYSWARN_NO_ENTITY, id);
return 0;
}

btVector3 ent1_pos; ent1_pos.m_floats[0] = e1->transform[12+0];
ent1_pos.m_floats[1] = e1->transform[12+1];
ent1_pos.m_floats[2] = e1->transform[12+2];

btVector3 ent2_pos; ent2_pos.m_floats[0] = e2->transform[12+0];
ent2_pos.m_floats[1] = e2->transform[12+1];
ent2_pos.m_floats[2] = e2->transform[12+2];

lua_pushnumber(lua, btDistance(ent1_pos, ent2_pos));
return 1;
}


int lua_GetEntityDirDot(lua_State * lua)
{
Expand Down Expand Up @@ -1567,26 +1602,67 @@ int lua_MoveEntityToSink(lua_State * lua)

btVector3 ent_pos; ent_pos.m_floats[0] = ent->transform[12+0];
ent_pos.m_floats[1] = ent->transform[12+1];
ent_pos.m_floats[2] = ent->transform[12+2] - 256.0; // Prevents digging into the floor.
ent_pos.m_floats[2] = ent->transform[12+2];

btVector3 sink_pos; sink_pos.m_floats[0] = sink->x;
sink_pos.m_floats[1] = sink->y;
sink_pos.m_floats[2] = sink->z;

if(engine_world.version < TR_II)
{
sink_pos.m_floats[2] = ent_pos.m_floats[2];
}
else
{
sink_pos.m_floats[2] = sink->z + 256.0; // Prevents digging into the floor.
}

btScalar dist = btDistance(ent_pos, sink_pos);
if(dist == 0.0) dist = 1.0; // Prevents division by zero.

btVector3 speed = ((ent_pos - sink_pos) / dist) * (sink->room_or_strength / 1024.0);
btVector3 speed = (((sink_pos - ent_pos) / dist) * ((btScalar)(sink->room_or_strength))) / 512.0;

ent->transform[12+0] -= speed.m_floats[0];
ent->transform[12+1] -= speed.m_floats[1];
ent->transform[12+2] -= speed.m_floats[2] * 64.0; // Vertical force is stronger.
ent->transform[12+0] += speed.m_floats[0];
ent->transform[12+1] += speed.m_floats[1];
ent->transform[12+2] += speed.m_floats[2] * 32.0;

Entity_UpdateRigidBody(ent, 1);

return 0;
}

int lua_MoveEntityToEntity(lua_State * lua)
{
if(lua_gettop(lua) < 3)
{
Con_Warning(SYSWARN_WRONG_ARGS, "[entity_to_move_id, entity_id, speed]");
return 0;
}

entity_p ent1 = World_GetEntityByID(&engine_world, lua_tointeger(lua, 1));
entity_p ent2 = World_GetEntityByID(&engine_world, lua_tointeger(lua, 2));
btScalar speed_mult = lua_tonumber(lua, 3);

btVector3 ent1_pos; ent1_pos.m_floats[0] = ent1->transform[12+0];
ent1_pos.m_floats[1] = ent1->transform[12+1];
ent1_pos.m_floats[2] = ent1->transform[12+2];

btVector3 ent2_pos; ent2_pos.m_floats[0] = ent2->transform[12+0];
ent2_pos.m_floats[1] = ent2->transform[12+1];
ent2_pos.m_floats[2] = ent2->transform[12+2];

btScalar dist = btDistance(ent1_pos, ent2_pos);
if(dist == 0.0) dist = 1.0; // Prevents division by zero.

btVector3 speed = ((ent2_pos - ent1_pos) / dist) * speed_mult; // FIXME!

ent1->transform[12+0] += speed.m_floats[0];
ent1->transform[12+1] += speed.m_floats[1];
ent1->transform[12+2] += speed.m_floats[2];
if(ent1->character) Character_UpdatePlatformPreStep(ent1);
Entity_UpdateRigidBody(ent1, 1);

return 0;
}

int lua_GetEntitySpeed(lua_State * lua)
{
Expand Down Expand Up @@ -2832,11 +2908,13 @@ void Engine_LuaRegisterFuncs(lua_State *lua)

lua_register(lua, "getEntityVector", lua_GetEntityVector);
lua_register(lua, "getEntityDirDot", lua_GetEntityDirDot);
lua_register(lua, "getEntityDistance", lua_GetEntityDistance);
lua_register(lua, "getEntityPos", lua_GetEntityPosition);
lua_register(lua, "setEntityPos", lua_SetEntityPosition);
lua_register(lua, "moveEntityGlobal", lua_MoveEntityGlobal);
lua_register(lua, "moveEntityLocal", lua_MoveEntityLocal);
lua_register(lua, "moveEntityToSink", lua_MoveEntityToSink);
lua_register(lua, "moveEntityToEntity", lua_MoveEntityToEntity);
lua_register(lua, "getEntitySpeed", lua_GetEntitySpeed);
lua_register(lua, "setEntitySpeed", lua_SetEntitySpeed);
lua_register(lua, "setEntityCollision", lua_SetEntityCollision);
Expand Down
40 changes: 36 additions & 4 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,36 @@ void Entity_UpdateRotation(entity_p entity)
}


void Entity_UpdateCurrentSpeed(entity_p entity, int zeroVz)
{
btScalar t = entity->current_speed * entity->character->speed_mult;
btScalar vz = (zeroVz)?(0.0):(entity->speed.m_floats[2]);

if(entity->dir_flag & ENT_MOVE_FORWARD)
{
vec3_mul_scalar(entity->speed.m_floats, entity->transform+4, t);
}
else if(entity->dir_flag & ENT_MOVE_BACKWARD)
{
vec3_mul_scalar(entity->speed.m_floats, entity->transform+4,-t);
}
else if(entity->dir_flag & ENT_MOVE_LEFT)
{
vec3_mul_scalar(entity->speed.m_floats, entity->transform+0,-t);
}
else if(entity->dir_flag & ENT_MOVE_RIGHT)
{
vec3_mul_scalar(entity->speed.m_floats, entity->transform+0, t);
}
else
{
vec3_set_zero(entity->speed.m_floats);
}

entity->speed.m_floats[2] = vz;
}


void Entity_AddOverrideAnim(struct entity_s *ent, int model_id)
{
skeletal_model_p sm = World_GetModelByID(&engine_world, model_id);
Expand Down Expand Up @@ -1621,8 +1651,6 @@ void Entity_CheckActivators(struct entity_s *ent)
if((cont->object_type == OBJECT_ENTITY) && (cont->object))
{
entity_p e = (entity_p)cont->object;
btScalar r = e->activation_offset[3];
r *= r;
if((e->type_flags & ENTITY_TYPE_INTERACTIVE) && (e->state_flags & ENTITY_STATE_ENABLED))
{
//Mat4_vec3_mul_macro(pos, e->transform, e->activation_offset);
Expand All @@ -1633,9 +1661,13 @@ void Entity_CheckActivators(struct entity_s *ent)
}
else if((e->type_flags & ENTITY_TYPE_PICKABLE) && (e->state_flags & ENTITY_STATE_ENABLED))
{
btScalar r = e->activation_offset[3];
r *= r;
btScalar *v = e->transform + 12;
if((e != ent) && ((v[0] - ppos[0]) * (v[0] - ppos[0]) + (v[1] - ppos[1]) * (v[1] - ppos[1]) < r) &&
(v[2] + 32.0 > ent->transform[12+2] + ent->bf.bb_min[2]) && (v[2] - 32.0 < ent->transform[12+2] + ent->bf.bb_max[2]))
if((e != ent) &&
((v[0] - ppos[0]) * (v[0] - ppos[0]) +
(v[1] - ppos[1]) * (v[1] - ppos[1]) +
(v[2] - ppos[2]) * (v[2] - ppos[2]) < r))
{
lua_ExecEntity(engine_lua, e->id, ent->id, ENTITY_CALLBACK_ACTIVATE);
}
Expand Down
Loading

0 comments on commit 2cf862c

Please sign in to comment.