• 🏆 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!

Units Give Gold Per second

Status
Not open for further replies.
Level 9
Joined
Apr 23, 2011
Messages
460
I'm not sure where to post this, I'm hoping this is it. I made a system for gold per second upgradable buildings/ units if chosen to do so. The system is modeled after "Troll and Elves" House system. Here is the system:

  • Setup
    • Events
      • Map initialization
    • Conditions
    • Actions
      • -------- Gold Amount Given Every [Time] --------
      • Set Gold[1] = 1
      • Set Gold[2] = 2
      • Set Gold[3] = 4
      • Set Gold[4] = 8
      • Set Gold[5] = 16
      • Set Gold[6] = 32
      • Set Gold[7] = 64
      • Set Gold[8] = 128
      • Set Gold[9] = 256
      • Set Gold[10] = 512
      • -------- Units and their Upgrades. Corespond to Gold --------
      • Set UnitType[1] = Base Unit
      • Set UnitType[2] = Upgrade1
      • Set UnitType[3] = Upgrade2
      • Set UnitType[4] = Upgrade3
      • Set UnitType[5] = Upgrade4
      • Set UnitType[6] = Upgrade5
      • Set UnitType[7] = Upgrade6
      • Set UnitType[8] = Upgrade7
      • Set UnitType[9] = Upgrade8
      • Set UnitType[10] = Upgrade9
  • Give gold
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in (Playable map area)) and do (Actions)
        • Loop - Actions
          • For each (Integer A) from 1 to 10, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Unit-type of (Picked unit)) Equal to UnitType[(Integer A)]
                • Then - Actions
                  • Set temp_Loc = (Position of (Picked unit))
                  • Player - Add Gold[(Integer A)] to (Owner of (Picked unit)) Current gold
                  • Floating Text - Create floating text that reads (+ + (String(Gold[(Integer A)]))) at temp_Loc with Z offset 0.00, using font size 10.00, color (100.00%, 100.00%, 100.00%), and 0.00% transparency
                  • Floating Text - Change (Last created floating text): Disable permanence
                  • Floating Text - Set the velocity of (Last created floating text) to 64.00 towards 90.00 degrees
                  • Floating Text - Change the lifespan of (Last created floating text) to 1.00 seconds
                  • Custom script: call RemoveLocation(udg_tempLoc)
                • Else - Actions
Variables: Gold(Integer), UnitType(Unit-Type), temp_Loc(Point).
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
Using skip remainin actions make the trigger ignore the rest of the actions.

You're looping from 1 to 10. If index 6 returns true, then you don't need to loop through the rest indexes. That's where skip remaining actions comes in handy.

Note that your system give gold from dead units.

You could create the floating text above the unit and not use a location.

You could use a hashtable instead of looping through the indexes.
 
Level 9
Joined
Apr 23, 2011
Messages
460
I'm aware of that issue in the dead units case. I'm afraid im not skilled enough to fix it. I'm not sure how to use hashtables :( I have to read up on them. And the making at a location was a preference of mine but i see how on unit would simply ignore the leak caused by a location. Thank you sir. I guess i should look into hashtables. I hear alot about them and know very little. Perhaps they could help me simplify my masses of "databases" in my map initialization of my own creation.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
That system has a bad complexity of O(n) where n is the number of types you want to be affected.

Much rather use a hashtable to map the unit types to their accompanying gold values (one field is unit type, other is some constant and the data stored is income). This gives a complexity of mostly O(1) which is greatly faster for large numbers of unit types. It could even potentially be faster than even a few units in your current system (definatly for 10).

Be aware that you might need to use JASS for this, seeing that GUI fails to treat object type IDs as integers like they really are
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
If you decide to use a hashtable, you can do it like this:

  • Untitled Trigger 088
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Hashtable - Create a hashtable
      • Set hash = (Last created hashtable)
      • Custom script: call SaveInteger(udg_hash, 'hfoo', 0, 10)
      • Custom script: call SaveInteger(udg_hash, 'hkni', 0, 20)
  • Untitled Trigger 103
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in (Playable map area) matching (((Matching unit) is alive) Equal to True)) and do (Actions)
        • Loop - Actions
          • Custom script: set udg_i = LoadInteger(udg_hash, GetUnitTypeId(GetEnumUnit()), 0)
          • Player - Add i to (Owner of (Picked unit)) Current gold
The variables use are:
hash (hashtable)
i (integer)

In the first trigger I make footmen give 10 gold and knights 20 gold. hfoo and hkni are the raw codes of the unit types. You can check the raw codes by pressing ctrl + d in the object editor.

In the second trigger I load the gold value into an integer variable i and then give the amount to the owner of the unit.
 
Status
Not open for further replies.
Top