Silly title to be honest, don't really know what to name this thread.
Anyway I stole all this code from blockmaker so that I could just have code that could move entities but when I try to run this ingame it doesn't work(nothing moves). I guess I missed something.
PHP Code:
#include <amxmodx>
#include <engine>
#define ENTMOVER_ADMIN_LEVEL ADMIN_USER
new playerViewModel[33][32];
new gGrabbed[33];
new Float:GrabLength[33];
new Float:GrabOffset[33][3];
public plugin_init()
{
register_clcmd("+entgrab", "cmdGrab", ENTMOVER_ADMIN_LEVEL);
register_clcmd("-entgrab", "cmdRelease", ENTMOVER_ADMIN_LEVEL);
}
public client_PreThink(id)
{
//make sure player is connected
if (is_user_connected(id))
{
if (gGrabbed[id] > 0)
{
//move the entity the player has grabbed
moveGrabbedEntity(id);
}
}
return PLUGIN_CONTINUE;
}
moveGrabbedEntity(id, Float:vMoveTo[3] = {0.0, 0.0, 0.0})
{
new iOrigin[3], iLook[3];
new Float:fOrigin[3], Float:fLook[3], Float:fDirection[3], Float:fLength;
get_user_origin(id, iOrigin, 1); //Position from eyes (weapon aiming)
get_user_origin(id, iLook, 3); //End position from eyes (hit point for weapon)
IVecFVec(iOrigin, fOrigin);
IVecFVec(iLook, fLook);
fDirection[0] = fLook[0] - fOrigin[0];
fDirection[1] = fLook[1] - fOrigin[1];
fDirection[2] = fLook[2] - fOrigin[2];
fLength = get_distance_f(fLook, fOrigin);
if (fLength == 0.0) fLength = 1.0; //avoid division by 0
//calculate the position to move the block
vMoveTo[0] = (fOrigin[0] + fDirection[0] * GrabLength[id] / fLength) + GrabOffset[id][0];
vMoveTo[1] = (fOrigin[1] + fDirection[1] * GrabLength[id] / fLength) + GrabOffset[id][1];
vMoveTo[2] = (fOrigin[2] + fDirection[2] * GrabLength[id] / fLength) + GrabOffset[id][2];
vMoveTo[2] = float(floatround(vMoveTo[2], floatround_floor));
//move the block and its sprite (if it has one)
moveEntity(id, gGrabbed[id], vMoveTo);
}
moveEntity(id, ent, Float:vMoveTo[3])
{
//set the position of the block
entity_set_origin(ent, vMoveTo);
//get the sprite that sits above the block (if any)
new sprite = entity_get_int(ent, EV_INT_iuser3);
//if sprite entity is valid
if (sprite)
{
//get size of block
new Float:vSizeMax[3];
entity_get_vector(ent, EV_VEC_maxs, vSizeMax);
//move the sprite onto the top of the block
vMoveTo[2] += vSizeMax[2] + 0.15;
entity_set_origin(sprite, vMoveTo);
}
}
public cmdGrab(id)
{
//make sure player has access to use this command
if (get_user_flags(id) & ENTMOVER_ADMIN_LEVEL)
{
//get the entity the player is aiming at and the length
new ent, body;
GrabLength[id] = get_user_aiming(id, ent, body);
//get who is currently grabbing the entity (if anyone)
new grabber = entity_get_int(ent, EV_INT_iuser2);
//if entity is not being grabbed by someone else
if (grabber == 0 || grabber == id)
{
//set the ent to 'being grabbed'
setGrabbed(id, ent);
}
}
return PLUGIN_HANDLED;
}
setGrabbed(id, ent)
{
new Float:fpOrigin[3];
new Float:fbOrigin[3];
new Float:fAiming[3];
new iAiming[3];
new bOrigin[3];
//get players current view model then clear it
entity_get_string(id, EV_SZ_viewmodel, playerViewModel[id], 32);
entity_set_string(id, EV_SZ_viewmodel, "");
get_user_origin(id, bOrigin, 1); //position from eyes (weapon aiming)
get_user_origin(id, iAiming, 3); //end position from eyes (hit point for weapon)
entity_get_vector(id, EV_VEC_origin, fpOrigin); //get player position
entity_get_vector(ent, EV_VEC_origin, fbOrigin); //get block position
IVecFVec(iAiming, fAiming);
FVecIVec(fbOrigin, bOrigin);
//set gGrabbed
gGrabbed[id] = ent;
GrabOffset[id][0] = fbOrigin[0] - iAiming[0];
GrabOffset[id][1] = fbOrigin[1] - iAiming[1];
GrabOffset[id][2] = fbOrigin[2] - iAiming[2];
//indicate that entity is being grabbed
entity_set_int(ent, EV_INT_iuser2, id);
}
public cmdRelease(id)
{
//make sure player has access to use this command
if (get_user_flags(id) & ENTMOVER_ADMIN_LEVEL)
{
//if player is grabbing an entity
if (gGrabbed[id])
{
//indicate that the block is no longer being grabbed
entity_set_int(gGrabbed[id], EV_INT_iuser2, 0);
//set the players view model back to what it was
entity_set_string(id, EV_SZ_viewmodel, playerViewModel[id]);
//indicate that player is not grabbing an object
gGrabbed[id] = 0;
}
}
return PLUGIN_HANDLED;
}