• 🏆 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!

Please clean up code : (

Status
Not open for further replies.
Level 31
Joined
Jul 10, 2007
Messages
6,306
Got way too much stuff D:

reply to thread with things you want to clean up and I'll pm the code =)


IsPlayerActive(playerId) instead of IsPlaying(player)

no shorthanding allowed like pid, no weird things like displayd or enableda, and no weird spacing like 1+3*4

1+3*4 -> 1 + 3*4

u is acceptable for a method that deals with 1 and only 1 unit and would have a variable name nearly identical to the method name for that unit

example:

JASS:
method onDeindex takes nothing returns nothing
    local unit deindexedUnit = GetIndexedUnit() //u is acceptable here, but frowned upon : p
endmethod

Command Income (this one's totally messed up, haha)
Chatroom
ChatroomAll
ChatroomAllies
ChatroomObservers
ChatroomAdmin
Gameboard (cleaned this one up quite a bit, but it still needs cleaning)
Income
Rounds
Start Income
Prevent Command
Share Bounty
Apply Income
Unit Income
Transfer On Leave
Shrine Upgrade
Shrine
SendPlayer
Summon
Path
Add Income
Unit Team
Paths
Summoning Trail
Plans

Thanks

Before
JASS:
library Towers uses Bonus, UnitIndexer, Game
    struct Towers extends array
        private static integer array next
        private static integer array prev
        private static boolean array a
        static method operator [] takes integer pid returns thistype
            return next[pid/3]
        endmethod
        method operator n takes nothing returns thistype
            return next[this]
        endmethod
        private static method index takes nothing returns boolean
            local integer i
            local integer u
            if (GetUnitRace(GetIndexedUnit()) == RACE_UNDEAD and IsUnitType(GetIndexedUnit(), UNIT_TYPE_STRUCTURE) and not Race(GetUnitTypeId(GetIndexedUnit())).exists  and not RaceTech(GetUnitTypeId(GetIndexedUnit())).exists and not AdvRaceTech(GetUnitTypeId(GetIndexedUnit())).exists) then
                set i = GetPlayerId(GetOwningPlayer(GetIndexedUnit()))/3
                set u = GetIndexedUnitId()+1
                if (0 == next[i]) then
                    set next[i] = u
                    set next[u] = 0
                    set prev[u] = 0
                else
                    set next[u] = next[i]
                    set prev[u] = 0
                    set prev[next[u]] = u
                    set next[i] = u
                endif
                set a[u] = true
            endif
            return false
        endmethod
        private static method deindex takes nothing returns boolean
            local integer i
            local integer u
            local unit m
            
            if (0 == GetIndexedUnitId()) then
                set m = GetTriggerUnit()
            else
                set m = GetIndexedUnit()
            endif
            
            if (GetUnitRace(m) == RACE_UNDEAD and IsUnitType(m, UNIT_TYPE_STRUCTURE) and not Race(GetUnitTypeId(m)).exists  and not RaceTech(GetUnitTypeId(m)).exists and not AdvRaceTech(GetUnitTypeId(m)).exists) then
                if (0 == GetIndexedUnitId()) then
                    set i = GetPlayerId(GetTriggerPlayer())/3
                    set u = GetUnitUserData(m)+1
                else
                    set i = GetPlayerId(GetOwningPlayer(GetIndexedUnit()))/3
                    set u = GetIndexedUnitId()+1
                endif
                
                if (a[u]) then
                    if (0 == prev[u]) then
                        set next[i] = next[u]
                    else
                        set next[prev[u]] = next[u]
                    endif
                    if (0 != next[u]) then
                        set prev[next[u]] = prev[u]
                    endif
                    set a[u] = false
                endif
            endif
            set m = null
            return false
        endmethod
        private static method init takes nothing returns boolean
            call RegisterUnitIndexEvent(Condition(function thistype.index), UnitIndexer.INDEX)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, Condition(function thistype.deindex))
            call RegisterUnitIndexEvent(Condition(function thistype.deindex), UnitIndexer.DEINDEX)
            return false
        endmethod
        private static method onInit takes nothing returns nothing
            call Game.onInitialize.register(Condition(function thistype.init))
            call Game.onInitializeSolo.register(Condition(function thistype.init))
        endmethod
    endstruct
endlibrary

After
JASS:
module LinkedList
    readonly thistype next
    readonly thistype prev
    
    method add takes thistype node returns nothing
        set node.prev = prev
        set node.next = this
        set prev.next = node
        set prev = node
    endmethod
    
    method remove takes nothing returns nothing
        set prev.next = next
        set next.prev = prev
    endmethod
    
    static method init takes thistype head returns nothing
        set head.next = head
        set head.prev = head
    endmethod
endmodule

JASS:
library Towers uses UnitIndexer, Game
    function IsTower takes unit whichUnit returns boolean
        return GetUnitRace(whichUnit) == RACE_UNDEAD and IsUnitType(whichUnit, UNIT_TYPE_STRUCTURE) and not Race(GetUnitTypeId(whichUnit)).exists  and not RaceTech(GetUnitTypeId(whichUnit)).exists and not AdvRaceTech(GetUnitTypeId(whichUnit)).exists
    endfunction

    /*
    *   Each player gets a list of towers
    *
    *   Lists go from 0 to 6
    */
    struct Towers extends array
        implement LinkedList
        
        private boolean added
        
        private static method onIndex takes nothing returns boolean
            local thistype playerId
            local thistype node
            
            /*
            *   Make sure that deindexed unit is a tower
            */
            if (IsTower(GetIndexedUnit())) then
                set playerId = GetPlayerId(GetOwningPlayer(GetIndexedUnit()))
                set node = GetIndexedUnitId() + 5
                
                call playerId.add(node)
                set node.added = true
            endif
            
            return false
        endmethod
        private static method onDeindex takes nothing returns boolean
            local thistype node
            local unit u
            
            if (0 == GetIndexedUnitId()) then
                set u = GetTriggerUnit()
            else
                set u = GetIndexedUnit()
            endif
            
            if (IsTower(u)) then
                set node = GetUnitUserData(u) + 5
                
                if (node.added) then
                    call node.remove()
                
                    set node.added = false
                endif
            endif
            
            set u = null
            
            return false
        endmethod
        
        private static method onInit takes nothing returns nothing
            local integer list = 5
            loop
                call init(list)
            
                exitwhen 0 == list
                set list = list - 1
            endloop
        
            call RegisterUnitIndexEvent(Condition(function thistype.onIndex), UnitIndexer.INDEX)
            
            call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), UnitIndexer.DEINDEX)
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.onDeindex)
        endmethod
    endstruct
