diff --git a/bomb.go b/bomb.go new file mode 100644 index 0000000..f992cdc --- /dev/null +++ b/bomb.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "time" +) + +type Bomb struct { + player *Connection + origin *System + target *System + start time.Time + fti int64 // frames to impact +} + +func NewBomb(from *Connection, to *System) *Bomb { + origin := from.System() + dist := origin.DistanceTo(to) + fti := int64(dist / (options.lightSpeed * options.bombSpeed)) + eta := time.Duration(fti) * time.Second / time.Duration(options.frameRate) + log_info("bomb from: %s to: %s ETA: %v", from.PlayerName(), to.Label(), eta) + return &Bomb{ + player: from, + origin: origin, + target: to, + fti: fti, + start: time.Now(), + } +} + +func (b *Bomb) Dead() bool { + return b.fti <= 0 +} + +func (b *Bomb) Tick(frame int64) { + b.fti -= 1 + if b.fti <= 0 { + b.target.Bombed(b.player) + } +} + +func (b *Bomb) String() string { + return fmt.Sprintf("[bomb from: %s to: %s lived: %s]", b.origin.Label(), b.target.Label(), time.Since(b.start)) +} diff --git a/commands.go b/commands.go index 16603a6..90059eb 100644 --- a/commands.go +++ b/commands.go @@ -207,7 +207,7 @@ var bombCommand = &Command{ dest_name := strings.Join(args, " ") to, ok := nameIndex[dest_name] if ok { - bomb(conn, to) + currentGame.Register(NewBomb(conn, to)) return } @@ -226,7 +226,7 @@ var bombCommand = &Command{ fmt.Fprintf(conn, "weapons are still reloading. Can bomb again in %v\n", conn.NextBomb()) return } - bomb(conn, to) + currentGame.Register(NewBomb(conn, to)) }, } @@ -246,15 +246,6 @@ var mkBombCommand = &Command{ }, } -func bomb(conn *Connection, to *System) { - conn.bombs -= 1 - delay := conn.System().BombTimeTo(to) - fmt.Fprintf(conn, "sending bomb to %s. ETA: %v\n", to.name, delay) - After(delay, func() { - to.Bombed(conn) - }) -} - func isCommand(name string) bool { _, ok := commandRegistry[name] return ok diff --git a/main.go b/main.go index 2731de9..e0b4943 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ var options struct { frameRate int miningRate int playerSpeed float64 + bombSpeed float64 economic int } @@ -117,5 +118,6 @@ func init() { flag.IntVar(&options.frameRate, "frame-rate", 100, "frame rate, in frames per second") flag.IntVar(&options.miningRate, "mining-rate", 1, "mining rate, in duckets per frame") flag.Float64Var(&options.playerSpeed, "player-speed", 0.8, "player travel speed, relative to C, the speed of light") + flag.Float64Var(&options.bombSpeed, "bomb-speed", 0.9, "bomb travel speed, relattive to C, the speed of light") flag.IntVar(&options.economic, "economic", 25000, "amount of money needed to win economic victory") }