I want to make random bomsite in every round when cvar bs_on is 1 and when admin turn it to 0 so everything back to normal.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <engine>
#define PLUGIN "RANDOM BOMSITE"
#define VERSION "1.0"
#define AUTHOR "VINAGHOST"
new bomsite = 0
new bomsite_id = 0
new g_szCfgFile[128];
#define MIN_SIZE 180.0
#define ENTITY_CLASS "func_bomb_target"
#define TASK_RADAR 1
#define TASK_BOMBONRADAR_TIME 3.0
#define TASK_LOOK 2
new Array:g_BS;
new Array:g_BSmaxs;
new Array:g_BSmins;
new Array:g_BSorigin;
new m_fakeHostage, m_fakeHostageDie
new gbPlanted=false;
new bool:d_bs
new bs_on
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("HLTV", "newround", "a", "1=0", "2=0")
register_event("BombDrop","bomb_planted","a","4=1");
bs_on = register_cvar("bs_on", "0")
register_clcmd("say /creat","rb_creat", ADMIN_BAN);
register_clcmd("say /save","rb_save", ADMIN_BAN);
m_fakeHostage = get_user_msgid("HostagePos");
m_fakeHostageDie = get_user_msgid("HostageK");
}
public plugin_cfg(){
get_configsdir(g_szCfgFile, 75);
new szMapName[32];
get_mapname(szMapName, 31);
format(g_szCfgFile, 127, "%s/randomBS/%s.ini", g_szCfgFile, szMapName);
g_BS = ArrayCreate(1, 2);
g_BSmaxs = ArrayCreate(3, 2);
g_BSmins = ArrayCreate(3, 2);
g_BSorigin = ArrayCreate(3, 2);
//BombSites
if(file_exists(g_szCfgFile)){
LoadFromFile();
}
else{
set_task(1.0, "task_Look4BS", TASK_LOOK);
}
d_bs = true
}
public task_Look4BS(){
new ent = -1;
new Float:fMax[3], Float:fMin[3], Float:fOrigin[3];
new Float:fMinS[3], Float:fMaxS[3];
do{
ent = find_ent_by_class ( ent, ENTITY_CLASS);
if(is_valid_ent ( ent )){
entity_get_vector(ent, EV_VEC_absmin, fMin);
entity_get_vector(ent, EV_VEC_absmax, fMax);
get_brush_entity_origin(ent, fOrigin);
engine_get_size(fMax, fOrigin, fMinS, fMaxS);
register_bombsite(ent, fMinS, fMaxS, fOrigin);
}
}
while(ent);
}
public newround()
{
if (get_pcvar_num(bs_on) == 1)
{
//random bomsite
static old
old = bomsite
bomsite = random_num(2, ArraySize(g_BS)-1)
if( old != bomsite)
{
bomsite_id = ArrayGetCell(g_BS, bomsite)
DispatchSpawn(bomsite_id);
}
gbPlanted=false;
client_print(0, print_chat, "id = %d", bomsite)
for (new i = 0; i < 2; i++)
remove_entity(ArrayGetCell(g_BS, i))
if (!task_exists(TASK_RADAR))
{
set_task(TASK_BOMBONRADAR_TIME, "task_UpDateRadar",TASK_RADAR,_,_,"b");
client_print(0, print_chat, "id = %d is shown in the radar", bomsite)
}
d_bs = false
}
else
{
if ( !d_bs)
{
//bring back bomsite ( I use only map de_dust2, de_dust)
for (new i = 0; i < 2; i++)
DispatchSpawn(ArrayGetCell(g_BS, i));
remove_entity(ArrayGetCell(g_BS, bomsite))
d_bs = true
if (task_exists(TASK_RADAR))
remove_task(TASK_RADAR)
client_print(0, print_chat,"Bomsite back to normal")
}
}
}
public rb_creat(id, level, cid){
if(!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED;
new Float:fMin[3]={-MIN_SIZE, -MIN_SIZE, -MIN_SIZE};
new Float:fMax[3]={MIN_SIZE, MIN_SIZE, MIN_SIZE};
new Float:fOrigin[3];
entity_get_vector(id, EV_VEC_origin, fOrigin);
new ent=CreateBS(fMin, fMax, fOrigin);
register_bombsite(ent, fMin, fMax, fOrigin);
return PLUGIN_HANDLED_MAIN;
}
public rb_save(id, level, cid){
if(cmd_access(id, level, cid, 1))
SaveToFile(id)
return PLUGIN_HANDLED_MAIN;
}
public bomb_planted(planter){
gbPlanted = true;
}
stock engine_get_size(const Float:fMax[3], Float:fOrigin[3], Float:fMinS[3], Float:fMaxS[3]){
//count Mins and Maxs
for(new i=0;i<3;i++){
fMaxS[i]=fMax[i]-fOrigin[i];
fMinS[i]=fOrigin[i]-fMax[i];
}
}
CreateBS(Float:fMin[3], Float:fMax[3], Float:fOrigin[3]){
new bombtarget = create_entity(ENTITY_CLASS);
if(bombtarget > 0)
{
DispatchKeyValue(bombtarget, "classname", ENTITY_CLASS);
entity_set_string(bombtarget, EV_SZ_classname, ENTITY_CLASS);
entity_set_origin(bombtarget, fOrigin);
entity_set_size(bombtarget, fMin, fMax);
entity_set_edict(bombtarget, EV_ENT_owner, 0);
entity_set_int(bombtarget, EV_INT_movetype, 0);
entity_set_int(bombtarget, EV_INT_solid, SOLID_TRIGGER);
entity_set_float(bombtarget,EV_FL_nextthink, halflife_time() + 0.01);
new ent=-1;
new szTargetName[32];
do{
ent=find_ent_in_sphere( ent, fOrigin, 300.0);
if(is_valid_ent(ent)){
entity_get_string(ent, EV_SZ_classname,szTargetName,31);
if(!equal(szTargetName, "func_breakable")) continue;
if(entity_get_float(ent, EV_FL_dmg_take)==0.0){
entity_get_string(ent, EV_SZ_targetname, szTargetName,31);
if(szTargetName[0]){
entity_set_string(bombtarget, EV_SZ_target, szTargetName);
break;
}
}
}
}
while(ent);
}
return bombtarget;
}
register_bombsite(ent, Float:fMin[3], Float:fMax[3], Float:fOrigin[3]){
ArrayPushCell(g_BS, ent);
ArrayPushArray(g_BSmins, fMin);
ArrayPushArray(g_BSmaxs, fMax);
ArrayPushArray(g_BSorigin, fOrigin);
}
SaveToFile(id){
if(file_exists(g_szCfgFile))
delete_file(g_szCfgFile);
new iVector[3];
new szLine[128];
if(ArraySize(g_BS) > 0)
{
for(new i=0;i<ArraySize(g_BS);i++)
{
ArrayGetArray(g_BSmins, i, iVector);
formatex(szLine, 63, "%.0f %.0f %.0f", iVector[0],iVector[1],iVector[2]);
ArrayGetArray(g_BSmaxs, i, iVector);
format(szLine, 127, "%s %.0f %.0f %.0f", szLine, iVector[0],iVector[1],iVector[2]);
ArrayGetArray(g_BSorigin, i, iVector);
format(szLine, 127, "%s %.0f %.0f %.0f", szLine, iVector[0],iVector[1],iVector[2]);
write_file(g_szCfgFile, szLine, -1);
}
}
client_print(id, print_center, "SAVED");
}
LoadFromFile(){
new szLine[64], iLen;
new szMin[3][10], szMax[3][10], szOrigin[3][10];
new Float:fMin[3], Float:fMax[3], Float:fOrigin[3];
for(new i=0;read_file(g_szCfgFile,i,szLine, 64,iLen);i++){
trim(szLine);
if(szLine[0]==';') continue;
parse(szLine, szMin[0], 9,szMin[1], 9, szMin[2], 9, szMax[0], 9, szMax[1], 9, szMax[2], 9, szOrigin[0], 9,szOrigin[1], 9, szOrigin[2], 9);
for(new j=0;j<3;j++){
fMin[j]=str_to_float(szMin[j]);
fMax[j]=str_to_float(szMax[j]);
fOrigin[j]=str_to_float(szOrigin[j]);
}
new ent=CreateBS(fMin, fMax, fOrigin);
if(ent!=-1){
register_bombsite(ent, fMin, fMax, fOrigin);
}
}
}
public showOnRadar(id)
{
if(m_fakeHostage && m_fakeHostageDie) //only 0 is false
{
new Float: ori_min[3]
new Float: ori_max[3]
entity_get_vector(bomsite_id,EV_VEC_absmin,ori_min)
entity_get_vector(bomsite_id,EV_VEC_absmax,ori_max)
message_begin(MSG_ONE_UNRELIABLE, m_fakeHostage, {0,0,0}, id);
write_byte(id);
write_byte(20);
write_coord(floatround((ori_max[0]+ori_min[0])/2));
write_coord(floatround((ori_max[1]+ori_min[1])/2));
write_coord(floatround((ori_max[2]+ori_min[2])/2));
message_end();
message_begin(MSG_ONE_UNRELIABLE, m_fakeHostageDie, {0,0,0}, id);
write_byte(20);
message_end();
}
}
public task_UpDateRadar(){
if(!gbPlanted)
{
new Players[32];
new playerCount, id;
get_players(Players, playerCount, "bc")
for ( new i=0; i<playerCount; i++){
id = Players[i];
showOnRadar(id)
}
}
}
stock RemoveEntityAll(name[])
{
new ent = engfunc(EngFunc_FindEntityByString, 0, "classname", name)
new temp
while (ent)
{
temp = engfunc(EngFunc_FindEntityByString, ent, "classname", name)
engfunc(EngFunc_RemoveEntity, ent)
ent = temp
}
}