core

package
v0.0.0-...-b239de3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package core includes all basic functions to generate and simulate the game world.

Index

Constants

View Source
const (
	GameSpeed    = 30            // iterations per second
	MovePerTick  = 0.02          // percent of movement per tick
	WorldXWidth  = 28            // world dimension X (28 * 64 = 1792pxl)
	WorldYHeight = 15            // world dimension Y (15 * 64 = 960pxl)
	BlockSize    = 64            // image size of tanks, barriers, buildings, ...
	BlockRadius  = BlockSize / 2 // radius of blocks (tanks)
	BallSize     = 20            // size of projectiles
	BallRadius   = BallSize / 2  // radius of projectiles
)

game settings

View Source
const (
	MacroAttackMove      = "AttackMove"
	MacroFireAndManeuver = "FireAndManeuver"
	MacroFireWall        = "FireWall"
	MacroGuardMode       = "GuardMode"
	MacroReset           = "nil"
)

macros

View Source
const (
	ShowExplosionIterations = 10               // duration of the explosion animation
	WeaponNone              = "None"           // no weapon for neutral objects (rock)
	WeaponCannon            = "Tank"           // weapon of a battle tank
	WeaponArtillery         = "Artillery"      // weapon of an artillery
	WeaponRockets           = "RocketLauncher" // weapon of a rocket launcher
)

weapons

View Source
const (
	TankRotationDelay = 467 * GameSpeed / 1000 // rotation delay in iterations (~467 ms)
	TankBudget        = 100                    // max. points = armor + damage + speed
	TankMinSpeed      = 25                     // min. Speed (calc budget-armor-damage)
	TankMinArmor      = 5                      // min armor
	TankMaxArmor      = 55                     // max armor
	TankMinDamage     = 15                     // min damage
	TankMaxDamage     = 70                     // max damage
)

tank attr

View Source
const (
	North     = 0
	Northeast = 45
	East      = 90
	Southeast = 135
	South     = 180
	Southwest = 225
	West      = 270
	Northwest = 315
)

tank angle (movement)

View Source
const (
	StatusMoving    = "Moving"    // tank is moving and can't fire
	StatusPreparing = "Preparing" // tank prepare for fire after moving (see prepTime)
	StatusReady     = "Ready"     // tank can fire
	StatusReloading = "Reloading" // tank reload weapon after fire
	StatusNoWeapon  = "NoWeapon"  // error: no weapon or not alive
)

tank status

View Source
const (
	RedTank  = "red"  // player 1 (red)
	BlueTank = "blue" // player 2 (blue)
)

player tanks

View Source
const (
	NeutralRock = "neutral_rock" // rock, assigned to no player
	RedRock     = "red_rock"     // rock of gamer 1 (red)
	BlueRock    = "blue_rock"    // rock of gamer 2 (blue)
	RedBase     = "red_base"     // base of gamer 1 (red)
	BlueBase    = "blue_base"    // base of gamer 2 (blue)
)

buildings

Variables

This section is empty.

Functions

func CheckBorders

func CheckBorders(p Position, r, screenWidth, screenHeight int) bool

CheckBorders returns true if the object collide with the borders.

func Distance

func Distance(a, b Position) float64

Distance returns the distance between the centers of two objects.

func IsCollided

func IsCollided(pos1 Position, rad1 int, pos2 Position, rad2 int) bool

IsCollided return true if two objects touch.

func Length

func Length(x, y float64) float64

Length calculate the vector length.

Sqrt(x*x + y*y)

func RelativeAngle

func RelativeAngle(me Position, other Position) int

RelativeAngle returns the relative position of OTHER as an angle. Return [0 ... 359]

North     = 0
Northeast = 45
East      = 90
Southeast = 135
South     = 180
Southwest = 225
West      = 270
Northwest = 315

Types

type Position

type Position struct {
	X  int
	Xf float64
	Y  int
	Yf float64
}

