[Help]CSSafety Library

Laiev

Hey Listen!!
Reaction score
188
you already have timerutils in your map, you don't need to copy it again...

also... CSSafety is deprecated
 

Laiev

Hey Listen!!
Reaction score
188
delete one TimerUtils trigger that you use, you only need one, not two...

the same goes for lots of other systems.

you don't need a copy of something that you got
 

PT.Dyland

New Member
Reaction score
0
My Attribute System doesn't work, Who can help me with????????
If you need I can give you my map, you will milk help me?
 

PT.Dyland

New Member
Reaction score
0
CSSafety
Code:
library CSSafety
//******************************************************************************************
//*
//* CSSafety 14.3
//* ¯¯¯¯¯¯¯¯
//*
//*  Utilities to make things safer. Currently this simply includes a timer recycling
//* Stack. Once you replace CreateTimer with NewTimer and DestroyTimer with ReleaseTimer
//* you no longer have to care about setting timers to null nor about timer related issues
//* with the handle index stack.
//*
//******************************************************************************************

    //==========================================================================================
    globals
        private timer array T
        private integer N = 0
    endglobals

    //==========================================================================================
    function Timer takes nothing returns timer
        if (N==0) then
            return CreateTimer()
        endif
     set N=N-1
     return T[N]
    endfunction

    //==========================================================================================
    function WinTimer takes timer t returns nothing
        call PauseTimer(t)
        if (N==8191) then
            debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")

            //stack is full, the map already has much more troubles than the chance of bug
            call DestroyTimer(t)
        else
            set T[N]=t
            set N=N+1
        endif    
    endfunction

endlibrary

//Forced by WE
function InitTrig_CSSafety takes nothing returns nothing
endfunction

Attribute System

Code:
library AttributeSystem initializer Init_AttributeSystem needs CSSafety

//***************************************************************************
//*                                                                         *
//* Attribute System v4                                                     *
//* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯               **URL in the works                    *
//* Requires:                                                               *
//* ¯¯¯¯¯¯¯¯¯                                                               *
//*                                                                         *
//* - A vJASS Preprocessor                                                  *
//* - CSSafety library                                                      *
//*                                                                         *
//* - The custom items and abilities found in this map                      *
//* - Updated rawcodes for the aforementioned objects                       * 
//*                                                                         *
//***************************************************************************


globals  
//Config. Options\\     
    // Attribute(Attribute System) ability rawcode
    private constant integer AS_Abil = 'A00i'
    // Blank item rawcode 
    private constant integer AS_Blank = 'I000'
    // Cancel item rawcode
    private constant integer AS_Cancel = 'I001'
    // Number of stat points remaining item rawcode
    private constant integer AS_Att = 'I005'
    // Increment Strength item rawcode
    private constant integer AS_Str = 'I004'
    // Increment Agility item rawcode
    private constant integer AS_Agi = 'I002'
    // Increment Intelligence item rawcode
    private constant integer AS_Int = 'I003'   

//Do not touch past here unless you pain for death!!\\
    private item array AS_I0
    private item array AS_I1
    private item array AS_I2
    private item array AS_I3
    private item array AS_I4
    private item array AS_I5
    private item array AS_IAtt0
    private item array AS_IAtt1
    private item array AS_IAtt2
    private item array AS_IAtt3
    private item array AS_IAtt4
    private item array AS_IAtt5
    
    private integer array AS_PointsPerLevel
    private integer array AS_LastLevel
    private integer array AS_Points
    private boolean array AS_Enabled    
    private boolean array AS_On
    
    private unit AS_Hero
endglobals


//**Library of functions**\\

//Enable system for hero
function AS_Enable takes unit u, integer i returns nothing
    local integer id = GetPlayerId(GetOwningPlayer(u))
    
    if AS_Enabled[id] then
        call BJDebugMsg("Error: "+GetPlayerName(GetOwningPlayer(u))+" already has the system running.")
        return
    endif
    
    set AS_I0[id] = CreateItem(AS_Str,0,0)
    call SetItemVisible(AS_I0[id],false)
    
    set AS_I1[id] = CreateItem(AS_Att,0,0)
    call SetItemVisible(AS_I1[id],false)
    
    set AS_I2[id] = CreateItem(AS_Agi,0,0)
    call SetItemVisible(AS_I2[id],false)
    
    set AS_I3[id] = CreateItem(AS_Blank,0,0)
    call SetItemVisible(AS_I3[id],false)
    
    set AS_I4[id] = CreateItem(AS_Int,0,0)
    call SetItemVisible(AS_I4[id],false)
    
    set AS_I5[id] = CreateItem(AS_Cancel,0,0)
    call SetItemVisible(AS_I5[id],false)
    call SetItemDroppable(AS_I5[id],false)
    
    set AS_LastLevel[id] = GetHeroLevel(u)
    set AS_Enabled[id] = true
    set AS_PointsPerLevel[id] = i
