Senior Member
Join Date: Apr 2020
Location: Córdoba, Argentina
|
![Old](images/statusicon/post_old.svg)
03-13-2022
, 18:39
Random Spawn Entitys
|
#1
|
Hi, well i use this code to spawn "presents" entity in the origins that I previously saved, so when the "presents" entities spawn they all spawn in all the locations I save, I'd like it to be limited to just 3 randomly in the previously saved locations.
I think it is something of these functions that you have to see but I don't know how to code it:
PHP Code:
//----------------------------------------------------------------------------------------------
public plugin_cfg()
{
//Create some variables
static sConfigsDir[64], sFile[128]
//Get config folder directory
get_configsdir(sConfigsDir, sizeof sConfigsDir - 1)
//Get mapname
static sMapName[32]
get_mapname(sMapName, sizeof sMapName - 1)
//Format .cfg file directory
formatex(sFile, sizeof sFile - 1, "%s/presents/%s_presents_origins.cfg", sConfigsDir, sMapName)
//If file doesn't exist return
if ( !file_exists(sFile) ) return
//Some variables
static sFileOrigin[3][32], sFileAngles[3][32], iLine, iLength, sBuffer[256]
static sTemp1[128], sTemp2[128]
static Float:fOrigin[3], Float:fAngles[3]
//Read file
while ( read_file(sFile, iLine++, sBuffer, sizeof sBuffer - 1, iLength) )
{
if ( (sBuffer[0] == ';') || !iLength ) continue
strtok(sBuffer, sTemp1, sizeof sTemp1 - 1, sTemp2, sizeof sTemp2 - 1, '|', 0)
parse(sTemp1, sFileOrigin[0], sizeof sFileOrigin[] - 1, sFileOrigin[1], sizeof sFileOrigin[] - 1, sFileOrigin[2], sizeof sFileOrigin[] - 1)
fOrigin[0] = str_to_float(sFileOrigin[0])
fOrigin[1] = str_to_float(sFileOrigin[1])
fOrigin[2] = str_to_float(sFileOrigin[2])
parse(sTemp2, sFileAngles[0], sizeof sFileAngles[] - 1, sFileAngles[1], sizeof sFileAngles[] - 1, sFileAngles[2], sizeof sFileAngles[] - 1)
fAngles[0] = str_to_float(sFileAngles[0])
fAngles[1] = str_to_float(sFileAngles[1])
fAngles[2] = str_to_float(sFileAngles[2])
//Spawn presents on origins saved in .cfg file
func_spawn(fOrigin)
}
}
//----------------------------------------------------------------------------------------------
public func_spawn(Float:origin[3])
{
//Create new entity
new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
//Set classname to "present"
set_pev(ent, pev_classname, "present")
//Set entity origins
engfunc(EngFunc_SetOrigin, ent, origin)
//Create blast effect
func_make_blast(origin)
//Emit spawn sound
engfunc(EngFunc_EmitSound, ent, CHAN_AUTO, sound_respawn[random_num(0, sizeof sound_respawn - 1)], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
//Set random player model
engfunc(EngFunc_SetModel, ent, model_present[random_num(0, sizeof model_present - 1)])
//Spawn entity
dllfunc(DLLFunc_Spawn, ent)
//Make it solid
set_pev(ent, pev_solid, SOLID_BBOX)
//Set entity size
engfunc(EngFunc_SetSize, ent, {-2.0, -2.0, -4.0}, {2.0, 2.0, 4.0})
}
but in case it is not there I leave the complete code
PHP Code:
// SUPERHERO XP Present
/* CVARS - copy and paste to shconfig.cfg
//XP Present
present_respawn_time 60.0 //Time between present disapear and respawn
present_blast 1 //Turns blast on/off
present_blast_color 255 255 255 //Color of the blast (default 255 255 255)
//XP earned is a random value between these two.
present_minxp 50 //Min value
present_maxxp 100 //Max value
*/
/*
Ripped from Pick up present v2.01
made by FakeNick
http://forums.alliedmods.net/showthread.php?t=82285&highlight=Present
*/
#include <superheromod>
#include <amxmisc>
//---------- Plugin Customization --------//
// Models and sounds are randomly chosen, add as many, as you want
new const model_present[][] = {
"models/present/w_present.mdl",
"models/present/w_present2.mdl"
}
new const sound_respawn[][] = {
"present/respawn.wav",
"present/respawn2.wav"
}
new const sound_pick[][] = {
"present/pick.wav"
}
//------- Do not edit below this point ------//
new gSpriteExplo
new pcvar_respawn_time, pcvar_blast, pcvar_blast_color, pcvar_minxp, pcvar_maxxp
//----------------------------------------------------------------------------------------------
public plugin_init()
{
// Plugin Info
register_plugin("SUPERHERO XP Present", "1.0", "FakeNick / Fr33m@n")
// Make sure sh is active
if ( !sh_is_active() ) return
// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
pcvar_respawn_time = register_cvar("present_respawn_time", "60.0")
pcvar_blast = register_cvar("present_blast", "1")
pcvar_blast_color = register_cvar("present_blast_color", "255 255 255")
pcvar_minxp = register_cvar("present_minxp", "50")
pcvar_maxxp = register_cvar("present_maxxp", "100")
// Register admin commands
register_clcmd("say !add", "func_add_present")
register_clcmd("say !remove", "func_remove_present")
register_clcmd("say !removeall", "func_remove_present_all")
register_clcmd("say !save", "func_save_origins")
register_clcmd("say !rotate", "func_rotate_present")
// Forwards
register_forward(FM_Touch, "forward_touch")
register_forward(FM_Think, "forward_think")
// Register dictionary
register_dictionary("present.txt")
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
new x
for(x = 0; x < sizeof model_present; x++) {
engfunc(EngFunc_PrecacheModel, model_present[x])
}
for (x = 0; x < sizeof sound_respawn; x++) {
engfunc(EngFunc_PrecacheSound, sound_respawn[x])
}
for (x = 0; x < sizeof sound_pick; x++) {
engfunc(EngFunc_PrecacheSound, sound_pick[x])
}
gSpriteExplo = engfunc(EngFunc_PrecacheModel, "sprites/shockwave.spr")
}
//----------------------------------------------------------------------------------------------
public plugin_cfg()
{
//Create some variables
static sConfigsDir[64], sFile[128]
//Get config folder directory
get_configsdir(sConfigsDir, sizeof sConfigsDir - 1)
//Get mapname
static sMapName[32]
get_mapname(sMapName, sizeof sMapName - 1)
//Format .cfg file directory
formatex(sFile, sizeof sFile - 1, "%s/presents/%s_presents_origins.cfg", sConfigsDir, sMapName)
//If file doesn't exist return
if ( !file_exists(sFile) ) return
//Some variables
static sFileOrigin[3][32], sFileAngles[3][32], iLine, iLength, sBuffer[256]
static sTemp1[128], sTemp2[128]
static Float:fOrigin[3], Float:fAngles[3]
//Read file
while ( read_file(sFile, iLine++, sBuffer, sizeof sBuffer - 1, iLength) )
{
if ( (sBuffer[0] == ';') || !iLength ) continue
strtok(sBuffer, sTemp1, sizeof sTemp1 - 1, sTemp2, sizeof sTemp2 - 1, '|', 0)
parse(sTemp1, sFileOrigin[0], sizeof sFileOrigin[] - 1, sFileOrigin[1], sizeof sFileOrigin[] - 1, sFileOrigin[2], sizeof sFileOrigin[] - 1)
fOrigin[0] = str_to_float(sFileOrigin[0])
fOrigin[1] = str_to_float(sFileOrigin[1])
fOrigin[2] = str_to_float(sFileOrigin[2])
parse(sTemp2, sFileAngles[0], sizeof sFileAngles[] - 1, sFileAngles[1], sizeof sFileAngles[] - 1, sFileAngles[2], sizeof sFileAngles[] - 1)
fAngles[0] = str_to_float(sFileAngles[0])
fAngles[1] = str_to_float(sFileAngles[1])
fAngles[2] = str_to_float(sFileAngles[2])
//Spawn presents on origins saved in .cfg file
func_spawn(fOrigin)
}
}
//----------------------------------------------------------------------------------------------
public func_add_present(id)
{
//Check command access
if ( !access(id, ADMIN_KICK) ) return
//Create some variables
new Float:fOrigin[3], origin[3], name[32], map[32]
//Get player origins
get_user_origin(id, origin, 3)
//Make float origins from integer origins
IVecFVec(origin, fOrigin)
//Check the player aiming
if ( (engfunc(EngFunc_PointContents, fOrigin) != CONTENTS_SKY) && (engfunc(EngFunc_PointContents, fOrigin) != CONTENTS_SOLID) )
{
//Get his name and map name for log creating
get_user_name(id, name, sizeof name - 1)
get_mapname(map, sizeof map - 1)
//Create log file or log admin command
log_to_file("presents.log", "[%s] has created present on map %s", name, map)
//Finally spawn present
func_spawn(fOrigin)
//Print success and save info information
client_print(id, print_chat, "%L", LANG_PLAYER, "SUCC_ADD", origin[0], origin[1], origin[2])
client_print(id, print_chat, "%L", LANG_PLAYER, "SAVE_INFO")
}
else
{
//That location is unavaiables, so print information
client_print(id, print_chat, "%L", LANG_PLAYER, "LOCATION_UN")
}
}
//----------------------------------------------------------------------------------------------
public func_remove_present(id)
{
//Check command access
if ( !access(id, ADMIN_KICK) ) return
//Create some variables
static ent, body, name[32], map[32]
//Check player aiming
get_user_aiming(id, ent, body)
//Check ent validity
if ( pev_valid(ent) )
{
//Check entity classname
static classname[32]
pev(ent, pev_classname, classname, sizeof classname - 1)
//Player is aiming at present
if ( !strcmp(classname, "present", 1) )
{
//Get user name and map name for log creating
get_user_name(id, name, sizeof name - 1)
get_mapname(map, sizeof map - 1)
//Create log file or log admin command
log_to_file("presents.log", "[%s] has removed present from map %s", name, map)
//Finally remove the entity
engfunc(EngFunc_RemoveEntity, ent)
//Print success inforamtion
client_print(id, print_chat, "%L", LANG_PLAYER, "SUCC_REMOVE")
}
else
{
//Player must aim at present
client_print(id, print_chat, "%L", LANG_PLAYER, "PRESENT_AIM")
}
}
}
//----------------------------------------------------------------------------------------------
public func_remove_present_all(id)
{
//Check command access
if ( !access(id, ADMIN_KICK) ) return
//Create some variables
new ent = -1, count, name[32], map[32]
count = 0
//Find presents
while ( (ent = engfunc(EngFunc_FindEntityByString, ent, "classname", "present")) )
{
//Increase count
count++
//Remove presents
engfunc(EngFunc_RemoveEntity, ent)
}
//Print information
client_print(id, print_chat, "%L", LANG_PLAYER, "REMOVE_ALL", count)
//Get player name and map name
get_user_name(id, name, sizeof name - 1)
get_mapname(map, sizeof map - 1)
//Log command to file
log_to_file("presents.log", "[%s] has removed all presents from map %s", name, map)
//Print save information
client_print(id, print_chat, "%L", LANG_PLAYER, "SAVE_INFO")
}
//----------------------------------------------------------------------------------------------
public func_save_origins(id)
{
//Check command access
if ( !access(id, ADMIN_KICK) ) return
//Create some variables
static sConfigsDir[64], sFile[128], name[32], map[32]
//Get config folder directory
get_configsdir(sConfigsDir, sizeof sConfigsDir - 1)
//Get map name
static sMapName[32]
get_mapname(sMapName, sizeof sMapName - 1)
//Format .cfg file directory
formatex(sFile, sizeof sFile - 1, "%s/presents/%s_presents_origins.cfg", sConfigsDir, sMapName)
//If file already exist, delete file
if ( file_exists(sFile) ) {
delete_file(sFile)
}
//Some variables
new iEnt = -1, Float:fEntOrigin[3], Float:fEntAngles[3], iCount
static sBuffer[256]
//Find presents on this map
while ( (iEnt = engfunc(EngFunc_FindEntityByString, iEnt, "classname", "present")) )
{
//Get origins and angles
pev(iEnt, pev_origin, fEntOrigin)
pev(iEnt, pev_angles, fEntAngles)
formatex(sBuffer, sizeof sBuffer - 1, "%f %f %f | %f %f %f", fEntOrigin[0], fEntOrigin[1], fEntOrigin[2], fEntAngles[0], fEntAngles[1], fEntAngles[2])
//Create file
write_file(sFile, sBuffer, -1)
//Increase count variable
iCount++
}
//Get user name and map name
get_user_name(id, name, sizeof name - 1)
get_mapname(map, sizeof map - 1)
//Log admin command
log_to_file("presents.log", "[%s] has saved presents on map %s", name, map)
//Print success information
client_print(id, print_chat, "%L", LANG_PLAYER, "SUCC_SAVE", iCount, sMapName)
}
//----------------------------------------------------------------------------------------------
public func_rotate_present(id)
{
//Check command access
if ( !access(id, ADMIN_KICK) ) return
//Some variables
static ent, body, name[32], map[32]
//Get user aiming
get_user_aiming(id, ent, body)
//Check entity validity
if ( pev_valid(ent) )
{
//Check classname
static sClassname[32]
pev(ent, pev_classname, sClassname, sizeof sClassname - 1)
//Player is aiming at present
if ( !strcmp(sClassname, "present", 1) )
{
//Get angles
static Float:fAngles[3]
pev(ent, pev_angles, fAngles)
//Rotate present
fAngles[1] += 90.0
set_pev(ent, pev_angles, fAngles)
//Get user name and map name
get_user_name(id, name, sizeof name - 1)
get_mapname(map, sizeof map - 1)
//Log admin command
log_to_file("presents.log", "[%s] has rotated present on map %s", name, map)
//Print success information
client_print(id, print_chat, "%L", LANG_PLAYER, "SUCC_ROTATE")
}
else
{
//Print failure information
client_print(id, print_chat, "%L", LANG_PLAYER, "PRESENT_AIM")
}
}
}
//----------------------------------------------------------------------------------------------
public func_spawn(Float:origin[3])
{
//Create new entity
new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
//Set classname to "present"
set_pev(ent, pev_classname, "present")
//Set entity origins
engfunc(EngFunc_SetOrigin, ent, origin)
//Create blast effect
func_make_blast(origin)
//Emit spawn sound
engfunc(EngFunc_EmitSound, ent, CHAN_AUTO, sound_respawn[random_num(0, sizeof sound_respawn - 1)], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
//Set random player model
engfunc(EngFunc_SetModel, ent, model_present[random_num(0, sizeof model_present - 1)])
//Spawn entity
dllfunc(DLLFunc_Spawn, ent)
//Make it solid
set_pev(ent, pev_solid, SOLID_BBOX)
//Set entity size
engfunc(EngFunc_SetSize, ent, {-2.0, -2.0, -4.0}, {2.0, 2.0, 4.0})
}
//----------------------------------------------------------------------------------------------
public func_make_blast(Float:fOrigin[3])
{
if ( !get_pcvar_num(pcvar_blast) ) return
//Create origin variable
new origin[3]
//Make float origins from integer origins
FVecIVec(fOrigin, origin)
//Get blast color
new Float:rgbF[3], rgb[3]
func_get_rgb(rgbF)
FVecIVec(rgbF, rgb)
//Finally create blast
//smallest ring
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(TE_BEAMCYLINDER) // 21
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2])
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2] + 385)
write_short(gSpriteExplo)
write_byte(0) // startframe
write_byte(0) // framerate
write_byte(4) // life
write_byte(60) // width
write_byte(0) // noise
write_byte(rgb[0]) // r
write_byte(rgb[1]) // g
write_byte(rgb[2]) // b
write_byte(100) // brightness
write_byte(0) // speed
message_end()
// medium ring
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(TE_BEAMCYLINDER) // 21
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2])
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2] + 470)
write_short(gSpriteExplo)
write_byte(0) // startframe
write_byte(0) // framerate
write_byte(4) // life
write_byte(60) // width
write_byte(0) // noise
write_byte(rgb[0]) // r
write_byte(rgb[1]) // g
write_byte(rgb[2]) // b
write_byte(100) // brightness
write_byte(0) // speed
message_end()
// largest ring
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(TE_BEAMCYLINDER) // 21
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2])
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2] + 555)
write_short(gSpriteExplo)
write_byte(0) // startframe
write_byte(0) // framerate
write_byte(4) // life
write_byte(60) // width
write_byte(0) // noise
write_byte(rgb[0]) // r
write_byte(rgb[1]) // g
write_byte(rgb[2]) // b
write_byte(100) // brightness
write_byte(0) // speed
message_end()
//Create nice light effect
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
write_byte(TE_DLIGHT) //27
write_coord(origin[0])
write_coord(origin[1])
write_coord(origin[2])
write_byte(floatround(240.0 / 5.0)) // radius
write_byte(rgb[0]) // r
write_byte(rgb[1]) // g
write_byte(rgb[2]) // b
write_byte(8) // life
write_byte(60) // decay
message_end()
}
//----------------------------------------------------------------------------------------------
public func_get_rgb(Float:rgb[3])
{
static color[12], parts[3][4]
get_pcvar_string(pcvar_blast_color, color, 11)
parse(color, parts[0], 3, parts[1], 3, parts[2], 3)
rgb[0] = floatstr(parts[0])
rgb[1] = floatstr(parts[1])
rgb[2] = floatstr(parts[2])
}
//----------------------------------------------------------------------------------------------
public forward_touch(ent, id)
{
//Check entity validity
if ( !pev_valid(ent) ) return FMRES_IGNORED
//Create classname variable
static class[20]
//Get class
pev(ent, pev_classname, class, sizeof class - 1)
//Check classname
if ( !equali(class, "present") ) return FMRES_IGNORED
//Make sure that toucher is alive
if ( !is_user_alive(id) ) return FMRES_IGNORED
//Make present not solid
set_pev(ent, pev_solid, SOLID_NOT)
//Don't draw that present anymore (thanks connor)
set_pev(ent, pev_effects, EF_NODRAW)
//Set respawn time
set_pev(ent, pev_nextthink, get_gametime() + get_pcvar_float(pcvar_respawn_time))
//Emit pick sound
engfunc(EngFunc_EmitSound, ent, CHAN_ITEM, sound_pick[random_num(0, sizeof sound_pick - 1)], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
new random_xp = random_num(get_pcvar_num(pcvar_minxp), get_pcvar_num(pcvar_maxxp))
if ( random_xp <= 0 ) return FMRES_IGNORED
new name[32]
get_user_name(id, name, charsmax(name))
sh_set_user_xp(id, random_xp, true)
sh_chat_message(0, -1, "%s got %d XP for picking up a present", name, random_xp)
return FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------
public forward_think(ent)
{
//Create class variable
new class[20]
//Get entity class
pev(ent, pev_classname, class, sizeof class - 1)
//Check entity class
if ( !equali(class,"present") ) return FMRES_IGNORED
//If that present isn't drawed, time to respawn it
if (pev(ent, pev_effects) & EF_NODRAW)
{
//Create origin variable
new Float:origin[3]
//Get origins
pev(ent, pev_origin, origin)
//Emit random respawn sound
engfunc(EngFunc_EmitSound, ent, CHAN_AUTO, sound_respawn[random_num(0, sizeof sound_respawn - 1)], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
//Make nice blast (from frostnades by Avalanche)
func_make_blast(origin)
//Make present solid
set_pev(ent, pev_solid, SOLID_BBOX)
//Draw present
set_pev(ent, pev_effects, pev(ent,pev_effects) & ~EF_NODRAW)
}
return FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------
|
|