awacke1 commited on
Commit
9d551a5
·
verified ·
1 Parent(s): 42db519

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +77 -35
index.html CHANGED
@@ -53,6 +53,7 @@
53
  };
54
  let resources = {};
55
  let counts = {};
 
56
 
57
  function initializeEntities() {
58
  Object.keys(entities).forEach(category => {
@@ -68,9 +69,9 @@
68
  for (let col = 0; col < cols; col++) {
69
  let x = col * offsetX + 150;
70
  let y = row * hexHeight + (col % 2 ? offsetY : 0) + 50;
71
- hexGrid.push({ x, y, type: "empty", entities: [], pollinated: false });
72
  }
73
- }
74
  }
75
 
76
  function selectEntity(entity) {
@@ -118,24 +119,6 @@
118
  return neighbors;
119
  }
120
 
121
- function pollinate() {
122
- hexGrid.forEach((hex, index) => {
123
- if (hex.entities.some(e => entities.insects.includes(e))) {
124
- hex.entities.forEach(entity => {
125
- if (entities.plants.includes(entity)) {
126
- getNeighbors(index).forEach(n => {
127
- if (hexGrid[n].type === "empty" && Math.random() < 0.1) {
128
- hexGrid[n].entities.push(entity);
129
- hexGrid[n].type = "occupied";
130
- counts[entity]++;
131
- }
132
- });
133
- }
134
- });
135
- }
136
- });
137
- }
138
-
139
  function aStar(startIdx, goalIdx) {
140
  const openSet = new Set([startIdx]);
141
  const cameFrom = {};
@@ -175,30 +158,89 @@
175
  return path;
176
  }
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  function moveAnimals() {
179
  hexGrid.forEach((hex, index) => {
 
180
  hex.entities.forEach((entity, i) => {
181
  if (entities.animals.includes(entity)) {
182
- const neighbors = getNeighbors(index);
183
- const mateIdx = neighbors.find(n => hexGrid[n].entities.includes(entity));
184
- if (mateIdx && Math.random() < 0.05) {
185
- const emptyNeighbor = neighbors.find(n => hexGrid[n].type === "empty");
186
- if (emptyNeighbor) {
187
- hexGrid[emptyNeighbor].entities.push(entity);
188
- hexGrid[emptyNeighbor].type = "occupied";
189
- counts[entity]++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  }
191
- } else if (Math.random() < 0.2) {
192
- const path = aStar(index, mateIdx || neighbors[Math.floor(Math.random() * neighbors.length)]);
193
- if (path.length > 1) {
194
- hexGrid[path[1]].entities.push(entity);
195
- hex.entities.splice(i, 1);
196
- if (hex.entities.length === 0) hex.type = "empty";
197
- hexGrid[path[1]].type = "occupied";
 
 
 
 
 
 
 
 
 
 
198
  }
199
  }
200
  }
201
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  });
203
  }
204
 
 
53
  };
54
  let resources = {};
55
  let counts = {};
56
+ let animalPairs = new Map(); // Tracks paired animals
57
 
58
  function initializeEntities() {
59
  Object.keys(entities).forEach(category => {
 
69
  for (let col = 0; col < cols; col++) {
70
  let x = col * offsetX + 150;
71
  let y = row * hexHeight + (col % 2 ? offsetY : 0) + 50;
72
+ hexGrid.push({ x, y, type: "empty", entities: [], nourishment: 0 });
73
  }
74
+ });
75
  }
76
 
77
  function selectEntity(entity) {
 
119
  return neighbors;
120
  }
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  function aStar(startIdx, goalIdx) {
123
  const openSet = new Set([startIdx]);
124
  const cameFrom = {};
 
158
  return path;
159
  }
160
 
161
+ function pollinate() {
162
+ hexGrid.forEach((hex, index) => {
163
+ if (hex.entities.some(e => entities.insects.includes(e))) {
164
+ hex.entities.forEach(entity => {
165
+ if (entities.plants.includes(entity) && Math.random() < 0.2) {
166
+ hex.entities.push(entity); // Spawn plant baby in same hex
167
+ counts[entity]++;
168
+ }
169
+ });
170
+ }
171
+ });
172
+ }
173
+
174
  function moveAnimals() {
175
  hexGrid.forEach((hex, index) => {
176
+ let animalsToMove = [];
177
  hex.entities.forEach((entity, i) => {
178
  if (entities.animals.includes(entity)) {
179
+ let nourishment = hex.nourishment;
180
+ const hasPlants = hex.entities.some(e => entities.plants.includes(e));
181
+ const hasInsects = hex.entities.some(e => entities.insects.includes(e));
182
+
183
+ // Eat plants or insects for nourishment
184
+ if (hasPlants && Math.random() < 0.3) {
185
+ const plantIdx = hex.entities.findIndex(e => entities.plants.includes(e));
186
+ hex.entities.splice(plantIdx, 1);
187
+ nourishment += 2;
188
+ counts[hex.entities[plantIdx]]--;
189
+ } else if (hasInsects && Math.random() < 0.5) {
190
+ const insectIdx = hex.entities.findIndex(e => entities.insects.includes(e));
191
+ hex.entities.splice(insectIdx, 1);
192
+ nourishment += 1;
193
+ counts[hex.entities[insectIdx]]--;
194
+ }
195
+
196
+ // Pair bonding and reproduction
197
+ const mateCount = hex.entities.filter(e => e === entity).length;
198
+ if (mateCount >= 2 && nourishment >= 2 && Math.random() < 0.1) {
199
+ hex.entities.push(entity); // Spawn baby in same hex
200
+ counts[entity]++;
201
+ nourishment -= 2; // Cost of reproduction
202
+ if (!animalPairs.has(entity + index)) {
203
+ animalPairs.set(entity + index, true); // Mark as paired
204
  }
205
+ }
206
+
207
+ // Movement logic
208
+ const neighbors = getNeighbors(index);
209
+ let goalIdx = neighbors.find(n => hexGrid[n].entities.some(e => entities.plants.includes(e)));
210
+ if (!goalIdx) goalIdx = neighbors.find(n => hexGrid[n].entities.some(e => entities.insects.includes(e)));
211
+ if (!goalIdx) goalIdx = neighbors[Math.floor(Math.random() * neighbors.length)];
212
+
213
+ const path = aStar(index, goalIdx);
214
+ if (path.length > 1) {
215
+ animalsToMove.push({ entity, fromIdx: index, toIdx: path[1], nourishment });
216
+ if (animalPairs.has(entity + index)) {
217
+ // Move pair together
218
+ const pairIdx = hex.entities.indexOf(entity, i + 1);
219
+ if (pairIdx !== -1) {
220
+ animalsToMove.push({ entity, fromIdx: index, toIdx: path[1], nourishment });
221
+ }
222
  }
223
  }
224
  }
225
  });
226
+
227
+ // Process animal movement
228
+ animalsToMove.forEach(move => {
229
+ const fromHex = hexGrid[move.fromIdx];
230
+ const toHex = hexGrid[move.toIdx];
231
+ const entityIdx = fromHex.entities.indexOf(move.entity);
232
+ if (entityIdx !== -1) {
233
+ fromHex.entities.splice(entityIdx, 1);
234
+ toHex.entities.push(move.entity);
235
+ toHex.nourishment += move.nourishment;
236
+ if (fromHex.entities.length === 0) fromHex.type = "empty";
237
+ toHex.type = "occupied";
238
+ if (animalPairs.has(move.entity + move.fromIdx)) {
239
+ animalPairs.delete(move.entity + move.fromIdx);
240
+ animalPairs.set(move.entity + move.toIdx, true);
241
+ }
242
+ }
243
+ });
244
  });
245
  }
246