endfunction

//When the hero levels up
private function AS_Level takes unit u returns nothing
    local integer points
    local integer id = GetPlayerId(GetOwningPlayer(u))
    
    if AS_Enabled[id]==true then
        set points = ((GetHeroLevel(u)-AS_LastLevel[id])*AS_PointsPerLevel[id])+AS_Points[id]
        set AS_LastLevel[id] = GetHeroLevel(u)
        if GetUnitAbilityLevel(u,AS_Abil)<1 and AS_On[id]==false then
            call UnitAddAbility(u,AS_Abil) 
            call UnitMakeAbilityPermanent(u,true,AS_Abil)
        endif
        if AS_On[id]==true then
            call SetItemCharges(AS_I1[id],points)
        endif
        set AS_Points[id] = points
        call IssueImmediateOrder(u,"replenishon") 
    endif  
endfunction

//Swap the inventory
private function AS_SwapInv takes unit u, integer whichway returns nothing
    local integer id = GetPlayerId(GetOwningPlayer(u))
    local item it
    
    if whichway==1 then
        set AS_IAtt0[id] = UnitRemoveItemFromSlot(u,0)
        call SetItemVisible(AS_IAtt0[id],false)
        call SetItemVisible(AS_I0[id],true)
        call UnitAddItem(u,AS_I0[id])
        
        set AS_IAtt1[id] = UnitRemoveItemFromSlot(u,1)
        call SetItemVisible(AS_IAtt1[id],false)
        call SetItemCharges(AS_I1[id],AS_Points[id])
        call SetItemVisible(AS_I1[id],true)
        call UnitAddItem(u,AS_I1[id])
        
        set AS_IAtt2[id] = UnitRemoveItemFromSlot(u,2)
        call SetItemVisible(AS_IAtt2[id],false)
        call SetItemVisible(AS_I2[id],true)
        call UnitAddItem(u,AS_I2[id])
        
        set AS_IAtt3[id] = UnitRemoveItemFromSlot(u,3)
        call SetItemVisible(AS_IAtt3[id],false)
        call SetItemVisible(AS_I3[id],true)
        call UnitAddItem(u,AS_I3[id])
        
        set AS_IAtt4[id] = UnitRemoveItemFromSlot(u,4)
        call SetItemVisible(AS_IAtt4[id],false)
        call SetItemVisible(AS_I4[id],true)
        call UnitAddItem(u,AS_I4[id])
        
        set AS_IAtt5[id] = UnitRemoveItemFromSlot(u,5)
        call SetItemVisible(AS_IAtt5[id],false)
        call SetItemVisible(AS_I5[id],true)
        call UnitAddItem(u,AS_I5[id])
        
        set AS_On[id] = true
    else
        set it = UnitRemoveItemFromSlot(u,0)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt0[id],true)
        call UnitAddItem(u,AS_IAtt0[id])
        
        set it = UnitRemoveItemFromSlot(u,1)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt1[id],true)
        call UnitAddItem(u,AS_IAtt1[id])
        
        set it = UnitRemoveItemFromSlot(u,2)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt2[id],true)
        call UnitAddItem(u,AS_IAtt2[id])
        
        set it = UnitRemoveItemFromSlot(u,3)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt3[id],true)
        call UnitAddItem(u,AS_IAtt3[id])
        
        set it = UnitRemoveItemFromSlot(u,4)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt4[id],true)
        call UnitAddItem(u,AS_IAtt4[id])
        
        set it = UnitRemoveItemFromSlot(u,5)
        call SetItemVisible(it,false)
        call SetItemVisible(AS_IAtt5[id],true)
        call UnitAddItem(u,AS_IAtt5[id])
        
        set AS_On[id] = false
    endif    
    
    set it = null
endfunction

//Returns what slot the item is in
private function AS_GetItemSlot takes unit hero, item it returns integer
    local integer i = 0
	
    loop
        exitwhen i==6
        if UnitItemInSlot(hero,i)==it then
            return i
        endif
        set i = i + 1
    endloop
	
    return -1
endfunction

//**Working Functions**\\

//Add attribute option when the hero levels
private function AS_level_Actions takes nothing returns nothing
    call AS_Level(GetTriggerUnit())
