System Autocast System

Status
Not open for further replies.

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Code:
//====================================================================================
//The following Triggers may be removed if you have Kattana's System already in place.
//====================================================================================

function H2I takes handle H returns integer
    return H
    return 0
endfunction

function LocalVars takes nothing returns gamecache
    return InitGameCache("jasslocalvars.w3v")
    //======Replace "InitGameCache("jasslocalvars.w3v")" with a global Variable!======
endfunction

function GetHandleTimer takes handle h, string key returns timer
    return GetStoredInteger(LocalVars(),I2S(H2I(h)),key)
    return null
endfunction

function GetHandleString takes handle h, string key returns string
    return GetStoredInteger(LocalVars(),I2S(H2I(h)),key)
    return ""
endfunction

function GetHandleUnit takes handle h, string key returns unit
    return GetStoredInteger(LocalVars(),I2S(H2I(h)),key)
    return null
endfunction

//====================================================================================
//=============The following functions must be implemented into the map.==============
//====================================================================================

function StopAutoCast takes unit Caster returns nothing
//================Call this function to stop a unit from autocasting.=================
    local string S = GetHandleString(Caster,"O")
    call DestroyTimer(GetHandleTimer(Caster,S))
    call FlushStoredMission(LocalVars(),S)
    call FlushStoredMission(LocalVars(),I2S(H2I(Caster)))
    set S = ""
endfunction

function AutoCastAbil takes nothing returns nothing
    local timer T = GetExpiredTimer()
    local unit u = GetHandleUnit(T,"u")
    local string O = GetHandleString (T,"O")
    call IssueImmediateOrder(u,O)
    set T = null
    set u = null
endfunction

function AutoCastInit takes unit Caster, string Order, real CoolDown returns nothing
//==========This is the Init function.  Call it to start a unit autocasting.==========
    local timer T = CreateTimer()
    local string O = Order
    local string t = I2S(H2I(T))
    local unit u = Caster
    call StoreInteger(LocalVars(),I2S(H2I(Caster)),O,H2I(T))
    call StoreInteger(LocalVars(),t,"O",S2I(Order))
    call StoreInteger(LocalVars(),t,"u",H2I(u))
    call TimerStart(T,CoolDown + 0.01,true,function AutoCastAbil)
	set O = ""
    set u = null
    set t = ""
    set T = null
endfunction

//========Description========//
This is an autocasting system, for any spell. To make a unit start autocasting, simply call the last function (AutoCastInit), with the Caster, Ability order, and the cooldown of the ability. Once you call this function, it will constantly order the unit to cast, but only when the cooldown of the spell has finished. It is compatable with Kattana's Local Handle Variable system, as well as Vexorian's Caster System.

//========Implement Instructions========//
In order to use this in your map, copy and paste the code into your map header, and change the LocalVars function to include your global gamecache. You must also properly initialize the game cache, in a trigger that runs at map initialization, like this:

Code:
Custom Script:    call FlushGameCache(InitGameCache("udg_GC"))
Custom Script:    set udg_GC = InitGameCache("udg_GC")

When an autocasting unit dies, you will have to call the StopAutoCast function with your dieing unit, so that the function will not keep ordering the dead unit to cast.

//========Features========//
-Accurate timer orders
-Completely MUI (Multi-Instanceable)
-Leakless
-An easy-off function to stop an autocasting unit, and remove all useless attached variables.
-Simple to use

Please give credit if you use this in your map! :)
 
Z

Zync

Guest
A tip or 2!

Leaks:
- You never destroy your timers in any of your function, you just set it to null.
Extra Steps:
- Why do you have the local variable "O" in AutoCastInit? Just use the Order paramater.
- Why do you "set O = null" (or "t = null")? It's a native string, that doesn't leak, only Objects leak.
Remark:
- This implementation only works for an instant spell. That is, one that does not target unit, location, region, or a group. (And who would want to auto-cast stomp, especially when there is no notation?) Since you're using JASS, you should include different functionality based on at least some of these target types. Afterall, this will never be used by another who can JASS, it's too specific, and what you have takes less time to write your own than it does to search through these forums.

Although this sort of thing is good practice! =D
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Yeah, this is the type of thing I did in my spare time in my Drafting class at school. :p

>> - You never destroy your timers in any of your function, you just set it to null.

I know. I wanted it to be able to be used on any unit, and if I destroyed the timer, it would stop autocasting. So I added another function (function StopAutoCast) to destroy the timer, so that way the mapper could stop the autocast anytime he wished.

>> - Why do you "set O = null" (or "t = null")?

Well you do have to nullify all locals. Handles leak for any type of variable, whereas all locals leak except for integers and reals.

This implementation only works for an instant spell. That is, one that does not target unit, location, region, or a group. (And who would want to auto-cast stomp, especially when there is no notation?) Afterall, this will never be used by another who can JASS, it's too specific, and what you have takes less time to write your own than it does to search through these forums.

Well I remember many times wanting an aoe spell to autocast. I tried to add very specific implement instructions, so that any mapper (including non-JASSers) would be able to use this easily.

Thanks for the feedback though. :eek:
 

Prometheus

Everything is mutable; nothing is sacred
Reaction score
589
I might want to use this but your implement instructions for this aren't very good, care to elaborate?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
This system was a big failure, as it required someone to know the order string, didn't allow for insufficient mana. Plus, it's based on the very outdated Kattana's Handle Variable system. :p

Can a mod/admin please move this to the graveyard? :p

Thanks :D
 
Status
Not open for further replies.
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