• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

RandomHero v1.2

  • RandomHero by Almia
  • Simple Random Hero Engine which allows you to create your RandomHero system.
  • JASS:
    //! 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
  • Demo:
    JASS:
    library Demo initializer init uses RandomHero
        
        private function init takes nothing returns nothing
            set RandomHero.add = 'Hamg'
            set RandomHero.add = 'Hmkg'
            set RandomHero.add = 'Hpal'
            set RandomHero.add = 'Hblm'
            call RandomHero.createHero(Player(0), 0, 0, 270)
            set RandomHero.remove = 'Hamg' // Shows an error if random hero is removed or not included.
            call RandomHero.createHero(Player(0), 0, 0, 270)
            call RandomHero.createHero(Player(0), 0, 0, 270)
            call RandomHero.createHero(Player(0), 0, 0, 270) // Empty,because we have removed a hero earlier.
        endfunction
    endlibrary
Contents

RandomHero v1.2 (Map)

Reviews
15:22, 22nd Jun 2013 PurgeandFire: Approved. It works. It is very simple but some people might find it useful.

Moderator

M

Moderator

15:22, 22nd Jun 2013
PurgeandFire: Approved. It works. It is very simple but some people might find it useful.
 
Level 10
Joined
Aug 21, 2010
Messages
316
If for example, my map has around 120 heroes, it would mean that I would then have to call the same function 120 times.

So the end result would be:

JASS:
call RandomHero.add( my hero ) x 120

If I'm right, it's terribly irritating proses.


JASS:
call RandomHero.add( myhero )
call RandomHero.setPlayerHeroPoint( my player, 0, 0 )
call RandomHero.createHero(my player)

->

A much better solution:

JASS:
function CreateRandomHeroForPlayer takes integer unittypeid, player wichPlayer, real x, real y, real angle returns unit

or

JASS:
function CreateRandomUnitForPlayer takes integer unittypeid, player wichPlayer, real x, real y, real angle returns integer

or

JASS:
function CreateRandomUnitForPlayer takes integer unittypeid, player wichPlayer, real x, real y, real angle returns nothing


And what is most important is that the function to be called only once


Simply make a main base for the registration of a hero and then in the main code make simple randomizer

Example:
JASS:
set my_hero_start_pointer = 1

set my_hero_unitTypeId[1] = 'Ekee'
set my_hero_unitTypeId[2] = 'Emoo'
set my_hero_unitTypeId[3] = 'Edem'
set my_hero_unitTypeId[4] = 'Ewar'

set my_hero_end_pointer = 4
//----------------------------------------------------------
JASS:
local integer first
local integer last
local integer my_hero_random_counter

set first = my_hero_start_pointer
set last = my_hero_end_pointer 
set my_hero_random_counter = GetRandomInt( first, last )

set examplemy_hero_unit = CreateUnit( my_player, my_hero_unitTypeId[my_hero_random_counter], my_x, my_y, my_angle )
 
sorry but is it this possible in gui?
If so, why would you create that simple system in vjass making it impossible to use by 75% of users, and other 25% who understand it also can create it by itself.
I think you shouldnt just create things to make it your resources bigger, how about making things useful, just look damage engine by looking for help, he knows coding but he wants to make it useful for all of us.
 
Level 10
Joined
Aug 21, 2010
Messages
316
@zv27
If you create 100 heroes you cant blame the system for not supporting that. In AOS maps is quite common that the teams dont got the same heroes ,so to make it viable in every situation my way is better.

I do not blame the system. At no point did I say that the system is bad or something.I just wanted to point out to him that the system should be a little more universal.I personally do not need this kind of system because I have my own system,the same as in dota. But simply, Almia is a great coder and I know he can do much better than this.(Not much better...a million times better)

sorry but is it this possible in gui?

Of course it is possible
And it's pretty easy
 
sorry but is it this possible in gui?
If so, why would you create that simple system in vjass making it impossible to use by 75% of users, and other 25% who understand it also can create it by itself.
I think you shouldnt just create things to make it your resources bigger, how about making things useful, just look damage engine by looking for help, he knows coding but he wants to make it useful for all of us.

I agree with HammerFist.

I don't see too much of a point in this resource, in my opinion. True, some people may not be familiar with using a stack. However, I'm not sure how many people would use it. Plus it is very simple. For example, if you don't have the unique-aspect enabled, it is the equivalent of just setting an array and finding a random integer.

It works, yes. I just think it is too simple. :) It is great an example to learn from, IMO. However, before making a script you have to consider the audience. Usually, shorter snippets belong in the JASS submission section--you can get one approved even if it is short (although this one may be too simple to be approved). Don't take this as me being harsh, I'm just giving a friendly opinion.

As for the actual system, I don't really see why the setPlayerPoint() has to be separate. I could be wrong. I just can't really think of an instance where you would need to separate the X and the Y from the creation, since the X and Y only matters for the creation. I suppose if you are mass-creating them, you may not want to repeat yourself by inputting it over and over, but in most cases it wouldn't matter. Besides, you'll save 2 variables and a chunk of code.
 
The List array is a bad idea,why not just CnP it over and over again? its much better than always writing the variable and might also destroy the stack if the indexing is ruined.

I actually submit my resources in this section because I always have my resources in Jass section not commented.


Anways, Im gonna update the code to allow you to remove the hero from stack if something happens(hero is sold, some events, etc). Also, Im planning to create a RandomHeroTable,which allows you to have different RandomHero stacks.

@zv27
The stack is needed.It will prevent users not to get any same heroes.
 
Level 10
Joined
Aug 21, 2010
Messages
316
The List array is a bad idea,why not just CnP it over and over again? its much better than always writing the variable and might also destroy the stack if the indexing is ruined.

I actually submit my resources in this section because I always have my resources in Jass section not commented.


Anways, Im gonna update the code to allow you to remove the hero from stack if something happens(hero is sold, some events, etc). Also, Im planning to create a RandomHeroTable,which allows you to have different RandomHero stacks.

@zv27
The stack is needed.It will prevent users not to get any same heroes.

I did not say anything about stack.Of course it is considered as the default
 
Top