From 142ad54c98074c46de1a556643e4965fb4e2ecfd Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Wed, 15 May 2019 16:17:00 -0500 Subject: [PATCH] collisions aite, constants are more fun --- TODO.org | 6 +++-- server/TODO.org | 10 ------- server/collisions.js | 4 ++- server/constants.js | 6 ++--- server/endpoints.js | 12 +++++++++ server/robot.js | 39 --------------------------- web/constants.js | 4 +-- web/draw.js | 7 ++--- web/main.js | 2 -- web/socket.js | 63 -------------------------------------------- web/test.html | 24 ----------------- web/test.js | 29 -------------------- 12 files changed, 28 insertions(+), 178 deletions(-) delete mode 100644 server/TODO.org delete mode 100644 web/socket.js delete mode 100644 web/test.html delete mode 100644 web/test.js diff --git a/TODO.org b/TODO.org index 35943c5..10afdde 100644 --- a/TODO.org +++ b/TODO.org @@ -1,6 +1,8 @@ todo: -- fix the robots +- make sure files in the right place for server stuff +maybe: - paddle movement bump -- fix collisions - more balls - fiddle with variables to optimize fun + + diff --git a/server/TODO.org b/server/TODO.org deleted file mode 100644 index a795ef8..0000000 --- a/server/TODO.org +++ /dev/null @@ -1,10 +0,0 @@ -list of things i should still do: -- fix collisions - -sorta extra/for later/unsure: -- fiddle with variables to optimize fun -- make sure files are in the right place and urls are appropriate for server -- double check all on-screen messages for fitting (look to the 'magic number...' comment) -- fix game not ending with just robots (is this actually broken?) -- polish -- debug touch diff --git a/server/collisions.js b/server/collisions.js index d360766..72d72f8 100644 --- a/server/collisions.js +++ b/server/collisions.js @@ -59,8 +59,10 @@ var handleWalls = function(ball, walls) { // modifies ball's velocity if it encounters an actual collision // wall is an endpoints for(var wall of walls) { + var stretched = wall.elongate(1.2); // this is done to fix collision detection? var next_spot = new Coord(ball.coord.x + ball.dx/c.FPS, ball.coord.y + ball.dy/c.FPS); // the next spot - if(segments_intersect(wall, new Endpoints(ball.coord, next_spot))) { + var ball_ep = new Endpoints(ball.coord, next_spot) + if(segments_intersect(wall, ball_ep.elongate(1.2))) { //there's a collision var wall_normal = new Vec((wall.f.x+wall.s.x)/2, (wall.f.y+wall.s.y)/2); // given by the midpoint wall_normal.normalize(); diff --git a/server/constants.js b/server/constants.js index 3f12a3d..22edec0 100644 --- a/server/constants.js +++ b/server/constants.js @@ -3,11 +3,11 @@ var c = { // matchmaker WS_PORT: 6789, - NUM_PLAYERS: 4, + NUM_PLAYERS: 10, FPS: 30, WAIT_TIME: 60000, // 1 minute MAX_GAMES: 100, // the most games allowed to go on at once, to be tweaked as needed for purposes - ROBO_TIME: 5, // time in seconds before filling out with robots + ROBO_TIME: 30, // time in seconds before filling out with robots // gamestate DYING_TIME_IN_FRAMES: 100, BOARD_RADIUS: 10, @@ -17,7 +17,7 @@ var c = { // ball MIN_INIT: 1, MAX_INIT: 3, // initial speed constraints - BALL_RADIUS: 0.5, + BALL_RADIUS: 0.3, MAX_SPEED: 15, SPEED_BUMP: 0.5, // field diff --git a/server/endpoints.js b/server/endpoints.js index f3641b2..6006c26 100644 --- a/server/endpoints.js +++ b/server/endpoints.js @@ -1,6 +1,8 @@ const c = require('./constants.js'); +const Coord = require('./coord.js'); +const Vec = Coord; var Endpoints = function(f,s,id) { // f,s are first, second coordinates, in increasing angle @@ -30,5 +32,15 @@ Endpoints.prototype.getAngles = function() { } return new AnglePair(fst-c.ANGLE_THRESH, snd+c.ANGLE_THRESH, this.id); } +Endpoints.prototype.elongate = function(v) { + // stretches the endpoints by multiple of v + var to_s = new Vec(v*(this.s.x-this.f.x), v*(this.s.y-this.f.y)); + var to_f = new Vec(v*(this.f.x-this.s.x), v*(this.f.y-this.s.y)); + // stretched vectors pointing in to s from f and vice versa + to_f.translate(this.s.x, this.s.y); + to_s.translate(this.f.x, this.f.y); + return new Endpoints(to_f, to_s) +} + module.exports = Endpoints; diff --git a/server/robot.js b/server/robot.js index f7bb316..f8b257a 100644 --- a/server/robot.js +++ b/server/robot.js @@ -69,45 +69,6 @@ Robot.prototype.standard_ai = function(data) { if(Math.abs(nearest_mod.x) < 0.2) this.status = 'x'; } - - - -Robot.prototype.bad_ai = function(data) { - if(typeof data == "string") - return; - if(data.balls.length == 0) - return; - // this is where ALL the work needs to get done - // i will start with a fairly naive approach, where i just move the paddle to the ball nearest to the region - var me = data.id; - var eps = field.genAllEndpoints(data.n, data.dead); - var my_ep = eps.find(x => x.id == me); - if(typeof my_ep == 'undefined') { - this.status = 'x'; - return; - } - // recall that i strip out superfluous data... this is kinda tricky, just be careful - var center = midpoint(my_ep.f, my_ep.s); - var nearest = data.balls[0]; - var bdist2 = center.dist2(nearest); - for(var ball of data.balls) { - var newdist = center.dist2(ball); - if(newdist < bdist2) { - nearest = ball; - bdist2 = newdist; - } - } - // k now i've got the nearest... just move it in the direction of the nearest endpoint, yeah? yeah! - // because the endpoints are given in increasing radians! Just have to do a check that - if(nearest.dist2(my_ep.f) > nearest.dist2(my_ep.s)) { - // double check which of 'u' or 'd' makes more sense, i think as is is probably fine but... - this.status = 'u'; - } - else { - this.status = 'd'; - } -} - var midpoint = function(a,b) { return new Coord((a.x+b.x)/2, (a.y+b.y)/2); } diff --git a/web/constants.js b/web/constants.js index ecf70aa..244e851 100644 --- a/web/constants.js +++ b/web/constants.js @@ -3,7 +3,7 @@ var c = { // matchmaker WS_PORT: 6789, - NUM_PLAYERS: 4, + NUM_PLAYERS: 10, MS_PER_FRAME: 100, WAIT_TIME: 60000, // 1 minute MAX_GAMES: 5, // the most games allowed to go on at once, to be tweaked as needed for purposes @@ -16,7 +16,7 @@ var c = { // ball MIN_INIT: 3, MAX_INIT: 5, // initial speed constraints - BALL_RADIUS: 0.5, + BALL_RADIUS: 0.3, MAX_SPEED: 15, SPEED_BUMP: 0.2, // field diff --git a/web/draw.js b/web/draw.js index 93d11f5..8c44362 100644 --- a/web/draw.js +++ b/web/draw.js @@ -49,7 +49,7 @@ var draw = function(state, ctx) { } // balls for(var b of state.balls) { - drawBall(ctx, bcolor, b); + drawBall(ctx, bcolor, b, state.dead.length, state.n); } // finally the paddles... for(var eps of livingzones) { @@ -88,11 +88,12 @@ var dist = function(c1, c2) { return Math.sqrt((c1.x-c2.x)**2 + (c1.y-c2.y)**2); } -var drawBall = function(ctx, color, coord) { +var drawBall = function(ctx, color, coord, nlive, nmax) { ctx.save(); ctx.fillStyle = color; ctx.beginPath(); - ctx.arc(coord.x, coord.y, c.BALL_RADIUS, 0, 2*Math.PI, false); + var rad = c.BALL_RADIUS; + ctx.arc(coord.x, coord.y, rad, 0, 2*Math.PI, false); ctx.fill(); ctx.restore(); } diff --git a/web/main.js b/web/main.js index 822b920..be4d700 100644 --- a/web/main.js +++ b/web/main.js @@ -55,7 +55,6 @@ var main = function() { // starts everything, gets us going, setup ish document.onclick = function(e) {location.reload()} // okay, outside of 'input' file... document.onkeydown = function(e) {if(e.keyCode == '71') location.reload()}; drawWin(ctx); - console.log("winner"); return; } else if(e.data == "l") { @@ -65,7 +64,6 @@ var main = function() { // starts everything, gets us going, setup ish drawOverlay(ctx,2); //second place else drawOverlay(ctx, place); - console.log("loser"); return; } var state = JSON.parse(e.data); diff --git a/web/socket.js b/web/socket.js deleted file mode 100644 index c55b0ac..0000000 --- a/web/socket.js +++ /dev/null @@ -1,63 +0,0 @@ - -var prefixurl = "ws://localhost:6789" - -var keystate = ""; - -function doIt() { - console.log("aaa") - var ws = new WebSocket(prefixurl); - ws.onopen = function(event) { - ws.send("Hello World!"); - }; - ws.onmessage = function(event) { - console.log(event.data); - } -} - -function onload() { - var timer = 0; - document.getElementById("play").addEventListener('click',e=>doIt()); - document.getElementById("stop").addEventListener('click',e=>clearInterval(timer)); - console.log("ssss"); - - var ws = new WebSocket(prefixurl); - ws.onmessage = function(event) { - console.log("event.data") - } - timer = window.setInterval(function(){sendInput(ws);},1000); - window.onkeydown = (e=>keypressHandler(e,true)); - window.onkeyup = (e=>keypressHandler(e,false)); -} - -window.addEventListener("DOMContentLoaded",e=> onload()); - - -function sendInput(ws) { - if(keystate != '') { - ws.send(keystate); - } -} - -function keypressHandler(evt,isdn) { - if(evt.keyCode == '38') { - if(isdn) { - keystate = 'u'; - } - else if(keystate=='u') { - keystate = ''; - } - } - else if(evt.keyCode == '40') { - if(isdn) { - keystate = 'd'; - } - else if(keystate=='d') { - keystate = ''; - } - } -} - -function recievemessage(evt) { - - -} diff --git a/web/test.html b/web/test.html deleted file mode 100644 index 0198bad..0000000 --- a/web/test.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - Pong Battle Royale - - - - Testing page for the websocket infrastructure I've drummed up -
- - - -
- response from server: -
-
- - diff --git a/web/test.js b/web/test.js deleted file mode 100644 index 5c37389..0000000 --- a/web/test.js +++ /dev/null @@ -1,29 +0,0 @@ - -var prefixurl = "ws://localhost:6789" - -theSocket = null; - - -function send(k, s) { - // sends key k thru socket s - theSocket.send(k); -} - -function start() { - // opens up a nice websocket - theSocket = new WebSocket(prefixurl); - theSocket.onmessage = function (e) { - // newdiv = document.createElement('div') - // newdiv.innerText = "data:" + e.data; - // document.getElementById('content').appendChild(newdiv); - } -} - - - -function onload() { - document.getElementById("connect").addEventListener('click', e=> start()); - document.getElementById("up").addEventListener('click', e=> send('u',theSocket)); - document.getElementById("dn").addEventListener('click', e=> send('d',theSocket)); -} -window.addEventListener("DOMContentLoaded", e => onload()); -- 2.39.2