summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/simulation/game.js52
-rw-r--r--src/simulation/game.test.js28
-rw-r--r--src/util.js11
3 files changed, 89 insertions, 2 deletions
diff --git a/src/simulation/game.js b/src/simulation/game.js
index 991d125..f0c4815 100644
--- a/src/simulation/game.js
+++ b/src/simulation/game.js
@@ -1,6 +1,13 @@
'use strict';
+import { random_choice, apply } from '../util.js';
+import { senses } from './senses.js';
+import { actions } from './actions.js';
+import { lattice_rules } from './lattice_rules.js';
+import { validity } from './validity.js';
+
+
function is_wall(size, x, y) {
return (
x === 0 || x === size-1 ||
@@ -44,3 +51,48 @@ export function setup_board(size) {
}));
return lattice;
}
+
+
+export function create_world(size, teams) {
+ const lattice = setup_board(size);
+
+ const agents = teams.reduce(
+ (agents, team, team_num) => {
+ const team_cells = lattice.map((row, y) => row.map((cell, x) => [x, y, cell])).flat()
+ // only check cells with the right team
+ .filter(([x, y, cell]) => cell.type === 'empty' && cell.flags.team === team_num)
+ return agents.concat(team.reduce(
+ (acc, agent) => {
+ const available_cells = team_cells.filter(([x, y, cell]) => acc.reduce(
+ (occupied, a) => occupied && ((a.x !== x) || (a.y !== y)),
+ true
+ ))
+ const [x, y, ..._] = random_choice(available_cells);
+ const orientation = random_choice([ 'n', 'e', 's', 'w' ]);
+ return [...acc, {...agent, x, y, flags: { ...agent.flags, orientation } }];
+ },
+ []
+ ));
+ },
+ []
+ ).flat();
+
+ return { lattice, lattice_rules, agents, actions, senses, validity };
+};
+
+
+// team structure:
+// {
+// agents
+// genome
+// }
+
+
+//export function create_team(size, genome_size, n_internal) {
+// const n_input = senses.reduce((acc, sense) => acc + sense.size, 0);
+// const n_output = senses.reduce((acc, sense) => acc + sense.size, 0);
+// const genome = apply(
+// s => mut_genome_insert(
+// ),
+// );
+//}
diff --git a/src/simulation/game.test.js b/src/simulation/game.test.js
index ac65618..219dce6 100644
--- a/src/simulation/game.test.js
+++ b/src/simulation/game.test.js
@@ -1,6 +1,7 @@
'use strict';
-import { setup_board } from './game.js';
+import { apply } from '../util.js';
+import { setup_board, create_world } from './game.js';
test("set up boards correctly", () => {
@@ -31,3 +32,28 @@ test("set up boards correctly", () => {
[ W, W, W, W, W, W, W, W, W, ],
]);
});
+
+
+test("creating a world works correctly", () => {
+ const id = 0;
+ const agent = (id) => ({ id, net: `${id}`, state: `s${id}` });
+ const team1 = [agent(0), agent(1)];
+ const team2 = [agent(2), agent(3)];
+ const team3 = [agent(4), agent(5)];
+ const team4 = [agent(6), agent(7)];
+
+ const world = create_world(6, [team1, team2, team3, team4]);
+ const agent_cell = (agent) => {
+ const { x, y } = agent;
+ return world.lattice[y][x];
+ };
+
+ expect(agent_cell(world.agents[0])).toEqual({ type: 'empty', flags: { team: 0 } });
+ expect(agent_cell(world.agents[1])).toEqual({ type: 'empty', flags: { team: 0 } });
+ expect(agent_cell(world.agents[2])).toEqual({ type: 'empty', flags: { team: 1 } });
+ expect(agent_cell(world.agents[3])).toEqual({ type: 'empty', flags: { team: 1 } });
+ expect(agent_cell(world.agents[4])).toEqual({ type: 'empty', flags: { team: 2 } });
+ expect(agent_cell(world.agents[5])).toEqual({ type: 'empty', flags: { team: 2 } });
+ expect(agent_cell(world.agents[6])).toEqual({ type: 'empty', flags: { team: 3 } });
+ expect(agent_cell(world.agents[7])).toEqual({ type: 'empty', flags: { team: 3 } });
+});
diff --git a/src/util.js b/src/util.js
index 74233f4..ecae45e 100644
--- a/src/util.js
+++ b/src/util.js
@@ -10,7 +10,7 @@ export function create(obj, proto=Object.prototype) {
};
-export function random_choice(collection, r) {
+export function random_choice(collection, r=Math.random()) {
const idx = Math.floor(collection.length * r);
return collection[idx];
}
@@ -41,3 +41,12 @@ export function deepEqual(a, b, debug=false) {
return a === b;
}
}
+
+
+export function apply(f, n, x0) {
+ if (n == 0) {
+ return x0;
+ } else {
+ return f(apply(f, n-1, x0));
+ }
+}