Position stores x and y as an int and as a float value. Update() work with the float value and round to int.

func CalcPosFromAngle

func CalcPosFromAngle(start Position, angle, length int) Position

CalcPosFromAngle returns the new coordinates with given angle and length. The angle is defined with North is 0° (see North, East, South, West, ...).

func NewPosition

func NewPosition(x, y int) Position

NewPosition return a position and set x,y float + int

func (*Position) Set

func (p *Position) Set(x, y int)

Set x,y float and int

func (*Position) Update

func (p *Position) Update(angle, speed int)

Update calculate the new position. Internally, float values are used. The int values are rounded. If the int values and the float values are differ, the int value is used.

type Projectile

type Projectile struct {
	// contains filtered or unexported fields
}

Projectile is created by a weapon. It moves in the world. It can collide with other objects and can explode.

func NewProjectile

func NewProjectile(world *World, parent *Tank, pos Position, angle, distance, speed, damage, aoeRadius int, collision bool) *Projectile

NewProjectile create a new projectile. When created in a world, it interacts with other objects. Forward the parents, otherwise the projectile will explode at the start.

For parameter description see Projectile.Pos(), Projectile.Angle(), Projectile.Distance(), Projectile.Speed(), Projectile.Damage(), Projectile.AoERadius() and Projectile.Collision().

func (*Projectile) Angle

func (p *Projectile) Angle() int

Angle is the start angle of the projectile. see North, South, East, ...

func (*Projectile) AoERadius

func (p *Projectile) AoERadius() int

AoERadius determines how many objects are hit in an explosion. see Explode()

func (*Projectile) Collision

func (p *Projectile) Collision() bool

Collision defines two types of projectiles:

true: the projectile flies until it hits a target or disappears after the maximum distance (WeaponCannon).
false: the projectile flies without any interaction and explode after the maximum distance (WeaponArtillery).

func (*Projectile) Damage

func (p *Projectile) Damage() int

Damage that is included in the calculation on a hit. see Tank.Hit()

func (*Projectile) Distance

func (p *Projectile) Distance() int

Distance between start position and current position.

func (*Projectile) EndPos

func (p *Projectile) EndPos() Position

EndPos returns the planned target position, but a collisions can happen earlier.

func (*Projectile) Explode

func (p *Projectile) Explode()

Explode destroys the projectile at the current position. All objects in range (see AoERadius) are hit (see Tank.Hit).

func (*Projectile) Exploded

func (p *Projectile) Exploded() bool

Exploded return true if the projectile is exploded.

func (*Projectile) Parent

func (p *Projectile) Parent() *Tank

Parent returns the tank from which the projectile was fired.

func (*Projectile) Pos

func (p *Projectile) Pos() Position

Pos is the current position of this projectile. Is changed by Update().

func (*Projectile) Remove

func (p *Projectile) Remove()

Remove this projectile from the world list World.Projectiles.

func (*Projectile) Speed

func (p *Projectile) Speed() int

Speed at which the bullet moves per tick.

func (*Projectile) StartPos

func (p *Projectile) StartPos() Position

StartPos is the initial position of this projectile.

func (*Projectile) TestInitialization

func (p *Projectile) TestInitialization(world *World, parent *Tank, pos, startPos, endPos Position, angle, distance, speed, damage, aoeRadius int, collision bool, exploded uint)

TestInitialization allows setting non-exported variables outside the core packet.

func (*Projectile) Update

func (p *Projectile) Update()

Update move the projectile, remove exploded projectile, calculate collisions and enforce the max distance.

type Tank

type Tank struct {
	// contains filtered or unexported fields
}

Tank is an object in World. It can be a tank, a building, a rock, ...

func NewTank

func NewTank(world *World, owner string, armor, damage int, weapon string) (*Tank, error)

NewTank return a new tank. The tank must be added to the world manually (see World.AddTank()).

The attributes armor, damage and speed are related. Every unused budget point (see TankBudget) is converted into speed.