endfunction

//Swap inventory for the hero when needed
private function AS_TRO_Child takes nothing returns nothing
    call IssueImmediateOrder(AS_Hero,"replenishon")
    call IssueImmediateOrder(AS_Hero,"replenishlifeoff")
    
    call ReleaseTimer(GetExpiredTimer() )
endfunction
private function AS_TRO takes unit hero returns nothing
    set AS_Hero = hero
    call TimerStart(NewTimer(),0.,false,function AS_TRO_Child)
endfunction 
private function AS_inven_Actions takes nothing returns nothing
    local unit hero = GetTriggerUnit()
    local integer order = GetIssuedOrderId()
    local integer temp = 0
    
    if not IsUnitType(hero,UNIT_TYPE_HERO) then
        set hero = null
        return
    endif
    if order==OrderId("replenish") then
        call UnitRemoveAbility(hero,AS_Abil)
        call AS_SwapInv(hero,1)
    elseif order==OrderId("replenishoff") then
        call AS_TRO(hero)
    endif 
	
    set hero = null
endfunction

//Add attributes when selected
private function AS_att_Actions takes nothing returns nothing
    local unit hero = GetManipulatingUnit()
    local item used = GetManipulatedItem()
    local integer id = GetPlayerId(GetOwningPlayer(hero))
    local integer array temp
	
    if not AS_On[id] then
        set hero = null
        set used = null
        return
    endif
    set temp[0] = AS_GetItemSlot(hero,used)
    set temp[1] = AS_Points[id]-1
    if temp[0]!=5 or temp[1]<0 then
        call ModifyHeroStat( temp[0]/2,hero,bj_MODIFYMETHOD_ADD,1)
        set AS_Points[id] = temp[1]
        call SetItemCharges(AS_I1[id],temp[1])
        if temp[1]==0 then
            call AS_SwapInv(hero,0)
        endif
    elseif temp[1]>=0 then
        call AS_SwapInv(hero,0)
        call UnitAddAbility(hero,AS_Abil)
        call UnitMakeAbilityPermanent(hero,true,AS_Abil)
        call IssueImmediateOrder(hero,"replenishon")
    else
        call AS_SwapInv(hero,0)
    endif
	
    set hero = null
    set used = null
endfunction

