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

Need feedback: Diplomacy Sample (Aplha)

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
I finnished the core of my diplomacy library. I would like some feedback on it and if you have two computers perhaps you could test it. (I would test for bugs myself, but I can't find the map saved in the "test" or "scenario" folder. The one thing i can't test is to neutral another player. If someone could try that on another computer I'd be truly thankful.

Player commands: all, faction name, player number (1-12) and color
To see commands in game type: "-d" or "-diplomacy"

Any feedback on The trigger, the messages, the sound or other things I should change - do tell.

I don't know if "PenDex" is the best solution to keep track of pending offers... but it stands for now.

Not sure what more to implement that I haven't already planned for (alliance, alliance limit, give gold/lumber and tax it).
JASS:
library Diplomacy initializer init 

globals
    //
    private constant real DISPLAY_TIME = 10.
    private constant integer PENDING_TIME = 15
    private constant real Give_Gold_Tax = 0.1
    private constant real Give_Wood_Tax = 0.1
    
    integer AllyLimit = 1 // You can change this through outside triggers. 
    //
    private timer array Timer
    private boolean array PendingOffer
    private integer array Offering
    private integer array Recieving 
    private string array Faction
    private force Active = CreateForce() 
    private hashtable hash = InitHashtable() 
    private player cmd_p 
    private integer array AllyCount //Should I cound the allies in the beginning? What happens if full?
    
    private sound s_Offer
    private sound s_Peace
    private sound s_War
    private sound s_Ally
endglobals



private function SetWords takes nothing returns nothing
    local string s = StringCase(GetEventPlayerChatString(), false)
    local string array word 
    local integer i = 1
    local integer j = 0
    set i = 0
    loop
        exitwhen i > StringLength(s) 
        if (SubString(s,i, i+1) == " ") then
            set j = j + 1
        else
            set word[j] = word[j] + SubString(s,i,i+1)
        endif
        set i = i + 1
    endloop
    if word[1] == "dark" or word[1] == "light" then
        set word[1] = word[1] + " " + word[2]
        set i = 2
        loop 
            exitwhen word[i+1] == null
            set word[i] = word[i+1]
            set i = i + 1
        endloop
    endif 
    set cmd_p = Player(LoadInteger(hash, 0, StringHash(word[1])))
    if (cmd_p == Player(0) and word[1] != "1" and word[1] != "red" and word[1] != "sparta") then
        set cmd_p = null 
    endif
    
endfunction 

private function DisplayFaction takes string factions returns string 
    local integer i = 0
    local integer j = 0
    local string s = SubString(factions, 0, StringLength(factions)-1)
    loop
        exitwhen i > StringLength(s) 
        if (SubString(s, i, i+1) == " ") then 
            set j = i
        endif
        set i = i + 1
    endloop
    if (j > 1) then
        set s = SubString(s, 0, j-1) + " and " + SubString(s, j+1, StringLength(s))
    endif
    return s
endfunction

//******************************//
//            Offers            //
//******************************//

private function DisplayExpiredMsg takes nothing returns nothing
    
endfunction

private function RemoveOffer takes integer f returns nothing
    call PauseTimer(Timer[f])
    call DestroyTimer(Timer[f])
    set PendingOffer[f] = false
endfunction

private function OfferExpires takes nothing returns nothing
    local integer tID = LoadInteger(hash, GetHandleId(GetExpiredTimer()), 0)
    call RemoveOffer(tID)
    if (tID <= 144) then 
        call DisplayTimedTextToPlayer(Player(Offering[tID]), 0,0, DISPLAY_TIME, "Your Peace offer to " + Faction[Recieving[tID]] + " has expired.")
        call DisplayTimedTextToPlayer(Player(Recieving[tID]), 0,0, DISPLAY_TIME, Faction[Offering[tID]] + "'s Peace offer has expired.\n")
        //set msg_n[PenDexPlayer_1[i]] = msg_n[PenDexPlayer_1[i]] + " " + Faction[PenDexPlayer_2[i]] + ","
    else
        call DisplayTimedTextToPlayer(Player(Offering[tID]), 0,0, DISPLAY_TIME, "Your Alliance offer to " + Faction[Recieving[tID]] + " has expired.")
        call DisplayTimedTextToPlayer(Player(Recieving[tID]), 0,0, DISPLAY_TIME, Faction[Offering[tID]] + "'s Alliance offer has expired.\n")
        //set msg_a[PenDexPlayer_1[i]] = msg_a[PenDexPlayer_1[i]] + " " + Faction[PenDexPlayer_2[i]] + ","
    endif
endfunction

private function MakeOffer takes integer f, integer pID_1, integer pID_2 returns nothing
    set Timer[f] = CreateTimer()
    set PendingOffer[f] = true 
    call TimerStart(Timer[f], 5, false, function OfferExpires)
    call SaveInteger(hash, GetHandleId(Timer[f]), 0, f)
    set Offering[f] = pID_1
    set Recieving[f] = pID_2
endfunction

//********************************//
//            Triggers            //
//********************************//

private function Reveal takes nothing returns boolean
    local integer cmd_pID
    local player p
    local string s = ""
    local string s1 = ""
    local string s2 = ""
    local string s3 = ""
    local integer i = 0
    call SetWords()
    set cmd_pID = GetPlayerId(cmd_p)
    if (cmd_p != null and IsPlayerInForce(cmd_p, Active) == true) then
        set s = Faction[cmd_pID] + "\n"
        loop
            exitwhen i > 11
            set p = Player(i) 
            if (IsPlayerInForce(p, Active)) then
                if (IsPlayerAlly(cmd_p,p) == true and GetPlayerAlliance(p, cmd_p, ALLIANCE_SHARED_VISION) == true and cmd_p != p) then
                    set s1 = s1 + " " + Faction[i] + ","
                elseif (IsPlayerAlly(cmd_p, p) == true and GetPlayerAlliance(cmd_p, p, ALLIANCE_SHARED_VISION) == false) then
                    set s2 = s2 + " " + Faction[i] + ","
                elseif (IsPlayerEnemy(cmd_p, p)) then 
                    set s3 = s3 + " " + Faction[i] + ","
                endif
            endif
            set i = i + 1
        endloop
        if (StringLength(s1) > 0) then
            set s1 = "Allied:" + DisplayFaction(s1) + ".\n"
        endif
        if (StringLength(s2) > 0) then
            set s2 = "Neutral:" + DisplayFaction(s2) + ".\n"
        endif
        if (StringLength(s3) > 0) then
            set s3 = "Hostile:" + DisplayFaction(s3) + "."
        endif
        call DisplayTimedTextToPlayer(GetTriggerPlayer(), 0, 0, DISPLAY_TIME, s+s1+s2+s3)
    endif 
    return false
endfunction

private function War takes nothing returns boolean
    local integer i = 13
    local player p = GetTriggerPlayer()
    local integer pID = GetPlayerId(p)
    local player p_i
    local integer cmd_pID
    local string msg = ""
    call SetWords()
    if (cmd_p != null) then
        set cmd_pID = GetPlayerId(cmd_p)
        if (cmd_pID < 12) then
            set i = cmd_pID
        elseif (cmd_pID == 12) then
            set i = 0
        endif
        loop
            exitwhen i > 11
            set p_i = Player(i) 
            if (IsPlayerAlly(p, p_i) == true and p != p_i) then
                call SetPlayerAllianceStateBJ(p, p_i, bj_ALLIANCE_UNALLIED)
                call SetPlayerAllianceStateBJ(p_i, p, bj_ALLIANCE_UNALLIED)
                if (PendingOffer[(pID+1)*12+(i+1)-12] == true) then
                    call RemoveOffer((pID+1)*12+(i+1)-12)
                endif
                if (PendingOffer[(i+1)*12+(pID+1)-12] == true) then
                    call RemoveOffer((i+1)*12+(pID+1)-12)
                endif
                if (PendingOffer[144+(pID+1)*12+(i+1)-12] == true) then
                    call RemoveOffer(144+(pID+1)*12+(i+1)-12)
                endif
                if (PendingOffer[144+(i+1)*12+(pID+1)-12] == true) then
                    call RemoveOffer(144+(i+1)*12+(pID+1)-12)
                endif
                if (GetLocalPlayer() == p_i) then
                    call StopSound(s_War, false, false) 
                    call StartSound(s_War) 
                endif
                set msg = msg + " " + Faction[i] + ","
            endif
            if (cmd_pID != 12) then
                set i = 12
            else
                set i = i + 1
            endif
        endloop
        if (StringLength(msg) > 0) then 
            if (GetLocalPlayer() == p) then
                call StopSound(s_War, false, false) 
                call StartSound(s_War) 
            endif
            set msg = Faction[pID] + " declares War on" + DisplayFaction(msg) + "."
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0,0, DISPLAY_TIME, msg)
        endif
    endif
    return false 
