Documentation ¶
Index ¶
- Constants
- Variables
- func AreaOf(playerCount int) float32
- func Bench(b *testing.B, create func(radius int) World, end int)
- func CrateCountOf(playerCount int) int
- func Lerp(a, b, factor float32) float32
- func LevelToScore(level uint8) int
- func ObstacleCountOf(playerCount int) int
- func RadiusOf(playerCount int) float32
- func RandString(n int) string
- func Test(t *testing.T, create func(radius int) World)
- type AABB
- type Angle
- func (angle Angle) Abs() float32
- func (angle Angle) ClampMagnitude(m Angle) Angle
- func (angle Angle) Diff(otherAngle Angle) (difference Angle)
- func (angle Angle) Float() float32
- func (angle Angle) Inv() Angle
- func (angle Angle) Lerp(otherAngle Angle, factor float32) Angle
- func (angle Angle) MarshalJSON() ([]byte, error)
- func (angle Angle) String() string
- func (angle *Angle) UnmarshalJSON(b []byte) error
- func (angle Angle) Vec2f() Vec2f
- type Armament
- type DeathReason
- type Entity
- func (entity *Entity) Active() bool
- func (entity *Entity) AimTarget() Vec2f
- func (entity *Entity) Altitude() float32
- func (entity *Entity) AltitudeOverlap(other *Entity) bool
- func (entity *Entity) ArmamentConsumption() []Ticks
- func (entity *Entity) ArmamentTransform(index int) Transform
- func (entity *Entity) BelowKeel(terrain Terrain) float32
- func (entity *Entity) Camera() (position Vec2f, visual, radar, sonar float32)
- func (entity *Entity) CanLand(parent *Entity) bool
- func (entity *Entity) ClearSpawnProtection()
- func (entity *Entity) Close()
- func (entity *Entity) Collides(otherEntity *Entity, seconds float32) bool
- func (entity *Entity) ConsumeArmament(index int)
- func (entity *Entity) Damage(damage Ticks) bool
- func (entity *Entity) DamagePercent() float32
- func (entity *Entity) Hash() float32
- func (entity *Entity) HealthPercent() float32
- func (entity *Entity) Initialize(entityType EntityType)
- func (entity *Entity) KillIn(ticks, killTime Ticks) bool
- func (entity *Entity) OwnerBoatAimTarget() Vec2f
- func (entity *Entity) Repair(ticks Ticks)
- func (entity *Entity) Replenish(amount Ticks)
- func (entity *Entity) SetActive(val bool)
- func (entity *Entity) SetAimTarget(target Vec2f)
- func (entity *Entity) SetAltitudeTarget(altitudeTarget float32)
- func (entity *Entity) SpawnProtection() float32
- func (entity *Entity) TurretAngles() []Angle
- func (entity *Entity) Update(ticks Ticks, worldRadius float32, terrain Terrain) (die bool)
- func (entity *Entity) UpdateSensor(otherEntity *Entity)
- type EntityID
- type EntityKind
- type EntitySubKind
- type EntityType
- func (entityType EntityType) AppendText(buf []byte) []byte
- func (entityType EntityType) Data() *EntityTypeData
- func (entityType EntityType) MarshalText() ([]byte, error)
- func (entityType EntityType) MaxHealth() Ticks
- func (entityType EntityType) ReducedLifespan(desiredLifespan Ticks) Ticks
- func (entityType EntityType) String() string
- func (entityType *EntityType) UnmarshalText(text []byte) (err error)
- func (entityType EntityType) UpgradePaths(score int) (upgradePaths []EntityType)
- func (entityType EntityType) UpgradesTo(nextEntityType EntityType, score int) bool
- type EntityTypeData
- type Guidance
- type IDPlayerData
- type Player
- func (player *Player) Camera() (pos Vec2f, visual, radar, sonar float32)
- func (player Player) CanRespawnWithTeam() bool
- func (player *Player) ClearDeath()
- func (player *Player) ClearRespawn()
- func (player *Player) Died(entity *Entity)
- func (player *Player) Friendly(other *Player) bool
- func (player *Player) IDPlayerData() IDPlayerData
- func (player *Player) PlayerID() PlayerID
- func (player *Player) Respawning() bool
- type PlayerData
- type PlayerID
- type PlayerSet
- func (set *PlayerSet) Add(player *Player)
- func (set *PlayerSet) AppendData(buf []IDPlayerData) []IDPlayerData
- func (set *PlayerSet) Contains(player *Player) bool
- func (set *PlayerSet) GetByID(playerID PlayerID) *Player
- func (set *PlayerSet) Len() int
- func (set *PlayerSet) Less(i, j int) bool
- func (set *PlayerSet) Pop() interface{}
- func (set *PlayerSet) Push(x interface{})
- func (set *PlayerSet) Remove(player *Player)
- func (set *PlayerSet) Swap(i, j int)
- type Sensor
- type Sensors
- type Team
- type TeamCode
- type TeamID
- type Terrain
- type Ticks
- type Transform
- type Turret
- type Vec2f
- func (vec Vec2f) Abs() Vec2f
- func (vec Vec2f) Add(otherVec Vec2f) Vec2f
- func (vec Vec2f) AddScaled(otherVec Vec2f, factor float32) Vec2f
- func (vec Vec2f) Angle() Angle
- func (vec Vec2f) Ceil() Vec2f
- func (vec Vec2f) Distance(otherVec Vec2f) float32
- func (vec Vec2f) DistanceSquared(otherVec Vec2f) float32
- func (vec Vec2f) Div(divisor float32) Vec2f
- func (vec Vec2f) Dot(otherVec Vec2f) float32
- func (vec Vec2f) Floor() Vec2f
- func (vec Vec2f) Length() float32
- func (vec Vec2f) LengthSquared() float32
- func (vec Vec2f) Lerp(otherVec Vec2f, factor float32) Vec2f
- func (vec Vec2f) Mul(factor float32) Vec2f
- func (vec Vec2f) Norm() Vec2f
- func (vec Vec2f) Rot180() Vec2f
- func (vec Vec2f) Rot90() Vec2f
- func (vec Vec2f) RotN90() Vec2f
- func (vec Vec2f) Round() Vec2f
- func (vec Vec2f) Sub(otherVec Vec2f) Vec2f
- type Velocity
- func (vel Velocity) AddClamped(amount float32, mag Velocity) Velocity
- func (vel Velocity) ClampMagnitude(mag Velocity) Velocity
- func (vel Velocity) ClampMin(min Velocity) Velocity
- func (vel Velocity) Float() float32
- func (vel Velocity) MarshalJSON() ([]byte, error)
- func (vel Velocity) String() string
- func (vel *Velocity) UnmarshalJSON(b []byte) error
- type World
Constants ¶
const ( DeathTypeBorder = "border" // Never Player or Entity DeathTypeTerrain = "terrain" // Never Player or Entity DeathTypeCollision = "collision" // requires Player or Entity DeathTypeRamming = "ramming" // requires Player DeathTypeSinking = "sinking" // requires Player and Entity )
const ( EntityKindInvalid = EntityKind(invalidEnumChoice) EntitySubKindInvalid = EntitySubKind(invalidEnumChoice) EntityTypeInvalid = EntityType(invalidEnumChoice) )
const ( PlayerIDInvalid = PlayerID(0) PlayerNameLengthMin = 3 PlayerNameLengthMax = 12 )
const ( TeamCodeBase = 36 TeamCodeInvalid = TeamCode(0) TeamIDInvalid = TeamID(0) TeamIDLengthMin = 1 TeamIDLengthMax = 6 TeamMembersMax = 6 )
No team
const ( TickPeriod = time.Second / 10 TicksPerSecond = Ticks(time.Second / TickPeriod) TicksPerDamage = 60 * TicksPerSecond // i.e. one damage takes one minute to regen TicksMax = Ticks(math32.MaxUint16) )
const ( // MeterPerSecond is 1 m/s MeterPerSecond Velocity = 1 << 5 VelocityMax = math32.MaxInt16 / float32(MeterPerSecond) VelocityMin = math32.MinInt16 / float32(MeterPerSecond) )
const ( MinRadius = 500 // Worlds grow this much larger than requested radius // Entities outside this * radius die instantly, to protect the world RadiusClearance = 1.1 // PlayerSpace Target space (square meters) per each PlayerSpace = 350000 CrateSpace = 30000 ObstacleSpace = 1000000 )
const AltitudeCollisionThreshold = 0.25
Entities within this of eachother's altitudes can collide
const EntityIDInvalid = EntityID(0)
const Pi = 32768
Variables ¶
var ( BoatLevelMax uint8 EntityRadiusMax float32 EntityTypeCount int BoatEntityTypesByLevel [][]EntityType )
var ( EntityKindAircraft EntityKind EntityKindBoat EntityKind EntityKindCollectible EntityKind EntityKindDecoy EntityKind EntityKindObstacle EntityKind EntityKindWeapon EntityKind EntitySubKindBattleship EntitySubKind EntitySubKindDepositor EntitySubKind EntitySubKindDepthCharge EntitySubKind EntitySubKindDredger EntitySubKind EntitySubKindHeli EntitySubKind EntitySubKindHovercraft EntitySubKind EntitySubKindPirate EntitySubKind EntitySubKindMine EntitySubKind EntitySubKindMissile EntitySubKind EntitySubKindPlane EntitySubKind EntitySubKindRam EntitySubKind EntitySubKindRocket EntitySubKind EntitySubKindSAM EntitySubKind EntitySubKindShell EntitySubKind EntitySubKindSonar EntitySubKind EntitySubKindSubmarine EntitySubKind EntitySubKindTanker EntitySubKind EntitySubKindTorpedo EntitySubKind EntityTypeBarrel EntityType EntityTypeCoin EntityType EntityTypeCrate EntityType EntityTypeHQ EntityType EntityTypeMark18 EntityType EntityTypeOilPlatform EntityType EntityTypeScrap EntityType )
Functions ¶
func CrateCountOf ¶
func LevelToScore ¶
levelToScore converts a boat level to a score required to upgrade score = (level^2 - 1) * 10 js must have same function
func ObstacleCountOf ¶
Types ¶
type AABB ¶
func (AABB) CornerCoordinates ¶
CornerCoordinates Center coords to corner coords
type Angle ¶
type Angle uint16
Angle is a 2 byte fixed-point representation of an angle. To convert a float in radians to an Angle use ToAngle.
func RandomAngle ¶
func RandomAngle() Angle
Returns a random angle (all possible angles are equally likely)
func (Angle) ClampMagnitude ¶
func (Angle) MarshalJSON ¶
func (*Angle) UnmarshalJSON ¶
type Armament ¶
type Armament struct { Type EntityType `json:"type"` Vertical bool `json:"vertical"` Length float32 `json:"length"` Width float32 `json:"width"` PositionForward float32 `json:"positionForward"` PositionSide float32 `json:"positionSide"` Angle Angle `json:"angle"` Turret *int `json:"turret,omitempty"` // If non-nil, index of turret }
Armament is the description of an armament in an EntityData. Armaments are not only weapons but also countermeasures.
func (*Armament) Similar ¶
Similar returns if the Armament is on the same turret and the same type as other.
func (*Armament) TurretIndex ¶
TurretIndex returns the index of the turret if it exists else -1.
type DeathReason ¶
type DeathReason struct { Type string `json:"message,omitempty"` Player string `json:"player,omitempty"` Entity EntityType `json:"entity,omitempty"` }
Has a custom (but regular) jsoniter marshaler
func (*DeathReason) FromPlayer ¶
func (reason *DeathReason) FromPlayer() bool
Returns whether the death was a result of player actions
type Entity ¶
Entity is an object in the world such as a boat, torpedo, crate or oil platform. Its size is 32 bytes for optimal efficiency. Cannot modify EntityType directly. Entity.Ticks is either damage or lifespan depending on the entity's type. Cannot modify EntityID except in World.
func (*Entity) AltitudeOverlap ¶
Returns if entities have sufficiently similar altitudes to collide
func (*Entity) ArmamentConsumption ¶
func (*Entity) ArmamentTransform ¶
ArmamentTransform Returns world transform
func (*Entity) ClearSpawnProtection ¶
func (entity *Entity) ClearSpawnProtection()
func (*Entity) Close ¶
func (entity *Entity) Close()
Close is called when an Entity is removed from a World.
func (*Entity) Collides ¶
Collides does a rectangle to rectangle collision with another Entity. Does not take into account altitude
func (*Entity) ConsumeArmament ¶
func (*Entity) DamagePercent ¶
DamagePercent returns an Entity's damage in the range [0, 1.0].
func (*Entity) HealthPercent ¶
HealthPercent returns an Entity's health in the range [0, 1.0].
func (*Entity) Initialize ¶
func (entity *Entity) Initialize(entityType EntityType)
Initialize is called whenever a boat's type is set/changed.
func (*Entity) OwnerBoatAimTarget ¶
When a weapon wants the turret target of the owner's ship
func (*Entity) Replenish ¶
Replenish replenishes the Entity's armaments by an amount. It starts with the ones that have the least time left. O(n^2) worst case if amount is very high.
func (*Entity) SetAimTarget ¶
func (*Entity) SetAltitudeTarget ¶
func (*Entity) SpawnProtection ¶
Returns value in the range [0,1] where 0 is full protection and 1 is no protection Intended to be multiplied by, for example, damage, to decrease damage taken while having been spawned recently
func (*Entity) TurretAngles ¶
func (*Entity) Update ¶
Update updates all the variables of an Entity such as Position, Direction, ArmamentConsumption etc. by an amount of time. It only modifies itself so each one can be processed by a different goroutine. seconds cannot be > 1.0.
func (*Entity) UpdateSensor ¶
UpdateSensor runs a simple AI for homing torpedoes/missiles.
type EntityID ¶
type EntityID uint32
func AllocateEntityID ¶
func (EntityID) AppendText ¶
func (EntityID) MarshalText ¶
func (*EntityID) UnmarshalText ¶
type EntityKind ¶
type EntityKind enumChoice
EntityKind is the kind of an entity one of: boat, collectible, obstacle, and weapon.
func ParseEntityKind ¶
func ParseEntityKind(s string) EntityKind
func (EntityKind) AppendText ¶
func (entityKind EntityKind) AppendText(buf []byte) []byte
func (EntityKind) MarshalText ¶
func (entityKind EntityKind) MarshalText() ([]byte, error)
func (EntityKind) String ¶
func (entityKind EntityKind) String() string
func (*EntityKind) UnmarshalText ¶
func (entityKind *EntityKind) UnmarshalText(text []byte) (err error)
type EntitySubKind ¶
type EntitySubKind enumChoice
EntitySubKind is the sub-kind of an entity such as: depth charge, missile, MTB, score, shell, submarine, torpedo, etc.
func ParseEntitySubKind ¶
func ParseEntitySubKind(s string) EntitySubKind
func (EntitySubKind) AppendText ¶
func (entitySubKind EntitySubKind) AppendText(buf []byte) []byte
func (EntitySubKind) MarshalText ¶
func (entitySubKind EntitySubKind) MarshalText() ([]byte, error)
func (EntitySubKind) String ¶
func (entitySubKind EntitySubKind) String() string
func (*EntitySubKind) UnmarshalText ¶
func (entitySubKind *EntitySubKind) UnmarshalText(text []byte) (err error)
type EntityType ¶
type EntityType enumChoice
EntityType is the type of an entity such as: crate, fairmileD, etc.
func ParseEntityType ¶
func ParseEntityType(s string) EntityType
func (EntityType) AppendText ¶
func (entityType EntityType) AppendText(buf []byte) []byte
func (EntityType) Data ¶
func (entityType EntityType) Data() *EntityTypeData
func (EntityType) MarshalText ¶
func (entityType EntityType) MarshalText() ([]byte, error)
func (EntityType) MaxHealth ¶
func (entityType EntityType) MaxHealth() Ticks
MaxHealth is the maximum health of an EntityType as Ticks.
func (EntityType) ReducedLifespan ¶
func (entityType EntityType) ReducedLifespan(desiredLifespan Ticks) Ticks
Returns a lifespan to start an entity's life at, so as to make it expire in desiredLifespan ticks
func (EntityType) String ¶
func (entityType EntityType) String() string
func (*EntityType) UnmarshalText ¶
func (entityType *EntityType) UnmarshalText(text []byte) (err error)
func (EntityType) UpgradePaths ¶
func (entityType EntityType) UpgradePaths(score int) (upgradePaths []EntityType)
func (EntityType) UpgradesTo ¶
func (entityType EntityType) UpgradesTo(nextEntityType EntityType, score int) bool
type EntityTypeData ¶
type EntityTypeData struct { // All units are SI (meters, seconds, etc.) Kind EntityKind `json:"kind"` SubKind EntitySubKind `json:"subkind"` Level uint8 `json:"level"` Limited bool `json:"limited"` NPC bool `json:"npc"` // only bots can use Lifespan Ticks `json:"lifespan"` Reload Ticks `json:"reload"` // time to reload Speed Velocity `json:"speed"` Length float32 `json:"length"` Width float32 `json:"width"` Draft float32 `json:"draft"` Radius float32 `json:"-"` InvSize float32 `json:"-"` Damage float32 `json:"damage"` // health of ship, or damage dealt by weapon AntiAircraft float32 `json:"antiAircraft"` // chance aircraft is shot down per second Stealth float32 `json:"stealth"` Sensors Sensors `json:"sensors"` Armaments []Armament `json:"armaments"` Turrets []Turret `json:"turrets"` Label string `json:"label"` }
EntityTypeData is the description of an EntityType.
type IDPlayerData ¶
type IDPlayerData struct { PlayerData PlayerID PlayerID `json:"playerID,omitempty"` }
type Player ¶
type Player struct { PlayerData DeathReason DeathReason DeathPos Vec2f DeathTime int64 DeathVisual float32 // if non-zero, in respawn animation DeathFromPlayer bool EntityID EntityID // contains filtered or unexported fields }
Player owns entities and has score
func (Player) CanRespawnWithTeam ¶
Says nothing about whether player is in a team. Only whether they are allowed to respawn with it if it exists.
func (*Player) ClearRespawn ¶
func (player *Player) ClearRespawn()
ClearRespawn clears after-death camera
func (*Player) IDPlayerData ¶
func (player *Player) IDPlayerData() IDPlayerData
func (*Player) Respawning ¶
Respawning returns if player is currently in the respawn animation
type PlayerData ¶
type PlayerData struct { Name string `json:"name,omitempty"` Score int `json:"score,omitempty"` TeamID TeamID `json:"team,omitempty"` }
func (*PlayerData) ScoreLess ¶
func (data *PlayerData) ScoreLess(other *PlayerData) bool
func (PlayerData) String ¶
func (data PlayerData) String() string
Formats player first as: [team] name (score)
type PlayerID ¶
type PlayerID uintptr
PlayerID is the unique id of a Player
func (PlayerID) AppendText ¶
func (PlayerID) MarshalText ¶
func (*PlayerID) UnmarshalText ¶
type PlayerSet ¶
type PlayerSet []*Player
PlayerSet Set with order
func (*PlayerSet) AppendData ¶
func (set *PlayerSet) AppendData(buf []IDPlayerData) []IDPlayerData
AppendData converts a PlayerSet to []IDPlayerData Uses append api to reuse old slice
type Sensor ¶
type Sensor struct {
Range float32 `json:"range"`
}
Sensor the description of a sensor in an EntityType.
type Sensors ¶
type Team ¶
type Team struct { JoinRequests PlayerSet Members PlayerSet // First member is owner Code TeamCode }
Team A group of players on the same team
type TeamCode ¶
type TeamCode uint32
TeamCode is a code that allows a Player to join a Team. Used with invite links.
func (TeamCode) AppendText ¶
func (TeamCode) MarshalText ¶
func (*TeamCode) UnmarshalText ¶
type TeamID ¶
type TeamID uint64
TeamID is a fixed-length string Team name that needs to be unique Use uint64 for fast comparisons and can store TeamIDLengthMax bytes
func (TeamID) AppendText ¶
func (TeamID) MarshalText ¶
func (*TeamID) UnmarshalText ¶
type Ticks ¶
type Ticks uint16
Ticks is a time measured in updates. It wraps after 65535 (109.225 minutes or 109 torpedoes worth of damage).
func DamageToTicks ¶
func (*Ticks) MarshalJSON ¶
func (Ticks) SaturatingSub ¶
func (*Ticks) UnmarshalJSON ¶
type Transform ¶
type Transform struct { Position Vec2f `json:"position"` Velocity Velocity `json:"velocity"` // TODO omitempty crashes client Direction Angle `json:"direction"` }
func ArmamentTransform ¶
func ArmamentTransform(entityType EntityType, entityTransform Transform, turretAngles []Angle, index int) Transform
ArmamentTransform returns the world transform of an Armament.
type Turret ¶
type Turret struct { PositionForward float32 `json:"positionForward"` PositionSide float32 `json:"positionSide"` Angle Angle `json:"angle"` AzimuthFL Angle `json:"azimuthFL"` AzimuthFR Angle `json:"azimuthFR"` AzimuthBL Angle `json:"azimuthBL"` AzimuthBR Angle `json:"azimuthBR"` }
Turret is the description of a turret's relative transform in an EntityType.
func (*Turret) CheckAzimuth ¶
Returns true only if the parameter is within the turrets valid azimuth ranges
type Velocity ¶
type Velocity int16
Velocity is a 11_5 fixed point representing any valid velocity.
func ToVelocity ¶
ToVelocity converts a float in m/s to a Velocity.
func (Velocity) AddClamped ¶
AddClamped adds a float to a Velocity and clamps it to mag.
func (Velocity) ClampMagnitude ¶
func (Velocity) MarshalJSON ¶
func (*Velocity) UnmarshalJSON ¶
type World ¶
type World interface { // AddEntity Adds a new entity to its sector // Cannot hold pointer after call is finished AddEntity(entity *Entity) // Count returns number of entities in the world // Cannot be called concurrently with writes Count() int // Debug Prints debug output to os.Stdout // Cannot be called concurrently with writes Debug() // EntityByID Gets an entity by its id // For reading and writing // Cannot hold pointers after call is finished EntityByID(entityID EntityID, callback func(entity *Entity) (remove bool)) // ForEntities Iterates all the entities and returns if stopped early // For reading and writing // Cannot hold pointer after call is finished ForEntities(callback func(entity *Entity) (stop, remove bool)) bool // ForEntitiesInRadius Iterates all the entities in a radius and returns if stopped early // Only for reading so no adding or modifying entities // Cannot hold pointer after call is finished ForEntitiesInRadius(position Vec2f, radius float32, callback func(r float32, entity *Entity) (stop bool)) bool // ForEntitiesAndOthers Iterates all the entities and other entities in a radius and returns if stopped early // For reading and writing // Cannot hold pointers after call is finished // Cannot modify position of entities // If entities are added during iteration may not iterate them // Skips radii that are <= 0 // Radii that are too big cause all sectors to be iterated ForEntitiesAndOthers(entityCallback func(entity *Entity) (stop bool, radius float32), otherCallback func(entity *Entity, otherEntity *Entity) (stop, remove, removeOther bool)) bool // Resize sets the max size of the World. // It may or may not reallocate parts of the World depending on the difference in radius. Resize(radius float32) // SetParallel Marks world as ready only for concurrent reads // Cannot remove or add during read only mode // Returns if can be read concurrently SetParallel(parallel bool) bool }
World A world holds entities