# Rules

In this game you must program the behavior of your spaceship. The goal is to destroy your opponent as many times as possible, while avoiding being destroyed.

At the beginning of the game both spacecrafts are placed in a squared area, their positions are random and their shields have 5 points. When a spaceship crosses the edge of the screen, it reappears on the opposite side. Shots follow the same behavior, although their range is limited to a distance of three fourths of the size of the area.

When an enemy shot hits a spacecraft its shield is decreased by 1 point, destroying it if the shield reaches 0 points. When you destroy your opponents spaceship your score is increased by 1 and your shield points are completely reset. All your other variables (like speed, position and direction) are kept in the same state. Your opponent will reappear in a new random location.

The winner is the player with the highest score at the end of the match.

# Game play

The simulation is discrete and is updated 50 times per second. Your code is executed in each loop of the simulation.

The maximum speed of your spacecraft is 5 units per cycle. Shots travel at a constant speed of 15 units per cycle.

At most you can fire one shot every 50 cycles. Your maximum acceleration or deceleration is 1/10 per cycle, and the speed in which you can turn is inversely dependent of the speed of your ship at that moment (ie. you can turn more quickly if your ship is stopped).

If the code you write has at least one error, the simulation will continue using the last working version of your code. This allows you to make changes to your code and avoid converting yourself in a sitting duck.

Finally, Code Games remembers the last code you wrote, including the code written in practice games, and uses it for new matches. This means that if a new tournament begins, and for any reason you are not online at that moment, you are safe: we will use the last remembered (working) code in your account.

# Language reference

The language used to program your spacecraft is very simple and its syntax is similar to Ruby. Variables have an unique data type (which is numeric) and you don't need to declare them. The default value for an uninitialized variable is 0, but the values persist in between simulation loops.

`if .. else .. end`

is the basic conditional control
structure.

if foo == 1 and bar > 2 # foo is one and bar is greater than two else # foo is not one, or bar is less or equal than 2 end

You can use variables as conditionals. In this case, any value not equal to 0 is treated as a true value.

if enemy_visible # we can see the enemy else # continue searching... end

There are predetermined functions (see actions and mathematical functions below) and you can also define your own. You can omit parentheses when calling a function that has no arguments.

function foo() # a function without arguments return 5 end function bar(x, y) return x + y end a = foo # a == 5 b = bar(a, 3) # b == 8

Also, there are some predetermined variables that you get updated on each simulation loop. These include information about your spacecraft and your enemy (see the reference below).

There are no repetition control structures and you can't use recursion. However, you can simulate these structures since your code is continuously executed and the values of your variables are kept from one execution to the next.

# Example: turn left for 5 turns (ie. 1/10th of a second) # i is initialized to 0 and its value is preserved between code executions if i < 5 # this block is executed for 5 consecutive times i = i + 1 left else # we're done turning left... do other stuff # ... or reset the counter to 0 to start turning left again next time if start_turning_again i = 0 end end

## Actions

The basic actions to move your spaceship and fire are:

- faster(impulse)
- Increases the speed of your ship, up to a maximum of 5. The argument
impulse is optional and can be negative (getting the same effect as
`slower(impulse)`

). The value of the argument is proportional to the acceleration. With an impulse of 1 the ship accelerates from 0 to 5 in 250 cycles, or 5 seconds. With an impulse of 2, it takes half the time at 125 cycles. Any value greater than or equal to 5 will take 50 cycles (1 second). - slower(impulse)
- Decreases the speed of your ship, until it is completely stopped. The
argument impulse is optional and can be negative (getting the same effect
as
`faster(impulse)`

). The parameter behaves similarly to the case of`faster`

. - left(amount)
- Turns your ship to the left, or counter-clockwise. The argument is
optional and can be negative, in which case the function behaves like
`right(amount)`

. The turn speed depends on the value of the argument, with a maximum that is inversely proportional to the speed of the ship.

With the ship completely stopped (ie. speed 0) and argument value equal to 1, the ship does a full turn of 360º in about 300 cycles (~5 seconds). With a value of 2, it takes around 157 cycles. Any value 5 or greater and the ship will do a full circle in around 60 cycles, or 1.2 seconds.

At full speed (ie. 5), making a full turn cannot be done in less than 160 cycles (~3.2 seconds). - right(amount)
- Turns your ship to the right, or clockwise. The argument is optional and
can be negative, in which case the function behaves like
`left(amount)`

. See`left`

for more details on the parameter values. - fire
- Fires a shot. Since there is a limitation of one shot every 50 cycles, it
is possible that
`fire`

does nothing. Given this, the function returns 1 if a shot was fired, and 0 otherwise. - rift
- Multiplies
**your current speed**by up to 2.5 times for 50 cycles (1 second). There is a cool-down period for a total of 100 cycles (or 2 seconds) after using this function, during which you cannot fire or rift again. Good for escaping or catching up to an opponent.

- direction
- The direction of your ship in degrees, ranging between 0 and 360. The value increases in a clockwise fashion and 0 points to the top of the screen.
- speed
- The speed of your spacecraft, ranges between 0 and 5.
- enemy_visible
- 1 if the enemy ship is visible from your position and direction; 0 otherwise. The vision angle of your ship is 180º and the maximum visibility distance is 300, which represents half the size of the gaming area.
- enemy_angle
- Relative angle of the enemy in reference to your direction, goes from -90 (to the left) to 90 (to the right). This variable is 0 if the enemy ship isn't visible.
- enemy_distance
- Distance towards the enemy ship, or 0 if it isn't visible. Opponents farther than 300 will be not be visible, so this variable takes values from 0 to 300.
- energy
- The value of your shield, initially 5. If you receive a shot with
`energy == 1`

your ship is destroyed, you will reappear in a random position with speed equal to 0, and your opponent scores one point. Also, your energy will be completely reset when you destroy your enemy.

## Mathematical functions

- abs(x)
- Absolute value of
`x`

. - sign(x)
- Sign of
`x`

, such that`x == sign(x) * abs(x)`

. - sin(x)
- Sine of
`x`

, in degrees. - cos(x)
- Cosine of
`x`

, in degrees. - tan(x)
- Tangent of
`x`

, in degrees. - atan2(y, x)
- Arc-tangent of the coordinate
`(x,y)`

, in degrees. The sign of`x`

and`y`

determine the quadrant of the resulting angle. - pi()
- The value of the constant π.
- rand(min, max)
- Returns a random value between the arguments
`min`

and`max`

. If the`min`

argument is omitted, it returns a random number between 0 and`max`

. If both arguments are omitted the result will range between 0 and 1. - sqrt(x)
- Square root of
`x`

. - pow(x, y)
`x`

to the power of`y`

.