• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] [Needs opinion] MiscTable

Because i wanna save damagetypes and attacktypes.
JASS:
library MiscTable /* v 1.1
*************************************************************************************
*
*    Miscellaneous Table is a table for child handles that can't be Saved/
*    Loaded through hashtables. Miscellaneous Table will allow users
*    to save these kinds of handles to hashtables.
*
*    Basically,the system is an integer table.
*
*************************************************************************************
*
*    Credits
*
*        Bribe
*        -----------------------
*
*           - For his Handle Library which Im inspired of.
*           - For his Table Library
*
*        weaaddar
*        -----------------------
*
*           - for discovering such typecasting method.
*
*        PurgeAndFire111
*        -----------------------
*
*           - for showing me Bribe's Handle Library and Typecasting methods
*
*************************************************************************************
*
*    API
*
*    NOTE
*
*        Some codes here are basically CnPed from Table
*
*    -----------------------
*    struct MiscTable
*    
*        static method create takes nothing returns MiscTable
*            creates a new MiscTable
*
*        method destroy takes nothing returns nothing
*            destroys MiscTable instance
*
*        method flush takes nothing returns nothing
*            clear all saved datas from an instance
*
*        method remove takes integer key returns nothing
*            remove assigned value
*
*        method operator []= takes integer key, $TYPE$ value returns nothing
*            assigns "key" as an index of value
*
*        method operator [] takes integer key returns $TYPE$
*            retrieves value from "key" index
*        
*        method has takes integer key returns boolean
*            check if an instance contains value that has "key index
*
*************************************************************************************/
    globals 
        private hashtable ht = InitHashtable()
        
        private key sizeK
        private key listK
    endglobals
    
    private struct dex extends array
        static method operator size takes nothing returns MiscTable
            return sizeK
        endmethod
        static method operator list takes nothing returns MiscTable
            return listK
        endmethod
    endstruct
    
    //! textmacro HANDLE_ARRAY takes TYPE , FUNC
    private struct $TYPE$str extends array
        method operator [] takes integer key returns $TYPE$
            return Convert$FUNC$(LoadInteger(ht, this, key))
        endmethod
        
        method operator []= takes integer key , $TYPE$ value returns nothing
            call SaveInteger(ht, this, key, GetHandleId(value))
        endmethod
    endstruct
    
    private module $TYPE$mod
        method operator $TYPE$ takes nothing returns $TYPE$s
            return this
        endmethod
    endmodule
    //! endtextmacro()

    //! runtextmacro HANDLE_ARRAY("Race", "race")
    //! runtextmacro HANDLE_ARRAY("AllianceType", "alliancetype")
    //! runtextmacro HANDLE_ARRAY("RacePref", "racepreference")
    //! runtextmacro HANDLE_ARRAY("IGameState", "igamestate")
    //! runtextmacro HANDLE_ARRAY("FGameState", "fgamestate")
    //! runtextmacro HANDLE_ARRAY("PlayerState", "playerstate")
    //! runtextmacro HANDLE_ARRAY("PlayerScore", "playerscore")
    //! runtextmacro HANDLE_ARRAY("PlayerGameResult", "playergameresult")
    //! runtextmacro HANDLE_ARRAY("UnitState", "unitstate")
    //! runtextmacro HANDLE_ARRAY("AIDifficulty", "aidifficulty")
    //! runtextmacro HANDLE_ARRAY("GameEvent", "gameevent")
    //! runtextmacro HANDLE_ARRAY("PlayerEvent", "playerevent")
    //! runtextmacro HANDLE_ARRAY("PlayerUnitEvent", "playerunitevent")
    //! runtextmacro HANDLE_ARRAY("WidgetEvent", "widgetevent")
    //! runtextmacro HANDLE_ARRAY("DialogEvent", "dialogevent")
    //! runtextmacro HANDLE_ARRAY("UnitEvent", "unitevent")
    //! runtextmacro HANDLE_ARRAY("LimitOp", "limitop")
    //! runtextmacro HANDLE_ARRAY("UnitType", "unittype")
    //! runtextmacro HANDLE_ARRAY("GameSpeed", "gamespeed")
    //! runtextmacro HANDLE_ARRAY("Placement", "placement")
    //! runtextmacro HANDLE_ARRAY("StartLocPrio", "startlocprio")
    //! runtextmacro HANDLE_ARRAY("GameDifficulty", "gamedifficulty")
    //! runtextmacro HANDLE_ARRAY("GameType", "gametype")
    //! runtextmacro HANDLE_ARRAY("MapFlag", "mapflag")
    //! runtextmacro HANDLE_ARRAY("MapVisibility", "mapvisibility")
    //! runtextmacro HANDLE_ARRAY("MapSetting", "mapsetting")
    //! runtextmacro HANDLE_ARRAY("MapDensity", "mapdensity")
    //! runtextmacro HANDLE_ARRAY("MapControl", "mapcontrol")
    //! runtextmacro HANDLE_ARRAY("PlayerColor", "playercolor")
    //! runtextmacro HANDLE_ARRAY("PlayerSlotState", "playerslotstate")
    //! runtextmacro HANDLE_ARRAY("VolumeGroup", "volumegroup")
    //! runtextmacro HANDLE_ARRAY("CameraField", "camerafield")
    //! runtextmacro HANDLE_ARRAY("BlendMode", "blendmode")
    //! runtextmacro HANDLE_ARRAY("RarityControl", "raritycontrol")
    //! runtextmacro HANDLE_ARRAY("TexMapFlags", "texmapflags")
    //! runtextmacro HANDLE_ARRAY("EffectType", "effecttype")
    //! runtextmacro HANDLE_ARRAY("Version", "version")
    //! runtextmacro HANDLE_ARRAY("ItemType", "itemtype")
    //! runtextmacro HANDLE_ARRAY("AttackType", "attacktype")
    //! runtextmacro HANDLE_ARRAY("DamageType", "damagetype")
    //! runtextmacro HANDLE_ARRAY("WeaponType", "weapontype")
    //! runtextmacro HANDLE_ARRAY("SoundType", "soundtype")
    //! runtextmacro HANDLE_ARRAY("PathingType", "pathingtype")
    
    struct MiscTable extends array
        
        /*
            implement all macro modules for syntax
        */
        
        implement racemod
        implement alliancetypemod
        implement racepreferencemod
        implement fgamestatemod
        implement igamestatemod
        implement playerstatemod
        implement playerscoremod
        implement playergameresultmod
        implement unitstatemod
        implement aidifficultymod
        implement gameeventmod
        implement playereventmod
        implement playeruniteventmod
        implement widgeteventmod
        implement dialogeventmod
        implement uniteventmod
        implement limitopmod
        implement unittypemod
        implement gamespeedmod
        implement placementmod
        implement startlocpriomod
        implement gamedifficultymod
        implement gametypemod
        implement mapflagmod
        implement mapdensitymod
        implement mapcontrolmod
        implement mapsettingmod
        implement mapvisibilitymod
        implement playercolormod
        implement playerslotstatemod
        implement volumegroupmod
        implement camerafieldmod
        implement blendmodemod
        implement raritycontrolmod
        implement texmapflagsmod
        implement effecttypemod
        implement versionmod
        implement itemtypemod
        implement attacktypemod
        implement damagetypemod
        implement weapontypemod
        implement soundtypemod
        implement pathingtypemod
        
        method has takes integer key returns boolean
            return HaveSavedInteger(ht, this, key)
        endmethod
        
        method remove takes integer key returns nothing
            call RemoveSavedInteger(ht, this, key)
        endmethod
        
        method flush takes nothing returns nothing
            call FlushChildHashtable(ht, this)
        endmethod
        
        static method create takes nothing returns MiscTable
            local MiscTable this = dex.list[0]
        
            if this == 0 then
                set this = more + 1
                set more = this
            else
                set dex.list[0] = dex.list[this]
                call dex.list.remove(this) //Clear hashed memory
            endif
        
            return this
        endmethod
        
        method destroy takes nothing returns nothing
            call this.flush()
        
            set dex.list[this] = dex.list[0]
            set dex.list[0] = this
        endmethod
    endstruct
