Skip to content

Commit

Permalink
Merge pull request #60 from RudeDude001/patch-2
Browse files Browse the repository at this point in the history
StandardDefences.go
  • Loading branch information
ogame-ninja committed Feb 23, 2021
2 parents fe98d1f + 4d4ee05 commit 9138f6f
Showing 1 changed file with 101 additions and 87 deletions.
188 changes: 101 additions & 87 deletions community/Standard_Defences.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
// set those variable as you wish
/* this script transport and build defences on every planet/moon
for any suggestion, help or bug report contact me on slack: RudeDude
set those variable as you wish */
homeworld = GetCachedCelestial("M:1:2:3") //this will be the main colony/moon to send transports
standardrocket = 10000 //rocketlaucher you want the script to build
standardllaser = 2500 //light laser you want the script to build
standardhlaser = 1000 //heavy laser you want the script to build
standardgauss = 200 //gauss cannon you want the script to build
standardplasma = 100 //plasma turret you want the script to build
standardrocket = 21000 //rocketlaucher you want the script to build
standardllaser = 4000 //light laser you want the script to build
standardhlaser = 2000 //heavy laser you want the script to build
standardgauss = 400 //gauss cannon you want the script to build
standardplasma = 200 //plasma turret you want the script to build
reservedslots = 2 // slot that you want to reserve
minmet = 0 // metal to preserve
mincrys = 0 // crystal to preserve
mindeut = 10000000 // deuterium to preserve
minNanites = 6 // required nanites to start building the defense

// the script starts here, you should not edit it
minNanites = 6 // required nanites level to start building the defense, if you want to build defences on moon should be set to zero
minShipyard = 8 //required shipyard level to start building the defense, make sure it matches the requirements for the def you willing to build
whereshouldbuild = GetCachedPlanets() //planets: GetCachedPlanets(), moons: GetCachedMoons(), planets+moons: GetCachedCelestials()