endlibrary
 
Last edited:
Command

JASS:
library Command uses Table, RemoveString

    globals
        private Table commandEnabledPlayer
        private trigger array commandTrigger
        private string array commandCheck
        private string array commandDescription
        private Table array commandPlayer
        private Table commandPointer
        private integer commandCount = 0
        private trigger systemTrigger
        private boolean array commandDisplay
        private boolean array commandEnabled
    endglobals
    
    struct CommandPlayer extends array
        /*
        *   Enable and disable a command for a player.
        */
        method operator enabled takes nothing returns boolean
            return commandEnabledPlayer.boolean[this]
        endmethod
        method operator enabled= takes boolean b returns nothing
            set commandEnabledPlayer.boolean[this] = b
        endmethod
        
        /*
        *   Outputs commands available to Player(this).
        */
        method operator commands takes nothing returns string
            local string s = ""
            local integer index = commandCount
            
            loop
                if (commandDisplay[index] and commandEnabledPlayer.boolean[index*12 + this] and commandEnabled[index]) then
                    if ("" == s) then
                        set s = SubString(commandCheck[index], 1, StringLength(commandCheck[index]))
                    else
                        set s = s + ", " + SubString(commandCheck[index], 1, StringLength(commandCheck[index]))
                    endif
                endif
                
                set index = index - 1
                exitwhen 0 == index
            endloop
            
            return s
        endmethod
    endstruct
    
    struct Command extends array
    
        /*
        *   Used to enable or disable a command.
        */
        method operator enabled takes nothing returns boolean
            return commandEnabled[this]
        endmethod
        method operator enabled= takes boolean b returns nothing
            set commandEnabled[this] = b
        endmethod
        
        /*
        *   Converts a string to a Command instance.
        */
        static method getCommand takes string command returns integer
            local integer i = StringHash(command)
            local thistype id
            local string s
            
            loop
                set id = commandPointer[i]
                set s = commandCheck[id]
                exitwhen null == s or s == command
                set i = i + 1
            endloop
            
            if (null == s) then
                return 0
            endif
            
            return id
        endmethod
        
        /*
        *   Main function. This evaluates player-chat input and 
        *   executes user code accordingly.
        */
        private static method eval takes nothing returns boolean
            local integer playerId = GetPlayerId(GetTriggerPlayer())
            local string entered = GetEventPlayerChatString()
            local thistype eventCommand = getCommand(GetEventPlayerChatStringMatched())
            local string check = commandCheck[eventCommand]
            local integer index = StringLength(check)
            local integer length = StringLength(entered)
            local string s = ""
            
            /*
            *   Iterating over characters to define 
            *   the command string. We stop after reaching 
            *   the first space character or if there are no 
            *   more characters left in the string.
            */
            loop
                exitwhen index == length
                set s = SubString(entered, index, index + 1)
                set index = index + 1
                exitwhen s == " "
            endloop
            
            /*
            *   Adjusting the check string in case 
            *   we hit a space in the above loop.
            */
            if (s == " ") then
                set check = check + " "
            endif
            
            /*
            *   Checking if the string is valid and enabled.
            *   We also evaluate player permissions.
            */
            if (SubString(entered, 0, index) == check and commandEnabled[eventCommand] and commandEnabledPlayer.boolean[eventCommand*12 + playerId]) then
                /*
                *   The command is valid, it is enabled and the player 
                *   is allowed to use it, so we fire the registered code.
                */
                call TriggerEvaluate(commandTrigger[eventCommand])
            endif
            
            return false
        endmethod
        
        static method operator [] takes string command returns Command
            local integer i = StringHash(command)
            local string s
            local thistype id
            local integer playerId
            local trigger t
            
            /*
            *   Attempting to get an already existing 
            *   instance. If we failed, s will be null.
            */
            loop
                set id = commandPointer[i]
                set s = commandCheck[id]
                exitwhen null == s or s == command
                set i = i + 1
            endloop
            
            /*
            *   This command does not exist yet.
            *   We thus create it.
            */
            if (null == s) then
            
                /*
                *   Initialize data for this instance.
                */
                set id = commandCount + 1
                set commandCount = id
                set commandDisplay[id] = true
                set commandEnabled[id] = true
                set commandCheck[id] = command
                set commandTrigger[id] = CreateTrigger()
                set commandPointer[i] = id
                
                /*
                *   Register the chat event 
                *   for all players.
                */
                set playerId = 11
                loop
                    call TriggerRegisterPlayerChatEvent(systemTrigger, Player(playerId), command, false)
                    set commandEnabledPlayer.boolean[id*12 + playerId] = true
                    exitwhen 0 == playerId
                    set playerId = playerId - 1
                endloop
                
                set t = null
            endif
            
            return id
        endmethod
        
        method operator func takes nothing returns string
            return commandCheck[this]
        endmethod
        
        method register takes code func returns nothing
            call TriggerAddCondition(commandTrigger[this], Filter(func))
        endmethod
        
        method operator [] takes integer playerId returns CommandPlayer
            return this*12 + playerId
        endmethod
        
        method operator description takes nothing returns string
            return commandDescription[this]
        endmethod
        method operator description= takes string s returns nothing
            set commandDescription[this] = s
        endmethod
        
        method operator display takes nothing returns boolean
            return commandDisplay[this]
        endmethod
        method operator display= takes boolean b returns nothing
            set commandDisplay[this] = b
        endmethod
        
        private static method onInit takes nothing returns nothing
            local integer i = 11
            loop
                set commandPlayer[i] = Table.create()
                exitwhen 0 == i
                set i = i - 1
            endloop
            
            set commandEnabledPlayer = Table.create()
            set commandPointer = Table.create()
            
            set systemTrigger = CreateTrigger()
            call TriggerAddCondition(systemTrigger, Condition(function thistype.eval))
        endmethod
    endstruct
endlibrary

That's about it for now.
I need to work on other things.
 
Status
Not open for further replies.
Top