Skip to content

Commit

Permalink
last working state
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Dec 17, 2012
1 parent 95cb76a commit dc73646
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 36 deletions.
178 changes: 148 additions & 30 deletions ai.js
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,27 @@
var BinaryClient = require('binaryjs').BinaryClient; var BinaryClient = require('binaryjs').BinaryClient;
var p = require('./static/js/packets'); var p = require('./static/js/packets');
var Entity = require('./static/js/entity');
var vec3 = require('./static/js/shade/gl-matrix').vec3;


var client = new BinaryClient('ws://localhost:4000'); var client = new BinaryClient('ws://localhost:4000');
var stream; var stream;


var room = process.argv[2];
console.log('connecting to ' + room + '...');

var bot = new Entity();
var entities = {};
var userId, username;

client.on('open', function() { client.on('open', function() {
stream = client.createStream(); stream = client.createStream();


stream.write(p.joinRoomPacket({
type: p.joinRoomPacket.typeId,
from: 0,
room: room
}));

stream.on('data', function(data) { stream.on('data', function(data) {
handlePacket(data); handlePacket(data);
}); });
Expand All @@ -19,18 +34,68 @@ client.on('open', function() {
runAI(); runAI();
}); });


function handlePacket(packet) { function handlePacket(data) {
if(packet instanceof Buffer) { var packet;
packet = p.parsePacket(packet, p.basePacket); if(data instanceof Buffer) {
packet = p.parsePacket(data, p.basePacket);
} }
else { else {
packet = p.objectifyPacket(packet); packet = p.objectifyPacket(data);
} }


switch(p.getPacketDesc(packet.type)) { switch(p.getPacketDesc(packet.type)) {
default: case p.joinPacket:
// do nothing right now var obj = p.parsePacket(data, p.joinPacket);
var ent = new Entity({
pos: [obj.x, obj.y, obj.z],
rot: [obj.rotX, obj.rotY, obj.rotZ],
color: [Math.random(), Math.random(), Math.random()]
});
ent.id = 'anon' + obj.id;
entities[obj.id] = ent;
break;
case p.leavePacket:
var obj = packet;
delete entities[obj.id];
break;
case p.newUserPacket:
var obj = packet;
userId = obj.id;
username = obj.name;

stream.write(p.messagePacket({
type: p.messagePacket.typeId,
from: 0,
name: username,
message: 'I am a bot here to destroy you. Please invite other friends as this is more fun with up to 8 players. Press F1 to chat.'
}));

break;
case p.statePacket:
var obj = p.parsePacket(data, p.statePacket);
if(obj.from !== 0) {
entities[obj.from].applyState(obj);
entities[obj.from].update(.1);
}
else {
bot.applyState(obj);
bot.update(.1);
}

break;
case p.gameOverPacket:
process.exit(0);
break; break;
case p.cmdPacket:
var obj = packet;
if(obj.method == 'die') {
if(obj.from !== 0) {
entities[obj.from].restart(obj.args[2]);
}
else {
bot.restart(obj.args[2]);
}
}
} }
} }


Expand All @@ -40,14 +105,14 @@ function initAI() {
var obj = { var obj = {
type: p.inputPacket.typeId, type: p.inputPacket.typeId,
from: 0, from: 0,
dt: .25, dt: 2.0,
sequenceId: id++, sequenceId: id++,
mouseX: 0, mouseX: 0,
mouseY: 0, mouseY: 0,
mouseDown: 0, mouseDown: 0,
left: 1, left: 0,
right: 0, right: 0,
up: 0, up: 1,
down: 0 down: 0
}; };


Expand All @@ -57,14 +122,14 @@ function initAI() {
obj = { obj = {
type: p.inputPacket.typeId, type: p.inputPacket.typeId,
from: 0, from: 0,
dt: 2.5, dt: 0.1,
sequenceId: id++, sequenceId: id++,
mouseX: 0, mouseX: 0,
mouseY: 0, mouseY: 0,
mouseDown: 0, mouseDown: 0,
left: 0, left: 1,
right: 0, right: 0,
up: 1, up: 0,
down: 0 down: 0
}; };


Expand All @@ -81,9 +146,9 @@ function runAI() {
var obj = { var obj = {
type: p.inputPacket.typeId, type: p.inputPacket.typeId,
from: 0, from: 0,
dt: Math.random() / 20, dt: dt,
sequenceId: id++, sequenceId: id++,
mouseX: -1, mouseX: 0,
mouseY: 0, mouseY: 0,
mouseDown: 0, mouseDown: 0,
left: 0, left: 0,
Expand All @@ -92,24 +157,77 @@ function runAI() {
down: 0 down: 0
}; };


var packet = p.makePacket(obj, p.inputPacket); var key = null;
stream.write(packet); for(var k in entities) {
key = k;
break;
}


obj = { if(entities[key]) {
type: p.inputPacket.typeId, var a = entities[key].pos;
from: 0, var b = bot.pos;
dt: Math.random() / 40, var dir = vec3.create();
sequenceId: id++, vec3.subtract(a, b, dir);
mouseX: 0,
mouseY: 0, var dist = vec3.length(dir);
mouseDown: 0,
left: 0, if(dist > 50) {
right: 0, if(bot.rot[1] > .03) {
up: 1, obj.mouseX = 1;
down: 0 }
}; else if(bot.rot[1] < -.03) {
obj.mouseX = -1;
}
else {
if(Math.abs(dir[0]) > Math.abs(dir[2])) {
if(dir[0] < 0) {
obj.left = 1;
}
else {
obj.right = 1;
}
}
else {
if(dir[2] < 0) {
obj.up = 1;
}
else {
obj.down = 1;
}
}
}
}

if(dist < 100) {
if(Math.random() < .1) {
var entIds = [];
var entInterps = [];
var seqIds = [];

for(var k in entities) {
entIds.push(entities[k].id);
entInterps.push(entities[k].interp);
seqIds.push(entities[k].sequenceId);
}

stream.write(p.clickPacket({
type: p.clickPacket.typeId,
from: 0,
entIds: entIds,
entInterps: entInterps,
seqIds: seqIds,
x: a[0],
y: a[1] + Math.random() * 50 - 25,
z: a[2],
x2: b[0],
y2: b[1],
z2: b[2]
}));
}
}
}


packet = p.makePacket(obj, p.inputPacket); var packet = p.makePacket(obj, p.inputPacket);
stream.write(packet); stream.write(packet);


setTimeout(runAI, 16); setTimeout(runAI, 16);
Expand Down
12 changes: 11 additions & 1 deletion main.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var Room = require('./room');
var Entity = require('./static/js/entity'); var Entity = require('./static/js/entity');
var level = require('./static/js/level'); var level = require('./static/js/level');


// For adding bots
var spawn = require('child_process').spawn

var app = express(); var app = express();
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views')); var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app); env.express(app);
Expand Down Expand Up @@ -360,10 +363,11 @@ function createUser(stream, room) {
if(room.count() > 1) { if(room.count() > 1) {
if(!room.startTime()) { if(!room.startTime()) {
room.start(); room.start();

room.broadcast(null, p.gameStartPacket({ room.broadcast(null, p.gameStartPacket({
type: p.gameStartPacket.typeId, type: p.gameStartPacket.typeId,
from: 0, from: 0,
started: room.startTime() started: Date.now()
})); }));


room.onEnd(function() { room.onEnd(function() {
Expand Down Expand Up @@ -427,6 +431,12 @@ function createRoom(name) {


console.log('room "' + name + '" created [' + roomCount() + ']'); console.log('room "' + name + '" created [' + roomCount() + ']');


// Spawn a bot
// var p = spawn('node', ['./ai', name]);
// p.stderr.on('data', function(data) {
// console.log('[ERROR] ' + data);
// });

return room; return room;
} }


Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added screens/final-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screens/final-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screens/final-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions static/css/main.css
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -154,4 +154,15 @@ body.ingame #ingame {
background: -ms-linear-gradient(top, #0088cc 0%,#0055cc 100%); /* IE10+ */ background: -ms-linear-gradient(top, #0088cc 0%,#0055cc 100%); /* IE10+ */
background: linear-gradient(to bottom, #0088cc 0%,#0055cc 100%); /* W3C */ background: linear-gradient(to bottom, #0088cc 0%,#0055cc 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0088cc', endColorstr='#0055cc',GradientType=0 ); /* IE6-9 */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0088cc', endColorstr='#0055cc',GradientType=0 ); /* IE6-9 */
}

.social {
padding: .5em 0;
position: absolute;
top: 0;
right: 0;
}

.social .item {
margin-bottom: .5em;
} }
3 changes: 2 additions & 1 deletion static/js/entity.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
}, },


getLowestHeight: function() { getLowestHeight: function() {
return Terrain.getHeight(this.pos[0], this.pos[2], true) + 10.0; //return Terrain.getHeight(this.pos[0], this.pos[2], true) + 10.0;
return 20.0;
}, },


hit: function() { hit: function() {
Expand Down
8 changes: 8 additions & 0 deletions static/js/level.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
var thickness = 1000; var thickness = 1000;
var opts = { wireframe: false }; var opts = { wireframe: false };


// var floor = new Cube([0, -50, 0],
// null,
// [w, 50, d],
// opts);
// floor.color = [.2, .2, .2];
// floor.collisionType = Collision.STATIC;
// scene.addObject(floor);

var front = new Cube([-thickness, 0, -thickness], var front = new Cube([-thickness, 0, -thickness],
null, null,
[w + thickness * 2, height, thickness], [w + thickness * 2, height, thickness],
Expand Down
5 changes: 2 additions & 3 deletions static/shaders/default.fsh
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ void main() {
vec3 nNormal = normalize(normal); vec3 nNormal = normalize(normal);


vec3 lightDir = normalize(vec3(100.0, 100.0, 100.0) - position); vec3 lightDir = normalize(vec3(100.0, 100.0, 100.0) - position);
float diffuse = max(0.0, dot(nNormal, lightDir)) * 1.2; float diffuse = max(0.0, dot(nNormal, lightDir)) * 3.0;


lightDir = normalize(vec3(800.0, 100.0, 800.0) - position); lightDir = normalize(vec3(800.0, 100.0, 800.0) - position);
float diffuse2 = max(0.0, dot(nNormal, lightDir)); float diffuse2 = max(0.0, dot(nNormal, lightDir)) * 3.0;



gl_FragColor = vec4(color, 1.0) * diffuse + vec4(color, 1.0) * diffuse2; gl_FragColor = vec4(color, 1.0) * diffuse + vec4(color, 1.0) * diffuse2;
} }
12 changes: 12 additions & 0 deletions views/index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
</head> </head>
<body class="intro"> <body class="intro">
<div id="intro"> <div id="intro">

<div class="social">
<div class="item">
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://octoshot.jlongster.com/" data-text="Check out Octoshot, a WebGL FPS entry for github's game off!" data-via="jlongster">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>

<div class="item">
<iframe src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fgithub.com%2Fjlongster%2Foctoshot&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;font=arial&amp;colorscheme=dark&amp;action=like&amp;height=21&amp;appId=203253883025618" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px;" allowTransparency="true"></iframe>
</div>
</div>

<div class="header"> <div class="header">
<img src="/img/octo-screen.png" /> <img src="/img/octo-screen.png" />


Expand Down

0 comments on commit dc73646

Please sign in to comment.