endfunction

private function Peace takes nothing returns boolean
    local integer i = 13
    local integer x
    local player p1 = GetTriggerPlayer()
    local player p_i
    local integer pID_1 = GetPlayerId(p1)
    local integer f1
    local integer f2
    local string msg1 = "" 
    local string msg2 = ""
    call SetWords()
    if (cmd_p != null) then
        set x = GetPlayerId(cmd_p)
        if (x < 12) then
            set i = x
        elseif (x == 12) then 
            set i = 0
        endif
    endif
    loop
        exitwhen i > 11
        set f1 = (pID_1+1)*12+(i+1)-12
        set f2 = (i+1)*12+(pID_1+1)-12
        set p_i = Player(i)
        
        // Accept Peace
        if (PendingOffer[f2] == true) then
            call RemoveOffer(f2)
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DISPLAY_TIME, Faction[i] + " and " + Faction[pID_1] + " are now neutral towards each other.")
            call SetPlayerAllianceStateBJ( p_i, p1, bj_ALLIANCE_NEUTRAL)
            call SetPlayerAllianceStateBJ( p1, p_i, bj_ALLIANCE_NEUTRAL)
        // Offer Peace
        elseif (IsPlayerEnemy(p1, p_i) == true and IsPlayerInForce(p_i, Active) == true and PendingOffer[f1] == false) then
            call MakeOffer(f1, pID_1, i)
            set msg1 = msg1 + " " + Faction[i] + ","
            call DisplayTimedTextToPlayer(p_i, 0,0, DISPLAY_TIME, Faction[pID_1] + " has offered you Peace.")
            if (GetLocalPlayer() == p_i) then
                call StopSound(s_Peace, false, false) 
                call StartSound(s_Peace) 
            endif
        // End Alliance into Neutral
        elseif (x < 12 and IsPlayerAlly(p1, p_i) == true and GetPlayerAlliance(p1, p_i, ALLIANCE_SHARED_VISION) == true and p1 != p_i) then
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0,0, DISPLAY_TIME, Faction[pID_1] + " has ended the alliance with " + Faction[i] + ".")
            call SetPlayerAllianceStateBJ( p_i, p1, bj_ALLIANCE_NEUTRAL)
            call SetPlayerAllianceStateBJ( p1, p_i, bj_ALLIANCE_NEUTRAL)
            if (GetLocalPlayer() == p1 or GetLocalPlayer() == p_i) then
                call StopSound(s_Peace, false, false) 
                call StartSound(s_Peace) 
            endif
        // Withdraw Peace Offer
        elseif (PendingOffer[f1] == true) then
            call RemoveOffer(f1)
            set msg2 = msg2 + " " + Faction[i] + ","
            call DisplayTimedTextToPlayer(p_i, 0,0, DISPLAY_TIME, Faction[pID_1] + " has withdrawn the peace offer.")
        endif
        if (x < 12) then
            set i = 12
        else
            set i = i + 1
        endif
    endloop
    if (StringLength(msg1) > 0) then 
        set msg1 = "You offered" + DisplayFaction(msg1) + " Peace."
        call DisplayTimedTextToPlayer(p1, 0,0, DISPLAY_TIME, msg1)
    endif
    if (StringLength(msg2) > 0) then 
        set msg2 = "You are no longer offering" + DisplayFaction(msg2) + " Peace."
        call DisplayTimedTextToPlayer(p1, 0,0, DISPLAY_TIME, msg2)
    endif
    return false 
