Permalink
Browse files

last working state

  • Loading branch information...
1 parent 95cb76a commit dc73646e1f43b4073515ae36de60c467eced4f24 @jlongster committed Dec 17, 2012
View
@@ -1,12 +1,27 @@
var BinaryClient = require('binaryjs').BinaryClient;
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 stream;
+var room = process.argv[2];
+console.log('connecting to ' + room + '...');
+
+var bot = new Entity();
+var entities = {};
+var userId, username;
+
client.on('open', function() {
stream = client.createStream();
+ stream.write(p.joinRoomPacket({
+ type: p.joinRoomPacket.typeId,
+ from: 0,
+ room: room
+ }));
+
stream.on('data', function(data) {
handlePacket(data);
});
@@ -19,18 +34,68 @@ client.on('open', function() {
runAI();
});
-function handlePacket(packet) {
- if(packet instanceof Buffer) {
- packet = p.parsePacket(packet, p.basePacket);
+function handlePacket(data) {
+ var packet;
+ if(data instanceof Buffer) {
+ packet = p.parsePacket(data, p.basePacket);
}
else {
- packet = p.objectifyPacket(packet);
+ packet = p.objectifyPacket(data);
}
switch(p.getPacketDesc(packet.type)) {
- default:
- // do nothing right now
+ case p.joinPacket:
+ 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;
+ 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]);
+ }
+ }
}
}
@@ -40,14 +105,14 @@ function initAI() {
var obj = {
type: p.inputPacket.typeId,
from: 0,
- dt: .25,
+ dt: 2.0,
sequenceId: id++,
mouseX: 0,
mouseY: 0,
mouseDown: 0,
- left: 1,
+ left: 0,
right: 0,
- up: 0,
+ up: 1,
down: 0
};
@@ -57,14 +122,14 @@ function initAI() {
obj = {
type: p.inputPacket.typeId,
from: 0,
- dt: 2.5,
+ dt: 0.1,
sequenceId: id++,
mouseX: 0,
mouseY: 0,
mouseDown: 0,
- left: 0,
+ left: 1,
right: 0,
- up: 1,
+ up: 0,
down: 0
};
@@ -81,9 +146,9 @@ function runAI() {
var obj = {
type: p.inputPacket.typeId,
from: 0,
- dt: Math.random() / 20,
+ dt: dt,
sequenceId: id++,
- mouseX: -1,
+ mouseX: 0,
mouseY: 0,
mouseDown: 0,
left: 0,
@@ -92,24 +157,77 @@ function runAI() {
down: 0
};
- var packet = p.makePacket(obj, p.inputPacket);
- stream.write(packet);
+ var key = null;
+ for(var k in entities) {
+ key = k;
+ break;
+ }
- obj = {
- type: p.inputPacket.typeId,
- from: 0,
- dt: Math.random() / 40,
- sequenceId: id++,
- mouseX: 0,
- mouseY: 0,
- mouseDown: 0,
- left: 0,
- right: 0,
- up: 1,
- down: 0
- };
+ if(entities[key]) {
+ var a = entities[key].pos;
+ var b = bot.pos;
+ var dir = vec3.create();
+ vec3.subtract(a, b, dir);
+
+ var dist = vec3.length(dir);
+
+ if(dist > 50) {
+ if(bot.rot[1] > .03) {
+ obj.mouseX = 1;
+ }
+ 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);
setTimeout(runAI, 16);
View
@@ -10,6 +10,9 @@ var Room = require('./room');
var Entity = require('./static/js/entity');
var level = require('./static/js/level');
+// For adding bots
+var spawn = require('child_process').spawn
+
var app = express();
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app);
@@ -360,10 +363,11 @@ function createUser(stream, room) {
if(room.count() > 1) {
if(!room.startTime()) {
room.start();
+
room.broadcast(null, p.gameStartPacket({
type: p.gameStartPacket.typeId,
from: 0,
- started: room.startTime()
+ started: Date.now()
}));
room.onEnd(function() {
@@ -427,6 +431,12 @@ function createRoom(name) {
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;
}
@@ -113,7 +113,7 @@ BinaryPack.Unpacker.prototype.readUInt64 = function(){
}
BinaryPack.Unpacker.prototype.readInt64 = function(){
- var uint64 = this.readInt64();
+ var uint64 = this.readUInt64();
return (uint64 < Math.pow(2, 63) ) ? uint64 : uint64 - Math.pow(2, 64);
}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -154,4 +154,15 @@ body.ingame #ingame {
background: -ms-linear-gradient(top, #0088cc 0%,#0055cc 100%); /* IE10+ */
background: linear-gradient(to bottom, #0088cc 0%,#0055cc 100%); /* W3C */
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;
}
View
@@ -91,7 +91,8 @@
},
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() {
View
@@ -7,6 +7,14 @@
var thickness = 1000;
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],
null,
[w + thickness * 2, height, thickness],
@@ -7,11 +7,10 @@ void main() {
vec3 nNormal = normalize(normal);
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);
- 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;
}
View
@@ -11,6 +11,18 @@
</head>
<body class="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">
<img src="/img/octo-screen.png" />

0 comments on commit dc73646

Please sign in to comment.