|
WC3JASS.com latest Snippets
Created by nel December 21, 2018, 03:09:29 AM
Game Clock It allows you format the time and get the elapsed time. //! zinc library GameClock { /* v1.0.0.7 ************************************************************************************* * * Game Clock * by nel * * It allows you format the time and get the elapsed time. * ************************************************************************************ * * SETTING * */ private constant string DELIMITER = " : "; /* It used to seperate the hour, minute, and second */ /* ************************************************************************************ * * struct GameClock extends array * * static method FormatTime takes real seconds return string * - It returns time as hh/mm/ss format. * * static method operator ElapsedTime takes nothing returns real * - It returns elapsed time. * * static method operator ElapsedTimeEx takes nothing returns string * - It returns elapsed time as hh/mm/ss format. * * static method operator Delimiter takes nothing returns string * - It returns GameClock's delimiter. * ************************************************************************************* * * Issue: * * This library will not give you an exact current elapsed time. * ************************************************************************************/ //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter if( $Time$ < 10 ) { format = format + "0" + I2S( $Time$ ) $delimiter$; } else { format = format + I2S( $Time$ ) $delimiter$; } //! endtextmacro public { struct GameClock [] { public { static method FormatTime( real seconds ) -> string { //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" ) //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" ) //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" ) return format; } static method operator ElapsedTime() -> real { } static method operator ElapsedTimeEx() -> string { } static method operator Delimiter() -> string { return DELIMITER; } } private static method onInit() { } }} } //! endzinc
Created by nel November 28, 2018, 11:50:18 PM
NoSaveGame It prevents to use Save Game function to all players. //! zinc library NoSaveGame /* v1.0.0.5 ************************************************************************************ * * No Save Game * by nel * * It prevents to use Save Game function to all players. * ***********************************************************************************/ requires optional SimError, /* http://www.wc3c.net/showthread.php?t=101260 */ optional RegisterGameEvent /* https://www.hiveworkshop.com/threads/snippet-registerevent-pack.250266/ */ /*********************************************************************************** * * SETTINGS * */ { public { /* * It's a content of the error message. */ string nsgErrMsg = "You are not allowed to use Save Game."; /* * Toggleable Error Message. */ /* * Toggleable NoSaveGame. */ } /* ***********************************************************************************/ private { struct NoSaveGame [] { private static method onInit() { static if( LIBRARY_RegisterGameEvent ) { if( nsg ) { static if( LIBRARY_SimError ) { SimError(pl, nsgErrMsg); } else { "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "|cffffcc00" + nsgErrMsg + "|r" ); } pl = null; } }); if( nsg ) { } return false; })); Stop = null; } else { if( nsg ) { if( nsgDisplayErrMsg ) { static if( LIBRARY_SimError ) { SimError(pl, nsgErrMsg); } else { "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "|cffffcc00" + nsgErrMsg + "|r" ); } } pl = null; } return false; })); if( nsg ) { } return false; })); Save = null; Stop = null; } }} } } //! endzinc
Created by AGD November 08, 2016, 08:04:14 AM
True and False BooleanExpressions Credits goes to Vexorian for the first idea. Vjass versionlibrary BoolexprUtils globals endglobals private module Init private static method filterTrue takes nothing returns boolean return true endmethod private static method filterFalse takes nothing returns boolean return false endmethod private static method onInit takes nothing returns nothing set BOOLEXPR_TRUE = Filter( function thistype.filterTrue) set BOOLEXPR_TRUE = Filter( function thistype.filterFalse) endmethod endmodule private struct S extends array implement Init endstruct endlibrary
Zinc version//! zinc library BoolexprUtils { public boolexpr BOOLEXPR_TRUE, BOOLEXPR_FALSE; module Init { static method onInit() { } } struct S extends array {module Init;} } //! endzinc
Created by AGD November 02, 2016, 03:36:36 AM
A useful library which allows you to recycle units (even dead ones, but they must leave a corpse), avoiding yet another permanent 0.04kb memory leak for each future CreateUnit() call. Scriptlibrary UnitRecycler /* v1.3b |=============| | Author: AGD | |=============| */requires /* */ReviveUnit /* https://wc3modding.info/4469/snippet-reviveunit/ */UnitDex /* http://www.hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/ */optional Table /* https://wc3modding.info/4611/snippet-new-table/ */optional TimerUtils /* https://wc3modding.info/5352/hibrid-timerutils/ */optional RegisterPlayerUnitEvent /* https://wc3modding.info/4907/snippet-registerplayerunitevent/ This system is important because CreateUnit() is one of the most processor-intensive function in the game and there are reports that even after they are removed, they still leave some bit of memory consumption (0.04 KB) on the RAM. Therefore it would be very helpful if you can minimize unit creation or so. This system also allows you to recycle dead units to avoid permanent 0.04 KB memory leak for each future CreateUnit() call. */ //! novjass [Credits] Aniki - For suggesting ideas on further improvements |-----| | API | |-----| - Returns unit of specified ID from the stock of recycled units. If there's none in the stock that matched the specified unit's rawcode, it will create a new unit instead - Returns null if the rawcode's unit-type is a hero or non-existent - Works similar to GetRecycledUnit() except that if the input rawcode's unit-type is a hero, it will be created via CreateUnit() instead - You can use this as an alternative to CreateUnit() - Recycles the specified unit and returns a boolean value depending on the success of the operation - Does nothing to hero units */function RecycleUnitEx takes unit u returns boolean/* - Works similar to RecycleUnit() except that if <u> is not recyclable, it will be removed via RemoveUnit() instead - You can use this as an alternative to RemoveUnit() */function RecycleUnitDelayed takes unit u, real delay returns nothing/* - Recycles the specified unit after <delay> seconds */function RecycleUnitDelayedEx takes unit u, real delay returns nothing/* - Works similar to RecycleUnitDelayed() except that it calls RecycleUnitEx() instead of RecycleUnit() - Creates a unit of type ID and adds it to the stock of recycled units then returns a boolean value depending on the success of the operation *///! endnovjass //CONFIGURATION SECTION globals /* The owner of the stocked/recycled units /* Determines if dead units will be automatically recycled after a delay designated by the <constant function DeathTime below> */ private constant boolean AUTO_RECYCLE_DEAD = true /* Error debug message prefix */ private constant string ERROR_PREFIX = "|CFFFF0000Operation Failed: " endglobals /* The delay before dead units will be recycled in case AUTO_RECYCLE_DEAD == true */ static if AUTO_RECYCLE_DEAD then private constant function DeathTime takes unit u returns real /*if <condition> then return someValue elseif <condition> then return someValue endif */ return 8.00 endfunction endif /* When recycling a unit back to the stock, these resets will be applied to the unit. You can add more actions to this or you can delete this textmacro if you don't need it. */ //! textmacro_once UNIT_RECYCLER_RESET //! endtextmacro //END OF CONFIGURATION /*==== Do not do changes below this line if you're not so sure on what you're doing ====*/ globals private keyword S endglobals static if LIBRARY_Table then if i == 0 then set count = count + 1 set S.table. integer[rawCode] = count set i = count endif else if i == 0 then set count = count + 1 set i = count endif endif return i endfunction static if DEBUG_MODE then private function Debug takes string msg returns nothing endfunction endif set i = GetIndex(rawCode) if stack[i] == 0 then else static if LIBRARY_Table then else endif set stack[i] = stack[i] - 1 endif debug call Debug(ERROR_PREFIX + "Specified unit-type does not exist") debug endif else debug call Debug(ERROR_PREFIX + "Attemp to retrieve a hero unit") return null endif endfunction return GetRecycledUnit(owner, rawCode, x, y, facing) endif debug call Debug("Cannot retrieve a hero unit, creating new unit") endfunction if not IsHeroUnitId(rawCode) and not stacked[uDex] and u != null then set i = GetIndex(rawCode) if not UnitAlive(u) and not ReviveUnit(u) then debug call Debug(ERROR_PREFIX + "Unable to recycle unit: Unable to revive dead unit") return false endif set stacked[uDex] = true //! runtextmacro optional UNIT_RECYCLER_RESET() set stack[i] = stack[i] + 1 static if LIBRARY_Table then set S.hash[i]. unit[stack[i]] = u else endif debug call Debug( "Successfully recycled " + GetUnitName(u)) return true debug else debug if stacked[uDex] then debug call Debug(ERROR_PREFIX + "Attempt to recycle an already recycled unit") debug elseif u == null then debug call Debug(ERROR_PREFIX + "Attempt to recycle a null unit") debug else debug call Debug(ERROR_PREFIX + "Attempt to recycle a hero unit") debug endif endif return false endfunction if not RecycleUnit(u) then debug call Debug("Cannot recycle the specified unit, removing unit") return false endif return true endfunction //! textmacro DELAYED_RECYCLE_TYPE takes EX private function RecycleTimer$EX$ takes nothing returns nothing static if LIBRARY_TimerUtils then call RecycleUnit$EX$(GetUnitById(GetTimerData(t))) call ReleaseTimer(t) else static if LIBRARY_Table then call RecycleUnit$EX$(S.hash[0]. unit[ key]) call S.hash[0].remove(key) else endif endif set t = null endfunction function RecycleUnitDelayed$EX$ takes unit u, real delay returns nothing static if LIBRARY_TimerUtils then call TimerStart(NewTimerEx(GetUnitId(u)), delay, false, function RecycleTimer$EX$) else static if LIBRARY_Table then else endif call TimerStart(t, delay, false, function RecycleTimer$EX$) set t = null endif endfunction //! endtextmacro //! runtextmacro DELAYED_RECYCLE_TYPE("") //! runtextmacro DELAYED_RECYCLE_TYPE("Ex") set u = CreateUnit(OWNER, rawCode, unitCampX, unitCampY, 270) if u != null then set i = GetIndex(rawCode) set stacked[GetUnitId(u)] = true set stack[i] = stack[i] + 1 static if LIBRARY_Table then set S.hash[i]. unit[stack[i]] = u else endif debug call Debug( "Adding " + GetUnitName(u) + " to stock") return true debug else debug call Debug(ERROR_PREFIX + "Attemp to stock a null unit") endif set u = null debug else debug call Debug(ERROR_PREFIX + "Attemp to stock a hero unit") endif return false endfunction static if AUTO_RECYCLE_DEAD then private function OnDeath takes nothing returns nothing call RecycleUnitDelayed(u, DeathTime(u)) endif set u = null endfunction endif private module Init static if LIBRARY_Table then static TableArray hash static Table table else endif private static method onInit takes nothing returns nothing static if AUTO_RECYCLE_DEAD then static if LIBRARY_RegisterPlayerUnitEvent then static if RPUE_VERSION_NEW then else endif else local code c = function OnDeath loop set i = i - 1 exitwhen i == 0 endloop endif endif static if LIBRARY_Table then set hash = TableArray[0x2000] set table = Table.create() endif // Hides recycled units at the top of the map beyond reach of the camera set unitCampX = 0.00 set bounds = null endmethod endmodule private struct S extends array implement Init endstruct endlibrary
Created by AGD September 04, 2016, 09:16:22 AM
This snippet is a replacement to the BJ function GetRandomSubGroup with the advantage of being able to directly enumerate random number of units in a group instead of creating another subgroup of an already existing group. Notice that this is not the same as GroupEnumUnitsInRangeCounted in which case the enumerated units are not random but is based on which units are picked first. library RandomIteration globals endglobals private function EnumUnits takes nothing returns boolean set unitCount = unitCount + 1 return false endfunction set chance = I2R(limit)/ I2R(unitCount) loop exitwhen u == null or limit == 0 set limit = limit - 1 endif endloop set u = null endfunction function EnumRandomUnitsInRectCounted takes group g, rect r, integer limit returns nothing set chance = I2R(limit)/ I2R(unitCount) loop exitwhen u == null or limit == 0 set limit = limit - 1 endif endloop set u = null endfunction endlibrary
Created by AGD September 04, 2016, 12:09:44 AM
A simple snippet used to check if a unit is invulnerable. False positives from mana shields are also accounted for. library InvulnerabilityChecker function CheckInvulnerability takes unit u returns boolean return check endfunction endlibrary
Child Boards
Coding Help
Ask questions and get answers on Jass Coding.
Jassdoc
This is a semi-formal, unofficial manual for the JASS scripting language used for scripting Maps and AI files in Blizzard Entertainment's Warcraft III. Because it is unofficial, there could be some errors and it may be incomplete. Please report errors and send suggestions for additions to moyack.
Jass Tutorials
Deposit your newly made Jass tutorials here. You may also view the other community-submitted tutorials.
|
|
|