endfunction

private function Ally takes nothing returns boolean
    local player p = GetTriggerPlayer()
    local integer pID = GetPlayerId(p) 
    local integer cmd_pID
    local integer f1
    local integer f2
    call SetWords()
    
    if (cmd_p != null and IsPlayerInForce(cmd_p, Active) == true and cmd_p != p) then
        set cmd_pID = GetPlayerId(cmd_p)
        set f1 = 144 + (pID+1)*12+(cmd_pID+1)-12
        set f2 = 144 + (cmd_pID+1)*12+(pID+1)-12
        
        // Accept Alliance 
        if (PendingOffer[f2] == true) then
            if (AllyCount[pID] < AllyLimit and AllyCount[cmd_pID] < AllyLimit) then
                set AllyCount[pID] = AllyCount[pID] + 1
                set AllyCount[cmd_pID] = AllyCount[cmd_pID] + 1 
                call RemoveOffer(f2)
                // Remove Pending Neutral Offers
                if (PendingOffer[f1-144] == true) then
                    call RemoveOffer(f1-144)
                endif
                if (PendingOffer[f2-144] == true) then
                    call RemoveOffer(f2-144)
                endif
                call DisplayTimedTextToPlayer(GetLocalPlayer(), 0,0, DISPLAY_TIME, Faction[cmd_pID] + " and " + Faction[pID] + " have formed an Alliance!")
                call SetPlayerAllianceStateBJ(cmd_p, p, bj_ALLIANCE_ALLIED_VISION)
                call SetPlayerAllianceStateBJ(p, cmd_p, bj_ALLIANCE_ALLIED_VISION)
                if (GetLocalPlayer() == cmd_p or GetLocalPlayer() == p) then
                    call StopSound(s_Ally, false, false) 
                    call StartSound(s_Ally) 
                endif
            else
                if (AllyCount[pID] >= AllyLimit) then
                    call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, "Can't Ally " + Faction[cmd_pID] + ". Ally Limit reached.")
                else
                    call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, "Can't Ally " + Faction[cmd_pID] + ". That faction has reached the Ally Limit. ")
                endif
            endif
        // Offer Alliance
        elseif (GetPlayerAlliance(cmd_p, p, ALLIANCE_SHARED_VISION) == false and PendingOffer[f1] == false) then  
            if (AllyCount[pID] < AllyLimit) then
                call MakeOffer(f1, pID, cmd_pID)
                call DisplayTimedTextToPlayer(cmd_p, 0,0, DISPLAY_TIME, Faction[pID] + " has offered you an Alliance.\n")
                call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, "You offered " + Faction[cmd_pID] + " an Alliance.\n")
                    if (GetLocalPlayer() == cmd_p) then
                    call StopSound(s_Offer, false, false) 
                    call StartSound(s_Offer) 
                endif
            else
                call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, "Can't Offer Alliance to " + Faction[cmd_pID] + ". Ally Limit reached.")
            endif
            // Withdraw Offer
        elseif (PendingOffer[f1] == true) then
            call RemoveOffer(f1)
            call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, Faction[pID] + " has withdrawn the Alliance offer.")
            call DisplayTimedTextToPlayer(p, 0,0, DISPLAY_TIME, "You are no longer offering " + Faction[cmd_pID] + " an Alliance.")
        endif
    endif
    return false 
