"Swap" system, is there anything like it ?

Komaqtion

You can change this now in User CP.
Hi!

I was just wondering a couple of things about swap systems...

First, is there any out there ? :S

Second, I've already made one here actually, but it's a bit messy atm and I'm not sure if I've got everything it should have :S

Could you take a look ? :eek:

JASS:
library SwapHero initializer Init requires PlayerColors, TimerUtils

    globals
        private constant boolean TIME_LIMIT_ENABLED = true
        private constant real TIME_LIMIT = 30.0
        
        private constant boolean ONE_SWAP_ONLY = true
        
        private constant boolean CHANGE_POS = true
        
        private force f = CreateForce()
        private group g = CreateGroup()
        private boolean array swapped
    endglobals
    
    private struct Heroes
        player target
        player self
        unit own
        timer t
        
        private static method TimeStop takes nothing returns nothing
            local thistype this = GetTimerData( GetExpiredTimer() )
            
            call DisplayTextToPlayer( this.self, 0.0, 0.0, "You've not recieved any answer from the swapping request, and it has therefore been removed !" )
            
            if swapped[GetPlayerId( this.self )] == true then
                call this.destroy()
            else
                set this.target = null
                set this.self = null
                set this.own = null
            endif
            
        endmethod
        
        static method create takes player target, player self, real time returns thistype
            local thistype this = thistype.allocate()
            local unit u
            
            set this.target = target
            set this.self = self
            
            call GroupEnumUnitsOfPlayer( g, self, null )
            
            loop
                set u = FirstOfGroup( g )
                
                exitwhen u == null or IsUnitType( u, UNIT_TYPE_HERO ) == true
            endloop
            
            set this.own = u
            
            if TIME_LIMIT_ENABLED then
                set this.t = NewTimer()
                
                call SetTimerData( this.t, this )
                call TimerStart( this.t, TIME_LIMIT, false, function thistype.TimeStop )
            endif
            
            return this
        endmethod
        
        private method onDestroy takes nothing returns nothing
            set swapped[GetPlayerId( this.self )] = true
        endmethod
        
    endstruct
    
    globals
        private player triggerer
        private Heroes array Data
    endglobals
    
    private function EnumHeroes takes nothing returns boolean
        local unit u = GetEnumUnit()
        local player p = GetOwningPlayer( u )
        
        if IsUnitType( u, UNIT_TYPE_HERO ) == true and p != triggerer then
            call DisplayTextToPlayer( triggerer, 0.0, 0.0, PlayerColoredString( GetPlayerId( p ), GetUnitName( u ) ) + " : " + PlayerColoredString( GetPlayerId( p ), GetPlayerName( p ) ) )
        endif
        
        return false
    endfunction

    private function HeroDisplay takes nothing returns nothing
        local player p = GetEnumPlayer()
        
        call GroupEnumUnitsOfPlayer( g, p, null )
        call ForGroup( g, function EnumHeroes )
    endfunction

    private function SwapList takes nothing returns boolean
        set triggerer = GetTriggerPlayer()
    
        call ForceEnumPlayers( f, null )
        call ForForce( f, function HeroDisplay )
        
        return false
    endfunction
    
    private function SwapHero takes nothing returns boolean
        local string s = GetEventPlayerChatString()
        local string s2 = SubString( s, 6, 8 )
        local integer i = S2I( s2 ) - 1
        local integer i2 = 0
        local integer i3 = 1
        local player targ = Player( i )
        local player self = GetTriggerPlayer()
        local integer tid = GetPlayerId( targ )
        local integer sid = GetPlayerId( self )
        local real x
        local real y
        local real x2
        local real y2
        local unit temp
        
        if SubString( s, 8, 9 ) == " " or SubString( s, 8, 9 ) == "" then
            set s2 = SubString( s, 6, 7 )
            set i = S2I( s2 ) - 1
        endif
        
        if self != targ and self != null and targ != null then
            
            if Heroes ( Data[sid] ).target != Heroes ( Data[tid] ).self and Heroes ( Data[sid] ).self != Heroes ( Data[tid] ).target and ( swapped[sid]!= true or swapped[tid]!= true ) then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've asked " + PlayerColoredString( tid, GetPlayerName( targ ) ) + " to swap heroes with you..." + "\n" + "Now you'll have to wait for him to accept !" )
                call DisplayTextToPlayer( targ, 0.0, 0.0, PlayerColoredString( sid, GetPlayerName( self ) ) + " wants to swap heroes with you." + "\n" + "Do you accept ?")
            elseif Heroes ( Data[sid] ).target == Heroes ( Data[tid] ).self and Heroes ( Data[sid] ).self == Heroes ( Data[tid] ).target then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've accepted " + PlayerColoredString( tid, GetPlayerName( targ ) ) + "'s request to swap heroes with you !" + "\n" + "You'll now recieve the new hero !" )
                call DisplayTextToPlayer( targ, 0.0, 0.0, PlayerColoredString( sid, GetPlayerName( self ) ) + " has accepted your offer to swap heroes..." + "\n" + "You'll now recieve the new hero !")
            elseif swapped[sid] == true then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've already swapped a hero before, and cannot do so twice !" )
            elseif swapped[tid] == true then
                call DisplayTextToPlayer( targ, 0.0, 0.0, "The player you wish to swap heroes with has already swapped before, and cannot do so twice ! Please choose another player to swap with..." )
            endif
            
            if swapped[sid] != true then
            
                if TIME_LIMIT_ENABLED then
                
                    if Data[sid] == 0 then
                        set Data[sid] = Heroes.create( targ, self, TIME_LIMIT )
                    else
                        set Heroes ( Data[sid] ).target = targ
                        set Heroes ( Data[sid] ).self = self
                    endif
                
                else
            
                    if Data[sid] == 0 then
                        set Data[sid] = Heroes.create( targ, self, 0.0 )
                    else
                        set Heroes ( Data[sid] ).target = targ
                        set Heroes ( Data[sid] ).self = self
                    endif
                
                endif
            
            endif
            
        endif
        
        loop
            
            if Heroes( Data[i2] ).self == targ and Heroes( Data[sid] ).target == targ and Heroes ( Data[i2] ).target == self and Heroes( Data[sid] ).self == self then
                
                loop
                    set x = GetUnitX( Heroes( Data[i2] ).own )
                    set y = GetUnitY( Heroes( Data[i2] ).own )
                    
                    call UnitDropItemPoint( Heroes( Data[i2] ).own, UnitItemInSlot( Heroes( Data[i2] ).own, i3 ), x, y )
                    
                    set x = GetUnitX( Heroes( Data[sid] ).own )
                    set y = GetUnitY( Heroes( Data[sid] ).own )
                    
                    call UnitDropItemPoint( Heroes( Data[sid] ).own, UnitItemInSlot( Heroes( Data[sid] ).own, i3 ), x, y )
                    
                    set i3 = i3 + 1
                    
                    exitwhen i3 > 6
                endloop
                
                if CHANGE_POS == true then
                    set x = GetUnitX( Heroes( Data[i2] ).own )
                    set y = GetUnitY( Heroes( Data[i2] ).own )
                    set x2 = GetUnitX( Heroes( Data[sid] ).own )
                    set y2 = GetUnitY( Heroes( Data[sid] ).own )
                    
                    call SetUnitX( Heroes( Data[sid] ).own, x )
                    call SetUnitY( Heroes( Data[sid] ).own, y )
                    
                    call SetUnitX( Heroes( Data[i2] ).own, x2 )
                    call SetUnitY( Heroes( Data[i2] ).own, y2 )
                endif
                
                call SetUnitOwner( Heroes( Data[i2].own ), self, true )
                call SetUnitOwner( Heroes( Data[sid].own ),targ, true )
                
                set temp = Heroes( Data[sid] ).own
                set Heroes ( Data[sid] ).target = null
                set Heroes ( Data[sid] ).self = null
                set Heroes ( Data[sid] ).own = Heroes ( Data[i2] ).own
                set Heroes ( Data[i2] ).target = null
                set Heroes ( Data[i2] ).self = null
                set Heroes ( Data[i2] ).own = temp
                set temp = null
                
                if ONE_SWAP_ONLY == true then
                    call Heroes( Data[sid] ).destroy()
                    call Heroes( Data[i2] ).destroy()
                endif
                
            endif
            
            set i2 = i2 + 1
            
            exitwhen i2 > 11
        endloop
        
        return false
    endfunction
    
    static if DEBUG_MODE then
    
            function sets takes nothing returns nothing
                set Data[1] = Heroes.create( Player( 0 ), Player( 1 ), 0.0 )
            endfunction
            
    endif

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        
        loop
            call TriggerRegisterPlayerChatEvent( t, Player( i ), "-swap", true )
            
            set i = i + 1
            
            exitwhen i > 11
        endloop
        
        call TriggerAddCondition( t, Condition( function SwapList ) )
        
        set t = CreateTrigger()
        set i = 0
        
        loop
            call TriggerRegisterPlayerChatEvent( t, Player( i ), "-swap ", false )
            
            set i = i + 1
            
            exitwhen i > 11
        endloop
        
        call TriggerAddCondition( t, Condition( function SwapHero ) )
        
        static if DEBUG_MODE then
        
            set t = CreateTrigger()
            
            call TriggerRegisterTimerEvent( t, 0.01, false )
            call TriggerAddAction( t, function sets )
        
        endif
        
    endfunction
    
