65
16
If you've ever played Spacewar!, you know it was a fun game. If you haven't, know this: it was (and is) one of the very first and most important computer games. And it's still fun! The clone I grew up on is this one, which is, apparently and unfortunately, Windows only. So I recreated it!
The KotH is hosted here: PPCG - Spacewar! King of the Hill. I encourage you to play as a human against at least one other bot to get a feel for how the game works.
The Game
- One frame is 30 milliseconds (thus, about 33 frames per second).
- The field is 800 pixels wide and 600 pixels tall.
- The field is toroidal, meaning that spaceships and missiles that move outside of the field reappear on the opposite side.
- There are two spaceships, red and blue.
- Red is positioned at x=50 and random y between 50, (field height - 50) pixels.
- Blue is positioned at x=(field width - 50) and random y between 50, (field height - 50) pixels.
- Both face x = (field width)/2.
- The available controls are:
- Turn left - 5 degrees per frame counterclockwise.
- Turn right - 5 degrees per frame clockwise.
- Fire missile - travels at an extra 10 pixels per frame in addition to the velocity of the ship, in the direction the ship was pointing.
- Fire engine - accelerates the spaceship at 0.30 pixels per frame in the direction the spaceship is pointing.
- Hyperspace jump - teleports to some random coordinates in the field, with a 25% chance of exploding. These random coordinates may be on top of the sun.
- The maximum speed for ships is 15 pixels per frame under engine power and 40 pixels per frame when gravity-boosted.
- When traveling faster than 15 pixels per frame, engine thrust may only change direction or slow down.
- Regarding missiles:
- Missiles travel in a straight line.
- Missiles may be fired at a maximum rate of 1 per 0.1 seconds.
- Missiles have a lifetime of 2.25 seconds.
- Ships have a maximum of 20 missiles each.
- Missiles are point particles internally.
- There is a sun in the very center that is extremely hazardous to your ship. The slightest contact is fatal. This sun also destroys missiles.
- The sun has gravity. The resultant acceleration is 5000/(distance^2) pixels/frame^2, where distance is in pixels. Spaceships and missiles are affected.
- Both ships have three strike zones: the nose, the left wing, and the right wing.
- A hit on the nose is instant death.
- A hit on either wing reduces the spaceship's turn rate and engine acceleration by half.
- If both wings are destroyed, the spaceship cannot be maneuvered and can only fire missiles.
- Ships may collide with each other.
- A nose-nose impact is fatal for both ships.
- A nose-wing impact destroys the wing.
- A wing-wing impact destroys both wings.
- Dead ships are solid and frozen until they explode 1 second later.
- After at least one ship has died, the field is reset 3 seconds later. Until then, the sun and any remaining missiles are still hazardous.
The original game also has deadly and indestructible asteroids, but I won't include those.
The Rules
- Your bot must be written in JavaScript.
- Your bot should limit its decision to about 10 milliseconds. If I notice consistent lag because of your bot, I'll disqualify it and let you know so you can fix it.
- Bots will have access to the following:
- Field width and field height
- Sun position and radius
- The position, rotation, velocity, shape, missile stock, and in-hyperspace status of both ships
- The position and velocity of all missiles
- When prompted, your bot should return a list of strings.
- These strings should be one of the following:
turn left
,turn right
,fire engine
,fire missile
,hyperspace
. Any other string will be ignored. - If there are any duplicates, only the first will be noted.
hyperspace
takes precedence over all others.turn left
andturn right
at the same time will have no effect.fire engine
will have no effect if the ship only has the nose or is dead.fire missile
will have no effect if a missile was fired too recently.
- These strings should be one of the following:
- In a change from the usual, your bot is allowed to exploit the behavior of other bots. I want to encourage a metagame.
- Bots may not emulate other bots. (I.e., no mind-reading.)
- Bots may not set any variables used by the game and physics code. (I.e., no cheating.)
Bot Implementation Details
I will be storing your bot in its own JavaScript file that is automatically included, with the filename bot_<name>.js
. So don't put any spaces or characters that would interfere with this or with naming a function in JavaScript. That's because you should define the following functions: <name>_setup(team)
and <name>_getActions(gameInfo, botVars)
. Further down the page, there exist textareas for the userbot, which you can edit to test your code.
<name>_setup(team)
This function is for you to define any variables that you want to persist. team
will be either "red"
or "blue"
. This function must return an object. Define variables like so:
var vars = {};
vars['example'] = "example";
return vars;
This vars
object will be passed in to the other function:
<name>_getActions(gameInfo, botVars)
botVars
is the object returned by <name>_setup(team)
. gameInfo
is an object containing the following variables:
redScore
blueScore
timeLeft
fieldWidth
fieldHeight
sun_x
sun_y
sun_r //sun's radius
gravityStrength //acceleration in pixels/frame^2 at 1 pixel away from the sun's center
engineThrust //acceleration in pixels/frame^2
speedLimit //maximum speed under engine power
maxSpeed //maximum speed from gravity boosts
red_x
red_y
red_rot //rotation in degrees
red_xv //x velocity
red_yv //y velocity
red_shape //one of "full ship", "left wing", "right wing", "nose only"
red_missileStock //the number of missiles red has left
red_inHyperspace //true if red is in hyperspace
red_exploded //until red explodes, it is still solid and hazardous
red_alive
// likewise for blue //
numMissiles
missiles //this is a list of objects, each with the following variables
x
y
xv
yv
Your bot has full access to these. I'm pretty sure that you can write to them and not affect the original variables, but don't do it anyway. A note on rotations: ships point in the +y direction, downwards, so anything that you want to align with the ship needs to be offset by 90 degrees. Also, positive rotation is clockwise.
This function must return a list of strings, representing your bot's actions. For example, ["turn right","thrust"]
. More details on this are in the Rules section.
Additional Details
You may also make use of the following:
LineIntersection(L1, L2)
L1 and L2 are two-element arrays of two-element arrays. That is, L1 := [[x1,y1],[x2,y2]]
and L2 := [[u1,v1],[u2,v2]]
. This function computes the intersection of two lines and returns this: [[x,y], [a,b]]
. [x,y]
are the coordinates of the intersection point and [a,b]
is a pair of ratios that express how far along each line the intersection point is. As in, a = 0.25
would mean that the intersection point is a quarter of the way from [x1,y1]
to [x2,y2]
, and likewise for b
. If there is no intersection, an empty array is returned.
window["shipShapes"]
var shipShapes = {
'full ship': [[-8,16],[0,-8],[8,16]],
'left wing': [[-8,16],[0,-8],[4,4],[0,8],[0,16]],
'right wing':[[-4,4],[0,-8],[8,16],[0,16],[0,8]],
'nose only': [[-4,4],[0,-8],[4,4],[0,8]]
};
These are the coordinates of the ships' polygons. To make getting the current coordinates easier, you may also use...
getShipCoords(<color>)
getShipCoords("red")
will return the current coordinates of the vertices of Red's ship, and likewise for getShipCoords("blue")
and Blue. These coordinates are in a list like so: [[x1,y1],[x2,y2],[x3,y3],...]
. Polygons are implicitly closed, so there is a line between the first and last coordinate pairs.
You may not access or alter any other variables or functions in use by the game/website. And definitely don't name your functions the same. I don't foresee that this will be a problem, but if your bot breaks the game code, that's one possibility. There is no logging or catching of exceptions.
Winning
- Every pairing of bots shall be played at least 10 times, both ways. (So, at least 20 games total.)
- Aim to have the highest win/loss ratios overall. If your bot does super well against one other bot, but loses against the other three, that's not as good as winning against two and losing against two (as a general rule of thumb).
- For every bot, the ratios (wins+1)/(losses+1) will be calculated, then the mean and standard deviation of these ratios will be calculated. A higher mean will have priority, and in case means are within 1 unit of each other, the lower variance will have priority.
- Scoring will begin either in a week from today or after three days of no new submissions. This is so I don't have to repeat any pairing of bots.
Above all, have fun!
Leaderboard (2016-01-08 05:15):
# Name Mean StdDev
1. Helios 13.625 6.852
2. EdgeCase 8.335 8.155
3. OpponentDodger 8.415 8.186
4. OrbitBot 5.110 6.294
5. SunAvoider 5.276 6.772
6. DangitBobby 3.320 4.423
7. SprayAndPray 3.118 4.642
8. Engineer 3.903 6.315
9. RighthandedSpasms 1.805 2.477
10. AttackAndComeBack 2.521 2.921
11. PanicAttack 2.622 3.102
12. FullSpeedAhead 2.058 3.295
13. UhhIDKWhatToCallThisBot 2.555 3.406
14. MissilesPlusScore 0.159 0.228
15. Hyper 0.236 0.332
16. RandUmmm 0.988 1.329
17. Kamikaze 0.781 1.793
Note: This is subject to change as I run more games. Plus, the ordering of ranks 9-13 bothers me, so I may tweak the scoring method to better match one's intuition of how they should be ranked.
(Means and standard deviations were rounded to three decimal digits. Also, Hyper
should be HYPER
but that messes up the highlighting. :P)
Any scores?.... – ev3commander – 2015-12-24T12:35:43.917
Does it log caught exceptions? – TheNumberOne – 2015-12-24T15:39:51.007
The angle is measured from the top vertical axis clockwise. – TheNumberOne – 2015-12-24T16:24:45.143
1You should specify that calling
LineIntersection
on non-intersecting segments returns an empty array. – LegionMammal978 – 2015-12-24T17:23:39.777Congrats on making the hot questions list! – cat – 2015-12-24T22:31:59.987
@TheNumberOne: No exceptions are caught. I've added that note on rotations. – El'endia Starman – 2015-12-25T02:03:13.673
@LegionMammal978: Good spot. Added. – El'endia Starman – 2015-12-25T02:03:22.460
@BlockCoder1392: Added a note on that at the end. I don't want to re-run battles, so I'll be waiting at least a few days before I start tallying scores. – El'endia Starman – 2015-12-25T02:03:56.380
Someone should make a bot that avoids the other player :P – ev3commander – 2015-12-25T02:17:13.780
@BlockCoder1392: That would be pretty easy for me, actually. I wrote a "RammingSpeed" bot last night that tries to hit the other player and avoids the sun. It'd be a simple matter of flipping signs. – El'endia Starman – 2015-12-25T02:53:10.740
1I think I did it! – ev3commander – 2015-12-25T13:17:06.833
3@CrazyPython: I'd dispute the first two considering that I basically copied a game, but the third is exactly what I wanted. Thanks! :D – El'endia Starman – 2015-12-26T06:38:16.463
@El'endiaStarman No kind of game has ever happened on PPCG before. It takes a lot of work to make a perfect clone. – noɥʇʎԀʎzɐɹƆ – 2015-12-26T17:07:12.297
Should there be a chatroom on this? – noɥʇʎԀʎzɐɹƆ – 2015-12-27T01:04:02.157
@CrazyPython: There is.
– El'endia Starman – 2015-12-27T01:19:53.440@Sleafar: WHOOPS. Fixed. – El'endia Starman – 2015-12-27T06:06:57.780
Is it just me ... or is the site not displaying any of the bots? – TheNumberOne – 2015-12-28T21:29:22.313
@TheNumberOne: I hard refreshed the page on my computer and I see them all. Try refreshing. – El'endia Starman – 2015-12-28T21:33:02.390
@El'endiaStarman Ya, that fixed it :) – TheNumberOne – 2015-12-28T22:06:00.893
Still not seeing the other bots. :⊂ – Rɪᴋᴇʀ – 2015-12-30T19:53:09.647
I think there should be scores now – ev3commander – 2016-01-09T00:50:23.170
@BlockCoder1392: Yeah, I'll do that today/tonight. – El'endia Starman – 2016-01-09T00:54:14.290
HYPER wasn't last with a 25% chance of dying each frame? – Generic User – 2016-01-11T02:07:56.600
@GenericUser: That's an artifact of the scoring system, which shall be reworked. HYPER has a smaller standard deviation than the last two... – El'endia Starman – 2016-01-11T02:45:18.193
@El'endiaStarman is there any possibility to add multiple user bots to the simulation? That way i can trial other submissions before you officially add them to the simulation. – Moogie – 2016-01-11T21:12:50.283
@Moogie: On the website proper, I'd have to add extra functionality for that, and I'm not really inclined to do so. You can, however, easily save the website to your computer and add JS files for your bots. Or just open up multiple tabs and test different user bots in each. – El'endia Starman – 2016-01-11T21:19:30.000
@El'endiaStarman: I am just wondering when the leader board will be updated? I notice that my entry back in Jan has never been officially tested. – Moogie – 2016-04-10T23:57:21.800
@Moogie: I had been planning on adding a leaderboard feature to the website, which would be the last feature I add. I've just been working on other stuff. – El'endia Starman – 2016-04-11T02:15:56.177
Hi, how to check a ship shape is 'nose only' ? Because testing getShipCoords dont work... – TuxCrafting – 2016-06-18T22:23:20.143
@TùxCräftîñg:
red_shape
orblue_shape
. It's in the question body. – El'endia Starman – 2016-06-18T23:56:00.713@El'endiaStarman about RammingSpeed - could you post that bot as a non-competing bot just-for-fun? – noɥʇʎԀʎzɐɹƆ – 2016-07-18T19:32:01.847
@AgentCrazyPython: I feel like there are already plenty of trivial bots. – El'endia Starman – 2016-07-18T19:47:22.230
@El'endiaStarman Oh. I thought you said it was pretty good. – noɥʇʎԀʎzɐɹƆ – 2016-07-18T20:01:21.637
updated leaderboard? – noɥʇʎԀʎzɐɹƆ – 2016-07-25T16:16:52.207
can you please please add a way to increase delay between frames. it's required for studying behavior. – noɥʇʎԀʎzɐɹƆ – 2016-08-22T22:48:03.017