Komaqtion
You can change this now in User CP.
- Reaction score
- 469
Hello !
I just saw a thread asking for such a system, and I remembered I "made" a system like it like half a year ago... XD
I also made a thread about it, but I lost interest in getting it to work, as it got quite messy
Anyways, I've taken up the challenge again, and I wanted to know what you think about it ?
Note: If you don't know what a "Swap" command is, it's like this:
Hope you got it
Here's the current code, but I haven't commented much and I would VERY much appreciate it if anyone would be so kind and test it in a small map online, or on LAN ! <3
What do you think ?
I just saw a thread asking for such a system, and I remembered I "made" a system like it like half a year ago... XD
I also made a thread about it, but I lost interest in getting it to work, as it got quite messy
Anyways, I've taken up the challenge again, and I wanted to know what you think about it ?
Note: If you don't know what a "Swap" command is, it's like this:
- Every player picks their hero.
- I randomed a hero, and got someone I didn't want
- I see Player 2 (Blue) (Player(1) in Jass ) has a hero I really want.
- I type "-swap 2" to see if he wants to swap heroes with me. (Type "-swap 1" if I want to use Jass-player-id's and that constant is true)
- Player 2 (Blue) types "-swap 1", and thereby accepts my request to swap heroes.
- The system starts the swap, and depending on different constants this can look different, but the main thing is that each player's heroes switch owners, so I not have Player 2 (Blue)'s hero, and he has mine
Hope you got it
Here's the current code, but I haven't commented much and I would VERY much appreciate it if anyone would be so kind and test it in a small map online, or on LAN ! <3
JASS:
library SwapHeroes initializer Init requires SimError, TimerUtils, optional PlayerColors
globals
private constant boolean HEROES_ON_INIT = true // Change this to true, if each player has a 'single' hero at Map Init !
private constant boolean SWAP_NEUTRALS = false // If you want the players to be able to swap heroes with the 'neutral' players (They don't have to write "-swap ##" back <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue :p" loading="lazy" data-shortname=":p" />)
private constant boolean SWAP_ALLIES_ONLY = true // If you only want the players to be able to swap with their allies !
private constant boolean SWITCH_POSITIONS = true // If you want the swapped heroes to also swap positions <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" />
private constant boolean REVIVE_IF_DEAD = true // If you swap with a player, and it's currently dead, then if this is true it'll revive the unit also <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" />
private constant boolean USE_GUI_PLAYER_IDS = false
private constant boolean USE_PLAYERS_COLOR_ON_NAME = true
private constant boolean RETAIN_PLAYER_COLORS = false
private constant boolean ONE_SWAP_ONLY = false
private constant boolean SWAP_TIME_LIMIT = true
private constant real TIME_LIMIT = 30.
private constant string SWAP_COMMAND = "-swap "
endglobals
native UnitAlive takes unit id returns boolean
globals
private group ENUM = CreateGroup()
private integer tempInt = 0
endglobals
private struct SwapData extends array
boolean hasSwapped
unit swapUnit
unit targetUnit
player swapPlayer
player targetPlayer
timer resetTimer
static method ResetSwapData takes nothing returns nothing
local thistype this = GetTimerData( GetExpiredTimer() )
set this.targetUnit = null
set this.targetPlayer = null
call ReleaseTimer( GetExpiredTimer() )
endmethod
endstruct
private function SwapActions takes nothing returns boolean
local player p
local player pt
local string s = GetEventPlayerChatString()
local boolean conds
local unit temp = null
if SubString( s, 0, 6 ) == SWAP_COMMAND then
set p = GetTriggerPlayer()
if USE_GUI_PLAYER_IDS then
set pt = Player( S2I( SubString( s, 6, StringLength( s ) ) ) - 1 )
else
set pt = Player( S2I( SubString( s, 6, StringLength( s ) ) ) )
endif
if SWAP_NEUTRALS then
if USE_GUI_PLAYER_IDS then
set conds = GetPlayerId( pt ) >= 17 or GetPlayerId( pt ) < 0 or pt == null or SwapData[ GetPlayerId( pt ) ].swapPlayer != null or pt == p or ( SWAP_ALLIES_ONLY and IsPlayerEnemy( p, pt ) )
else
set conds = GetPlayerId( pt ) >= 16 or GetPlayerId( pt ) < 0 or pt == null or SwapData[ GetPlayerId( pt ) ].swapPlayer != null or pt == p or ( SWAP_ALLIES_ONLY and IsPlayerEnemy( p, pt ) )
endif
else
if USE_GUI_PLAYER_IDS then
set conds = GetPlayerId( pt ) >= 13 or GetPlayerId( pt ) < 0 or pt == null or SwapData[ GetPlayerId( pt ) ].swapPlayer != null or pt == p or ( SWAP_ALLIES_ONLY and IsPlayerEnemy( p, pt ) )
else
set conds = GetPlayerId( pt ) >= 12 or GetPlayerId( pt ) < 0 or pt == null or SwapData[ GetPlayerId( pt ) ].swapPlayer != null or pt == p or ( SWAP_ALLIES_ONLY and IsPlayerEnemy( p, pt ) )
endif
endif
// Make sure you type a valid player
if conds then
call SimError( p, "You need to input a valid player-id !" )
return false
endif
// And also make sure you yourself have a valid hero to swap before starting <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" />
if SwapData[ GetPlayerId( p ) ].swapUnit == null then
call SimError( p, "You need to have a swap hero eligible yourself, before you can use the -swap command !" )
return false
endif
if SwapData[ GetPlayerId( p ) ].hasSwapped == true and ONE_SWAP_ONLY then
call SimError( p, "You have already swapped once, and can039;t swap again..." )
return false
endif
if SwapData[ GetPlayerId( pt ) ].hasSwapped == true and ONE_SWAP_ONLY then
call SimError( p, "The player you wish to swap with has already swapped once, and can039;t swap again !" )
return false
endif
if SWAP_TIME_LIMIT then
call ReleaseTimer( SwapData[ GetPlayerId( p ) ].resetTimer )
call ReleaseTimer( SwapData[ GetPlayerId( pt ) ].resetTimer )
endif
set SwapData[ GetPlayerId( p ) ].targetPlayer = pt
set SwapData[ GetPlayerId( p ) ].targetUnit = SwapData[ GetPlayerId( p ) ].swapUnit
// Start switch ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin :D" loading="lazy" data-shortname=":D" />
if SwapData[ GetPlayerId( pt ) ].targetPlayer == p and SwapData[ GetPlayerId( pt ) ].targetUnit == SwapData[ GetPlayerId( p ) ].swapUnit then
set temp = SwapData[ GetPlayerId( p ) ].swapUnit
if RETAIN_PLAYER_COLORS then
call SetUnitOwner( SwapData[ GetPlayerId( pt ) ].swapUnit, p, false )
else
call SetUnitOwner( SwapData[ GetPlayerId( pt ) ].swapUnit, p, true )
endif
set SwapData[ GetPlayerId( p ) ].swapUnit = SwapData[ GetPlayerId( pt ) ].swapUnit
if RETAIN_PLAYER_COLORS then
call SetUnitOwner( temp, pt, false )
else
call SetUnitOwner( temp, pt, true )
endif
set SwapData[ GetPlayerId( pt ) ].swapUnit = temp
if USE_PLAYERS_COLOR_ON_NAME then
static if USE_PLAYERS_COLOR_ON_NAMELIBRARY_PlayerColors then
call DisplayTextToPlayer( pt, 0.0, 0.0, "You have accepted " + GetPlayerNameColored( pt ) + "039;s swap request !" )
call DisplayTextToPlayer( pt, 0.0, 0.0, GetPlayerNameColored( p ) + "has accepted you request to swap heroes !" )
endif
else
call DisplayTextToPlayer( pt, 0.0, 0.0, "You have accepted " + GetPlayerName( pt ) + "039;s swap request !" )
call DisplayTextToPlayer( pt, 0.0, 0.0, GetPlayerName( p ) + "has accepted you request to swap heroes !" )
endif
if SWITCH_POSITIONS then
call SetUnitX( SwapData[ GetPlayerId( pt ) ].swapUnit, GetUnitX( SwapData[ GetPlayerId( p ) ].swapUnit ) )
call SetUnitX( SwapData[ GetPlayerId( p ) ].swapUnit, GetUnitX( SwapData[ GetPlayerId( pt ) ].swapUnit ) )
call SetUnitY( SwapData[ GetPlayerId( pt ) ].swapUnit, GetUnitY( SwapData[ GetPlayerId( p ) ].swapUnit ) )
call SetUnitY( SwapData[ GetPlayerId( p ) ].swapUnit, GetUnitY( SwapData[ GetPlayerId( pt ) ].swapUnit ) )
endif
if REVIVE_IF_DEAD then
if not UnitAlive( SwapData[ GetPlayerId( pt ) ].swapUnit ) then
call ReviveHero( SwapData[ GetPlayerId( pt ) ].swapUnit, GetUnitX( SwapData[ GetPlayerId( pt ) ].swapUnit ), GetUnitY( SwapData[ GetPlayerId( pt ) ].swapUnit ), true )
endif
if not UnitAlive( SwapData[ GetPlayerId( p ) ].swapUnit ) then
call ReviveHero( SwapData[ GetPlayerId( p ) ].swapUnit, GetUnitX( SwapData[ GetPlayerId( p ) ].swapUnit ), GetUnitY( SwapData[ GetPlayerId( p ) ].swapUnit ), true )
endif
endif
set SwapData[ GetPlayerId( pt ) ].hasSwapped = true
set SwapData[ GetPlayerId( pt ) ].hasSwapped = true
endif
set p = null
set pt = null
elseif SWAP_TIME_LIMIT then
set SwapData[ GetPlayerId( p ) ].resetTimer = NewTimer()
call SetTimerData( SwapData[ GetPlayerId( p ) ].resetTimer, SwapData[ GetPlayerId( p ) ] )
call TimerStart( SwapData[ GetPlayerId( p ) ].resetTimer, TIME_LIMIT, false, function SwapData.ResetSwapData )
if USE_PLAYERS_COLOR_ON_NAME then
static if USE_PLAYERS_COLOR_ON_NAMELIBRARY_PlayerColors then
call DisplayTextToPlayer( pt, 0.0, 0.0, "You have sent a request to " + GetPlayerNameColored( pt ) + ", to swap heroes !" )
call DisplayTextToPlayer( pt, 0.0, 0.0, GetPlayerNameColored( p ) + "has sent you a swap request ! Will you accept ?" )
endif
else
call DisplayTextToPlayer( pt, 0.0, 0.0, "You have sent a request to " + GetPlayerName( pt ) + ", to swap heroes !" )
call DisplayTextToPlayer( pt, 0.0, 0.0, GetPlayerName( p ) + "has sent you a swap request ! Will you accept ?" )
endif
endif
set s = null
return false
endfunction
private function PickInitHeroes takes nothing returns boolean
local unit u = GetFilterUnit()
local player p = GetOwningPlayer( u )
if tempInt == 0 and IsUnitType( u, UNIT_TYPE_HERO ) and not IsUnitType( u, UNIT_TYPE_STRUCTURE ) and UnitAlive( u ) and SwapData[ GetPlayerId( p ) ].swapUnit == null then
set SwapData[ GetPlayerId( p ) ].swapUnit = u
set tempInt = 1
endif
set u = null
set p = null
return false
endfunction
private function OnInitHeroes takes nothing returns nothing
local integer i = 0
local player p
loop
if SWAP_NEUTRALS then
exitwhen i >= 16
else
exitwhen i >= 12
endif
set tempInt = 0
set p = Player( i )
if SwapData[ GetPlayerId( p ) ].swapUnit != null and GetPlayerController( p ) == MAP_CONTROL_USER and GetPlayerController( p ) == PLAYER_SLOT_STATE_PLAYING then
call GroupEnumUnitsOfPlayer( ENUM, p, Filter( function PickInitHeroes ) )
endif
set i = i + 1
endloop
set p = null
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
local player p = null
loop
set p = Player( i )
if SWAP_NEUTRALS then
exitwhen i >= 16
if i >= 12 and ( GetPlayerController( p ) == MAP_CONTROL_COMPUTER or GetPlayerController( p ) == MAP_CONTROL_CREEP ) then
set SwapData[ GetPlayerId( p ) ].hasSwapped = false
set SwapData[ GetPlayerId( p ) ].swapPlayer = p
endif
else
exitwhen i >= 12
endif
if GetPlayerController( p ) == MAP_CONTROL_USER and GetPlayerSlotState( p ) == PLAYER_SLOT_STATE_PLAYING then
call TriggerRegisterPlayerChatEvent( t, p, SWAP_COMMAND, false )
set SwapData[ GetPlayerId( p ) ].hasSwapped = false
set SwapData[ GetPlayerId( p ) ].swapPlayer = p
endif
set i = i + 1
endloop
call TriggerAddCondition( t, Condition( function SwapActions ) )
if HEROES_ON_INIT then
call OnInitHeroes()
endif
set p = null
set t = null
endfunction
endlibrary
What do you think ?