First the storage
A list to hold the available buy options and the buy list Code:
struct buybot_item_list_s
{
char szItem[32];
};
vector<buybot_item_list_s> ValidItems;
struct buybot_s
{
char szItem[32];
};
vector<buybot_s> BuyBot;
Next I setup the functions for creating the list of stuff you can buy Code:
void func_itemlistadd()//add to list of valid items
{
buybot_item_list_s tmp;
for(int i=0;i<ValidItems.size();i++)
if(!strcmp(ValidItems.szItem,cmd.argS(1).c_str()))
{
Con_Echo("Item is already in buybot item list!");
return;
}
strcpy(tmp.szItem,cmd.argS(1).c_str());
ValidItems.push_back(tmp);
}
void func_itemlist()//list valid items
{
for(int i=0;i<ValidItems.size();i++)
Con_Echo("%s",ValidItems.szItem);
}
And then the actual buybot functions. Code:
bool ValidItem(const char *item)//check if item being added is valid
{
for(int i=0;i<ValidItems.size();i++)
{
if(!strcmp(ValidItems.szItem,item))
return true;
}
return false;
}
void func_buyadd()//add an item to the buybot
{
if (!ValidItem(cmd.argS(1).c_str()))
{
Con_Echo("Item is invalid");
return;
}
buybot_s tmp;
for(int i=0;i<BuyBot.size();i++)
if((!strcmp(BuyBot.szItem,cmd.argS(1).c_str())) && (!strstr(cmd.argS(1).c_str(),"primammo")) && (!strstr(cmd.argS(1).c_str(),"secammo")))
{
Con_Echo("Item is already in buybot!");
return;
}
strcpy(tmp.szItem,cmd.argS(1).c_str());
BuyBot.push_back(tmp);
}
void func_buydel()//delete an item from the buybot
{
for(int i=0;i<BuyBot.size();i++)
if(!strcmp(BuyBot.szItem,cmd.argS(1).c_str()))
{
BuyBot.erase(BuyBot.begin() + i, BuyBot.begin() + i + 1);
Con_Echo("Item deleted");
return;
}
}
void func_buylist()//list items the buybot will buy
{
for(int i=0;i<BuyBot.size();i++)
Con_Echo("%s",BuyBot.szItem);
}
void func_buy()//buy items
{
char* buf;
for(int i=0;i<BuyBot.size();i++)
{
sprintf(buf,"#%s",BuyBot.szItem);
cmd.exec(buf);
}
}
Now here is the one snag i have run into. I cant buy AtRoundStart automatically.
It works fine however if I just bind a key or mouse button the "buy". Calling it from AtRoundStart or ResetHUD causes the game to exit as soon as the round starts.
Also i found that using an alias with later (eg. alias buytest "later 1 buy") also causes it to crash, but (alias buytest "buy") doesnt.
Anoyone have any ideas about what the problem could be?
Other than that, There are a couple things that could use tweaking but If you find it useful then enjoy =)
edit...
Problem i was having is now fixed. Thank you Xen and Azorbix for the suggestions
here is what i did
I setup a bool "bCanBuy" and a timer "buytimer"
So in atRoundStart I have this Code:
if(cvar.buybot)
{
buytimer.countdown (2);
bCanBuy = true;
}
then in Hud_Redraw Code:
if(buytimer.expired() && bCanBuy)
{
cmd.exec("buy");
bCanBuy = false;
}