Name | Type | is_array | initial_value |
Force | force | Yes | |
Lboard | leaderboard | No | |
Switch_SourceForce | force | No | |
Switch_SourcePlayer | player | No | |
Switch_SuccessTrigger | trigger | No | |
Switch_TargetForce | force | No | |
Switch_TargetPlayer | player | No |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library AdvancedSwitchSystem /* v1.4.1
*************************************************************************************
*
* by jim7777
*
* This is a switch system that will allow a player to switch between forces.
*
* Pros:
* Configurable enough
* Allows more than 2 forces
* Can be GUI friendly
* Pretty compact data index
* Proper player colors (thanks to Ammorth's GetPlayerColored library)
* Provides a safety measure if any changes in our force are executed during a switch process.
* Cons:
* Will require Event if it isn't set to GUI friendly mode.
* Doesn't automatically refreshes table data when forces are modified outside this system (you must use the functions if you'll do this)
*
*
*************************************************************************************
*
* */ uses /*
*
* */ Table /* http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
* */ GetPlayerColored /* http://www.wc3c.net/showthread.php?t=101692
* */ optional Event /* http://www.hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/
*
************************************************************************************
*
* SETTINGS
*
*/
globals
/*
* TRUE:
* *When a switch process is successful, it will run the trigger set by the Switch_SuccessTrigger variable
* FALSE:
* *Requires Event
* *When a switch process is successful, codes registered via the RegisterOnSwitchSuccess function will run
*/
private constant boolean GUI_FRIENDLY = true
/*
* TRUE:
* *Automatically purges/update data whenever a force is modified outside this system
* FALSE:
* *If you want to purge/update data manually
*/
private constant boolean AUTO_PURGE = true
/*
* TRUE:
* *accepts a minimum of 3 forces and a maximum of 12 forces.
* *the command will take two values as input, e.g. -switch 12 wherein 1 is the force index and 2 is the player's index in force 1
* FALSE:
* *allows two forces (min and max)
* *this command will only take one value as input, e.g. -switch 1 where 1 is the player's index in the opposite force.
*/
private constant boolean TWO_DIG = true
/*
* The command if a player wants the switch list or start a switch process
*/
private constant string COM = "-switch"
/*
* The command to type if a player agrees on a switch process
*/
private constant string COM_OK = "-ok"
/*
* The command to type if a player disagrees on a switch process
*/
private constant string COM_NO = "-no"
/*
* How long should the switch process last if ever voting criteria isn't fulfilled yet
*/
private constant real TIMEOUT = 10
/*
* This is the default setting if switch is on by the time this system is initiated (true = on; false = off)
*/
private boolean switchOn = true
endglobals
/*
************************************************************************************
*
* Functions
*
* function AddSwitchForce takes force whichForce returns nothing
* - Adds a force and its players to the switch system
*
* function RemoveSwitchForce takes force whichForce returns nothing
* - Removes a force from this system
*
* function GetPlayerSwitchForce takes player whichPlayer returns force
* - Returns the current force of a player that is registered on this system.
* - Returns null if it doesn't exist
*
* function GetPlayerSwitchIndex takes player whichPlayer returns integer
* - Returns the player's index in its switch force.
*
* function IsSwitchOnGoing takes nothing returns boolean
* - Returns true if there's an ongoing switch process.
*
* function SwitchEnable takes boolean flag returns boolean
* - Enables or disable this system
* - Returns false if there's an ongoing switch process.
*
* function IsSwitchEnabled takes nothing returns boolean
* - Returns true if this system is enabled
*
* function UpdatePlayerSwitchForce takes player whichPlayer returns boolean
* - Re-updates a player's force that is registered on this system.
* - Returns true if it is successful
*
* function SwitchForceRemovePlayer takes force whichForce, player whichPlayer returns boolean
* - Removes a player from a registered force on this system.
* - Returns true if it is successful
*
* function AddPlayerToSwitchForce takes force f, player p returns boolean
* - Adds a player to a registered force on this system.
* - Returns true if it is successful
* - This is useful for adding unused player slots
*
* function RegisterOnSwitchSuccess takes code c returns nothing
* - Registers a function to the system that is to be called when a switch is successful.
* - This is only available when GUI_FRIENDLY is set to false.
* - Requires the Event library
************************************************************************************
*/
globals
private boolean onGoing = false
private hashtable ht = InitHashtable()
private integer inQueue = 0
private integer max = 0
private integer stringLength
private TableArray dataTable
private TableArray playerDataTable
endglobals
private struct VoteSwitch
boolean array voted[11]
integer vote = 1
integer voters
static boolean ended = false
static player sourcePlayer
static player targetSwitchPlayer
static timer time
static thistype this
static if not GUI_FRIENDLY then
static Event Ev
endif
private static method recountPlayers takes nothing returns nothing
local integer i = 0
set this.voters = 0
loop
exitwhen i > 11
if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
set this.voters = this.voters + 1
endif
set i = i + 1
endloop
endmethod
private static method switchSuccess takes nothing returns nothing
local integer playerId = GetPlayerId(this.sourcePlayer)
local integer targetPlayerId = GetPlayerId(this.targetSwitchPlayer)
local integer playerForceTableId = R2I(playerDataTable[playerId].real[0]) //since tables no allow integahs
local integer targetPlayerForceTableId = R2I(playerDataTable[targetPlayerId].real[0])
local integer playerForceIndex = R2I(playerDataTable[playerId].real[1])
local integer targetPlayerForceIndex = R2I(playerDataTable[targetPlayerId].real[1])
/*
* the source force or the force of the player who initiated the switch process
*/
local force sourceForce = dataTable[playerForceTableId].force[0]
/*
* the force of our target player
*/
local force targetForce = dataTable[targetPlayerForceTableId].force[0]
set this.ended = true
/*
* start updating our table data
*/
call ForceRemovePlayer(sourceForce,this.sourcePlayer)
call ForceRemovePlayer(targetForce,this.targetSwitchPlayer)
set dataTable[playerForceTableId].player[playerForceIndex] = this.targetSwitchPlayer
set dataTable[targetPlayerForceTableId].player[targetPlayerForceIndex] = this.sourcePlayer
/*set dataTable[playerForceTableId].force[0] = targetForce
set dataTable[targetPlayerForceTableId].force[0] = sourceForce*/
call ForceAddPlayer(targetForce,this.sourcePlayer)
call ForceAddPlayer(sourceForce,this.targetSwitchPlayer)
set playerDataTable[playerId].real[0] = targetPlayerForceTableId
set playerDataTable[targetPlayerId].real[0] = playerForceTableId
set playerDataTable[playerId].real[1] = targetPlayerForceIndex
set playerDataTable[targetPlayerId].real[1] = playerForceIndex
set udg_Switch_SourcePlayer = this.sourcePlayer
set udg_Switch_TargetPlayer = this.targetSwitchPlayer
set udg_Switch_SourceForce = sourceForce
set udg_Switch_TargetForce = targetForce
/*
* start executing our trigger or fire our events
*/
static if GUI_FRIENDLY then
call TriggerExecute(udg_Switch_SuccessTrigger)
else
call this.Ev.fire()
endif
/*
* if everything was finished... perform finishing touches
*/
call ClearTextMessages()
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,8,"The switch process between " + GetPlayerNameColored(this.sourcePlayer) + " and " + GetPlayerNameColored(this.targetSwitchPlayer) + " has successfully completed.")
call this.destroy()
set onGoing = false
set sourceForce = null
set targetForce = null
endmethod
private static method onTimeout takes nothing returns nothing
set onGoing = false
call this.destroy()
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,"|CFFFF0303The switch voting process has timed out.|r")
call PauseTimer(thistype.time)
endmethod
private static method showList takes player p returns nothing
local integer i = 0
local integer forceIndex
local player indexedPlayer
loop
exitwhen i >= max
set forceIndex = 1
/*
* check if our selected table isn't empty
*/
if dataTable[i].real[0] > 1 then
/*
* well Loop 'em
*/
loop
exitwhen forceIndex == dataTable[i].real[0]
if not IsPlayerInForce(p,dataTable[i].force[0]) then
set indexedPlayer = dataTable[i].player[forceIndex]
static if TWO_DIG then
call DisplayTimedTextFromPlayer(p,0,0,10,"Type " + COM + " " + I2S(i) + "-" + I2S(forceIndex) + " to switch with " + GetPlayerNameColored(indexedPlayer))
else
call DisplayTimedTextFromPlayer(p,0,0,10,"Type " + COM + " " + I2S(forceIndex) + " to switch with " + GetPlayerNameColored(indexedPlayer))
endif
endif
set forceIndex = forceIndex + 1
endloop
endif
set i = i + 1
endloop
/*
* le space
*/
call DisplayTimedTextToPlayer(p,0,0,1," ")
endmethod
private static method initSwitch takes player p, player tp returns nothing
set onGoing = true
set this = thistype.allocate()
set this.sourcePlayer = p
set this.targetSwitchPlayer = tp
set this.voted[GetPlayerId(p)] = true
set udg_Switch_SourcePlayer = null
set udg_Switch_TargetPlayer = null
set udg_Switch_SourceForce = null
set udg_Switch_TargetForce = null
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,15,GetPlayerNameColored(this.sourcePlayer) + " would like to switch with " + GetPlayerNameColored(this.targetSwitchPlayer) + ". Type " + COM_OK + " to accept or " + COM_NO + " to reject.")
call recountPlayers()
call TimerStart(thistype.time,TIMEOUT,false,function thistype.onTimeout)
endmethod
static method start takes nothing returns nothing
local player triggerPlayer = GetTriggerPlayer()
local player targetPlayer
local string chatString = GetEventPlayerChatString()
local integer chatsStringLength = StringLength(chatString)
local integer playerId = GetPlayerId(triggerPlayer)
local integer targetId
local integer forceTableId
if switchOn then
if SubString(chatString,0,stringLength) == COM then
if stringLength == chatsStringLength and IsPlayerInForce(triggerPlayer,dataTable[R2I(playerDataTable[playerId].real[0])].force[0]) then
call thistype.showList(triggerPlayer)
elseif not onGoing then
if playerDataTable[playerId].boolean[0] and IsPlayerInForce(triggerPlayer,dataTable[R2I(playerDataTable[playerId].real[0])].force[0]) then//double check values
static if TWO_DIG then
set forceTableId = S2I(SubString(SubString(chatString,0,stringLength+2),stringLength+1,1))
if dataTable[forceTableId].real[0] > 9 and chatsStringLength >= stringLength+5 then
set chatString = SubString(chatString,0,stringLength+5)
set targetId = S2I(SubString(chatString,stringLength+3,2))
if targetId <= 0 or targetId > dataTable[forceTableId].real[0] then
set chatString = SubString(chatString,0,stringLength+4)
set targetId = S2I(SubString(chatString,stringLength+3,1))
endif
else
set chatString = SubString(chatString,0,stringLength+4)
set targetId = S2I(SubString(chatString,stringLength+3,1))
endif
set targetPlayer = dataTable[forceTableId].player[targetId]
if targetPlayer != null and dataTable[forceTableId].boolean[0] and dataTable[R2I(playerDataTable[playerId].real[0])].boolean[0] and not IsPlayerInForce(triggerPlayer,dataTable[forceTableId].force[0]) and playerDataTable[GetPlayerId(targetPlayer)].boolean[0] then
call thistype.initSwitch(triggerPlayer,targetPlayer)
else
debug call BJDebugMsg("[Switch System]: Invalid data values!")
call thistype.showList(triggerPlayer)
endif
else
if dataTable[0].boolean[0] and dataTable[1].boolean[0] then
if IsPlayerInForce(triggerPlayer,dataTable[0].force[0]) then
if dataTable[1].real[0] > 9 and chatsStringLength >= stringLength+3 then
set chatString = SubString(chatString,0,stringLength+3)
set targetId = S2I(SubString(chatString,stringLength+1,2))
if targetId <= 0 or targetId > dataTable[1].real[0] then
set chatString = SubString(chatString,0,stringLength+2)
set targetId = S2I(SubString(chatString,stringLength+1,1))
endif
else
set chatString = SubString(chatString,0,stringLength+2)
set targetId = S2I(SubString(chatString,stringLength+1,1))
endif
set targetPlayer = dataTable[1].player[targetId]
if playerDataTable[GetPlayerId(targetPlayer)].boolean[0] then
call thistype.initSwitch(triggerPlayer,targetPlayer)
else
debug call BJDebugMsg("[Switch System]: Invalid data value for player " + GetPlayerName(targetPlayer))
call thistype.showList(triggerPlayer)
endif
elseif IsPlayerInForce(triggerPlayer,dataTable[1].force[0]) then
if dataTable[0].real[0] > 9 and chatsStringLength >= stringLength+3 then
set chatString = SubString(chatString,0,stringLength+3)
set targetId = S2I(SubString(chatString,stringLength+1,2))
if targetId <= 0 or targetId > dataTable[0].real[0] then
set chatString = SubString(chatString,0,stringLength+2)
set targetId = S2I(SubString(chatString,stringLength+1,1))
endif
else
set chatString = SubString(chatString,0,stringLength+2) //i need to strip off strings.. damn substring native
set targetId = S2I(SubString(chatString,stringLength+1,1))
endif
set targetPlayer = dataTable[0].player[targetId]
if playerDataTable[GetPlayerId(targetPlayer)].boolean[0] then
call thistype.initSwitch(triggerPlayer,targetPlayer)
else
debug call BJDebugMsg("[Switch System]: Invalid data value for player " + GetPlayerName(targetPlayer))
call thistype.showList(triggerPlayer)
endif
else
debug call BJDebugMsg("[Switch System]: Player " + GetPlayerName(triggerPlayer) + " is in an unknown force. Exiting")
call thistype.showList(triggerPlayer)
endif
debug else
debug call BJDebugMsg("[Switch System]: Invalid force data. System crashed")
endif
endif
else
call DisplayTimedTextToPlayer(triggerPlayer,0,0,3,"This command is disabled for use.")
endif
else
call DisplayTimedTextToPlayer(triggerPlayer,0,0,5,"Please wait for the ongoing switch voting to finish.")
endif
elseif onGoing then
if chatString == COM_OK then
call recountPlayers()
if this.vote >= (this.voters-1) then //let us first double check before everything else
call PauseTimer(this.time)
call thistype.switchSuccess()
elseif not this.voted[playerId] then //double check fails? check if this player is allowed to vote
set this.vote = this.vote + 1
if this.vote >= (this.voters-1) then //perform a check
call PauseTimer(this.time)
call thistype.switchSuccess()
else
set this.voted[playerId] = true
call DisplayTimedTextToPlayer(triggerPlayer,0,0,8,GetPlayerNameColored(triggerPlayer) + " has accepted the switch offer. (" + I2S(this.vote) + "/" + I2S(this.voters) + ")")
endif
debug else
debug call BJDebugMsg("[Switch System]: Unable to process command " + COM_OK + ". Player " + GetPlayerName(triggerPlayer) + " has already voted.")
endif
elseif chatString == COM_NO then
call ClearTextMessages()
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,GetPlayerNameColored(triggerPlayer) + "|CFFFF0303 has declined the switch offer.|r") // so sad someone denied the offer
set onGoing = false
call this.destroy()
call PauseTimer(this.time)
endif
endif
elseif SubString(chatString,0,stringLength) == COM then
call DisplayTimedTextToPlayer(triggerPlayer,0,0,5,COM + " is disabled for this game.")
endif
set triggerPlayer = null
set targetPlayer = null
endmethod
endstruct
private function RecalculateForce takes force f, integer tid returns nothing
local integer forceHandle = GetHandleId(f)
local integer index = 0
local integer forceIndex = 1
local player p = null
if HaveSavedInteger(ht,forceHandle,0) then
loop
exitwhen index > 11
set p = Player(index)
if IsPlayerInForce(p,f) and playerDataTable[index].real[0] == tid then
set dataTable[tid].player[forceIndex] = p
set playerDataTable[index].real[1] = forceIndex
set forceIndex = forceIndex + 1
endif
set index = index + 1
endloop
set dataTable[tid].real[0] = forceIndex
debug else
debug call BJDebugMsg("[Switch System]: Failed to update force " + I2S(forceHandle) + ". Force not registered.")
endif
set p = null
endfunction
/*
* this function is for filling out missing index..
*/
private function FindForceIndex takes nothing returns integer
local integer i = 0
local integer int
/*
* we'll use a dummy table to find a missing index in the system's index
*/
if dataTable[13].boolean[0] and inQueue > 0 then
set int = R2I(dataTable[13].real[inQueue])//why no intagahs
set inQueue = inQueue - 1 //just for stacked force
return int
endif
return max
endfunction
function AddSwitchForce takes force f returns nothing
local integer forceHandle = GetHandleId(f)
local integer i = 0
local integer forceIndex = 1
local integer recycledIndex = FindForceIndex()
static if not TWO_DIG then
local integer maxForces = 2
else
local integer maxForces = 11
endif
local player p = null
if max < maxForces then
if not dataTable[recycledIndex].boolean[0] then
call SaveInteger(ht,forceHandle,0,recycledIndex)
set dataTable[recycledIndex].force[0] = f
set dataTable[recycledIndex].boolean[0] = true
loop
exitwhen i > 11
set p = Player(i)
if IsPlayerInForce(p,f) and not playerDataTable[i].boolean[0] then
set dataTable[recycledIndex].player[forceIndex] = p
set playerDataTable[i].real[0] = recycledIndex
set playerDataTable[i].real[1] = forceIndex
set playerDataTable[i].boolean[0] = true
set forceIndex = forceIndex + 1
endif
set i = i + 1
endloop
set dataTable[recycledIndex].real[0] = forceIndex
if dataTable[13].boolean[0] then //just to stop max from increasing
if inQueue <= 0 then //successfully filled missing index
set inQueue = 0
set dataTable[13].boolean[0] = false
endif
else
set max = max + 1
endif
debug else
debug call BJDebugMsg("[Switch System]: Force " + I2S(forceHandle) + " is alredy registered.")
endif
debug else
debug call BJDebugMsg("[Switch System]: Only a maximum of " + I2S(maxForces+1) + ". Denying request for " + I2S(forceHandle))
endif
set p = null
endfunction
function RemoveSwitchForce takes force f returns nothing
local integer forceHandle = GetHandleId(f)
local integer i = 0
local integer playerForceIndex = 0
if HaveSavedInteger(ht,forceHandle,0) and not onGoing then
loop
exitwhen i == max
if dataTable[i].force[0] == f then
loop
exitwhen playerForceIndex > 11
if playerDataTable[playerForceIndex].real[0] == i and IsPlayerInForce(Player(playerForceIndex),f) then
call playerDataTable[playerForceIndex].flush()
endif
set playerForceIndex = playerForceIndex + 1
endloop
call dataTable[i].flush()
call FlushChildHashtable(ht,forceHandle)
set dataTable[13].boolean[0] = true
set inQueue = inQueue + 1
set dataTable[13].real[inQueue] = i
exitwhen true
endif
set i = i + 1
endloop
debug else
debug call BJDebugMsg("[Switch System]: Unable to remove force " + I2S(forceHandle) + ". Either this force is not registered or a switch process is ongoing.")
endif
endfunction
function GetPlayerSwitchIndex takes player p returns integer
return R2I(playerDataTable[GetPlayerId(p)].real[1])
endfunction
static if not GUI_FRIENDLY then
function RegisterOnSwitchSuccess takes code c returns nothing
call VoteSwitch.Ev.register(Filter(c))
endfunction
endif
function GetPlayerSwitchForce takes player p returns force
return dataTable[R2I(playerDataTable[GetPlayerId(p)].real[0])].force[0]
endfunction
function IsSwitchOnGoing takes nothing returns boolean
return onGoing
endfunction
function SwitchEnable takes boolean b returns boolean
if not onGoing then
set switchOn = b
return true
endif
return false
endfunction
function IsSwitchEnabled takes nothing returns boolean
return switchOn
endfunction
function UpdatePlayerSwitchForce takes player p returns boolean
local integer i
local integer playerId = GetPlayerId(p)
local integer targetPlayerForceTableId = R2I(playerDataTable[playerId].real[0])
local integer h = GetHandleId(dataTable[targetPlayerForceTableId].force[0])
if not onGoing then
if HaveSavedInteger(ht,h,0) and playerDataTable[playerId].boolean[0] then
set i = LoadInteger(ht,h,0)
call RecalculateForce(dataTable[targetPlayerForceTableId].force[0],targetPlayerForceTableId)
debug else
debug call BJDebugMsg("[Switch System]: Cannot update player " + GetPlayerName(p) + "'s registered force " + I2S(h) + ". Input values are not registered on this system.")
endif
debug else
debug call BJDebugMsg("[Switch System]: Cannot update "+ GetPlayerName(p) + "'s registered force: " + I2S(h) + ". A switch process is ongoing.")
endif
return false
endfunction
function SwitchForceRemovePlayer takes force f, player p returns boolean
local integer forceHandle = GetHandleId(f)
local integer playerId = GetPlayerId(p)
if not onGoing then
if HaveSavedInteger(ht,forceHandle,0) and playerDataTable[playerId].boolean[0] then
call ForceRemovePlayer(f,p)
call RecalculateForce(f,R2I(playerDataTable[playerId].real[0]))
call playerDataTable[playerId].flush()
debug else
debug call BJDebugMsg("[Switch System]: Cannot remove player " + GetPlayerName(p) + " from force " + I2S(forceHandle) + ". Input values are not registered on this system.")
endif
debug else
debug call BJDebugMsg("[Switch System]: Unable to remove player "+ GetPlayerName(p) + " from force " + I2S(forceHandle) + ". A switch process is ongoing.")
endif
return false
endfunction
function AddPlayerToSwitchForce takes force f, player p returns boolean
local integer forceHandle = GetHandleId(f)
local integer i
local integer playerForceIndex
local integer playerId = GetPlayerId(p)
if HaveSavedInteger(ht,forceHandle,0) and not playerDataTable[playerId].boolean[0] then
set i = LoadInteger(ht,forceHandle,0)
set playerForceIndex = R2I(dataTable[i].real[0])
/*
* no more checking! Just to allow unused slots to be registered on the system :/
*/
call ForceAddPlayer(f,p)
set dataTable[i].player[playerForceIndex] = p
set playerDataTable[playerId].real[0] = i
set playerDataTable[playerId].real[1] = playerForceIndex
set playerDataTable[playerId].boolean[0] = true
set dataTable[i].real[0] = playerForceIndex + 1
return true
debug else
debug call BJDebugMsg("[Switch System]: Unable to add "+GetPlayerName(p)+" to force " + I2S(forceHandle))
endif
return false
endfunction
private function OnDestroyForce takes force f returns nothing
static if AUTO_PURGE then
call RemoveSwitchForce(f)
endif
if onGoing and (IsPlayerInForce(VoteSwitch.sourcePlayer,f) or IsPlayerInForce(VoteSwitch.targetSwitchPlayer,f)) then
set onGoing = false
static if not DEBUG_MODE then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,"Switch process has been cancelled by the game.")
else
call BJDebugMsg("[Switch System]: Ongoing switch process terminated unexpectedly. Force " + I2S(GetHandleId(f)) + " was destroyed during the process.")
endif
endif
endfunction
private function OnClearForce takes force f returns nothing
static if AUTO_PURGE then
local integer forceHandle = GetHandleId(f)
local integer i = 0
local integer playerForceIndex = 0
if HaveSavedInteger(ht,forceHandle,0) then
loop
exitwhen i == max
if dataTable[i].force[0] == f then
loop
exitwhen playerForceIndex > 11
if playerDataTable[playerForceIndex].real[0] == i and IsPlayerInForce(Player(playerForceIndex),f) then
call playerDataTable[playerForceIndex].flush()
endif
set playerForceIndex = playerForceIndex + 1
endloop
exitwhen true
endif
set i = i + 1
endloop
endif
endif
if onGoing and (IsPlayerInForce(VoteSwitch.sourcePlayer,f) or IsPlayerInForce(VoteSwitch.targetSwitchPlayer,f)) then
set onGoing = false
static if not DEBUG_MODE then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,"Switch process has been cancelled by the game.")
else
call BJDebugMsg("[Switch System]: Ongoing switch process terminated unexpectedly. Force " + I2S(GetHandleId(f)) + " was cleared during the process.")
endif
endif
endfunction
private function OnForceRemovePlayer takes force f, player p returns nothing
if onGoing and not VoteSwitch.ended and (VoteSwitch.sourcePlayer == p or VoteSwitch.targetSwitchPlayer == p ) then
set onGoing = false
static if not DEBUG_MODE then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,"Switch process has been cancelled by the game.")
else
call BJDebugMsg("[Switch System]: Ongoing switch process terminated unexpectedly. Player " + GetPlayerName(p) + " was removed from force " + I2S(GetHandleId(f)) + " during the process. ")
endif
endif
endfunction
private function OnForceRemovePlayerSimp takes player p, force f returns nothing
if onGoing and not VoteSwitch.ended and (VoteSwitch.sourcePlayer == p or VoteSwitch.targetSwitchPlayer == p ) then
set onGoing = false
static if not DEBUG_MODE then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,"Switch process has been cancelled by the game.")
else
call BJDebugMsg("[Switch System]: Ongoing switch process terminated unexpectedly. Player " + GetPlayerName(p) + " was removed from force " + I2S(GetHandleId(f)) + " during the process. (Simp)")
endif
endif
endfunction
private module O
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
set dataTable = TableArray[14]
set playerDataTable = TableArray[12]
set VoteSwitch.time = CreateTimer()
set stringLength = StringLength(COM)
static if not GUI_FRIENDLY then
set VoteSwitch.Ev = Event.create()
endif
loop
exitwhen i > 11
call TriggerRegisterPlayerChatEvent(t,Player(i),"",false)
set i = i + 1
endloop
call TriggerAddAction(t,function VoteSwitch.start)
set t = null
endmethod
endmodule
private struct H extends array
implement O
endstruct
hook DestroyForce OnDestroyForce
hook ForceClear OnClearForce
hook ForceRemovePlayer OnForceRemovePlayer
hook ForceRemovePlayerSimple OnForceRemovePlayerSimp
endlibrary
//TESH.scrollpos=118
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.0.1
One map, one hashtable. Welcome to NewTable 3.1
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key)
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb)
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table 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
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library GetPlayerColored initializer init
// GetPlayerColored by Ammorth
// v1.2
// functions should be self-explanatory
// Credits to DioD for multiple fixes
globals
private string array PlayerColor
private integer array redhex
private integer array greenhex
private integer array bluehex
endglobals
function GetPlayerNameColored takes player p returns string // colored player name
return PlayerColor[GetHandleId(GetPlayerColor(p))]+GetPlayerName(p)+"|r"
endfunction
function GetPlayerTextColor takes player p returns string // only textcode
return PlayerColor[GetHandleId(GetPlayerColor(p))]
endfunction
function GetPlayerRedHex takes player p returns integer // integer value for red
return redhex[GetHandleId(GetPlayerColor(p))]
endfunction
function GetPlayerGreenHex takes player p returns integer // integer value for green
return greenhex[GetHandleId(GetPlayerColor(p))]
endfunction
function GetPlayerBlueHex takes player p returns integer // integer value for blue
return bluehex[GetHandleId(GetPlayerColor(p))]
endfunction
private function init takes nothing returns nothing
set PlayerColor[0] = "|CFFFF0303" // red
set PlayerColor[1] = "|CFF0042FF" // blue
set PlayerColor[2] = "|CFF1CE6B9" // teal
set PlayerColor[3] = "|CFF540081" // purple
set PlayerColor[4] = "|CFFFFFF01" // yellow
set PlayerColor[5] = "|CFFFE8A0E" // orange
set PlayerColor[6] = "|CFF20C000" // green
set PlayerColor[7] = "|CFFE55BB0" // pink
set PlayerColor[8] = "|CFF959697" // grey
set PlayerColor[9] = "|CFF7EBFF1" // light blue
set PlayerColor[10] = "|CFF106246" // dark green
set PlayerColor[11] = "|CFF4E2A04" // brown
set redhex[0] = 255
set redhex[1] = 0
set redhex[2] = 28
set redhex[3] = 84
set redhex[4] = 255
set redhex[5] = 254
set redhex[6] = 32
set redhex[7] = 229
set redhex[8] = 149
set redhex[9] = 126
set redhex[10] = 16
set redhex[11] = 78
set greenhex[0] = 3
set greenhex[1] = 66
set greenhex[2] = 230
set greenhex[3] = 0
set greenhex[4] = 255
set greenhex[5] = 138
set greenhex[6] = 192
set greenhex[7] = 91
set greenhex[8] = 150
set greenhex[9] = 191
set greenhex[10] = 98
set greenhex[11] = 42
set bluehex[0] = 3
set bluehex[1] = 255
set bluehex[2] = 185
set bluehex[3] = 129
set bluehex[4] = 1
set bluehex[5] = 14
set bluehex[6] = 0
set bluehex[7] = 176
set bluehex[8] = 151
set bluehex[9] = 241
set bluehex[10] = 70
set bluehex[11] = 4
endfunction
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Event /* v2.0.0.1
************************************************************************************
*
* Functions
*
* function CreateEvent takes nothing returns integer
* function TriggerRegisterEvent takes trigger t, integer ev returns nothing
*
************************************************************************************
*
* struct Event extends array
*
* static method create takes nothing returns thistype
* method registerTrigger takes trigger t returns nothing
* method register takes boolexpr c returns nothing
* method fire takes nothing returns nothing
*
************************************************************************************/
globals
private real q=0
endglobals
struct Event extends array
private static integer w=0
private static trigger array e
static method create takes nothing returns thistype
set w=w+1
set e[w]=CreateTrigger()
return w
endmethod
method registerTrigger takes trigger t returns nothing
call TriggerRegisterVariableEvent(t,SCOPE_PRIVATE+"q",EQUAL,this)
endmethod
method register takes boolexpr c returns nothing
call TriggerAddCondition(e[this],c)
endmethod
method fire takes nothing returns nothing
set q=0
set q=this
call TriggerEvaluate(e[this])
endmethod
endstruct
function CreateEvent takes nothing returns Event
return Event.create()
endfunction
function TriggerRegisterEvent takes trigger t,Event ev returns nothing
call ev.registerTrigger(t)
endfunction
function RegisterEvent takes boolexpr c,Event ev returns nothing
call ev.register(c)
endfunction
function FireEvent takes Event ev returns nothing
call ev.fire()
endfunction
endlibrary