//! zinc
library RandomHero/*v1.0
*************************************************************************************
*
* RandomHero by Almia
*
* Simple Random Hero Engine
*
*************************************************************************************
*
* */ requires /*
*
* */ Table /* hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
*************************************************************************************
*
* API
*
* static method add(integer heroId)
* - adds the raw code to the stack
*
* static method remove(integer heroId)
* - removes the wanted hero from the stack.
*
* static method setPlayerHeroPoint(player p, real x, real y)
* - Point where generated hero will be created. Correspons on the given player
*
* static method createHero(player p) -> unit
*
*************************************************************************************
*
* Credits to Bribe for the Table
* to Mag for not teaching me about how light Stack is.
*
*************************************************************************************/
{
/**********************************************
*
* Remove hero from stack
*
* If true, hero will be no longer generated
* if already generated. If false, hero will
* maintain in the stack
*
***********************************************/
constant boolean REMOVE_HERO_FROM_STACK = true;
/**********************************************
*
* Select Hero on Create
*
* If true, selects the generated hero for
* corresponding Player
*
**********************************************/
constant boolean SELECT_HERO_ON_CREATE = true;
Table heroTb;
module InitRH{ static method onInit() { init(); } }
// Random Hero Struct
public struct RandomHero[]
{
private static integer heroStack[];
private static integer heroCount = 0;
static method operator add=(integer heroId)
{
heroCount = heroCount + 1;
heroStack[heroCount] = heroId;
heroTb[heroId] = heroCount;
}
static method operator remove=(integer heroId)
{
integer id = heroTb[heroId];
if (0 < id)
{
heroStack[id] = heroStack[heroCount];
heroCount = heroCount - 1;
heroTb[heroId] = 0;
}
else{ BJDebugMsg(" RandomHero Error : Hero Id not found in the stack. "); }
}
static method createHero(player p, real x, real y, real angle) -> unit
{
integer randomId;
integer playerId;
bj_lastCreatedUnit = null;
if (heroCount > 0)
{
randomId = GetRandomInt(1, heroCount);
playerId = GetPlayerId(p);
if (REMOVE_HERO_FROM_STACK)
{
heroStack[randomId] = heroStack[heroCount];
heroCount = heroCount - 1;
}
bj_lastCreatedUnit = CreateUnit(p, heroStack[randomId], x, y, angle);
if (SELECT_HERO_ON_CREATE) { SelectUnitAddForPlayer(bj_lastCreatedUnit, p); }
}
else{ BJDebugMsg(" RandomHero Error : No more Heroes in the Hero Stack." ); }
return bj_lastCreatedUnit;
}
static method init() { heroTb = Table.create(); }
module InitRH;
}
}
//! endzinc