endfunction

private function Give takes nothing returns boolean
    return false 
endfunction

private function Help takes nothing returns boolean
    local string msg = "Diplomacy Commands:\n-reveal # or -r # \n-ally # or -alliance # \n-war # or -unally # \n-neutral # or -na # or -peace #\n"
    call DisplayTimedTextToPlayer(GetTriggerPlayer(), 0, 0, DISPLAY_TIME, msg)
    return false
endfunction

private function Leaves takes nothing returns boolean
    call ForceRemovePlayer(Active, GetTriggerPlayer())
    return false 
endfunction

//===========================================================================
private function init takes nothing returns nothing
    local trigger array t
    local integer i = 0
    loop
        exitwhen i > 5 
            set t[i] = CreateTrigger() 
        set i = i + 1
    endloop
    set i = 0
    loop
        exitwhen i > 11
        
        if (GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER) then
            call ForceAddPlayer(Active, Player(i))
            call TriggerRegisterPlayerChatEvent(t[0], Player(i), "-reveal", false)
            call TriggerRegisterPlayerChatEvent(t[0], Player(i), "-r", false)
            call TriggerRegisterPlayerChatEvent(t[1], Player(i), "-war", false)  
            call TriggerRegisterPlayerChatEvent(t[1], Player(i), "-unally", false)
            call TriggerRegisterPlayerChatEvent(t[2], Player(i), "-na", false)  
            call TriggerRegisterPlayerChatEvent(t[2], Player(i), "-neutral", false)  
            call TriggerRegisterPlayerChatEvent(t[2], Player(i), "-peace", false) 
            call TriggerRegisterPlayerChatEvent(t[3], Player(i), "-ally", false) 
            call TriggerRegisterPlayerChatEvent(t[3], Player(i), "-alliance", false) 
            call TriggerRegisterPlayerChatEvent(t[4], Player(i), "-diplomacy", false) 
            call TriggerRegisterPlayerChatEvent(t[4], Player(i), "-d", false) 
            call TriggerRegisterPlayerEventLeave(t[5], Player(i))
        endif
        set i = i + 1
    endloop    
    call TriggerAddCondition( t[0], Condition( function Reveal ))
    call TriggerAddCondition( t[1], Condition( function War ))
    call TriggerAddCondition( t[2], Condition( function Peace ))
    call TriggerAddCondition( t[3], Condition( function Ally ))
    call TriggerAddCondition( t[4], Condition( function Help ))
    call TriggerAddCondition( t[5], Condition( function Leaves ))
    set i = 0
    loop
        exitwhen t[i] == null
        set t[i] = null
        set i = i + 1
    endloop
    
    set s_Offer = gg_snd_PEACE
    set s_Peace = gg_snd_PEACE
    set s_War = gg_snd_WAR
    set s_Ally = gg_snd_ALLY
    
    set Faction[0] = "|c00FF0303Sparta|r"
    set Faction[1] = "|c000042FFAthens|r"
    set Faction[2] = "|c001CE6B9Argos|r"
    set Faction[3] = "|c00540081Thebes|r"
    set Faction[4] = "|c00FFFC01Epirus|r"
    set Faction[5] = "|c00fEBA0EMacedon|r"
    set Faction[6] = "|c0020C000Thessaly|r"
    set Faction[7] = "|c00E55BB0Crete|r"
    set Faction[8] = "|c00959697Persia|r"
    set Faction[9] = "|c007EBFF1Thrace|r"
    set Faction[10] = "|c00106246Rhodes|r"
    set Faction[11] = "|c004E2A04Troy|r"
    
    // Player 0
    call SaveInteger(hash,0, StringHash("red"),0)
    call SaveInteger(hash,0, StringHash("sparta"),0)
    call SaveInteger(hash,0, StringHash("1"),0)
    // Player 1
    call SaveInteger(hash,0, StringHash("blue"),1)
    call SaveInteger(hash,0, StringHash("athens"),1)
    call SaveInteger(hash,0, StringHash("2"),1)
    // Player 2
    call SaveInteger(hash,0, StringHash("teal"),2)
    call SaveInteger(hash,0, StringHash("argos"),2)
    call SaveInteger(hash,0, StringHash("3"),2)
    // Player 3
    call SaveInteger(hash,0, StringHash("purple"),3)
    call SaveInteger(hash,0, StringHash("thebes"),3)
    call SaveInteger(hash,0, StringHash("4"),3)
    // Player 4
    call SaveInteger(hash,0, StringHash("yellow"),4)
    call SaveInteger(hash,0, StringHash("epirus"),4)
    call SaveInteger(hash,0, StringHash("5"), 4)
    // Player 5
    call SaveInteger(hash,0, StringHash("orange"),5)
    call SaveInteger(hash,0, StringHash("epirus"),5)
    call SaveInteger(hash,0, StringHash("6"),5)
    // Player 6
    call SaveInteger(hash,0, StringHash("green"),6)
    call SaveInteger(hash,0, StringHash("thessaly"),6)
    call SaveInteger(hash,0, StringHash("7"), 6)
    // Player 7
    call SaveInteger(hash,0, StringHash("pink"),7)
    call SaveInteger(hash,0, StringHash("crete"),7)
    call SaveInteger(hash,0, StringHash("8"),7)
    // Player 8
    call SaveInteger(hash,0, StringHash("gray"),8)
    call SaveInteger(hash,0, StringHash("grey"),8)
    call SaveInteger(hash,0, StringHash("persia"),8)
    call SaveInteger(hash,0, StringHash("9"),8)
    // Player 9
    call SaveInteger(hash,0, StringHash("light blue"),9)
    call SaveInteger(hash,0, StringHash("lightblue"),9)
    call SaveInteger(hash,0, StringHash("lb"),9)
    call SaveInteger(hash,0, StringHash("thrace"),9)
    call SaveInteger(hash,0, StringHash("10"),9)
    // Player 10
    call SaveInteger(hash,0, StringHash("dark green"),10)
    call SaveInteger(hash,0, StringHash("darkgreen"),10)
    call SaveInteger(hash,0, StringHash("dg"),10)
    call SaveInteger(hash,0, StringHash("rhodes"),10)
    call SaveInteger(hash,0, StringHash("11"),10)
    // Player 11
    call SaveInteger(hash,0, StringHash("brown"),11)
    call SaveInteger(hash,0, StringHash("troy"),11)
    call SaveInteger(hash,0, StringHash("12"),11)
    // All Players
    call SaveInteger(hash,0, StringHash("all"),12)
    // Gold
    call SaveInteger(hash,0, StringHash("gold"),14)
    call SaveInteger(hash,0, StringHash("g"),14)
    // Lumber
    call SaveInteger(hash,0, StringHash("lumber"),15)
    call SaveInteger(hash,0, StringHash("l"),15)
    call SaveInteger(hash,0, StringHash("wood"),15)
    call SaveInteger(hash,0, StringHash("w"),15)
    