endlibrary

Demo
JASS:
/*
    Create an instance
*/
local MiscTable mt = MiscTable.create()

/*
    Use its methods
*/
local boolean cached = mt.has(1245)
set mt.attacktype[ GetHandleId(GetTriggerUnit()) ] = ATTACK_TYPE_CHAOS
set mt.damagetype[ 14241241 ] = DAMAGE_TYPE_DIVINE

/*
    Flush or Destroy
*/
call mt.flush()
call mt.destroy()

Changelogs:
- Removed the handle struct
 
Last edited:
Level 17
Joined
Apr 27, 2008
Messages
2,455
Huh, i'm missiing something or Convert functions are missing ?
Also, you don't really need to typecast just "hash" the handle id to a struct instance and with this struct instance you set an handle (whatever type you need) member.
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
Huh, i'm missiing something or Convert functions are missing ?
Also, you don't really need to typecast just "hash" the handle id to a struct instance and with this struct instance you set an handle (whatever type you need) member.

Typecasting should be significantly faster than using a struct shim, although I won't test it myself since I'll never use this =/
 
It doesn't even compile -_-

If you haven't even tested it, this doesn't have an actual purpose. The integers that are used by those states are aways less than an array size, just an FYI.

No, I don't see a use for this. And because you haven't even tested it, it looks like you don't have a use for it either, other than "because it's cool".

This resource can serve a nice spot in the graveyard, right next to Handle.
 
Top