Creep Camp Respawn

This bundle is marked as awaiting update. A staff member has requested changes to it before it can be approved.
Respawn system that automatically detect creep camps and make them respawn once the whole camp is dead. After X seconds. No work required for the user.

Code:
package CreepRespawn

import LinkedList
import LinkedListModule
import TimerUtils
import MapBounds
import Table

player target = players[0] //respawns preplaces units by player red
real radius = 300
real respawnTime = 2

LinkedList<group> camps = new LinkedList<group>()

Table hash = new Table()

class Creep
    use LinkedListModule
    vec2 pos
    angle a
    integer id
    unit creep
    player p
    LinkedList<Creep> camp

    construct(unit u, LinkedList<Creep> memberOf)
        creep = u
        id = creep.getTypeId()
        pos = creep.getPos()
        a = creep.getFacingAngle()
        p= creep.getOwner()
        camp = memberOf

function onDeathFilter() returns boolean
    return GetTriggerUnit().getOwner() == target

function isCampDead(LinkedList<Creep> camp) returns boolean
    boolean check = true

    for Creep c in camp
        if(c.creep.isAlive())
            check = false
            break

    return check

function unit.getCreep() returns Creep
    Creep current = Creep.first

    while(current.next != null and current.creep != this)
        current = current.next

    if(current.creep != this)
        current = null

    return current

function onDeath()
    Creep c = GetTriggerUnit().getCreep()

    if(isCampDead(c.camp))
        timer t = getTimer()
        hash.saveUnit(t.getHandleId(), c.creep)
        t.start(respawnTime, function onExpire)

function onExpire()
    timer t = GetExpiredTimer()
    integer h = t.getHandleId()
    unit u = hash.loadUnit(h)

    Creep c = u.getCreep()
    hash.flush()
    t.release()

    for creep in c.camp
        creep.creep = createUnit(creep.p, creep.id, creep.pos, creep.a)

function onEnter()
    unit u = GetTriggerUnit()
    if(u.getOwner() == target and u.getCreep() == null)
        location loc = GetUnitLoc(u)
        group detected = GetUnitsInRangeOfLocAll(radius, loc)
        for unit found in detected
            Creep c = found.getCreep()
            if(c != null)
                c.camp.push(new Creep(u, c.camp))
                break

function sortCreeps()
    group targets = GetUnitsOfPlayerAll(target)

    for unit first in targets
        if(first.getCreep() == null)
            location loc = GetUnitLoc(first)
            group detected = GetUnitsInRangeOfLocAll(radius, loc)
            LinkedList<Creep> camp = new LinkedList<Creep>()

            for unit found in detected
                if(found.getCreep() == null)
                    camp.push(new Creep(found, camp))
                else
                    detected.removeUnit(found)

            RemoveLocation(loc)
            detected.destr()

    targets.destr()

init
    sortCreeps()

    trigger t = CreateTrigger()
    t.registerAnyUnitEvent(EVENT_PLAYER_UNIT_DEATH)
    t.addCondition(Condition(function onDeathFilter))
    t.addAction(function onDeath)
   
    CreateTrigger()
    ..registerEnterRegion(playableMapRegion, null)
    ..addAction(function onEnter)
Contents

Creep Camp Respawn (Map)

Reviews
MyPad
A snippet designed for the majority of mappers in Wurst. Although I am not really verbose yet in Wurst, here are some of my pointers about it: You can correct me in some of my presumptions if you like. NOTES: In the init function, the trigger which...

MyPad

Spell Reviewer
Level 20
Joined
May 9, 2014
Messages
1,627
A snippet designed for the majority of mappers in Wurst. Although I am not really verbose yet in Wurst, here are some of my pointers about it:
You can correct me in some of my presumptions if you like.

NOTES:

  • In the init function, the trigger which would detect death events has a triggercondition and a triggeraction. Is this intended?
    [*]In the getCreep function, the loop can be shortened by 2 lines. (merge the negation of the condition for breaking the loop into the while)
  • The package could use some description, even if a little bit. (Although I already got the idea)
Overall, I find the package to be pretty straight-forward. @Diegoit's question on creep camps being created after initialization might prove intriguing.
 
Last edited:

Chaosy

Tutorial Reviewer
Level 39
Joined
Jun 9, 2011
Messages
13,083
Alright so I fixed it.

Your suggestion was fine - like I thought.
For some reason I had a second respawn script in my map... no clue why I added it originally.
Anyway, once I removed it your idea works.

Units added after game start will now be added to the system.

Now uses Table instead of standard hashtable

Removed hashtable usage from the sortCreep function
 
Top