//===========================================================================
private function Init_AttributeSystem takes nothing returns nothing
    local trigger level = CreateTrigger() 
    local trigger inven = CreateTrigger() 
    local trigger att = CreateTrigger()     
    
    call TriggerRegisterAnyUnitEventBJ(level, EVENT_PLAYER_HERO_LEVEL )
    call TriggerAddAction( level, function AS_level_Actions )
    
    call TriggerRegisterAnyUnitEventBJ( inven, EVENT_PLAYER_UNIT_ISSUED_ORDER )
    call TriggerRegisterAnyUnitEventBJ( inven, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
    call TriggerRegisterAnyUnitEventBJ( inven, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER )
    call TriggerAddAction( inven, function AS_inven_Actions )
    
    call TriggerRegisterAnyUnitEventBJ( att, EVENT_PLAYER_UNIT_USE_ITEM )
    call TriggerAddAction( att, function AS_att_Actions )
endfunction   

endlibrary
.
In my map it doesn't work and i can't understand what the problem. If you can fix it, i'll thank you very much.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
library TimerUtils initializer init
//*********************************************************************
//* TimerUtils (red+blue+orange flavors for 1.24b+)
//* ----------
//*
//*  To implement it , create a custom text trigger called TimerUtils
//* and paste the contents of this script there.
//*
//*  To copy from a map to another, copy the trigger holding this
//* library to your map.
//*
//* (requires vJass)   More scripts: htt://www.wc3c.net
//*
//* For your timer needs:
//*  * Attaching
//*  * Recycling (with double-free protection)
//*
//* set t=NewTimer()      : Get a timer (alternative to CreateTimer)
//* ReleaseTimer(t)       : Relese a timer (alt to DestroyTimer)
//* SetTimerData(t,2)     : Attach value 2 to timer
//* GetTimerData(t)       : Get the timer&#039;s value.
//*                         You can assume a timer&#039;s value is 0
//*                         after NewTimer.
//*
//* Multi-flavor:
//*    Set USE_HASH_TABLE to true if you don&#039;t want to complicate your life.
//*
//* If you like speed and giberish try learning about the other flavors.
//*
//********************************************************************

//================================================================
    globals
        //How to tweak timer utils:
        // USE_HASH_TABLE = true  (new blue)
        //  * SAFEST
        //  * SLOWEST (though hash tables are kind of fast)
        //
        // USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = true  (orange)
        //  * kinda safe (except there is a limit in the number of timers)
        //  * ALMOST FAST
        //
        // USE_HASH_TABLE = false, USE_FLEXIBLE_OFFSET = false (red)
        //  * THE FASTEST (though is only  faster than the previous method
        //                  after using the optimizer on the map)
        //  * THE LEAST SAFE ( you may have to tweak OFSSET manually for it to
        //                     work)
        //
        private constant boolean USE_HASH_TABLE      = true
        private constant boolean USE_FLEXIBLE_OFFSET = false

        private constant integer OFFSET     = 0x100000
        private          integer VOFFSET    = OFFSET
              
        //Timers to preload at map init:
        private constant integer QUANTITY   = 256
        
        //Changing this  to something big will allow you to keep recycling
        // timers even when there are already AN INCREDIBLE AMOUNT of timers in
        // the stack. But it will make things far slower so that&#039;s probably a bad idea...
        private constant integer ARRAY_SIZE = 8190

    endglobals

    //==================================================================================================
    globals
        private integer array data[ARRAY_SIZE]
        private hashtable     ht
    endglobals

    //It is dependent on jasshelper&#039;s recent inlining optimization in order to perform correctly.
    function SetTimerData takes timer t, integer value returns nothing
        static if(USE_HASH_TABLE) then
            // new blue
            call SaveInteger(ht,0,GetHandleId(t), value)
            
        elseif (USE_FLEXIBLE_OFFSET) then
            // orange
            static if (DEBUG_MODE) then
                if(GetHandleId(t)-VOFFSET&lt;0) then
                    call BJDebugMsg(&quot;SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer&quot;)
                endif
            endif
            set data[GetHandleId(t)-VOFFSET]=value
        else
            // new red
            static if (DEBUG_MODE) then
                if(GetHandleId(t)-OFFSET&lt;0) then
                    call BJDebugMsg(&quot;SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer&quot;)
                endif
            endif
            set data[GetHandleId(t)-OFFSET]=value
        endif        
    endfunction

    function GetTimerData takes timer t returns integer
        static if(USE_HASH_TABLE) then
            // new blue
            return LoadInteger(ht,0,GetHandleId(t) )
            
        elseif (USE_FLEXIBLE_OFFSET) then
            // orange
            static if (DEBUG_MODE) then
                if(GetHandleId(t)-VOFFSET&lt;0) then
                    call BJDebugMsg(&quot;SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer&quot;)
                endif
            endif
            return data[GetHandleId(t)-VOFFSET]
        else
            // new red
            static if (DEBUG_MODE) then
                if(GetHandleId(t)-OFFSET&lt;0) then
                    call BJDebugMsg(&quot;SetTimerData: Wrong handle id, only use SetTimerData on timers created by NewTimer&quot;)
                endif
            endif
            return data[GetHandleId(t)-OFFSET]
        endif        
    endfunction

    //==========================================================================================
    globals
        private timer array tT[ARRAY_SIZE]
        private integer tN = 0
        private constant integer HELD=0x28829022
        //use a totally random number here, the more improbable someone uses it, the better.
    endglobals

    //==========================================================================================
    function NewTimer takes nothing returns timer
        if (tN==0) then
            //If this happens then the QUANTITY rule has already been broken, try to fix the
            // issue, else fail.
            debug call BJDebugMsg(&quot;NewTimer: Warning, Exceeding TimerUtils_QUANTITY, make sure all timers are getting recycled correctly&quot;)
            static if( not USE_HASH_TABLE) then
                debug call BJDebugMsg(&quot;In case of errors, please increase it accordingly, or set TimerUtils_USE_HASH_TABLE to true&quot;)
                set tT[0]=CreateTimer()
                static if( USE_FLEXIBLE_OFFSET) then
                    if (GetHandleId(tT[0])-VOFFSET&lt;0) or (GetHandleId(tT[0])-VOFFSET&gt;=ARRAY_SIZE) then
                        //all right, couldn&#039;t fix it
                        call BJDebugMsg(&quot;NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.&quot;)
                        return null
                    endif
                else
                    if (GetHandleId(tT[0])-OFFSET&lt;0) or (GetHandleId(tT[0])-OFFSET&gt;=ARRAY_SIZE) then
                        //all right, couldn&#039;t fix it
                        call BJDebugMsg(&quot;NewTimer: Unable to allocate a timer, you should probably set TimerUtils_USE_HASH_TABLE to true or fix timer leaks.&quot;)
                        return null
                    endif
                endif
            endif
        else
            set tN=tN-1
        endif
        call SetTimerData(tT[tN],0)
     return tT[tN]
    endfunction

    //==========================================================================================
    function ReleaseTimer takes timer t returns nothing
        if(t==null) then
            debug call BJDebugMsg(&quot;Warning: attempt to release a null timer&quot;)
            return
        endif
        if (tN==ARRAY_SIZE) then
            debug call BJDebugMsg(&quot;Warning: Timer stack is full, destroying timer!!&quot;)

            //stack is full, the map already has much more troubles than the chance of bug
            call DestroyTimer(t)
        else
            call PauseTimer(t)
            if(GetTimerData(t)==HELD) then
                debug call BJDebugMsg(&quot;Warning: ReleaseTimer: Double free!&quot;)
                return
            endif
            call SetTimerData(t,HELD)
            set tT[tN]=t
            set tN=tN+1
        endif    
    endfunction

    private function init takes nothing returns nothing
     local integer i=0
     local integer o=-1
     local boolean oops = false
     
        static if( USE_HASH_TABLE ) then
            set ht = InitHashtable()
            loop
                exitwhen(i==QUANTITY)
                set tT<i>=CreateTimer()
                call SetTimerData(tT<i>, HELD)
                set i=i+1
            endloop
            set tN = QUANTITY
        else
            loop
                set i=0
                loop
                    exitwhen (i==QUANTITY)
                    set tT<i> = CreateTimer()
                    if(i==0) then
                        set VOFFSET = GetHandleId(tT<i>)
                        static if(USE_FLEXIBLE_OFFSET) then
                            set o=VOFFSET
                        else
                            set o=OFFSET
                        endif
                    endif
                    if (GetHandleId(tT<i>)-o&gt;=ARRAY_SIZE) then
                        exitwhen true
                    endif
                    if (GetHandleId(tT<i>)-o&gt;=0)  then
                        set i=i+1
                    endif
                endloop
                set tN = i
                exitwhen(tN == QUANTITY)
                set oops = true
                exitwhen not USE_FLEXIBLE_OFFSET
                debug call BJDebugMsg(&quot;TimerUtils_init: Failed a initialization attempt, will try again&quot;)               
            endloop
            
            if(oops) then
                static if ( USE_FLEXIBLE_OFFSET) then
                    debug call BJDebugMsg(&quot;The problem has been fixed.&quot;)
                    //If this message doesn&#039;t appear then there is so much
                    //handle id fragmentation that it was impossible to preload
                    //so many timers and the thread crashed! Therefore this
                    //debug message is useful.
                elseif(DEBUG_MODE) then
                    call BJDebugMsg(&quot;There were problems and the new timer limit is &quot;+I2S(i))
                    call BJDebugMsg(&quot;This is a rare ocurrence, if the timer limit is too low:&quot;)
                    call BJDebugMsg(&quot;a) Change USE_FLEXIBLE_OFFSET to true (reduces performance a little)&quot;)
                    call BJDebugMsg(&quot;b) or try changing OFFSET to &quot;+I2S(VOFFSET) )
                endif
            endif
        endif

    endfunction

