Spaces:
Running
Running
File size: 6,586 Bytes
a32dc8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
import { Villager } from './routines.js';
/**
* Environmental Interaction System for Villager AI
*
* This implementation handles how villagers interact with their environment,
* including resource gathering and building usage.
*/
class EnvironmentInteractionSystem {
constructor() {
this.resources = new Map(); // Map of resource IDs to resource objects
this.buildings = new Map(); // Map of building IDs to building objects
}
/**
* Add a resource to the environment
* @param {string} id - Resource identifier
* @param {Resource} resource - Resource object
*/
addResource(id, resource) {
this.resources.set(id, resource);
}
/**
* Add a building to the environment
* @param {string} id - Building identifier
* @param {Building} building - Building object
*/
addBuilding(id, building) {
this.buildings.set(id, building);
}
/**
* Find the nearest resource of a specific type
* @param {Array} position - Search position [x, y, z]
* @param {string} type - Resource type
* @returns {Resource|null} Nearest resource or null if none found
*/
findNearestResource(position, type) {
let nearest = null;
let minDistance = Infinity;
for (const [id, resource] of this.resources) {
if (resource.type === type && resource.amount > 0) {
const distance = this.calculateDistance(position, resource.position);
if (distance < minDistance) {
minDistance = distance;
nearest = resource;
}
}
}
return nearest;
}
/**
* Find the nearest building of a specific type
* @param {Array} position - Search position [x, y, z]
* @param {string} type - Building type
* @returns {Building|null} Nearest building or null if none found
*/
findNearestBuilding(position, type) {
let nearest = null;
let minDistance = Infinity;
for (const [id, building] of this.buildings) {
if (building.type === type) {
const distance = this.calculateDistance(position, building.position);
if (distance < minDistance) {
minDistance = distance;
nearest = building;
}
}
}
return nearest;
}
/**
* Calculate distance between two points
* @param {Array} pos1 - First position [x, y, z]
* @param {Array} pos2 - Second position [x, y, z]
* @returns {number} Distance between points
*/
calculateDistance(pos1, pos2) {
return Math.sqrt(
Math.pow(pos1[0] - pos2[0], 2) +
Math.pow(pos1[1] - pos2[1], 2) +
Math.pow(pos1[2] - pos2[2], 2)
);
}
/**
* Gather a resource
* @param {Villager} villager - Villager gathering the resource
* @param {Resource} resource - Resource to gather
* @returns {boolean} True if successful
*/
gatherResource(villager, resource) {
if (!resource || resource.amount <= 0) return false;
// Check if villager is close enough to gather
if (this.calculateDistance(villager.position, resource.position) > resource.gatherRadius) {
return false;
}
// Gather the resource
const amount = Math.min(resource.gatherAmount, resource.amount);
resource.amount -= amount;
villager.inventory.add(resource.type, amount);
return true;
}
/**
* Use a building
* @param {Villager} villager - Villager using the building
* @param {Building} building - Building to use
* @returns {boolean} True if successful
*/
useBuilding(villager, building) {
if (!building) return false;
// Check if villager is close enough to use
if (this.calculateDistance(villager.position, building.position) > building.useRadius) {
return false;
}
// Use the building
return building.use(villager);
}
}
/**
* Resource class
*/
class Resource {
constructor(id, type, position, amount, gatherRadius = 2, gatherAmount = 1) {
this.id = id;
this.type = type; // 'wood', 'stone', 'food', etc.
this.position = position;
this.amount = amount;
this.gatherRadius = gatherRadius;
this.gatherAmount = gatherAmount;
}
}
/**
* Building class
*/
class Building {
constructor(id, type, position, useRadius = 3) {
this.id = id;
this.type = type; // 'house', 'workshop', 'market', etc.
this.position = position;
this.useRadius = useRadius;
this.occupancy = 0;
this.maxOccupancy = 1;
}
/**
* Use the building
* @param {Villager} villager - Villager using the building
* @returns {boolean} True if successful
*/
use(villager) {
if (this.occupancy >= this.maxOccupancy) return false;
this.occupancy++;
// Simulate building usage
setTimeout(() => {
this.occupancy--;
}, 5000); // Occupied for 5 seconds
return true;
}
}
/**
* Villager inventory system
*/
class Inventory {
constructor() {
this.items = new Map(); // Map of item types to quantities
}
/**
* Add items to inventory
* @param {string} type - Item type
* @param {number} amount - Amount to add
*/
add(type, amount) {
if (this.items.has(type)) {
this.items.set(type, this.items.get(type) + amount);
} else {
this.items.set(type, amount);
}
}
/**
* Remove items from inventory
* @param {string} type - Item type
* @param {number} amount - Amount to remove
* @returns {boolean} True if successful
*/
remove(type, amount) {
if (!this.items.has(type)) return false;
const current = this.items.get(type);
if (current < amount) return false;
this.items.set(type, current - amount);
if (this.items.get(type) === 0) {
this.items.delete(type);
}
return true;
}
/**
* Get item count
* @param {string} type - Item type
* @returns {number} Item count
*/
getCount(type) {
return this.items.get(type) || 0;
}
}
// Add inventory to Villager class
Villager.prototype.inventory = new Inventory();
export { EnvironmentInteractionSystem, Resource, Building, Inventory, Villager }; |