// the script starts here, you should not edit the code below
func buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma, one, goalrocket, two, goalllaser, three, goalhlaser, four, goalgauss, five, goalplasma) {
Dtostay = NewResources(0, 0, mindeut)
tostay = NewResources(minmet, mincrys, mindeut)
BID, BCD, _, RCD = celestial.ConstructionsBeingBuilt()
facilities, err = celestial.GetFacilities()
NF = facilities.NaniteFactory
SY = facilities.Shipyard
missingrocket = Max(0, goalrocket - sumrocket)
missingllaser = Max(0, goalllaser - sumllaser)
missinghlaser = Max(0, goalhlaser - sumhlaser)
Expand All @@ -27,7 +29,8 @@ func buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma
hlaserPrice = GetPrice(HEAVYLASER, 1)
gaussPrice = GetPrice(GAUSSCANNON, 1)
plasmaPrice = GetPrice(PLASMATURRET, 1)
res, _ = homeworld.GetResources()
sumres, _ = homeworld.GetResources()
res = sumres.Sub(tostay)
price1 = rocketPrice.Mul(missingrocket)
price2 = llaserPrice.Mul(missingllaser)
price3 = hlaserPrice.Mul(missinghlaser)
Expand All @@ -39,29 +42,29 @@ func buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma
goalhlaser = 0
goalgauss = 0
goalplasma = 0
if res.Gte(price1.Add(price2).Add(price3).Add(price4).Add(price5).Sub(Dtostay)) {
if res.Gte(price1.Add(price2).Add(price3).Add(price4).Add(price5)) {
goalrocket = missingrocket
goalllaser = missingllaser
goalhlaser = missinghlaser
goalgauss = missingauss
goalplasma = missingplasma
restosend = price1.Add(price2).Add(price3).Add(price4).Add(price5)
} else if res.Gte(price1.Add(price2).Add(price3).Add(price4).Sub(Dtostay)) && (missingrocket > 0 || missingllaser > 0 || missinghlaser > 0 || missingauss > 0) {
} else if res.Gte(price1.Add(price2).Add(price3).Add(price4)) && (missingrocket > 0 || missingllaser > 0 || missinghlaser > 0 || missingauss > 0) {
goalrocket = missingrocket
goalllaser = missingllaser
goalhlaser = missinghlaser
goalgauss = missingauss
restosend = price1.Add(price2).Add(price3).Add(price4)
} else if res.Gte(price1.Add(price2).Add(price3).Sub(Dtostay)) && (missingrocket > 0 || missingllaser > 0 || missinghlaser > 0) {
} else if res.Sub(tostay).Gte(price1.Add(price2).Add(price3)) && (missingrocket > 0 || missingllaser > 0 || missinghlaser > 0) {
goalrocket = missingrocket
goalllaser = missingllaser
goalhlaser = missinghlaser
restosend = price1.Add(price2).Add(price3)
} else if res.Gte(price1.Add(price2).Sub(Dtostay)) && (missingrocket > 0 || missinglaserini > 0) {
} else if res.Gte(price1.Add(price2)) && (missingrocket > 0 || missinglaserini > 0) {
goalrocket = missingrocket
goalllaser = missingllaser
restosend = price1.Add(price2)
} else if res.Gte(price1.Sub(Dtostay)) && missingrocket > 0 {
} else if res.Gte(price1) && missingrocket > 0 {
goalrocket = missingrocket
restosend = price1
} else if missingplasma > 0 {
Expand Down Expand Up @@ -102,76 +105,87 @@ func buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma
}

//loop def
for celestial in GetCachedPlanets() {
Print("started thread for the planet ", celestial.GetCoordinate())
SleepRandSec(50, 70)
sync = import("sync")
wg = new(sync.WaitGroup)
i = 0
for celestial in whereshouldbuild { //for planets: GetCachedPlanets(), for moons: GetCachedMoons(), for planets+moons use GetCachedCelestials()
SleepRandSec(30, 40)
wg.Add(1)
i += 1
Print("starting thread", i, " for the planet ", celestial.GetCoordinate())
//the go func start a thread for every celestial
go func (celestial) {
res, err = homeworld.GetResources()
queuedef , _, _ = celestial.GetProduction()
defense, err = celestial.GetDefense()
fleets, _ = GetFleets()
slots = GetSlots()
AvailSlots = slots.Total - slots.InUse
destination = nil
for fleet in fleets {
destination = fleet.Destination
}
rocketinqueue = 0
llaserinqueue = 0
hlaserinqueue = 0
gaussinqueue = 0
plasmainqueue = 0
for def in queuedef {
queueID = def.ID
xiteminqueue = def.Nbr
if queueID == ROCKETLAUNCHER && xiteminqueue {
rocketinqueue += xiteminqueue
} else if queueID == LIGHTLASER && xiteminqueue {
llaserinqueue += xiteminqueue
} else if queueID == HEAVYLASER && xiteminqueue {
hlaserinqueue += xiteminqueue
} else if queueID == GAUSSCANNON && xiteminqueue {
gaussinqueue += xiteminqueue
} else if queueID == PLASMATURRET && xiteminqueue {
plasmainqueue += xiteminqueue
go func (celestial) {
for {
res, err = homeworld.GetResources()
tostay = NewResources(minmet, mincrys, mindeut)
queuedef , _, _ = celestial.GetProduction()
defense, err = celestial.GetDefense()
fleets, _ = GetFleets()
slots = GetSlots()
AvailSlots = slots.Total - slots.InUse
destination = nil
for fleet in fleets {
destination = fleet.Destination
}
rocketinqueue = 0
llaserinqueue = 0
hlaserinqueue = 0
gaussinqueue = 0
plasmainqueue = 0
for def in queuedef {
queueID = def.ID
xiteminqueue = def.Nbr
if queueID == ROCKETLAUNCHER && xiteminqueue {
rocketinqueue += xiteminqueue
} else if queueID == LIGHTLASER && xiteminqueue {
llaserinqueue += xiteminqueue
} else if queueID == HEAVYLASER && xiteminqueue {
hlaserinqueue += xiteminqueue
} else if queueID == GAUSSCANNON && xiteminqueue {
gaussinqueue += xiteminqueue
} else if queueID == PLASMATURRET && xiteminqueue {
plasmainqueue += xiteminqueue
}
}
sumrocket = defense.RocketLauncher + rocketinqueue
sumllaser = defense.LightLaser + llaserinqueue
sumhlaser = defense.HeavyLaser + hlaserinqueue
sumgauss = defense.GaussCannon + gaussinqueue
sumplasma = defense.PlasmaTurret + plasmainqueue
BID, BCD, _, _ = celestial.ConstructionsBeingBuilt()
facilities, err = celestial.GetFacilities()
NF = facilities.NaniteFactory
SY = facilities.Shipyard
if !celestial.GetCoordinate().Equal(destination) && res.Sub(tostay).Gte(GetPrice(PLASMATURRET, 1)) && NF >= minNanites && SY >= minShipyard && BID != NANITEFACTORY && BID != SHIPYARD && (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma <standardplasma) && AvailSlots > reservedslots {
Print(celestial.GetCoordinate(), "Started func build defense")
buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma, ROCKETLAUNCHER, standardrocket, LIGHTLASER, standardllaser, HEAVYLASER, standardhlaser, GAUSSCANNON, standardgauss, PLASMATURRET, standardplasma)
SleepRandSec(30, 60)
} else if sumrocket >= standardrocket && sumllaser >= standardllaser && sumhlaser >= standardhlaser && sumgauss >= standardgauss && sumplasma >= standardplasma {
Print(celestial.GetCoordinate(), "Defense completed on the planet, thread " + i, " done")
wg.Done()
return
}else if NF < minNanites || SY < minShipyard {
Print(celestial.GetCoordinate(), "Need to build facilities first")
SleepRandSec(30, 60)
} else if (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma < standardplasma) && !res.Sub(tostay).Gte(GetPrice(PLASMATURRET, 1)) {
Print(celestial.GetCoordinate(), "Not enough resources, can't build defenses atm")
SleepRandSec(30, 60)
} else if (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma < standardplasma) && (BID == NANITEFACTORY || BID == SHIPYARD) {
Print(celestial.GetCoordinate(), "Nanites or Shipyard on building, can't build defenses atm")
SleepRandSec(30, 60)
} else if AvailSlots <= reservedslots {
Print(celestial.GetCoordinate(), "There aren't enough available slots atm")
SleepRandSec(30, 60)
} else if celestial.GetCoordinate().Equal(destination) {
Print(celestial.GetCoordinate(), "Transport already flying to this coords")
SleepRandSec(30, 60)
}
Rmin = Random(20, 40)
Print(celestial.GetCoordinate(), "sleep", Rmin, " min")
SleepMin(Rmin)
continue
}
sumrocket = defense.RocketLauncher + rocketinqueue
sumllaser = defense.LightLaser + llaserinqueue
sumhlaser = defense.HeavyLaser + hlaserinqueue
sumgauss = defense.GaussCannon + gaussinqueue
sumplasma = defense.PlasmaTurret + plasmainqueue
BID, BCD, _, _ = celestial.ConstructionsBeingBuilt()
facilities, err = celestial.GetFacilities()
NF = facilities.NaniteFactory
SY = facilities.Shipyard
if !celestial.GetCoordinate().Equal(destination) && res.Gte(GetPrice(PLASMATURRET, 1)) && NF >= minNanites && SY >= 8 && BID != NANITEFACTORY && BID != SHIPYARD && (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma <standardplasma) && AvailSlots > reservedslots {
Print(celestial.GetCoordinate(), "started func build defense")
buildefense(celestial, sumrocket, sumllaser, sumhlaser, sumgauss, sumplasma, ROCKETLAUNCHER, standardrocket, LIGHTLASER, standardllaser, HEAVYLASER, standardhlaser, GAUSSCANNON, standardgauss, PLASMATURRET, standardplasma)
SleepRandSec(30, 60)
} else if NF < minNanites || SY < 8 {
Print(celestial.GetCoordinate(), "Need to build facilities first")
SleepRandSec(30, 60)
} else if (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma < standardplasma) && !res.Gte(GetPrice(PLASMATURRET, 1)) {
Print(celestial.GetCoordinate(), "Not enough resources, can't build defenses atm")
SleepRandSec(30, 60)
} else if (sumrocket < standardrocket || sumllaser < standardllaser || sumhlaser < standardhlaser || sumgauss < standardgauss || sumplasma < standardplasma) && (BID != NANITEFACTORY || BID != SHIPYARD) {
Print(celestial.GetCoordinate(), "Nanites or Shipyard on building, can't build defenses atm")
SleepRandSec(30, 60)
} else if sumrocket >= standardrocket && sumllaser >= standardllaser && sumhlaser >= standardhlaser && sumgauss >= standardgauss && sumplasma >= standardplasma {
Print(celestial.GetCoordinate(), "Defense completed on the planet, congratulations")
return
} else if AvailSlots <= reservedslots {
Print(celestial.GetCoordinate(), "There aren't enough available slots atm")
SleepRandSec(30, 60)
} else {
SleepRandSec(30, 60)
}
Rmin = Random(50, 100)
Print(celestial.GetCoordinate(), "sleep", Rmin, " min")
SleepMin(Rmin)
continue
} (celestial)
}
<-OnQuitCh
wg.Wait()
Print("Defences built on every planet, congratulations")

0 comments on commit 9138f6f

Please sign in to comment.