#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
/* The info of this plugin */
#define PLUGIN_VERSION "0.7"
#define PLUGIN_TAG "CAM"
/* Just dont touch it */
#define CAMERA_OWNER EV_INT_iuser1
#define CAMERA_CLASSNAME "trigger_camera"
#define CAMERA_MODEL "models/rpgrocket.mdl"
new bool:g_bInThirdPerson[33] = false;
new g_pCvar_fCameraDistance, g_pCvar_iCameraForced;
public plugin_init()
{
register_plugin("Obscura Cam", PLUGIN_VERSION, "Nani (SkumTomteN@Alliedmodders)")
register_think(CAMERA_CLASSNAME, "FW_CameraThink")
RegisterHam(Ham_Killed, "player", "fw_PlayerKilled_Post", 1)
RegisterHam(Ham_Spawn, "player", "fw_PlayerSpawn_Post", 1)
g_pCvar_iCameraForced = register_cvar("cam_forced", "0")
g_pCvar_fCameraDistance = register_cvar("cam_distance", "150.0")
register_clcmd("say /cam", "CMD_ToggleCam")
register_clcmd("sayteam /cam", "CMD_ToggleCam")
register_clcmd("cam", "CMD_ToggleCam")
}
public plugin_precache()
{
precache_model(CAMERA_MODEL)
}
public plugin_natives()
{
register_native("set_user_camera", "Native_SetCamera", 1)
register_native("get_user_camera", "Native_GetCamera", 1)
}
public Native_SetCamera(iPlayer, Thirdperson)
{
if(!is_user_alive(iPlayer))
return;
if(!Thirdperson)
{
Set_CameraEnt(iPlayer)
}
else
{
Remove_CameraEnt(iPlayer, 1)
}
}
public Native_GetCamera(iPlayer)
{
if(!is_user_connected(iPlayer) || !g_bInThirdPerson[iPlayer])
return 0;
return 1;
}
public client_disconnected(iPlayer)
{
Remove_CameraEnt(iPlayer, 0)
}
public fw_PlayerSpawn_Post(iPlayer)
{
if(!is_user_alive(iPlayer))
return HAM_IGNORED;
if(g_bInThirdPerson[iPlayer] || get_pcvar_num(g_pCvar_iCameraForced))
{
Set_CameraEnt(iPlayer)
}
else
{
Remove_CameraEnt(iPlayer, 1)
}
return HAM_HANDLED;
}
public fw_PlayerKilled_Post(iVictim, iKiller, iShouldGIB)
{
if(!is_user_connected(iVictim) || is_user_alive(iVictim))
return HAM_IGNORED;
if(g_bInThirdPerson[iVictim])
{
Remove_CameraEnt(iVictim, 0)
}
return HAM_HANDLED;
}
public CMD_ToggleCam(iPlayer)
{
if(get_pcvar_num(g_pCvar_iCameraForced))
{
client_printc(iPlayer, "!g[%s]!n Camera is forced on !t3rd person!n by server manager!", PLUGIN_TAG)
return;
}
g_bInThirdPerson[iPlayer] = ~g_bInThirdPerson[iPlayer]
if(is_user_alive(iPlayer))
{
if(g_bInThirdPerson[iPlayer])
{
Set_CameraEnt(iPlayer)
client_print(0, print_center, "3rd Person Mode")
}
else
{
Remove_CameraEnt(iPlayer, 1)
client_print(0, print_center, "1st Person Mode")
}
}
else /* He toggles when not alive */
{
client_printc(iPlayer, "!g[%s]!n Your camera will be !t%s!n when you spawn!", PLUGIN_TAG, g_bInThirdPerson[iPlayer] ? "3rd Person" : "1st Person")
}
}
public Set_CameraEnt(iPlayer)
{
new iEntity = create_entity(CAMERA_CLASSNAME);
if(!is_valid_ent(iEntity)) return;
entity_set_model(iEntity, CAMERA_MODEL)
entity_set_int(iEntity, CAMERA_OWNER, iPlayer)
entity_set_string(iEntity, EV_SZ_classname, CAMERA_CLASSNAME)
entity_set_int(iEntity, EV_INT_solid, SOLID_NOT)
entity_set_int(iEntity, EV_INT_movetype, MOVETYPE_FLY)
entity_set_int(iEntity, EV_INT_rendermode, kRenderTransTexture)
attach_view(iPlayer, iEntity)
entity_set_float(iEntity, EV_FL_nextthink, get_gametime())
}
public Remove_CameraEnt(iPlayer, AttachView)
{
if(AttachView) attach_view(iPlayer, iPlayer)
new iEntity = -1;
while((iEntity = find_ent_by_class(iEntity, CAMERA_CLASSNAME)))
{
if(!is_valid_ent(iEntity))
continue;
if(entity_get_int(iEntity, CAMERA_OWNER) == iPlayer)
{
entity_set_int(iEntity, EV_INT_flags, FL_KILLME)
dllfunc(DLLFunc_Think, iEntity)
}
}
}
public FW_CameraThink(iEntity)
{
new iOwner = entity_get_int(iEntity, CAMERA_OWNER);
if(!is_user_alive(iOwner) || is_user_bot(iOwner))
return;
if(!is_valid_ent(iEntity))
return;
new Float:fPlayerOrigin[3], Float:fCameraOrigin[3], Float:vAngles[3], Float:vBack[3];
entity_get_vector(iOwner, EV_VEC_origin, fPlayerOrigin)
entity_get_vector(iOwner, EV_VEC_view_ofs, vAngles)
fPlayerOrigin[2] += vAngles[2];
entity_get_vector(iOwner, EV_VEC_v_angle, vAngles)
angle_vector(vAngles, ANGLEVECTOR_FORWARD, vBack)
fCameraOrigin[0] = fPlayerOrigin[0] + (-vBack[0] * get_pcvar_float(g_pCvar_fCameraDistance))
fCameraOrigin[1] = fPlayerOrigin[1] + (-vBack[1] * get_pcvar_float(g_pCvar_fCameraDistance))
fCameraOrigin[2] = fPlayerOrigin[2] + (-vBack[2] * get_pcvar_float(g_pCvar_fCameraDistance))
engfunc(EngFunc_TraceLine, fPlayerOrigin, fCameraOrigin, IGNORE_MONSTERS, iOwner, 0)
new Float:flFraction; get_tr2(0, TR_flFraction, flFraction)
if(flFraction != 1.0)
{
flFraction *= get_pcvar_float(g_pCvar_fCameraDistance); /* Automatic :) */
fCameraOrigin[0] = fPlayerOrigin[0] + (-vBack[0] * flFraction)
fCameraOrigin[1] = fPlayerOrigin[1] + (-vBack[1] * flFraction)
fCameraOrigin[2] = fPlayerOrigin[2] + (-vBack[2] * flFraction)
}
entity_set_vector(iEntity, EV_VEC_origin, fCameraOrigin)
entity_set_vector(iEntity, EV_VEC_angles, vAngles)
entity_set_float(iEntity, EV_FL_nextthink, get_gametime() + 0.01)
}
stock client_printc(index, const text[], any:...)
{
static szMsg[128]; vformat(szMsg, sizeof(szMsg) - 1, text, 3)
replace_all(szMsg, sizeof(szMsg) - 1, "!g", "^x04")
replace_all(szMsg, sizeof(szMsg) - 1, "!n", "^x01")
replace_all(szMsg, sizeof(szMsg) - 1, "!t", "^x03")
static g_MsgSayText; if(!g_MsgSayText) g_MsgSayText = get_user_msgid("SayText")
static MaxPlayers; MaxPlayers = get_maxplayers();
if(!index)
{
for(new i = 1; i <= MaxPlayers; i++)
{
if(!is_user_connected(i))
continue;
message_begin(MSG_ONE_UNRELIABLE, g_MsgSayText, _, i);
write_byte(i);
write_string(szMsg);
message_end();
}
}
else
{
message_begin(MSG_ONE_UNRELIABLE, g_MsgSayText, _, index);
write_byte(index);
write_string(szMsg);
message_end();
}
}