From 8323707d3a2945f8fbd4d4e925a04d1eb6f0a3b7 Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Sat, 11 Nov 2023 14:14:30 -0600
Subject: prevent movement when frozen and allow seeing past the lattice edge

---
 src/simulation/actions.js      |  4 ++++
 src/simulation/actions.test.js | 12 ++++++++++
 src/simulation/senses.js       |  5 ++--
 src/simulation/senses.test.js  | 52 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/src/simulation/actions.js b/src/simulation/actions.js
index 78345ce..4ee17d8 100644
--- a/src/simulation/actions.js
+++ b/src/simulation/actions.js
@@ -6,6 +6,7 @@ const threshold = 0.5;
 const move_forward = {
   size: 1,
   propose: (world, agent, head) => {
+    if (agent.flags.frozen === true) { return []; }
     if (head[0] > threshold) {
       const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation];
       const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation];
@@ -26,6 +27,7 @@ const move_forward = {
 const move_backward = {
   size: 1,
   propose: (world, agent, head) => {
+    if (agent.flags.frozen === true) { return []; }
     if (head[0] > threshold) {
       const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation];
       const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation];
@@ -46,6 +48,7 @@ const move_backward = {
 const turn_left = {
   size: 1,
   propose: (world, agent, head) => {
+    if (agent.flags.frozen === true) { return []; }
     if (head[0] > threshold) {
       const orientation = { n: 'w', e: 'n', s: 'e', w: 's' }[agent.flags.orientation];
       return [{
@@ -64,6 +67,7 @@ const turn_left = {
 const turn_right = {
   size: 1,
   propose: (world, agent, head) => {
+    if (agent.flags.frozen === true) { return []; }
     if (head[0] > threshold) {
       const orientation = { n: 'e', e: 's', s: 'w', w: 'n' }[agent.flags.orientation];
       return [{
diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js
index e0b29dc..e8406bc 100644
--- a/src/simulation/actions.test.js
+++ b/src/simulation/actions.test.js
@@ -280,3 +280,15 @@ test("take flag", () => {
 
   expect(take_flag.propose(world, agent, [0])).toEqual([]);
 });
+
+
+test("frozen agents cannot move", () => {
+  const agent = { id: 0, x: 0, y: 0, flags: { orientation: 'n', frozen: true } };
+
+  const world = { agents: [agent] };
+
+  expect(move_forward.propose(world, agent, [1])).toEqual([]);
+  expect(move_backward.propose(world, agent, [1])).toEqual([]);
+  expect(turn_left.propose(world, agent, [1])).toEqual([]);
+  expect(turn_right.propose(world, agent, [1])).toEqual([]);
+});
diff --git a/src/simulation/senses.js b/src/simulation/senses.js
index d3e7c1e..9cc6c41 100644
--- a/src/simulation/senses.js
+++ b/src/simulation/senses.js
@@ -99,8 +99,9 @@ function vision_idx_to_world_pos(world, agent, idx) {
 function see_cell(world, x, y) {
   const team = 0;
   const orientation = 0;
-  if (!world.lattice[y][x]) {
-    throw new Error(`${x}, ${y}`);
+  if (!world.lattice[y] || !world.lattice[y][x]) {
+    // beyond the map edge
+    return [ 0, 0, 0 ];
   }
   const type = {
     active:   -0.8,
diff --git a/src/simulation/senses.test.js b/src/simulation/senses.test.js
index 502646e..68b1b30 100644
--- a/src/simulation/senses.test.js
+++ b/src/simulation/senses.test.js
@@ -393,3 +393,55 @@ test("see agents", () => {
     ew, _, _, _, _, _, _, _, _, _, _, _, _, _, _, dn, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
   ]);
 });
+
+
+test("seeing 'nothing' looks like teamless empty squares", () => {
+  const lattice = [[]];
+  const agent = { id: 0, x: 0, y: 0, flags: { team: 0, orientation: 'n' } };
+  const world = {
+    lattice, agents: [agent],
+  };
+
+  const _ = [
+    0.0, 0.0, 0.0,
+  ];
+  const an = [ 
+    -0.8 + 0.0625*(-10/11),   // team + identity
+    -0.8 + 0.0625*(-36/37),   // orientation + identity
+     0.0 + 0.0625*(-498/499), // class + identity
+  ];
+
+  expect(see.read(world, agent)).toEqual([
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, an, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, 
+  ]);
+});
-- 
cgit v1.2.1