System Creep Camp Manipulator

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
///////////////////////////////////////////////////////////////
//  
//  < Creep Camp Manipulator >  ~ v1.0.1 ~
//
//      by kingking
//
//  ==============
//  What is this ?
//  ==============
//  Creep Camp Manipulator is a simple system for providing
//  efficient creep camp spawning. 
/// It will form a collection of creep types and provides 
//  easy interface to let you to spawn whole camp of creeps.
//
//  -----------
//  Usage :
//  -----------
//  ========================
//  Struct 1 : CreepType
//  ========================
//  CreepType.create()
//  -> Return an instance.
//
//  set .type = <unitType>
//  -> Rawcode of creep.
//
//  set .owner = <unit's owner>
//  -> Owner of creep.
//
//  ========================
//  Struct 2 : CreepTroop
//  ========================
//  CreepTroop.create()
//  -> Return an instance.
//
//  call .addCreepType(CreepType, number)
//  -> Add a certain number of creep type.
//
//  call .removeCreepType(CreepType, number)
//  -> Remove a certain number of creep type.
//
//  ========================
//  Struct 3 : CreepCamp
//  ========================
//  CreepCamp.create()
//  -> Return an instance.
//
//  set .x = x
//  -> X-coordinate for camp.
//
//  set .y = y 
//  -> Y-coordinate for camp.
//
//  set .OnClear = function
//  -> Function that will be fired when the creeps in camp are all died.
//
//  set .OnSpawn = function
//  -> Function that will be fired when creeps are spawned.
//
//  call .enumCreeps(code)
//  -> Enum all creeps in camp.
//
//  call .spawn(CreepTroop)
//  -> Spawn the collection of creeps at the point.
//
//  .active ->
//  -> Is the camp active?
//
//  .amount ->
//  -> Amount of creeps in camp.
//
//  ====================================
//  Example for .OnClear callback :
//  ====================================
//  function OnClear takes CreepCamp whichCreepCamp returns nothing
//
//  ====================================
//  Example for .OnSpawn callback :
//  ====================================
//  function OnSpawnCreep takes unit whichUnit returns nothing
//
//  ******************************************************************
//  * IMPORTANT : NEVER DESTROY ANY CREEP CAMP MANIPULATOR's STRUCT. *
//  *             NEVER USE RemoveUnit() on CREEP.                   *
//  ******************************************************************
//
//  =================
//  Implementation :
//  =================
//  1) Copy this library to your map.
//  2) Copy Table to your map if your map does not have it.
//  3) Setup it correctly.
//  4) Enjoy it!
//
//  ==============
//  Requirement :
//  ==============
//  Table
//  Latest version of Jasshelper.
///////////////////////////////////////////////////////////////////////////
library CCM requires Table
    
    struct CreepType
        integer type
        player owner
        real facing
    endstruct
    
    struct CreepTroop
        public thistype prev
        public thistype next
        public CreepType cd
        
        static method create takes nothing returns thistype
            local thistype this = .allocate()
            set this.prev = this
            set this.next = this
            return this
        endmethod
        
        private method addCreep takes CreepType d returns nothing
            local thistype new
            debug if d == 0 then
                debug call BJDebugMsg("Creep Camp Manipulator Error! <null> CreepData is added into list!")
                debug return
            debug endif
            set new = thistype.allocate()
            set new.next = this
            set new.prev = this.prev
            set this.prev.next = new
            set this.prev = new
            set new.cd = d
        endmethod
        
        private method removeCreep takes CreepType d returns nothing
            local thistype iterator = this.next
            loop
            exitwhen iterator == this
                if iterator.cd == d then
                    set this.prev.next = iterator.next
                    set this.next.prev = iterator.prev
                    call iterator.deallocate()
                    set iterator.prev = 0
                    set iterator.next = 0
                    set iterator.cd = 0
                    set iterator = this
                else
                    set iterator = iterator.next
                endif
            endloop
        endmethod
        
        method addCreepType takes CreepType d, integer number returns nothing
            loop
            exitwhen number == 0
                call .addCreep(d)
                set number = number - 1
            endloop
        endmethod
        
        method removeCreepType takes CreepType d, integer number returns nothing
            loop
            exitwhen number == 0
                call .removeCreep(d)
                set number = number - 1
            endloop
        endmethod
    endstruct
    
    private keyword CreepStruct
    private keyword Data
    
    function interface OnClearCamp takes integer whichData returns nothing
    function interface OnSpawnUnit takes unit whichUnit returns nothing
    
    struct CreepCamp
        real x
        real y
        readonly boolean active
        OnClearCamp OnClear
        OnSpawnUnit OnSpawn
        public group g
        public integer amount
        
        static method create takes nothing returns thistype
            local thistype this = .allocate()
            set .active = false
            set .amount = 0
            set .g = CreateGroup()
            return this
        endmethod
        
        method spawn takes CreepTroop troop returns nothing
            local unit u
            local CreepTroop iterator = troop
            loop
                set iterator = iterator.next
            exitwhen iterator == troop
                set u = CreateUnit(iterator.cd.owner,iterator.cd.type,.x,.y,iterator.cd.facing)
                call CreepStruct.add(u,this)
                call .OnSpawn.execute(u)
            endloop
            set u = null
        endmethod
        
        method enumCreeps takes code c returns nothing
            call ForGroup(.g,c)
        endmethod
        
        method decrement takes nothing returns nothing
            set .amount = .amount - 1
            if .amount == 0 then
                call .OnClear.execute(this)
                set .active = false
            endif
        endmethod
    endstruct
        
    private struct CreepStruct 
        private static HandleTable data
        unit u
        CreepCamp cc
        
        static method add takes unit whichUnit, CreepCamp cc returns nothing
            local thistype this = .allocate()
            set .u = whichUnit
            set .cc = cc
            call GroupAddUnit(.cc.g,u)
            set .cc.amount = .cc.amount + 1
            set thistype.data[whichUnit] = this
        endmethod
        
        method onDestroy takes nothing returns nothing
            call thistype.data.flush(.u)
            call GroupRemoveUnit(.cc.g,.u)
            call .cc.decrement()
            set .cc = 0
            set .u = null
        endmethod
        
        private static method Cond takes nothing returns boolean
            local CreepStruct this = thistype.data[GetTriggerUnit()]
            if this != 0 then
                call .destroy()
            endif
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local trigger trig = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_DEATH)
            call TriggerAddCondition(trig,Condition(function thistype.Cond))
            set thistype.data = HandleTable.create()
        endmethod
    endstruct
    
    
