# 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 beggining 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 decresed 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 resetted. 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.

# Gameplay

The simulation is discrete and is refreshed 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 10 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 alllows you to make changes to your code avoiding 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.

`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 eneny else # continue searching... end

There are predetermined functions 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 as the result of the simulation. These include information abour 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.

# simulate for (i = 0; i < 5; i++) { ... } # i is 0 initially and its value is preserved between code executions if i < 5 # this block is executed 5 times in a row i = i + 1 else # here we are outside the "for" # setting i to 0 we can reset the for and execute the loop again if reset_the_for i = 0 end end

## Actions

The basic actions to move your spaceship and fire are:

- faster(impulse)
- Increases the speed of your ship, upto 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. - 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. 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 and argument value equal to 1, the ship does a full turn of 360º in about 300 cycles. - right(amount)
- Turns your ship to the right. The argument is optional and can be
negative, in which case the function behaves like
`left(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 and argument value equal to 1, the ship does a full turn of 360º in about 300 cycles. - 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 fire, and 0 otherwise. - rift
- Quickly increases the speed upto 2.5 times the current speed of your ship for a short period of time. There is a cooldown of 150 cicles after using this function, during which you cannot fire or rift again.

- direction
- The direction of your ship in sexagesimal 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.
- 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 resetted 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 sexagesimal degrees. - cos(x)
- Cosine of
`x`

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

, in sexagesimal degrees. - atan2(y, x)
- Arctangent of
`x`

, in sexagesimal 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`

.