Raised This Month: $ Target: $400
 0% 

Random Spawn Entitys


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Arje
Senior Member
Join Date: Apr 2020
Location: Córdoba, Argentina
Old 03-13-2022 , 18:39   Random Spawn Entitys
Reply With Quote #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(sConfigsDirsizeof sConfigsDir 1)
    
    
//Get mapname
    
static sMapName[32]
    
get_mapname(sMapNamesizeof sMapName 1)
    
    
//Format .cfg file directory
    
formatex(sFilesizeof sFile 1"%s/presents/%s_presents_origins.cfg"sConfigsDirsMapName)
    
    
//If file doesn't exist return
    
if ( !file_exists(sFile) ) return
    
    
//Some variables
    
static sFileOrigin[3][32], sFileAngles[3][32], iLineiLengthsBuffer[256]
    static 
sTemp1[128], sTemp2[128]
    static 
Float:fOrigin[3], Float:fAngles[3]
    
    
//Read file
    
while ( read_file(sFileiLine++, sBuffersizeof sBuffer 1iLength) )
    {
        if ( (
sBuffer[0] == ';') || !iLength ) continue
        
        
strtok(sBuffersTemp1sizeof sTemp1 1sTemp2sizeof sTemp2 1'|'0)
        
        
parse(sTemp1sFileOrigin[0], sizeof sFileOrigin[] - 1sFileOrigin[1], sizeof sFileOrigin[] - 1sFileOrigin[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(sTemp2sFileAngles[0], sizeof sFileAngles[] - 1sFileAngles[1], sizeof sFileAngles[] - 1sFileAngles[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_CreateNamedEntityengfunc(EngFunc_AllocString"info_target"))
    
    
//Set classname to "present"
    
set_pev(entpev_classname"present")
    
    
//Set entity origins
    
engfunc(EngFunc_SetOriginentorigin)
    
    
//Create blast effect
    
func_make_blast(origin)
    
    
//Emit spawn sound
    
engfunc(EngFunc_EmitSoundentCHAN_AUTOsound_respawn[random_num(0sizeof sound_respawn 1)], VOL_NORMATTN_NORM0PITCH_NORM)
            
    
//Set random player model
    
engfunc(EngFunc_SetModelentmodel_present[random_num(0sizeof model_present 1)])
    
    
//Spawn entity
    
dllfunc(DLLFunc_Spawnent)
    
//Make it solid
    
set_pev(entpev_solidSOLID_BBOX)
    
//Set entity size
    
engfunc(EngFunc_SetSizeent, {-2.0, -2.0, -4.0}, {2.02.04.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_timepcvar_blastpcvar_blast_colorpcvar_minxppcvar_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(0sizeof model_presentx++) {
        
engfunc(EngFunc_PrecacheModelmodel_present[x])
    }
    for (
0sizeof sound_respawnx++) {
        
engfunc(EngFunc_PrecacheSoundsound_respawn[x])
    }
    for (
0sizeof sound_pickx++) {
        
engfunc(EngFunc_PrecacheSoundsound_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(sConfigsDirsizeof sConfigsDir 1)
    
    
//Get mapname
    
static sMapName[32]
    
get_mapname(sMapNamesizeof sMapName 1)
    
    
//Format .cfg file directory
    
formatex(sFilesizeof sFile 1"%s/presents/%s_presents_origins.cfg"sConfigsDirsMapName)
    
    
//If file doesn't exist return
    
if ( !file_exists(sFile) ) return
    
    
//Some variables
    
static sFileOrigin[3][32], sFileAngles[3][32], iLineiLengthsBuffer[256]
    static 
sTemp1[128], sTemp2[128]
    static 
Float:fOrigin[3], Float:fAngles[3]
    
    
//Read file
    
while ( read_file(sFileiLine++, sBuffersizeof sBuffer 1iLength) )
    {
        if ( (
sBuffer[0] == ';') || !iLength ) continue
        
        
strtok(sBuffersTemp1sizeof sTemp1 1sTemp2sizeof sTemp2 1'|'0)
        
        
parse(sTemp1sFileOrigin[0], sizeof sFileOrigin[] - 1sFileOrigin[1], sizeof sFileOrigin[] - 1sFileOrigin[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(sTemp2sFileAngles[0], sizeof sFileAngles[] - 1sFileAngles[1], sizeof sFileAngles[] - 1sFileAngles[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(idADMIN_KICK) ) return
    
    
//Create some variables
    
new Float:fOrigin[3], origin[3], name[32], map[32]
    
    
//Get player origins
    
get_user_origin(idorigin3)
    
    
//Make float origins from integer origins
    
IVecFVec(originfOrigin)
    
    
//Check the player aiming
    
if ( (engfunc(EngFunc_PointContentsfOrigin) != CONTENTS_SKY) && (engfunc(EngFunc_PointContentsfOrigin) != CONTENTS_SOLID) )
    {
        
//Get his name and map name for log creating
        
get_user_name(idnamesizeof name 1)
        
        
get_mapname(mapsizeof map 1)
        
        
//Create log file or log admin command
        
log_to_file("presents.log""[%s] has created present on map %s"namemap)
        
        
//Finally spawn present
        
func_spawn(fOrigin)
        
        
//Print success and save info information
        
client_print(idprint_chat"%L"LANG_PLAYER"SUCC_ADD"origin[0], origin[1], origin[2])
        
client_print(idprint_chat"%L"LANG_PLAYER"SAVE_INFO")
    }
    else
    {
        
//That location is unavaiables, so print information
        
client_print(idprint_chat"%L"LANG_PLAYER"LOCATION_UN")
    }    
}
//----------------------------------------------------------------------------------------------
public func_remove_present(id)
{
    
//Check command access
    
if ( !access(idADMIN_KICK) ) return
    
    
//Create some variables
    
static entbodyname[32], map[32]
    
    
//Check player aiming
    
get_user_aiming(identbody)
    
    
//Check ent validity
    
if ( pev_valid(ent) )
    {
        
//Check entity classname
        
static classname[32]
        
pev(entpev_classnameclassnamesizeof classname 1)
        
        
//Player is aiming at present
        
if ( !strcmp(classname"present"1) )
        {
            
//Get user name and map name for log creating
            
get_user_name(idnamesizeof name 1)
            
get_mapname(mapsizeof map 1)
            
            
//Create log file or log admin command
            
log_to_file("presents.log""[%s] has removed present from map %s"namemap)
            
            
//Finally remove the entity
            
engfunc(EngFunc_RemoveEntityent)
            
            
//Print success inforamtion
            
client_print(idprint_chat"%L"LANG_PLAYER"SUCC_REMOVE")
        }
        else
        {
            
//Player must aim at present
            
client_print(idprint_chat"%L"LANG_PLAYER"PRESENT_AIM")
        }
    }
}
//----------------------------------------------------------------------------------------------
public func_remove_present_all(id)
{
    
//Check command access
    
if ( !access(idADMIN_KICK) ) return
    
    
//Create some variables
    
new ent = -1countname[32], map[32]
    
count 
    
    
//Find presents
    
while ( (ent engfunc(EngFunc_FindEntityByStringent"classname""present")) )
    {
        
//Increase count
        
count++
        
//Remove presents
        
engfunc(EngFunc_RemoveEntityent)
    }
    
//Print information
    
client_print(idprint_chat"%L"LANG_PLAYER"REMOVE_ALL"count)
    
    
//Get player name and map name
    
get_user_name(idnamesizeof name 1)
    
get_mapname(mapsizeof map 1)
    
    
//Log command to file
    
log_to_file("presents.log""[%s] has removed all presents from map %s"namemap)
    
    
//Print save information
    
client_print(idprint_chat"%L"LANG_PLAYER"SAVE_INFO")
}
//----------------------------------------------------------------------------------------------
public func_save_origins(id)
{
    
//Check command access
    
if ( !access(idADMIN_KICK) ) return

    
//Create some variables
    
static sConfigsDir[64], sFile[128], name[32], map[32]

    
//Get config folder directory
    
get_configsdir(sConfigsDirsizeof sConfigsDir 1)

    
//Get map name
    
static sMapName[32]
    
get_mapname(sMapNamesizeof sMapName 1)
    
    
//Format .cfg file directory
    
formatex(sFilesizeof sFile 1"%s/presents/%s_presents_origins.cfg"sConfigsDirsMapName)
    
    
//If file already exist, delete file
    
if ( file_exists(sFile) ) {
        
delete_file(sFile)
    }

    
//Some variables
    
new iEnt = -1Float:fEntOrigin[3], Float:fEntAngles[3], iCount
    
static sBuffer[256]
    
    
//Find presents on this map
    
while ( (iEnt engfunc(EngFunc_FindEntityByStringiEnt"classname""present")) )
    {
        
//Get origins and angles
        
pev(iEntpev_originfEntOrigin)
        
pev(iEntpev_anglesfEntAngles)
        
        
formatex(sBuffersizeof sBuffer 1"%f %f %f | %f %f %f"fEntOrigin[0], fEntOrigin[1], fEntOrigin[2], fEntAngles[0], fEntAngles[1], fEntAngles[2])
    
        
//Create file
        
write_file(sFilesBuffer, -1)
        
        
//Increase count variable
        
iCount++
    }
    
//Get user name and map name
    
get_user_name(idnamesizeof name 1)
    
get_mapname(mapsizeof map 1)
    
    
//Log admin command
    
log_to_file("presents.log""[%s] has saved presents on map %s"namemap)
    
    
//Print success information
    
client_print(idprint_chat"%L"LANG_PLAYER"SUCC_SAVE"iCountsMapName)
}
//----------------------------------------------------------------------------------------------
public func_rotate_present(id)
{
    
//Check command access
    
if ( !access(idADMIN_KICK) ) return
        
    
//Some variables
    
static entbodyname[32], map[32]
    
    
//Get user aiming
    
get_user_aiming(identbody)
    
    
//Check entity validity
    
if ( pev_valid(ent) )
    {
        
//Check classname
        
static sClassname[32]
        
pev(entpev_classnamesClassnamesizeof sClassname 1)
        
        
//Player is aiming at present
        
if ( !strcmp(sClassname"present"1) )
        {
            
//Get angles
            
static Float:fAngles[3]
            
pev(entpev_anglesfAngles)
            
            
//Rotate present
            
fAngles[1] += 90.0
            set_pev
(entpev_anglesfAngles)
            
            
//Get user name and map name
            
get_user_name(idnamesizeof name 1)
            
get_mapname(mapsizeof map 1)
            
            
//Log admin command
            
log_to_file("presents.log""[%s] has rotated present on map %s"namemap)
            
            
//Print success information
            
client_print(idprint_chat"%L"LANG_PLAYER"SUCC_ROTATE")
        }
        else
        {
            
//Print failure information
            
client_print(idprint_chat"%L"LANG_PLAYER"PRESENT_AIM")
        }
    }
}
//----------------------------------------------------------------------------------------------
public func_spawn(Float:origin[3])
{
    
//Create new entity    
    
new ent engfunc(EngFunc_CreateNamedEntityengfunc(EngFunc_AllocString"info_target"))
    
    
//Set classname to "present"
    
set_pev(entpev_classname"present")
    
    
//Set entity origins
    
engfunc(EngFunc_SetOriginentorigin)
    
    
//Create blast effect
    
func_make_blast(origin)
    
    
//Emit spawn sound
    
engfunc(EngFunc_EmitSoundentCHAN_AUTOsound_respawn[random_num(0sizeof sound_respawn 1)], VOL_NORMATTN_NORM0PITCH_NORM)
            
    
//Set random player model
    
engfunc(EngFunc_SetModelentmodel_present[random_num(0sizeof model_present 1)])
    
    
//Spawn entity
    
dllfunc(DLLFunc_Spawnent)
    
//Make it solid
    
set_pev(entpev_solidSOLID_BBOX)
    
//Set entity size
    
engfunc(EngFunc_SetSizeent, {-2.0, -2.0, -4.0}, {2.02.04.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(fOriginorigin)
    
    
//Get blast color
    
new Float:rgbF[3], rgb[3]
    
func_get_rgb(rgbF)
    
FVecIVec(rgbFrgb)
    
    
//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_colorcolor11)
    
    
parse(colorparts[0], 3parts[1], 3parts[2], 3)
    
rgb[0] = floatstr(parts[0])
    
rgb[1] = floatstr(parts[1])
    
rgb[2] = floatstr(parts[2])
}
//----------------------------------------------------------------------------------------------
public forward_touch(entid)
{
    
//Check entity validity
    
if ( !pev_valid(ent) ) return FMRES_IGNORED

    
//Create classname variable
    
static class[20]

    
//Get class
    
pev(entpev_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(entpev_solidSOLID_NOT)
    
//Don't draw that present anymore (thanks connor)
    
set_pev(entpev_effectsEF_NODRAW)
    
//Set respawn time
    
set_pev(entpev_nextthinkget_gametime() + get_pcvar_float(pcvar_respawn_time))
    
    
//Emit pick sound
    
engfunc(EngFunc_EmitSoundentCHAN_ITEMsound_pick[random_num(0sizeof sound_pick 1)], VOL_NORMATTN_NORM0PITCH_NORM)
    
    new 
random_xp random_num(get_pcvar_num(pcvar_minxp), get_pcvar_num(pcvar_maxxp))

    if ( 
random_xp <= ) return FMRES_IGNORED

    
new name[32]
    
get_user_name(idnamecharsmax(name))

    
sh_set_user_xp(idrandom_xptrue)
    
sh_chat_message(0, -1"%s got %d XP for picking up a present"namerandom_xp)

    return 
FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------
public forward_think(ent)
{
    
//Create class variable
    
new class[20]
    
    
//Get entity class
    
pev(entpev_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(entpev_effects) & EF_NODRAW)
    {
        
//Create origin variable
        
new Float:origin[3]
        
        
//Get origins
        
pev(entpev_originorigin)
        
        
//Emit random respawn sound
        
engfunc(EngFunc_EmitSoundentCHAN_AUTOsound_respawn[random_num(0sizeof sound_respawn 1)], VOL_NORMATTN_NORM0PITCH_NORM)
        
        
//Make nice blast (from frostnades by Avalanche)
        
func_make_blast(origin)
        
        
//Make present solid
        
set_pev(entpev_solidSOLID_BBOX)
        
        
//Draw present
        
set_pev(entpev_effectspev(ent,pev_effects)  & ~EF_NODRAW)
    }
    
    return 
FMRES_IGNORED
}
//---------------------------------------------------------------------------------------------- 
Arje is offline
Dyaus
Member
Join Date: Aug 2021
Old 03-13-2022 , 20:33   Re: Random Spawn Entitys
Reply With Quote #2

can i have related files : models/sounds/confings... ? i have some ideas but i want to test them first since i don't have much experience on this , if an idea works i'll post it here
Dyaus is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:09.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode