Post by ShaunCraft15 on Jul 13, 2023 20:17:13 GMT
hello, this is new zp50 flashlight based on ConnorMcLeod custom flashlight
tested and it worked good, if there is bug tell me only
What good:
- Smooth and fast
- No Tasks in code
- short code (optimized)
/*================================================================================
-----------------------
-*- [ZP] Flashlight -*-
-----------------------
This plugin is part of Zombie Plague Mod and is distributed under the
terms of the GNU General Public License. Check ZP_ReadMe.txt for details.
================================================================================*/
#include <amxmodx>
#include <engine>
#include <hamsandwich>
#include <cs_ham_bots_api>
#include <zp50_core>
// Constants
const FLASHLIGHT_IMPULSE = 100
const MAXPLAYERS = 32
// Bitsums
#define GetPlayerBit(%1,%2) (%1 & (1 << (%2 & 31)))
#define GetBoolPlayerBit(%1,%2) (GetPlayerBit(%1,%2) ? true : false)
#define SetPlayerBit(%1,%2) %1 |= (1 << (%2 & 31))
#define RemovePlayerBit(%1,%2) %1 &= ~(1 << (%2 & 31))
// Sound
new const FLASHLIGHT_SOUND[] = "items/flashlight1.wav"
// Vars
new fFlashLightActive, Float:fFlashLightTime[MAXPLAYERS+1], fFlashLightBattery[MAXPLAYERS+1]
new fMsgFlashLight, fMsgFlashBat
new fMaxPlayers
// Pcvars
new cvar_flashlight_starting_charge
new cvar_flashlight_custom, cvar_flashlight_radius
new cvar_flashlight_distance, cvar_flashlight_show_all
new cvar_flashlight_drain_rate, cvar_flashlight_charge_rate
new cvar_flashlight_color_R, cvar_flashlight_color_G, cvar_flashlight_color_B
public plugin_init()
{
register_plugin("[ZP] Flashlight", ZP_VERSION_STRING, "ZP Dev Team")
RegisterHam(Ham_Player_ImpulseCommands, "player", "Ham_ImpulseCommands_Pre")
RegisterHamBots(Ham_Player_ImpulseCommands, "Ham_ImpulseCommands_Pre")
RegisterHam(Ham_Killed, "player", "Ham_PlayerKilled_Pre")
RegisterHamBots(Ham_Killed, "Ham_PlayerKilled_Pre")
fMsgFlashLight = get_user_msgid("Flashlight")
fMsgFlashBat = get_user_msgid("FlashBat")
register_event("HLTV", "Event_NewRound", "a", "1=0", "2=0")
fMaxPlayers = get_maxplayers()
cvar_flashlight_starting_charge = register_cvar("zp_flashlight_starting_charge", "100")
cvar_flashlight_custom = register_cvar("zp_flashlight_custom", "0")
cvar_flashlight_radius = register_cvar("zp_flashlight_radius", "10")
cvar_flashlight_distance = register_cvar("zp_flashlight_distance", "1000")
cvar_flashlight_show_all = register_cvar("zp_flashlight_show_all", "1")
cvar_flashlight_drain_rate = register_cvar("zp_flashlight_drain_rate", "1")
cvar_flashlight_charge_rate = register_cvar("zp_flashlight_charge_rate", "5")
cvar_flashlight_color_R = register_cvar("zp_flashlight_color_R", "100")
cvar_flashlight_color_G = register_cvar("zp_flashlight_color_G", "100")
cvar_flashlight_color_B = register_cvar("zp_flashlight_color_B", "100")
}
public plugin_precache()
precache_sound(FLASHLIGHT_SOUND)
public plugin_cfg()
server_cmd("mp_flashlight 1")
public client_putinserver(this)
ResetFlashLight(this)
public Ham_PlayerKilled_Pre(this)
ResetFlashLight(this)
public Event_NewRound()
for(new fIndex = 1; fIndex <= fMaxPlayers; fIndex++)
ResetFlashLight(fIndex)
public zp_fw_core_infect_post(this)
ResetFlashLight(this)
public zp_fw_core_cure_post(this)
ResetFlashLight(this)
public Ham_ImpulseCommands_Pre(this)
{
if(!is_user_alive(this))
return;
new fImpulse = entity_get_int(this, EV_INT_impulse)
// Not flashlight impulse
if(fImpulse != FLASHLIGHT_IMPULSE)
return;
// Flashlight turned on
if(entity_get_int(this, EV_INT_effects) & EF_DIMLIGHT)
return;
// Block flashlight for zombies
if(zp_core_is_zombie(this))
{
entity_set_int(this, EV_INT_impulse, 0)
return;
}
// Block default flashlight if pcvar is active
if(get_pcvar_num(cvar_flashlight_custom))
{
if(GetPlayerBit(fFlashLightActive, this))
TurnOffFlashLight(this)
else if(fFlashLightBattery[this])
TurnOnFlashLight(this)
entity_set_int(this, EV_INT_impulse, 0)
}
}
public client_PreThink(this)
{
// Disabled??
if(!get_pcvar_num(cvar_flashlight_custom))
return;
static Float:fGameTime
fGameTime = get_gametime()
if(get_pcvar_float(cvar_flashlight_drain_rate) && fFlashLightTime[this] && fFlashLightTime[this] <= fGameTime)
{
if(GetPlayerBit(fFlashLightActive, this))
{
// Automatic turn off if battery is over
if(!fFlashLightBattery[this])
{
TurnOffFlashLight(this)
return;
}
fFlashLightTime[this] = get_pcvar_float(cvar_flashlight_drain_rate) + fGameTime
fFlashLightBattery[this]--
}
else
{
if(fFlashLightBattery[this] >= get_pcvar_num(cvar_flashlight_starting_charge))
{
fFlashLightTime[this] = 0.0
return;
}
fFlashLightTime[this] = get_pcvar_float(cvar_flashlight_charge_rate) + fGameTime
fFlashLightBattery[this]++
}
message_begin(MSG_ONE_UNRELIABLE, fMsgFlashBat, _, this)
{
write_byte(fFlashLightBattery[this])
}
message_end()
}
// Some bugfixes
if(GetPlayerBit(fFlashLightActive, this))
if(get_pcvar_num(cvar_flashlight_custom))
CreateFlashLight(this)
else
RemovePlayerBit(fFlashLightActive, this)
}
// Create flashlight aura
CreateFlashLight(this)
{
static fOrigin[3], fAim[3], fDistance
// Get Player's origin
get_user_origin(this, fOrigin, 1)
get_user_origin(this, fAim, 3)
// Get Distance
fDistance = get_distance(fOrigin, fAim)
// Check distance
if(fDistance > get_pcvar_num(cvar_flashlight_distance))
return;
static fDecay, fAttn
fDecay = fDistance * 255 / get_pcvar_num(cvar_flashlight_distance)
fAttn = 256 + fDecay * 5
// Show flashlight to all players??
if(get_pcvar_num(cvar_flashlight_show_all))
message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
else
message_begin(MSG_ONE_UNRELIABLE, SVC_TEMPENTITY, _, this)
{
// Create flashlight aura
write_byte(TE_DLIGHT)
write_coord(fAim[0])
write_coord(fAim[1])
write_coord(fAim[2])
write_byte(get_pcvar_num(cvar_flashlight_radius))
write_byte((get_pcvar_num(cvar_flashlight_color_R)<<8) / fAttn)
write_byte((get_pcvar_num(cvar_flashlight_color_G)<<8) / fAttn)
write_byte((get_pcvar_num(cvar_flashlight_color_B)<<8) / fAttn)
write_byte(1)
write_byte(fDecay)
}
message_end()
}
// Reset flashlight
ResetFlashLight(this)
{
if(1 <= this <= fMaxPlayers)
{
// Check if flashlight custom is off
if(!get_pcvar_num(cvar_flashlight_custom))
{
if(entity_get_int(this, EV_INT_effects) & EF_DIMLIGHT)
entity_set_int(this, EV_INT_impulse, FLASHLIGHT_IMPULSE) // Turn off flashlight
else
entity_set_int(this, EV_INT_impulse, 0) // Clear any stored impulse
// Update Flashlight HUD
message_begin(MSG_ONE_UNRELIABLE, fMsgFlashLight, _, this)
{
write_byte(0) // Toggle
write_byte(get_pcvar_num(cvar_flashlight_starting_charge)) // Battery
}
message_end()
return;
}
fFlashLightBattery[this] = get_pcvar_num(cvar_flashlight_starting_charge)
if(!GetPlayerBit(fFlashLightActive, this))
return;
RemovePlayerBit(fFlashLightActive, this)
fFlashLightTime[this] = 0.0
UpdateFlashLightHUD(this)
}
}
TurnOnFlashLight(this)
{
// Emit Flashlight sound
emit_sound(this, CHAN_WEAPON, FLASHLIGHT_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
// Set Flashlight flag
SetPlayerBit(fFlashLightActive, this)
// Update Flashlight HUD
UpdateFlashLightHUD(this)
// Drain Flashlight
fFlashLightTime[this] = get_pcvar_float(cvar_flashlight_drain_rate) + get_gametime()
}
TurnOffFlashLight(this)
{
// Emit Flashlight sound
emit_sound(this, CHAN_WEAPON, FLASHLIGHT_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
// Remove Flashlight flag
RemovePlayerBit(fFlashLightActive, this)
// Update Flashlight HUD
UpdateFlashLightHUD(this)
// Charging Flashlight
fFlashLightTime[this] = get_pcvar_float(cvar_flashlight_charge_rate) + get_gametime()
}
// Update Flashlight HUD
UpdateFlashLightHUD(this)
{
message_begin(MSG_ONE_UNRELIABLE, fMsgFlashLight, _, this)
{
write_byte(GetBoolPlayerBit(fFlashLightActive, this)) // Toggle
write_byte(fFlashLightBattery[this]) // Battery
}
message_end()
}