Browse Source

Added fly back to origin under certain conditions

master
Khaled Nassar 4 years ago
parent
commit
3923161ae5
3 changed files with 52 additions and 3 deletions
  1. +8
    -0
      main.go
  2. +6
    -0
      models/models.go
  3. +38
    -3
      strategies/linker.go

+ 8
- 0
main.go View File

@@ -82,6 +82,8 @@ func parseLine(text string) bool {
Richness: 0,
Owner: -2,
Ships: -2,
Capturing: false,
CapturedBy: -2,
Turns: -2,
}
stars = append(stars, star)
@@ -99,6 +101,12 @@ func parseLine(text string) bool {
star.Ships = ships
star.Turns = turns
star.FlightsAllowed = 3
if star.Capturing && !star.Unowned() {
star.Capturing = false
if !star.OwnedByMe() {
star.CapturedBy = -2
}
}
break
case "link":
var source_idx, _ = strconv.Atoi(parts[1])

+ 6
- 0
models/models.go View File

@@ -9,6 +9,8 @@ type Star struct {
Ships int
Turns int
FlightsAllowed int
Capturing bool
CapturedBy int
}

type Flight struct {
@@ -38,4 +40,8 @@ func (s *Star) Friendly() bool {

func (s *Star) Enemy() bool {
return s.Owner == 2
}

func (s *Star) Captured() bool {
return s.CapturedBy >= 0
}

+ 38
- 3
strategies/linker.go View File

@@ -5,11 +5,16 @@ import (
"fmt"
)

const (
capturingShipThreshold int = 10
)

type LinkerStrategy struct {
}

func (ls *LinkerStrategy) Execute(stars []*models.Star, flights []models.Flight, links []models.Link) {
// Link then attack
// Send back ships, link, then attack
ls.sendHomiesBack(stars, flights, links)
ls.linkFriendlies(stars, flights, links)
ls.attack(stars, flights, links)
}
@@ -27,7 +32,7 @@ func (ls *LinkerStrategy) attack(stars []*models.Star, flights []models.Flight,
}

// Wait till we have a sufficient number of ships
if s.Ships < 10 {
if ls.insufficientShipsForCapturing(s) {
continue
}

@@ -40,6 +45,11 @@ func (ls *LinkerStrategy) attack(stars []*models.Star, flights []models.Flight,
continue
}

// In the process of capturing the star anyway
if t.Capturing {
continue
}

// Check if ship is within distance
//fmt.Printf("TARGET => IDX=%d, X=%d, Y=%d, OWNER=%d, RICH=%d, SHIPS=%d\n", t.Idx, t.X, t.Y, t.Owner, t.Richness, t.Ships)
flightTurnDistance := flightTurnDistance(s, t)
@@ -62,6 +72,8 @@ func (ls *LinkerStrategy) attack(stars []*models.Star, flights []models.Flight,
// TODO: Find best destination
// Fly to this target, and skip processing all remaining potential target stars
fmt.Printf("fly %d %d %d\n", i, j, shipCount)
t.Capturing = true
t.CapturedBy = s.Idx
s.FlightsAllowed--
break
}
@@ -82,7 +94,6 @@ func (ls *LinkerStrategy) linkFriendlies(stars []*models.Star, flights []models.
}
}

// Let's try Linking first
// Keep a map of all the distances
for i := 0; i < len(friendly); i++ {
src := stars[i]
@@ -144,10 +155,34 @@ func (ls *LinkerStrategy) linkFriendlies(stars []*models.Star, flights []models.
}
}

func (ls *LinkerStrategy) sendHomiesBack(stars []*models.Star, flights []models.Flight, links []models.Link) {
for i, s := range stars {
if s.OwnedByMe() && s.Captured() && ls.insufficientShipsForCapturing(s) && s.FlightsAllowed > 0 {
richnessRoundsToCapture = capturingShipThreshold / s.Richness

if richnessRoundsToCapture == 0 {
richnessRoundsToCapture = 1
}

timeToCapture = s.Turns + (richnessRoundsToCapture - 1) * 5
capturingStar = stars[s.CapturedBy]
distanceToOrigin = flightTurnDistance(capturingStar, s)
if flightTurnDistance < timeToCapture {
s.FlightsAllowed--
fmt.Printf("fly %d %d %d\n", s.Idx, capturingStar.Idx, s.Ships)
}
}
}
}

func (s *LinkerStrategy) insufficientShipsForLinking(star *models.Star) bool {
return star.Ships < 5
}

func (s* LinkerStrategy) insufficientShipsForCapturing(star *models.Star) bool {
return star.Ships < capturingShipThreshold
}

func (s *LinkerStrategy) linkedOrInProgress(src *models.Star, dst *models.Star, links []models.Link, flights []models.Flight) bool {
linking := false


Loading…
Cancel
Save