This is a game of capture the flag, heavily inspired and based off of Red vs. Blue - Pixel Team Battlebots. That was an awesome question (thank you very much Calvin'sHobbies; I hope you don't mind that I shamelessly stole a lot of code from you) -- here's another team based king-of-the-hill. Hopefully, capture the flag will require more team cooperation as well as more strategy.
To mix it up, you are considered on the red team if the last digit of your id is between 0
and 4
inclusive. This should prevent the exact same teams from battling again, if the same people decide to answer. The board is 350px
by 350px
. The blue team starts on the upper half of the board and the red team starts on the lower half.
The way you play capture the flag is as follows: the object of the game is to take the opposing team's flag and bring it back to your own side. If you are on their side, you can be tagged and sent to jail. If you are in jail, then you can't move. If you are on your side, your job is to tag opposing team members to send them to jail. The only way to get out of jail is for someone on your team who is free to tag everyone in jail. (Note that jail is located on the opposing team's side).
Specifically:
- There is a constant -
FIELD_PADDING
- set to 20. This is the padding for the field. If it were zero, then the flags and jail would be exactly on the corners of the canvas. Since it is not, the flag and jail are 20 pixels away from the corners. - The blue flag (remember: blue team is on the upper half) is located at
(WIDTH - FIELD_PADDING, FIELD_PADDING) = (330, 20)
i.e. top-right corner. - The red flag is at
(FIELD_PADDING, HEIGHT - FIELD_PADDING) = (20, 330)
- The blue jail (where red members are kept) is at
(20, 20)
i.e. blue side, top left. - The red jail, where blue members are kept, is at
(330, 330)
Every team member starts randomly at a position 45 < x < 305
and 45 < y < 175
for blue and 175 < y < 305
for red. No team member can go within DEFENSE_RADIUS = 25
pixels of their own flag or their own jail (unless, of course, your own flag was taken by an opposing bot, in which case you need to tag that bot). This is to prevent puppy-guarding like bots. If you go within that range, you are "pushed" back. Similarly, no team member can go out of bounds (less than zero or more than 350) -- if you do, you are pushed back to the nearest legal place you can be.
Every time you move, you use up strength
. Your strength
starts out at 20
and is replenished by 2
every turn. The amount of strength you use is equal to the distance you travel. If your strength would become negative by moving to a certain place, you are prevented from making that move. It's probably a good idea to just go at speed 2
for normal chasing. You should only use higher speeds if you are close to winning and need the extra speed (in my opinion).
Spec:
The spec is quite similar to the Pixel Team Battlebots question. You should write a code block (remember, no global variables) in javascript. It should return an object with an x
-value and y
-value representing your change in x and change in y values. The following answer:
return {
x: 0,
y: -2
};
always moves up, until it hits a wall. You may not edit 8 hours after posting. You have access to the following variables in your code:
move
-- the round number, starting at 0tJailed
-- an array of all players on your team that are jailedeJailed
-- an array of all players on the opposing team that are jailedteam
-- an array of all players on your team, NOT just the ones near youenemies
-- an array of all players on the other team, NOT just the ones near youtFlag
-- your flag (you're trying to protect it)eFlag
-- the other flag (you're trying to steal it)messages
-- explained below- A list of constants:
WIDTH = 350
,HEIGHT = 350
,FIELD_PADDING = 20
,DEFENSE_RADIUS = 25
.
Every player has the following properties:
x
andy
strength
id
Every flag has the following properties:
x
andy
pickedUpBy
-- the player who currently has the flag, or null if no player has the flag.
Now, messages
is an object that is shared among your teammates. I don't care what you do with it. The same object is shared and passed to every one of your team members. This is the only way you can communicate. You can attach properties to it, share objects, etc. It can be as big as you want -- no size limit.
Every turn the following happens:
- The list of players (both red and blue) is randomly shuffled for turn order.
- Every player makes a move.
- If any red team members touch (within 10 pixels of) any blue team members on red's side, send the blue team members to jail, and vice versa. A jailed player drops his/her flag and has strength drop to zero. Note that the step function (code you provide) is still called -- so you can get/set messages, but you can't move while in jail.
- If any player is touching (within 10 pixels of) the other flag, then the other flag is marked as "picked up by" that player. When the player moves, the flag moves -- until the player is tagged and goes to jail, that is.
- If any player is touching the other side's jail, free everyone in that jail. When a player is freed from jail, he/she is teleported to a random location on his/her side.
Hints:
- At least in regular capture the flag, attacks work much better when many players go at once, because it tends to confuse defenders as to which player they should chase.
- Similarly, defenders might want to coordinate who they are chasing so that attacks don't go through
Controller: http://jsfiddle.net/prankol57/4L7fdmkk/
Full screen controller: http://jsfiddle.net/prankol57/4L7fdmkk/embedded/result/
Let me know if there are any bugs -- I'm sure there are many. I only started working on this yesterday.
Good luck.
If anyone wants to see an example game, I made an example bot that you can copy and paste into the "testbot" textarea (the testbot creates two duplicates on each team; check both red team and blue team):
var r2 = Math.sqrt(2);
if (this.id === -1) {
// red team 1
// go after flag regardless of what is going on
if (eFlag.pickedUpBy !== null && eFlag.pickedUpBy.id === this.id) {
return {
x: 0,
y: 2
};
}
return {
x: this.x < eFlag.x ? r2 : -r2,
y: this.y < eFlag.y ? r2 : -r2
};
}
if (this.id === -2) {
// blue team 1
// a) go after opposing team members on your side b) get the other flag if no enemies on your side
var closestEnemy = null;
for (var i = 0; i < enemies.length; ++i) {
if (enemies[i].y < HEIGHT/2 && (closestEnemy === null || enemies[i].y < closestEnemy.y)) {
closestEnemy = enemies[i];
}
}
if (closestEnemy !== null) {
return {
x: this.x < closestEnemy.x ? r2 : -r2,
y: this.y < closestEnemy.y ? r2 : -r2
};
}
if (eFlag.pickedUpBy !== null && eFlag.pickedUpBy.id === this.id) {
return {
x: 0,
y: -2
};
}
return {
x: this.x < eFlag.x ? r2 : -r2,
y: this.y < eFlag.y ? r2 : -r2
};
}
if (this.id === -3) {
// red team 2
// a) defend the flag b) if at least half of enemies in jail and no enemies on this side, free jailed reds and quickly return
var closestEnemy = null;
for (var i = 0; i < enemies.length; ++i) {
if (enemies[i].y > HEIGHT/2 && (closestEnemy === null || enemies[i].y > closestEnemy.y)) {
closestEnemy = enemies[i];
}
}
if (closestEnemy !== null) {
return {
x: this.x < closestEnemy.x ? r2 : -r2,
y: this.y < closestEnemy.y ? r2 : -r2
};
}
if (enemies.length / eJailed.length <= 1 && tJailed.length > 0) {
return {
x: this.x < FIELD_PADDING ? r2 : -r2,
y: this.y < FIELD_PADDING ? r2 : -r2
};
}
if (this.y < 350/2) return {x: 0, y: 2};
return {
x: this.x < tFlag.x ? r2 : -r2,
y: this.y < tFlag.y ? r2 : -r2
};
}
if (this.id === -4) {
// blue team 2
// a) try freeing jail if there are jailed team members b) capture the flag
if (tJailed.length > 0) {
return {
x: this.x < WIDTH - FIELD_PADDING ? r2 : -r2,
y: this.y < HEIGHT - FIELD_PADDING ? r2 : -r2
};
}
if (eFlag.pickedUpBy !== null && eFlag.pickedUpBy.id === this.id) {
return {
x: 0,
y: -2
};
}
return {
x: this.x < eFlag.x ? r2 : -r2,
y: this.y < eFlag.y ? r2 : -r2
};
}