TankMinArmor < armor < TankMaxArmor
TankMinDamage < damage < TankMaxDamage
TankMinSpeed < speed

func (*Tank) ActiveMacro

func (t *Tank) ActiveMacro() bool

ActiveMacro returns if this tank is controlled by a macro. see SetMacro().

func (*Tank) Alive

func (t *Tank) Alive() bool

Alive returns true if the Health is not 0.

func (*Tank) Angle

func (t *Tank) Angle() int

Angle of the tank. see North, South, East, ...

func (*Tank) Armor

func (t *Tank) Armor() int

Armor reduces damage with each hit. Armor can be so high that no damage is dealt.

func (*Tank) Backward

func (t *Tank) Backward()

Backward send the tank back. see Command().

func (*Tank) Blocked

func (t *Tank) Blocked() bool

Blocked return true if movement has ended because the path was blocked. Set by Update() and reset by Forward() and Backward().

func (*Tank) Command

func (t *Tank) Command() int

Command returns the current move command of the tank. 1 is forward; 0 is stop; -1 is backward

func (*Tank) Fire

func (t *Tank) Fire(fireAngle, distance int) (success bool, txt string)

Fire is a simple call for Weapon.Fire()

func (*Tank) FireAt

func (t *Tank) FireAt(pos Position) (success bool, txt string)

FireAt is a wrapper for Fire() and convert the position to fireAngle and distance.

func (*Tank) Forward

func (t *Tank) Forward()

Forward send the tank forward. see Command().

func (*Tank) Health

func (t *Tank) Health() int

Health points of the tank. Is reduced by Hit(). Is used by Alive().

func (*Tank) Hit

func (t *Tank) Hit(damage int)

Hit calculate the damage on a direct hit. Reduce the damage by Armor(). Call Remove() for death tanks.

func (*Tank) ID

func (t *Tank) ID() string

ID is a unique id of this tank.

func (*Tank) LastRotate

func (t *Tank) LastRotate() uint64

LastRotate returns at which iteration the last rotation was.

func (*Tank) Left

func (t *Tank) Left() (success bool, status string)

Left turn the tank direction 45° left. see Angle()

func (*Tank) Moving

func (t *Tank) Moving() bool

Moving returns the status true if the tank is moving. see Command()

func (*Tank) Owner

func (t *Tank) Owner() string

Owner returns who control this object.

func (*Tank) Pos

func (t *Tank) Pos() Position

Pos returns the current position.

func (*Tank) Remove

func (t *Tank) Remove()

Remove the tank from World.

func (*Tank) Right

func (t *Tank) Right() (success bool, status string)

Right turn the tank direction 45° right. see Angle()

func (*Tank) SetMacro

func (t *Tank) SetMacro(macro func(t *Tank))

SetMacro sets a macro that is called with every update. Remove it with 'nil'.

func (*Tank) SetPosition

func (t *Tank) SetPosition(pos Position, angle int)

SetPosition set a new tank position without collision check.

func (*Tank) Speed

func (t *Tank) Speed() int

Speed of the tank.

func (*Tank) Status

func (t *Tank) Status() (rdy bool, status string)

Status returns the weapon status: (StatusMoving, StatusPreparing, StatusReloading, StatusReady or StatusNoWeapon). see Weapon.Status

func (*Tank) Stop

func (t *Tank) Stop()

Stop the movement. Weapons can only build up when the tank is stationary see Command(). see Weapon.PreparationTime()

func (*Tank) TestInitialization

func (t *Tank) TestInitialization(world *World, id, owner string, weapon *Weapon, health, armor, speed int, pos Position, command, angle int, isBlocked bool, lastRotate uint64, macro func(t *Tank))

TestInitialization allows setting non-exported variables outside the core packet.

func (*Tank) Update

func (t *Tank) Update()

Update calculate movement, check collisions, check world borders and set Weapon.LastMove().