endlibrary


So, this will basicall list all heroes and the owning players by typing:
"-swap"

And when you then want to swap, you'll type:
"-swap X" (X - Is the number of the player you want to swap with (GUI range, from 1 - 12))

And then you have to wait for him to accept by typing "-swap yourplayernumber"...

Anything I've missed here ? :S

Third, Will this be useful ? :S
(I'd think so, as I made this by *request from this thread:
http://www.thehelper.net/forums/showthread.php?t=140752)

* By "request", I mean that I saw that he needed it, so I tried to make it XD

Fourth, Have I done it "The right way" ? XD
I mean, I haven't done this from a very wierd approach or concept, right ? :S

Here's a Demo Map, and the system is under the category "Own Systems" and is called "Swap" (Oddly enough :p)
 

Attachments

Komaqtion

You can change this now in User CP.
Just gonna bump this, since I haven't really gotten any help yet...

I really need help with this since I'm just hoping I've done it correctly so far :S
 

Komaqtion

You can change this now in User CP.
Hellu ?!

What's wrong !!!!????
Why can't I get any help ?!

Please, if there's some information you're missing, I'll try to provide it to you...
 

quraji

zap
Please, if there's some information you're missing, I'll try to provide it to you...
How about, what do you need help with? :p

Is there something wrong with just changing the owners of both heroes to the other player, then changing their position? I didn't really look at the code much, but it seems overcomplicated.

Anyways, I saw (I think) that you were not swapping items, but I think they'd be part of the swap, wouldn't they? What if we're swapping a caster hero and a melee hero? All the spellcasting items I have will be useless for my new fighter :(
I guess you should add a config variable for swapping items.
 

Komaqtion

You can change this now in User CP.
How about, what do you need help with? :p
Well, it might not be help exactly, but I want someone to review it, and see if there are any flawes in it (Though I'm unsure if it actually works, as I can't seem to test it correctly... :() or something you want added or if, as you said, it's overcomplicated, help me change it to work better...

I've now added so that the item will get swapped too (They were just dropped on the ground before :p)

JASS:
library SwapHero initializer Init requires PlayerColors, TimerUtils

    globals
        private constant boolean TIME_LIMIT_ENABLED = true
        private constant real TIME_LIMIT = 30.0
        
        private constant boolean ONE_SWAP_ONLY = true
        
        private constant boolean CHANGE_POS = true
        
        private force f = CreateForce()
        private group g = CreateGroup()
        private boolean array swapped
    endglobals
    
    private struct Heroes
        player target
        player self
        unit own
        timer t
        
        private static method TimeStop takes nothing returns nothing
            local thistype this = GetTimerData( GetExpiredTimer() )
            
            call DisplayTextToPlayer( this.self, 0.0, 0.0, "You've not recieved any answer from the swapping request, and it has therefore been removed !" )
            
            if swapped[GetPlayerId( this.self )] == true then
                call this.destroy()
            else
                set this.target = null
                set this.self = null
                set this.own = null
            endif
            
        endmethod
        
        static method create takes player target, player self, real time returns thistype
            local thistype this = thistype.allocate()
            local unit u
            
            set this.target = target
            set this.self = self
            
            call GroupEnumUnitsOfPlayer( g, self, null )
            
            loop
                set u = FirstOfGroup( g )
                
                exitwhen u == null or IsUnitType( u, UNIT_TYPE_HERO ) == true
            endloop
            
            set this.own = u
            
            if TIME_LIMIT_ENABLED then
                set this.t = NewTimer()
                
                call SetTimerData( this.t, this )
                call TimerStart( this.t, TIME_LIMIT, false, function thistype.TimeStop )
            endif
            
            return this
        endmethod
        
        private method onDestroy takes nothing returns nothing
            set swapped[GetPlayerId( this.self )] = true
        endmethod
        
    endstruct
    
    globals
        private player triggerer
        private Heroes array Data
    endglobals
    
    private function EnumHeroes takes nothing returns boolean
        local unit u = GetEnumUnit()
        local player p = GetOwningPlayer( u )
        
        if IsUnitType( u, UNIT_TYPE_HERO ) == true and p != triggerer then
            call DisplayTextToPlayer( triggerer, 0.0, 0.0, PlayerColoredString( GetPlayerId( p ), GetUnitName( u ) ) + " : " + PlayerColoredString( GetPlayerId( p ), GetPlayerName( p ) ) )
        endif
        
        return false
    endfunction

    private function HeroDisplay takes nothing returns nothing
        local player p = GetEnumPlayer()
        
        call GroupEnumUnitsOfPlayer( g, p, null )
        call ForGroup( g, function EnumHeroes )
    endfunction

    private function SwapList takes nothing returns boolean
        set triggerer = GetTriggerPlayer()
    
        call ForceEnumPlayers( f, null )
        call ForForce( f, function HeroDisplay )
        
        return false
    endfunction
    
    private function SwapHero takes nothing returns boolean
        local string s = GetEventPlayerChatString()
        local string s2 = SubString( s, 6, 8 )
        local integer i = S2I( s2 ) - 1
        local integer i2 = 0
        local integer i3 = 0
        local player targ = Player( i )
        local player self = GetTriggerPlayer()
        local integer tid = GetPlayerId( targ )
        local integer sid = GetPlayerId( self )
        local real x
        local real y
        local real x2
        local real y2
        local unit temp
        local item array it1
        local item array it2
        
        if SubString( s, 8, 9 ) == " " or SubString( s, 8, 9 ) == "" then
            set s2 = SubString( s, 6, 7 )
            set i = S2I( s2 ) - 1
        endif
        
        if self != targ and self != null and targ != null then
            
            if Heroes ( Data[sid] ).target != Heroes ( Data[tid] ).self and Heroes ( Data[sid] ).self != Heroes ( Data[tid] ).target and ( swapped[sid]!= true or swapped[tid]!= true ) then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've asked " + PlayerColoredString( tid, GetPlayerName( targ ) ) + " to swap heroes with you..." + "\n" + "Now you'll have to wait for him to accept !" )
                call DisplayTextToPlayer( targ, 0.0, 0.0, PlayerColoredString( sid, GetPlayerName( self ) ) + " wants to swap heroes with you." + "\n" + "Do you accept ?")
            elseif Heroes ( Data[sid] ).target == Heroes ( Data[tid] ).self and Heroes ( Data[sid] ).self == Heroes ( Data[tid] ).target then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've accepted " + PlayerColoredString( tid, GetPlayerName( targ ) ) + "'s request to swap heroes with you !" + "\n" + "You'll now recieve the new hero !" )
                call DisplayTextToPlayer( targ, 0.0, 0.0, PlayerColoredString( sid, GetPlayerName( self ) ) + " has accepted your offer to swap heroes..." + "\n" + "You'll now recieve the new hero !")
            elseif swapped[sid] == true then
                call DisplayTextToPlayer( self, 0.0, 0.0, "You've already swapped a hero before, and cannot do so twice !" )
            elseif swapped[tid] == true then
                call DisplayTextToPlayer( targ, 0.0, 0.0, "The player you wish to swap heroes with has already swapped before, and cannot do so twice ! Please choose another player to swap with..." )
            endif
            
            if swapped[sid] != true then
            
                if TIME_LIMIT_ENABLED then
                
                    if Data[sid] == 0 then
                        set Data[sid] = Heroes.create( targ, self, TIME_LIMIT )
                    else
                        set Heroes ( Data[sid] ).target = targ
                        set Heroes ( Data[sid] ).self = self
                    endif
                
                else
            
                    if Data[sid] == 0 then
                        set Data[sid] = Heroes.create( targ, self, 0.0 )
                    else
                        set Heroes ( Data[sid] ).target = targ
                        set Heroes ( Data[sid] ).self = self
                    endif
                
                endif
            
            endif
            
        endif
        
        loop
            
            if Heroes( Data[i2] ).self == targ and Heroes( Data[sid] ).target == targ and Heroes ( Data[i2] ).target == self and Heroes( Data[sid] ).self == self then
                
                loop
                    set x = GetUnitX( Heroes( Data[i2] ).own )
                    set y = GetUnitY( Heroes( Data[i2] ).own )
                    set it1[i3] = UnitItemInSlot( Heroes( Data[i2] ).own, i3 )
                    
                    call UnitDropItemPoint( Heroes( Data[i2] ).own, it1[i3], x, y )
                    
                    set x = GetUnitX( Heroes( Data[sid] ).own )
                    set y = GetUnitY( Heroes( Data[sid] ).own )
                    set it2[i3] = UnitItemInSlot( Heroes( Data[sid] ).own, i3 )
                    
                    call UnitDropItemPoint( Heroes( Data[sid] ).own, it1[i3], x, y )
                    call UnitAddItem( Heroes( Data[sid] ).own, it1[i3] )
                    call UnitAddItem( Heroes( Data[i2] ).own, it2[i3] )
                    
                    set i3 = i3 + 1
                    
                    exitwhen i3 > 6
                endloop
                
                if CHANGE_POS == true then
                    set x = GetUnitX( Heroes( Data[i2] ).own )
                    set y = GetUnitY( Heroes( Data[i2] ).own )
                    set x2 = GetUnitX( Heroes( Data[sid] ).own )
                    set y2 = GetUnitY( Heroes( Data[sid] ).own )
                    
                    call SetUnitX( Heroes( Data[sid] ).own, x )
                    call SetUnitY( Heroes( Data[sid] ).own, y )
                    
                    call SetUnitX( Heroes( Data[i2] ).own, x2 )
                    call SetUnitY( Heroes( Data[i2] ).own, y2 )
                endif
                
                call SetUnitOwner( Heroes( Data[i2].own ), self, true )
                call SetUnitOwner( Heroes( Data[sid].own ),targ, true )
                
                set temp = Heroes( Data[sid] ).own
                set Heroes ( Data[sid] ).target = null
                set Heroes ( Data[sid] ).self = null
                set Heroes ( Data[sid] ).own = Heroes ( Data[i2] ).own
                set Heroes ( Data[i2] ).target = null
                set Heroes ( Data[i2] ).self = null
                set Heroes ( Data[i2] ).own = temp
                set temp = null
                
                if ONE_SWAP_ONLY == true then
                    call Heroes( Data[sid] ).destroy()
                    call Heroes( Data[i2] ).destroy()
                endif
                
            endif
            
            set i2 = i2 + 1
            
            exitwhen i2 > 11
        endloop
        
        return false
    endfunction
    
    static if DEBUG_MODE then
    
            function sets takes nothing returns nothing
                set Data[1] = Heroes.create( Player( 0 ), Player( 1 ), 0.0 )
                
                call BJDebugMsg( "Created !" )
            endfunction
            
    endif

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        
        loop
            call TriggerRegisterPlayerChatEvent( t, Player( i ), "-swap", true )
            
            set i = i + 1
            
            exitwhen i > 11
        endloop
        
        call TriggerAddCondition( t, Condition( function SwapList ) )
        
        set t = CreateTrigger()
        set i = 0
        
        loop
            call TriggerRegisterPlayerChatEvent( t, Player( i ), "-swap ", false )
            
            set i = i + 1
            
            exitwhen i > 11
        endloop
        
        call TriggerAddCondition( t, Condition( function SwapHero ) )
        
        static if DEBUG_MODE then
        
            set t = CreateTrigger()
            
            call TriggerRegisterTimerEvent( t, 0.01, false )
            call TriggerAddAction( t, function sets )
        
        endif
        
    endfunction
    
endlibrary


And again, I'm unsure if it actually works :S
Any1 wanna test it ? :eek:
 

Komaqtion

You can change this now in User CP.
I'm sorry but I will bump this again... :(

I really just want to make it work correctly :(

Please, if you see any flawes or if this code can be reduced to some extent, let me know ! :D
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • tom_mai78101 tom_mai78101:
    I have a GIF though
  • tom_mai78101 tom_mai78101:
    In the GIF, I've configured it so the lower the "Main Value" is, the better. I had to compress it pretty far down though.
  • tom_mai78101 tom_mai78101:
    And this is the thread with the video that shows the wall clip. https://tasvideos.org/Forum/Topics/23453
    +2
  • tom_mai78101 tom_mai78101:
    Hmm, about the Headline News, I noticed threads are being moved into the subforum (Health News, Environmental News, etc.). When that happens, the TH Forum Home page loses the articles, and instead would show old articles posted 1 or 2 weeks ago.
  • tom_mai78101 tom_mai78101:
    What do we do with the Home page?
  • Ghan Ghan:
    I added those forums to the filter for that widget.
  • tom_mai78101 tom_mai78101:
    Oh nice. They're back. Thanks.
  • tom_mai78101 tom_mai78101:
    Now I think it makes more sense for me to put news in their own subforums, without worry.
  • The Helper The Helper:
    Awesome Ghan thanks! I was purposely not moving the first 15 news articles in Headline news to the different subforums but I guess I don't have to do that now?
  • tom_mai78101 tom_mai78101:
    Question: Is there a way to remove thread redirects? It creates a copy of the moved thread and takes up space, and I am leaning towards wanting to remove them in the Headline News. But if they have an expiration date, I guess I'm fine with it.
  • The Helper The Helper:
    If you move a thread please leave a permanent redirect. You can delete any redirects after 6 months. The redirects are left to help Search Engines find the moved content.
  • tom_mai78101 tom_mai78101:
    What if you move the permanent redirect, not the thread?
  • The Helper The Helper:
    I think that works but I have not messed with it. You can delete redirects though if you have to that will not delete the original thread
  • The Helper The Helper:
    if a redirect ends up in the same forum as the post it goes to though I think the redirect drops or fails or something but they are not bugged out and when you are working on an indirect the original post is safe.
  • The Helper The Helper:
    Happy Early Friday :)
    +1
  • V-SNES V-SNES:
    Happy Friday :)
  • tom_mai78101 tom_mai78101:
    Fun Friday for me
  • tom_mai78101 tom_mai78101:
    Happy Fun Friday to all.
    +2
  • The Helper The Helper:
    Happy Sunday everyone!!!
  • V-SNES V-SNES:
    Happy Sunday!!!
    +1
  • jonas jonas:
    Happy monday :p
  • jonas jonas:
    Everyone hates mondays?
    +1
  • The Helper The Helper:
    Happy Tuesday!
  • jonas jonas:
    Happy belated tuesday

    Staff online

    Members online

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top