endlibrary


Supports :
-> Form a camp which consists of random creeps on random points.(Clever users will know how to achieve it :) )
-> Dynamic amount of creep types.
-> Automatical camp clearing callback by using O(n) method.

Limitation(I don't think you can go beyond it. If you exceed it, you are doing something mad :p ) :
Max 8191 creep types.
Max 8191 creep types in creep troops.
Max 8191 camps.
Max 8191 creeps on map.

Cons :
-> Requires you to create reviving function by yourself, but I don't think this is a con, because you may add some conditions to control the reviving of creeps. ;)

Requires : Table

Example :
JASS:
library SetupCreeps initializer Init requires CCM, TimerUtils
    
    globals
        CreepTroop ScorpionGroup
    endglobals
    
    private function OnExpire takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local CreepCamp c = GetTimerData(t)
        call c.spawn(ScorpionGroup)
        call ReleaseTimer(t)
        set t = null
    endfunction
    
    private function ReviveCreeps takes CreepCamp camp returns nothing
        local timer t = NewTimer()
        call SetTimerData(t,camp)
        call TimerStart(t,5.,false,function OnExpire)
        set t = null
    endfunction
    
    private function CreateEffects takes unit whichUnit returns nothing
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Human\\Avatar\\AvatarCaster.mdl",whichUnit,"origin"))
    endfunction
    
    private function Init takes nothing returns nothing
        local CreepType ScorpionCreeps
        local CreepCamp ScorpionCamp
        
        set ScorpionGroup = CreepTroop.create() //A group of creep types.
        
        set ScorpionCreeps = CreepType.create() //Create a type of creep
        set ScorpionCreeps.type = 'nano'//rawcode of creep
        set ScorpionCreeps.owner = Player(14)//owner of creep
        set ScorpionCreeps.facing = 270. //facing of creep
        
        call ScorpionGroup.addCreepType(ScorpionCreeps,3)//Add 3 same creep type to the group
        
        set ScorpionCamp = CreepCamp.create()//Create a creep camp on the point.
        set ScorpionCamp.x = -1800. //setup position of the camp.
        set ScorpionCamp.y = 4000.
        
        set ScorpionCamp.OnClear = ReviveCreeps//add revive timer for the camp
        set ScorpionCamp.OnSpawn = CreateEffects//add special effect creating callback when the camp of creeps are created.
        
        call ScorpionCamp.spawn(ScorpionGroup)//spawn creeps
    endfunction
    
endlibrary

- You will have 3 scorpion creeps(owned by Neutral Hostile) at -1800,4000.
- When scorpions are spawned, they will have avatar effect
- When the scorpions in map are all died, the new scorpions will be spawned after 5 seconds. :D
 

Attachments

  • Creep Camp Manipulator.w3x
    42.6 KB · Views: 254

Jesus4Lyf

Good Idea™
Reaction score
397
This seems to be a CreateUnit wrapper (it just batch spawns units and counts them?). The fact that this does not handle timed respawning and lacks initialising critical struct members in its constructor tells me this was probably neither well thought through, or written to actually be practical for use.

Furthermore, it is systems like this which fill up the Tutorials and Resources forums - the ones which seem to contain just almost enough content or functionality to warrant being called a snippet or system, but not quite enough functionality to convince people that they're more than just something to submit for the hell of it.

Please, write things which are useful. Write things with clean interfaces (if a creep camp must have an x/y loc, make sure it is set in the constructor). Write things which take less time to learn than to code things yourself. :)

PS.
JASS:
//  *             NEVER USE RemoveUnit() on CREEP.                   *

I would call that a bug. :)

If you wrote this in a way that recycled the creeps for respawning, it would warrant a useful system. Until this does something neat like that, I think it shall remain in the graveyard.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top