func (*Tank) Weapon

func (t *Tank) Weapon() *Weapon

Weapon of the tank. see NewWeaponCannon, NewWeaponArtillery.

type Target

type Target struct {
	Tank          *Tank
	Distance      int
	RelativeAngle int
}

Target provide the tank, the distance and the relative angle to the tank. It is used by CloseTargets() and PossibleTargets(). see RelativeAngle().

func CloseTargets

func CloseTargets(t *Tank, filter ...string) []Target

CloseTargets returns all objects in the world that are theoretical in weapon range. The weapon type is irrelevant (WeaponCannon or WeaponArtillery) and the angle of the tank is ignored. The list is sorted by distance (from the closest to the farthest).

func PossibleTargets

func PossibleTargets(t *Tank, filter ...string) []Target

PossibleTargets extends CloseTargets. It only returns objects that can actually be attacked,depending on the weapon type. However, it may be necessary for the battle tank to change its angle. The list is sorted by the rotation required to reach the target.

type Weapon

type Weapon struct {
	// contains filtered or unexported fields
}

Weapon can be mounted on vehicles or buildings. It generates bullets with Fire().

func NewWeaponArtillery

func NewWeaponArtillery(world *World, parent *Tank, damage int) *Weapon

NewWeaponArtillery return the weapon for an artillery. It's slow and can't collide with other tanks. Explode at the destination with big aoe but less damage (-10%).

func NewWeaponCannon

func NewWeaponCannon(world *World, parent *Tank, damage int) *Weapon

NewWeaponCannon return the weapon for a battle tank. It's fast and collide with other tanks. The damage is very height on single target (+50%).

func NewWeaponRocketLauncher

func NewWeaponRocketLauncher(world *World, parent *Tank, damage int) *Weapon

NewWeaponRocketLauncher return the weapon for a rocket launcher. It's like the artillery but the damage is used to reduce the reload time.

func (*Weapon) AnyFireAngle

func (w *Weapon) AnyFireAngle() bool

AnyFireAngle enables any launch direction. Otherwise, the projectile always fires in the tank direction.

func (*Weapon) AoERadius

func (w *Weapon) AoERadius() int

AoERadius is the area damage radius. Objects within the radius will be damaged even without a direct hit. see: Projectile.Explode() for calculation.

func (*Weapon) Damage

func (w *Weapon) Damage() int

Damage is the base damage on hits. Is reduced by armor. see Tank.Hit() for calculation.

func (*Weapon) Fire

func (w *Weapon) Fire(vehiclePos Position, vehicleAngle, fireAngle, distance int) (success bool, status string)

Fire creates a new projectile. The attributes fireAngle and distance determine the direction and distance of the shot.

If AnyFireAngle() is false, attribute fireAngle is overridden by attribute vehicleAngle.
If ProjectileCollision() is true, attribute distance is overridden by Range().
Attribute distance is limited by Range().

see NewProjectile()

func (*Weapon) LastFire

func (w *Weapon) LastFire() uint64

LastFire returns at witch iteration the last fire was. see status StatusReloading

func (*Weapon) LastMove

func (w *Weapon) LastMove() uint64

LastMove returns at witch iteration the last move was. see status StatusPreparing

func (*Weapon) PreparationTime

func (w *Weapon) PreparationTime() uint64

PreparationTime is the time (=iteration ticks) the weapon has to build up after a movement. see status StatusPreparing

func (*Weapon) ProjectileCollision

func (w *Weapon) ProjectileCollision() bool

ProjectileCollision if is active, the projectile will be stopped by objects in the fly path and explodes. Otherwise, it will explode after the specified range.

true - only explodes on contact
false - only explodes at the end of the distance

func (*Weapon) ProjectileSpeed

func (w *Weapon) ProjectileSpeed() int

ProjectileSpeed is the velocity of the bullet.

func (*Weapon) Range

func (w *Weapon) Range() int