endlibrary
</i></i></i></i></i></i>

JASS:
library CSSafety requires TimerUtils
endlibrary

Try this.
Implement TimerUtils and replace the code in CSSafety with the new CSSafety library.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
I meant what is the problem for "it doesn't work"? :)
Jasshelper threw error or Warcraft 3 won't launch the map?
 

PT.Dyland

New Member
Reaction score
0
It means: when I was on my map, if the system operates, every hero level up will add AS_Abil for heroes. But in my map as heroes level up, no matter what happens, when this system may be installed so
 

emjlr3

Change can be a good thing
Reaction score
395
it uses NewTimer/ReleaseTimer

so any variant should work fine.

remember you have to manually register units with [ljass]AS_Enable takes unit u, integer i[/ljass] where i is the number of stat points to grant each level. 1 unit/player max.
 

Laiev

Hey Listen!!
Reaction score
188
Trigger:
  • MyTrigger
    • Events
      • Something...
    • Coditions
      • Something if need...
    • Actions
      • set TempUnit = &lt;my unit&gt;
      • set TempInteger = &lt;some value, this is the amount of stats the TempUnit will get when level up&gt;
      • Custom Script: call AS_Enable (udg_TempUnit, udg_TempInteger)
 

emjlr3

Change can be a good thing
Reaction score
395
I wrote a detailed readme with the system - use it
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top