endfunction

endlibrary

Attached the map below.
 

Attachments

  • Diplomacy Sample 0.05a.w3x
    39 KB · Views: 52
Last edited:
Level 26
Joined
Mar 19, 2008
Messages
3,140
I don't have time to check it, yet you could apply things that you should already know after being helped by me and other active members, i.e:

- improve efficiency by storing data localy insteading of spamming e.g Player() throughout the script.

You seem to know how to use GetLocalPlayer, yet when I see:
JASS:
private function DisplayMsgAll takes string s, real t returns nothing
    local integer p = 0
    loop
        exitwhen p > 11
            call DisplayTimedTextToPlayer(Player(p), 0,0, t, s)
        set p = p + 1
    endloop
endfunction
I'm getting wierd thoughts ^)^ call DisplayTimedTextToPlayer(GetLocalPlayer(), 0,0, t, s)

Aproach for private function GetPlayer could be improved probably with Table usage.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
- improve efficiency by storing data localy insteading of spamming e.g Player() throughout the script.

Okey so i create a local variable for pID and another for p

I'm getting wierd thoughts ^)^
:mwahaha:

Added a table like you told me.

Updated map and code in Thread Start.

JASS:
    if (j > 0) then 
        if (j > 1) then
            set i = 0
            loop
                exitwhen i > StringLength(msg1) 
                if (SubString(msg1,i,i+1) == " ") then
                    set j = i 
                endif
                set i = i + 1
            endloop
            set msg1 = SubString(msg1, 0, j-1) + " and " + SubString(msg1, j+1, StringLength(msg1))
        endif
        set msg1 = "You offered peace to" + SubString(msg1, 0, StringLength(msg1)-1) + ".\n"
        call DisplayTimedTextToPlayer(p1, 0,0, DISPLAY_TIME, msg1)
    endif
    /*if (k > 0) then 
        if (k > 1) then
            set i = 0
            loop
                exitwhen i > StringLength(msg2) 
                if (SubString(msg2,i,i+1) == " ") then
                    set k = i 
                endif
                set i = i + 1
            endloop
            set msg2 = SubString(msg2, 0, k-1) + " and " + SubString(msg2, k+1, StringLength(msg2))
        endif
        set msg2 = "You are no longer offering" + SubString(msg2, 0, StringLength(msg2)-1) + " peace.\n"
        call DisplayTimedTextToPlayer(p1, 0,0, DISPLAY_TIME, msg2)
    endif/*

This part, and every other part like it could be turned into a seperate function:
JASS:
private function SetFactionMsg takes string factions, boolean shuffle returns string 
    local integer i = 0
    local integer j = 0
    local string s = SubString(factions, 0, StringLength(factions)-1)
    loop
        exitwhen i > StringLength(s) 
        if (SubString(s, i, i+1) == " ") then 
            set j = i
        endif
        set i = i + 1
    endloop
    if (j > 1) then
        set s = SubString(s, 0, j-1) + " and " + SubString(s, j+1, StringLength(s))
    endif
    return s
endfunction
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
I used this:
JASS:
set cmd_p = Player(LoadInteger(hash, 0, StringHash(Word[1])))
to load the player. The problem I am having now is that if i type "-r " it will register null as player 0 ? Meaning it would work as if i had typed "-r 1" or "-r red"

JASS:
    // Player 0
    call SaveInteger(hash,0, StringHash("red"),0)
    call SaveInteger(hash,0, StringHash("sparta"),0)
    call SaveInteger(hash,0, StringHash("1"),0)

is it because of null = 0 in integer?

I did this to fix the issue:

JASS:
    set cmd_p = Player(LoadInteger(hash, 0, StringHash(Word[1])))
    if (cmd_p == Player(0) and Word[1] != "1" and Word[1] != "red" and Word[1] != "sparta") then
        set cmd_p = null 
    endif
but im not in love with this solution...
 
Last edited:
Level 15
Joined
Aug 7, 2013
Messages
1,337
For player variables I just create an array so I don't need to call the native to cast an integer to a player.

e.g.

JASS:
local integer i = 0
loop
  exitwhen i == TOTAL_PLAYERS
  set players[i] = Player(i)
  set i = i + 1
endloop

It should be faster, and also you'll need an array versus 12 separate variables if you want to make stuff easily MPI.
 
Status
Not open for further replies.
Top