Range is the maximum range of the weapon. WeaponCannon disappear after the range. WeaponArtillery explode after the range.

see Projectile.Collision()

func (*Weapon) ReloadTime

func (w *Weapon) ReloadTime() uint64

ReloadTime is the time (=iteration ticks) the weapon has to be reloaded after firing. see status StatusReloading

func (*Weapon) Status

func (w *Weapon) Status() (rdy bool, status string)

Status returns true if the weapon is ready to fire. Otherwise, the reason is returned (StatusMoving, StatusPreparing, StatusReloading or StatusReady). see: PreparationTime() and ReloadTime().

func (*Weapon) TestInitialization

func (w *Weapon) TestInitialization(world *World, parent *Tank, typ string, rng int, prepTime, reloadTime uint64, projSpeed, damage, aoeRadius int, projCollision, anyFireAngle bool, lastMove, lastFire uint64)

TestInitialization allows setting non-exported variables outside the core packet.

func (*Weapon) Type

func (w *Weapon) Type() string

Type is the weapon name. see: WeaponCannon or WeaponArtillery.

func (*Weapon) Update

func (w *Weapon) Update(isMoving bool)

Update is called with each iteration and updates internal variables.

type World

type World struct {
	// contains filtered or unexported fields
}

World is the game and holds all active objects on the map.

func NewWorld

func NewWorld(XWidth, YHeight int) *World

NewWorld create a new world. The attributes XWidth and YHeight are blocks (64x64). see WorldXWidth and WorldYHeight.

func (*World) AddTank

func (w *World) AddTank(tank *Tank)

AddTank adds a new tank to the world. Use Tank.SetPosition() to set the correct position.

func (*World) BuyTank

func (w *World) BuyTank(tank *Tank) error

BuyTank buy a tank and place it near the home base.

func (*World) CashStat

func (w *World) CashStat() (cashRed, cashBlue int)

CashStat returns the current cash amount of the players. The amount is slowly generated by the home base.

see Update().

func (*World) Clear

func (w *World) Clear(prefix string)

Clear all tanks (objects) with prefix from world. Kill a player like 'red' or 'blue'.

func (*World) Freeze

func (w *World) Freeze(status bool)

Freeze disable the Update() routine if true.

func (*World) IsFrozen

func (w *World) IsFrozen() bool

IsFrozen returns the freeze state. see Freeze()

func (*World) Iteration

func (w *World) Iteration() uint64

Iteration returns the current calculation round.

func (*World) Projectiles

func (w *World) Projectiles() []*Projectile

Projectiles returns all flying bullets.

func (*World) ScreenHeight

func (w *World) ScreenHeight() int

ScreenHeight is the GUI width (= YHeight * 64)

func (*World) ScreenWidth

func (w *World) ScreenWidth() int

ScreenWidth is the GUI width (= XWidth * 64)

func (*World) SetCash

func (w *World) SetCash(cashRed, cashBlue int)

SetCash overwrites the current value of both players.

func (*World) Tanks

func (w *World) Tanks() []*Tank

Tanks returns the tank list (= all destructible objects)

func (*World) TestInitialization

func (w *World) TestInitialization(xWidth, yHeight int, iteration uint64, tanks []*Tank, projectiles []*Projectile, freeze bool, cashRed, cashBlue float64)

TestInitialization allows setting non-exported variables outside the core packet.

func (*World) UnitCount

func (w *World) UnitCount() (red, blue int)

UnitCount returns the sum of all units. This number is used for the victory condition.

func (*World) Update

func (w *World) Update()

Update is called 30 times (see GameSpeed) per second. The method also calls Update() of all tanks and all projectiles.

func (*World) UpdateN

func (w *World) UpdateN(n int)

UpdateN calls Update() n-times.

func (*World) XWidth

func (w *World) XWidth() int

XWidth return the block width

func (*World) YHeight

func (w *World) YHeight() int

YHeight return the block height

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL