From 0bc6937afe4f1ae34a96bdba2330391ead73da7d Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Sun, 3 Dec 2017 23:37:44 +0100 Subject: [PATCH] Cleanup for submission --- lemming_sim_student.js | 23 +++++++++++++++++++---- sensors.js | 20 +++++++++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lemming_sim_student.js b/lemming_sim_student.js index 6fb6a52..d5071ae 100644 --- a/lemming_sim_student.js +++ b/lemming_sim_student.js @@ -1,7 +1,8 @@ /* Lemmings - robot and GUI script. * * Copyright 2016 Harmen de Weerd - * Copyright 2017 Johannes Keyser, James Cooke, George Kachergis, Yorick van Pelt + * Copyright 2017 Johannes Keyser, James Cooke, George Kachergis + * Copyright 2017 Niels Boer, Tanja van Alfen, Yorick van Pelt * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,6 +17,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +/* +The simulator was edited quite extensively to make it easier to work with. +The essential changes are the color sensor and gyroscope (in sensors.js) and the robot behavior +(in class Lemming in this file). Sensor placement was changed as well. +Other changes: object-orientation. es6. fixed the bay. consolidated drawing behavior, +svg pathseg workaround, robot debug display. Huge rewrites. +Feel free to use any of this code. + */ +// es6 modules are behind a flag in firefox. +// import { ColorSensor, DistanceSensor, Gyroscope } from './sensors.js' "use strict" // Simulation settings; please change anything that you think makes sense. var simInfo = { @@ -32,6 +43,7 @@ var simInfo = { debugMouse: true, // allow dragging any object with the mouse wanderRate: 0.0002, }; +// our own lemming implementation class Lemming extends Robot { constructor(props) { super(Object.assign({ @@ -66,6 +78,8 @@ class Lemming extends Robot { ].concat(props.sensors || []) }, props)) } + // turn this amount of degrees to a side + // exploit prototypal inheritance to temporarily override robot.move turnDeg(degs, cb) { if (Math.abs(degs) >= 320) return this.turnDeg(320, x => this.turnDeg(degs -320, cb)) @@ -83,8 +97,7 @@ class Lemming extends Robot { } } move() { - //if (sim.curSteps % 250 == 0) this.turnDeg(-90) - // TODO: Define Lemming program here. + // our lemming behaviour const {carry: [r,g,b], dist, wall} = this.getSensors() let block = 0 if (r > (g+b)) { @@ -346,10 +359,12 @@ function redblocksatwall(sim) { }) return result } +// automatically run the experiments based on the values from the html async function experimentRunner(stopCondition=blocksSorted) { initSimulation() while(!stopCondition(sim) && sim.curSteps < simInfo.maxSteps) { sim.runSteps(500) + // take a break for the gui to stay responsive await promiseTimeout(50) } @@ -364,6 +379,7 @@ async function experimentRunner(stopCondition=blocksSorted) { sim = null return rv } +// run a number of experiments async function runExperiments() { let count = +document.getElementById('ffwdtimes').value while(count--) { @@ -411,7 +427,6 @@ class Bay { robot.sensors.forEach(sensor => { makeInteractiveElement(sensor, this.elem) }) - // todo: removeinteractiveelement? this.repaint() } repaint() { diff --git a/sensors.js b/sensors.js index ac48bf1..445849d 100644 --- a/sensors.js +++ b/sensors.js @@ -1,5 +1,23 @@ -// Description of robot(s), and attached sensor(s) used by InstantiateRobot() +/* Lemmings - sensor code. + * + * Copyright 2016 Harmen de Weerd + * Copyright 2017 Johannes Keyser, James Cooke, George Kachergis + * Copyright 2017 Niels Boer, Tanja van Alfen, Yorick van Pelt + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ class Sensor { constructor(id, props) { Object.assign(this, {