package main import ( v "bitbucket.org/hackerbots/vector" ) type Robot struct { Id string `json:"id"` Name string `json:"name"` Stats Stats `json:"-"` TargetSpeed float32 `json:"-"` Speed float32 `json:"speed"` Health int `json:"health"` RepairCounter float32 `json:"repair"` Position v.Point2d `json:"position"` Heading v.Vector2d `json:"heading"` MoveTo *v.Point2d `json:"-"` FireAt *v.Point2d `json:"-"` Scanners []Scanner `json:"scanners"` LastFired int `json:"-"` Collision bool `json:"collision"` Hit bool `json:"hit"` } // This is the subset of data we send to players about robots // that are not theirs. type OtherRobot struct { Id string `json:"id"` Name string `json:"name"` Position v.Point2d `json:"position"` Heading v.Vector2d `json:"heading"` Health int `json:"health"` } func (r Robot) GetTruncatedDetails() OtherRobot { return OtherRobot{ Id: r.Id, Name: r.Name, Position: r.Position, Heading: r.Heading, Health: r.Health, } } type RobotSorter struct { Robots []OtherRobot } func (s RobotSorter) Len() int { return len(s.Robots) } func (s RobotSorter) Swap(i, j int) { s.Robots[i], s.Robots[j] = s.Robots[j], s.Robots[i] } func (s RobotSorter) Less(i, j int) bool { return s.Robots[i].Id < s.Robots[j].Id } // TODO - how do I not duplicate this code??? type AllRobotSorter struct { Robots []BotHealth } func (s AllRobotSorter) Len() int { return len(s.Robots) } func (s AllRobotSorter) Swap(i, j int) { s.Robots[i], s.Robots[j] = s.Robots[j], s.Robots[i] } func (s AllRobotSorter) Less(i, j int) bool { return s.Robots[i].RobotId < s.Robots[j].RobotId } type Stats struct { Hp int `json:"-"` Speed float32 `json:"-"` Acceleration float32 `json:"-"` WeaponRadius int `json:"-"` ScannerRadius int `json:"-"` TurnSpeed int `json:"-"` FireRate int `json:"-"` WeaponDamage int `json:"-"` WeaponSpeed float32 `json:"-"` } // We request stats using an integer between 1 and 100, the // integer values map to sensible min-max ranges type StatsRequest struct { Hp int `json:"hp"` Speed int `json:"speed"` Acceleration int `json:"acceleration"` WeaponRadius int `json:"weapon_radius"` ScannerRadius int `json:"scanner_radius"` TurnSpeed int `json:"turn_speed"` FireRate int `json:"fire_rate"` WeaponDamage int `json:"weapon_damage"` WeaponSpeed int `json:"weapon_speed"` } func DeriveStats(request StatsRequest) Stats { s := Stats{} // Conversion Tables var hp_min float32 = 20.0 var hp_max float32 = 200.0 s.Hp = int(((float32(request.Hp) / 100.0) * (hp_max - hp_min)) + hp_min) var speed_min float32 = 40.0 var speed_max float32 = 200.0 s.Speed = ((float32(request.Speed) / 100.0) * (speed_max - speed_min)) + speed_min var accel_min float32 = 20.0 var accel_max float32 = 200.0 s.Acceleration = ((float32(request.Acceleration) / 100.0) * (accel_max - accel_min)) + accel_min var wep_rad_min float32 = 5.0 var wep_rad_max float32 = 60.0 s.WeaponRadius = int(((float32(request.WeaponRadius) / 100.0) * (wep_rad_max - wep_rad_min)) + wep_rad_min) var scan_rad_min float32 = 100.0 var scan_rad_max float32 = 400.0 s.ScannerRadius = int(((float32(request.ScannerRadius) / 100.0) * (scan_rad_max - scan_rad_min)) + scan_rad_min) var turn_spd_min float32 = 30.0 var turn_spd_max float32 = 300.0 s.TurnSpeed = int(((float32(request.TurnSpeed) / 100.0) * (turn_spd_max - turn_spd_min)) + turn_spd_min) var fire_rate_min float32 = 10.0 var fire_rate_max float32 = 2000.0 s.FireRate = int(fire_rate_max+300.0) - int(((float32(request.FireRate)/100.0)*(fire_rate_max-fire_rate_min))+fire_rate_min) var weapon_damage_min float32 = 0.0 var weapon_damage_max float32 = 20.0 s.WeaponDamage = int(((float32(request.WeaponDamage) / 100.0) * (weapon_damage_max - weapon_damage_min)) + weapon_damage_min) var weapon_speed_min float32 = 80.0 var weapon_speed_max float32 = 600.0 s.WeaponSpeed = float32(((float32(request.WeaponSpeed) / 100.0) * (weapon_speed_max - weapon_speed_min)) + weapon_speed_min) return s } func (s StatsRequest) Valid() bool { total := (s.Speed + s.Hp + s.WeaponRadius + s.ScannerRadius + s.Acceleration + s.TurnSpeed + s.FireRate) // allowing for 50 pts in every category if total > 500 { return false } return true } type Instruction struct { MoveTo *v.Point2d `json:"move_to,omitempty"` FireAt *v.Point2d `json:"fire_at,omitempty"` TargetSpeed *float32 `json:"target_speed,omitempty"` Repair *bool `json:"repair,omitempty"` Stats Stats `json:"stats"` }