- Joined
- Jan 11, 2009
- Messages
- 3,414
Hello everyone!
As it looks now, there are only two or three things in the way of releasing World in Flames, one of thee being the need of a proper sea trading system.
The principle of this system is that uncontrollable ships are being sent from the ports on the map to different allied players, generating income for both if it reaches it's destination.
In my system, i loop through all players on the map, group their ports, and count their allies. Then i use a couple of loops to stack the allied players in an array based on how many ports they have (their so called "trade power"), where 1 is the one with the most ports. I then start sending ships from the first port in my group, and tell it to go to the closest port owned by the target ally, where port 1 sends to the ally that is first in the array, port 2 sends to the ally that comes second, and so on. I also use ModuloInteger(i, maxallies) to make sure that if i have more ports than allies, say 3 allies and 4 ports, port no. 4 will send to ally 1 again.
Now, my script is very sloppy and unfinished, and has many apparent flaws:
*Although it doesn't sent trade ships to an ally that has no ports, it does not check a new ally in it's stead but just continues onto the next port and leaves the current one without having sent any ship.
*If a port is captured while a ship is heading towards it, i so far have no method of telling the ship to head towards a new port.
*It.. well, it doesn't work!
Here is the code:
Can anyone help me sort this out?
As it looks now, there are only two or three things in the way of releasing World in Flames, one of thee being the need of a proper sea trading system.
The principle of this system is that uncontrollable ships are being sent from the ports on the map to different allied players, generating income for both if it reaches it's destination.
In my system, i loop through all players on the map, group their ports, and count their allies. Then i use a couple of loops to stack the allied players in an array based on how many ports they have (their so called "trade power"), where 1 is the one with the most ports. I then start sending ships from the first port in my group, and tell it to go to the closest port owned by the target ally, where port 1 sends to the ally that is first in the array, port 2 sends to the ally that comes second, and so on. I also use ModuloInteger(i, maxallies) to make sure that if i have more ports than allies, say 3 allies and 4 ports, port no. 4 will send to ally 1 again.
Now, my script is very sloppy and unfinished, and has many apparent flaws:
*Although it doesn't sent trade ships to an ally that has no ports, it does not check a new ally in it's stead but just continues onto the next port and leaves the current one without having sent any ship.
*If a port is captured while a ship is heading towards it, i so far have no method of telling the ship to head towards a new port.
*It.. well, it doesn't work!
Here is the code:
JASS:
library TradeSys initializer Init
globals
integer array numports[12]
private player temp_player
private timer interval
private player array pl[12] //The "p" variable from your snippet that i had to rename, since p was a bit too commonly used for a global variable!
endglobals
private function GetPlayerPortPacific takes nothing returns boolean
if GetOwningPlayer(GetFilterUnit()) != temp_player then
return false
endif
if GetUnitY(GetFilterUnit()) > -2000 then //Theese functions make sure that the system doesn't pick ports on the other theatre.
return false
endif
return true
endfunction
private function GetPlayerPortEurope takes nothing returns boolean
if GetOwningPlayer(GetFilterUnit()) != temp_player then
return false
endif
if GetUnitY(GetFilterUnit()) < -2500 then
return false
endif
return true
endfunction
private function onLoop takes nothing returns nothing
local integer p = 0
local integer i = 0
local real x = 0
local real y = 0
local integer allies = 0
local group g1 = CreateGroup()
local group g2 = CreateGroup()
local unit u = null
local unit ship
local player target
//Trade Power variables
local player b
local player q
local integer i2 = 0
local integer n = 0
local integer m = 0
loop
exitwhen p > 11
set allies = 0
set i = 0
loop
exitwhen i > 11
if GetPlayerAlliance(Player(p), Player(i), ALLIANCE_SHARED_VISION) == true then
set allies = allies+1
endif
set i = i+1
endloop
set i = 0
if allies != 0 then
set temp_player = Player(p)
call GroupEnumUnitsOfType(g1, UnitId2String('h00R'), Filter(function GetPlayerPortPacific)) //Group ports owned by the player
loop
//in this loop, the index of the allies is 'i'.
set u = FirstOfGroup(g1) //...but the loop ends when the player runs out of ports.
exitwhen u == null
set x = GetUnitX(u)
set y = GetUnitY(u)
//The following two loops are for listing the players in an array in order of their ammount of ports possesed.
loop
exitwhen i2 > 11
set b = Player(i2)
if target != Player(i2) and IsPlayerAlly(target,b) then
set pl[n] = b
set n = n + 1
endif
set i2 = i2 + 1
endloop
set i2 = 0
loop
exitwhen n == 1 or i2 >= n
set m = i2
loop
set m = m + 1
exitwhen m >= n
if numports[GetPlayerId(pl[i2])] < numports[GetPlayerId(pl[m])] then
set q = pl[i2]
set pl[i2] = pl[m]
set pl[m] = q
endif
endloop
set i2 = i2 + 1
endloop
set target = pl[ModuloInteger(i, allies)] //this line makes sure that the picked ally is the one with the highest trading power.
set temp_player = Player(i)
call GroupEnumUnitsOfType(g2, UnitId2String('h00R'), Filter(function GetPlayerPortPacific)) //Group ports owned by the target ally
if CountUnitsInGroup(g2) != 0 then
set u = GetClosestUnitInGroup(x, y, g2)
set ship = CreateUnit(Player(p), 'h01B', x, y, 0.)
call IssueTargetOrder(ship, "channel", u)
endif
call GroupRemoveUnit(g1, u)
endloop
endif
set i = i+1
endloop
set p = 0
set i = 0
set m = 0
set n = 0
set i2 = 0
set u = null
loop
exitwhen p > 11
set allies = 0
set i = 0
loop
exitwhen i > 11
if GetPlayerAlliance(Player(p), Player(i), ALLIANCE_SHARED_VISION) == true then
set allies = allies+1
endif
set i = i+1
endloop
set i = 0
if allies != 0 then
set temp_player = Player(p)
call GroupEnumUnitsOfType(g1, UnitId2String('h00R'), Filter(function GetPlayerPortEurope)) //Group ports owned by the player
loop
//in this loop, the index of the allies is 'i'.
set u = FirstOfGroup(g1) //...but the loop ends when the player runs out of ports.
exitwhen u == null
set x = GetUnitX(u)
set y = GetUnitY(u)
loop
exitwhen i2 > 11
set b = Player(i2)
if target != Player(i2) and IsPlayerAlly(target,b) then
set pl[n] = b
set n = n + 1
endif
set i2 = i2 + 1
endloop
set i2 = 0
loop
exitwhen n == 1 or i2 >= n
set m = i2
loop
set m = m + 1
exitwhen m >= n
if numports[GetPlayerId(pl[i2])] < numports[GetPlayerId(pl[m])] then
set q = pl[i2]
set pl[i2] = pl[m]
set pl[m] = q
endif
endloop
set i2 = i2 + 1
endloop
set target = pl[ModuloInteger(i, allies)] //this line makes sure that the picked ally is the one with the highest trading power.
set temp_player = Player(i)
call GroupEnumUnitsOfType(g2, UnitId2String('h00R'), Filter(function GetPlayerPortEurope)) //Group ports owned by the target ally
if CountUnitsInGroup(g2) != 0 then
set u = GetClosestUnitInGroup(x, y, g2)
set ship = CreateUnit(Player(p), 'h01B', x, y, 0.)
call IssueTargetOrder(ship, "channel", u)
endif
call GroupRemoveUnit(g1, u)
endloop
endif
set i = i+1
endloop
endfunction
private function IsUnitHarbour takes nothing returns boolean
local integer p = GetPlayerId(GetOwningPlayer(GetFilterUnit()))
if GetUnitTypeId(GetFilterUnit()) == 'h00R' then
set numports = numports + 1
return true
endif
return false
endfunction
private function Init takes nothing returns nothing
local integer i = 0
local group g = CreateGroup()
set interval = CreateTimer()
call TimerStart(interval, 20, true, function onLoop)
loop
exitwhen i > 11
call GroupEnumUnitsOfPlayer(g, Player(i), Filter(function IsUnitHarbour))
call GroupClear(g)
set i = i+1
endloop
endfunction
endlibrary
Can anyone help me sort this out?
Last edited: