| 1 | package net.minecraft.world.entity.ai;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.ImmutableSet;
|
| 6 | import com.google.common.collect.Maps;
|
| 7 | import com.google.common.collect.Sets;
|
| 8 | import com.mojang.datafixers.util.Pair;
|
| 9 | import com.mojang.serialization.Codec;
|
| 10 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 11 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Objects;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.Map.Entry;
|
| 19 | import net.minecraft.server.level.ServerLevel;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.VisibleForDebug;
|
| 22 | import net.minecraft.world.attribute.EnvironmentAttribute;
|
| 23 | import net.minecraft.world.attribute.EnvironmentAttributeSystem;
|
| 24 | import net.minecraft.world.entity.LivingEntity;
|
| 25 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 26 | import net.minecraft.world.entity.ai.behavior.BehaviorControl;
|
| 27 | import net.minecraft.world.entity.ai.memory.ExpirableValue;
|
| 28 | import net.minecraft.world.entity.ai.memory.MemoryMap;
|
| 29 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 30 | import net.minecraft.world.entity.ai.memory.MemorySlot;
|
| 31 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 32 | import net.minecraft.world.entity.ai.sensing.Sensor;
|
| 33 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 34 | import net.minecraft.world.entity.schedule.Activity;
|
| 35 | import net.minecraft.world.phys.Vec3;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public class Brain<E extends LivingEntity> {
|
| 39 | private static final int SCHEDULE_UPDATE_DELAY = 20;
|
| 40 | private final Map<MemoryModuleType<?>, MemorySlot<?>> memories = Maps.newHashMap();
|
| 41 | private final Map<SensorType<? extends Sensor<? super E>>, Sensor<? super E>> sensors = Maps.newLinkedHashMap();
|
| 42 | private final Map<Integer, Map<Activity, Set<BehaviorControl<? super E>>>> availableBehaviorsByPriority = Maps.newTreeMap();
|
| 43 | private @Nullable EnvironmentAttribute<Activity> schedule;
|
| 44 | private final Map<Activity, Set<Pair<MemoryModuleType<?>, MemoryStatus>>> activityRequirements = Maps.newHashMap();
|
| 45 | private final Map<Activity, Set<MemoryModuleType<?>>> activityMemoriesToEraseWhenStopped = Maps.newHashMap();
|
| 46 | private Set<Activity> coreActivities = Sets.newHashSet();
|
| 47 | private final Set<Activity> activeActivities = Sets.newHashSet();
|
| 48 | private Activity defaultActivity = Activity.IDLE;
|
| 49 | private long lastScheduleUpdate = -9999L;
|
| 50 |
|
| 51 | public static <E extends LivingEntity> Brain.Provider<E> provider(final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes) {
|
| 52 | return new Brain.Provider<>(ImmutableList.of(), sensorTypes, var0 -> List.of());
|
| 53 | }
|
| 54 |
|
| 55 | public static <E extends LivingEntity> Brain.Provider<E> provider(
|
| 56 | final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes, final Brain.ActivitySupplier<E> activities
|
| 57 | ) {
|
| 58 | return new Brain.Provider<>(ImmutableList.of(), sensorTypes, activities);
|
| 59 | }
|
| 60 |
|
| 61 | @Deprecated
|
| 62 | public static <E extends LivingEntity> Brain.Provider<E> provider(
|
| 63 | final Collection<? extends MemoryModuleType<?>> memoryTypes,
|
| 64 | final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes,
|
| 65 | final Brain.ActivitySupplier<E> activities
|
| 66 | ) {
|
| 67 | return new Brain.Provider<>(memoryTypes, sensorTypes, activities);
|
| 68 | }
|
| 69 |
|
| 70 | @VisibleForTesting
|
| 71 | protected Brain(
|
| 72 | final Collection<? extends MemoryModuleType<?>> memoryTypes,
|
| 73 | final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes,
|
| 74 | final List<ActivityData<E>> activities,
|
| 75 | final MemoryMap memories,
|
| 76 | final RandomSource randomSource
|
| 77 | ) {
|
| 78 | for (MemoryModuleType<?> memoryType : memoryTypes) {
|
| 79 | this.registerMemory(memoryType);
|
| 80 | }
|
| 81 |
|
| 82 | for (SensorType<? extends Sensor<? super E>> sensorType : sensorTypes) {
|
| 83 | Sensor<? super E> newSensor = (Sensor<? super E>)sensorType.create();
|
| 84 | newSensor.randomlyDelayStart(randomSource);
|
| 85 | this.sensors.put(sensorType, newSensor);
|
| 86 |
|
| 87 | for (MemoryModuleType<?> type : newSensor.requires()) {
|
| 88 | this.registerMemory(type);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | for (ActivityData<E> activity : activities) {
|
| 93 | this.addActivity(activity.activityType(), activity.behaviorPriorityPairs(), activity.conditions(), activity.memoriesToEraseWhenStopped());
|
| 94 | }
|
| 95 |
|
| 96 | for (MemoryMap.Value<?> memory : memories) {
|
| 97 | this.setMemoryInternal(memory);
|
| 98 | }
|
| 99 |
|
| 100 | this.setCoreActivities(ImmutableSet.of(Activity.CORE));
|
| 101 | this.useDefaultActivity();
|
| 102 | }
|
| 103 |
|
| 104 | private void registerMemory(final MemoryModuleType<?> memoryType) {
|
| 105 | this.memories.putIfAbsent(memoryType, MemorySlot.create());
|
| 106 | }
|
| 107 |
|
| 108 | public Brain() {
|
| 109 | this.setCoreActivities(ImmutableSet.of(Activity.CORE));
|
| 110 | this.useDefaultActivity();
|
| 111 | }
|
| 112 |
|
| 113 | public Brain.Packed pack() {
|
| 114 | final MemoryMap.Builder builder = new MemoryMap.Builder();
|
| 115 | this.forEach(new Brain.Visitor() {
|
| 116 | @Override
|
| 117 | public <U> void acceptEmpty(final MemoryModuleType<U> type) {
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public <U> void accept(final MemoryModuleType<U> type, final U value, final long timeToLive) {
|
| 122 | if (type.canSerialize()) {
|
| 123 | builder.add(type, ExpirableValue.of(value, timeToLive));
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public <U> void accept(final MemoryModuleType<U> type, final U value) {
|
| 129 | if (type.canSerialize()) {
|
| 130 | builder.add(type, ExpirableValue.of(value));
|
| 131 | }
|
| 132 | }
|
| 133 | });
|
| 134 | return new Brain.Packed(builder.build());
|
| 135 | }
|
| 136 |
|
| 137 | private <T> @Nullable MemorySlot<T> getMemorySlotIfPresent(final MemoryModuleType<T> memoryType) {
|
| 138 | return (MemorySlot<T>)this.memories.get(memoryType);
|
| 139 | }
|
| 140 |
|
| 141 | private <T> MemorySlot<T> getMemorySlot(final MemoryModuleType<T> memoryType) {
|
| 142 | MemorySlot<T> result = this.getMemorySlotIfPresent(memoryType);
|
| 143 | if (result == null) {
|
| 144 | throw new IllegalStateException("Unregistered memory fetched: " + memoryType);
|
| 145 | } else {
|
| 146 | return result;
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | public boolean hasMemoryValue(final MemoryModuleType<?> type) {
|
| 151 | return this.checkMemory(type, MemoryStatus.VALUE_PRESENT);
|
| 152 | }
|
| 153 |
|
| 154 | public void clearMemories() {
|
| 155 | this.memories.values().forEach(MemorySlot::clear);
|
| 156 | }
|
| 157 |
|
| 158 | public <U> void eraseMemory(final MemoryModuleType<U> type) {
|
| 159 | MemorySlot<U> slot = this.getMemorySlotIfPresent(type);
|
| 160 | if (slot != null) {
|
| 161 | slot.clear();
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | public <U> void setMemory(final MemoryModuleType<U> type, final @Nullable U value) {
|
| 166 | this.setMemoryInternal(type, value);
|
| 167 | }
|
| 168 |
|
| 169 | public <U> void setMemoryWithExpiry(final MemoryModuleType<U> type, final U value, final long timeToLive) {
|
| 170 | this.setMemoryInternal(type, value, timeToLive);
|
| 171 | }
|
| 172 |
|
| 173 | public <U> void setMemory(final MemoryModuleType<U> type, final Optional<? extends U> optionalValue) {
|
| 174 | this.setMemoryInternal(type, (U)optionalValue.orElse(null));
|
| 175 | }
|
| 176 |
|
| 177 | private <U> void setMemoryInternal(final MemoryMap.Value<U> value) {
|
| 178 | ExpirableValue<U> expirableValue = value.value();
|
| 179 | if (expirableValue.timeToLive().isPresent()) {
|
| 180 | this.setMemoryInternal(value.type(), expirableValue.value(), expirableValue.timeToLive().get());
|
| 181 | } else {
|
| 182 | this.setMemoryInternal(value.type(), expirableValue.value());
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | private <U> void setMemoryInternal(final MemoryModuleType<U> type, U value, final long tileToLive) {
|
| 187 | MemorySlot<U> slot = this.getMemorySlotIfPresent(type);
|
| 188 | if (slot != null) {
|
| 189 | if (isEmptyCollection(value)) {
|
| 190 | value = null;
|
| 191 | }
|
| 192 |
|
| 193 | if (value == null) {
|
| 194 | slot.clear();
|
| 195 | } else {
|
| 196 | slot.set(value, tileToLive);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | private <U> void setMemoryInternal(final MemoryModuleType<U> type, @Nullable U value) {
|
| 202 | MemorySlot<U> slot = this.getMemorySlotIfPresent(type);
|
| 203 | if (slot != null) {
|
| 204 | if (value != null && isEmptyCollection(value)) {
|
| 205 | value = null;
|
| 206 | }
|
| 207 |
|
| 208 | if (value == null) {
|
| 209 | slot.clear();
|
| 210 | } else {
|
| 211 | slot.set(value);
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | public <U> Optional<U> getMemory(final MemoryModuleType<U> type) {
|
| 217 | return Optional.ofNullable(this.getMemorySlot(type).value());
|
| 218 | }
|
| 219 |
|
| 220 | public <U> @Nullable Optional<U> getMemoryInternal(final MemoryModuleType<U> type) {
|
| 221 | MemorySlot<U> slot = this.getMemorySlotIfPresent(type);
|
| 222 | return slot == null ? null : Optional.ofNullable(slot.value());
|
| 223 | }
|
| 224 |
|
| 225 | public <U> long getTimeUntilExpiry(final MemoryModuleType<U> type) {
|
| 226 | return this.getMemorySlot(type).timeToLive();
|
| 227 | }
|
| 228 |
|
| 229 | public void forEach(final Brain.Visitor visitor) {
|
| 230 | this.memories.forEach((memoryModuleType, slot) -> callVisitor(visitor, (MemoryModuleType<?>)memoryModuleType, (MemorySlot<?>)slot));
|
| 231 | }
|
| 232 |
|
| 233 | private static <U> void callVisitor(final Brain.Visitor visitor, final MemoryModuleType<U> memoryModuleType, final MemorySlot<?> slot) {
|
| 234 | slot.visit(memoryModuleType, visitor);
|
| 235 | }
|
| 236 |
|
| 237 | public <U> boolean isMemoryValue(final MemoryModuleType<U> memoryType, final U value) {
|
| 238 | MemorySlot<U> slot = this.getMemorySlotIfPresent(memoryType);
|
| 239 | return slot != null && Objects.equals(value, slot.value());
|
| 240 | }
|
| 241 |
|
| 242 | public boolean checkMemory(final MemoryModuleType<?> type, final MemoryStatus status) {
|
| 243 | MemorySlot<?> slot = this.getMemorySlotIfPresent(type);
|
| 244 | return slot == null
|
| 245 | ? false
|
| 246 | : status == MemoryStatus.REGISTERED || status == MemoryStatus.VALUE_PRESENT && slot.hasValue() || status == MemoryStatus.VALUE_ABSENT && !slot.hasValue();
|
| 247 | }
|
| 248 |
|
| 249 | public void setSchedule(final EnvironmentAttribute<Activity> schedule) {
|
| 250 | this.schedule = schedule;
|
| 251 | }
|
| 252 |
|
| 253 | public void setCoreActivities(final Set<Activity> activities) {
|
| 254 | this.coreActivities = activities;
|
| 255 | }
|
| 256 |
|
| 257 | @Deprecated
|
| 258 | @VisibleForDebug
|
| 259 | public Set<Activity> getActiveActivities() {
|
| 260 | return this.activeActivities;
|
| 261 | }
|
| 262 |
|
| 263 | @Deprecated
|
| 264 | @VisibleForDebug
|
| 265 | public List<BehaviorControl<? super E>> getRunningBehaviors() {
|
| 266 | List<BehaviorControl<? super E>> runningBehaviours = new ObjectArrayList<>();
|
| 267 |
|
| 268 | for (Map<Activity, Set<BehaviorControl<? super E>>> behavioursByActivities : this.availableBehaviorsByPriority.values()) {
|
| 269 | for (Set<BehaviorControl<? super E>> behaviors : behavioursByActivities.values()) {
|
| 270 | for (BehaviorControl<? super E> behavior : behaviors) {
|
| 271 | if (behavior.getStatus() == Behavior.Status.RUNNING) {
|
| 272 | runningBehaviours.add(behavior);
|
| 273 | }
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | return runningBehaviours;
|
| 279 | }
|
| 280 |
|
| 281 | public void useDefaultActivity() {
|
| 282 | this.setActiveActivity(this.defaultActivity);
|
| 283 | }
|
| 284 |
|
| 285 | public Optional<Activity> getActiveNonCoreActivity() {
|
| 286 | for (Activity activity : this.activeActivities) {
|
| 287 | if (!this.coreActivities.contains(activity)) {
|
| 288 | return Optional.of(activity);
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | return Optional.empty();
|
| 293 | }
|
| 294 |
|
| 295 | public void setActiveActivityIfPossible(final Activity activity) {
|
| 296 | if (this.activityRequirementsAreMet(activity)) {
|
| 297 | this.setActiveActivity(activity);
|
| 298 | } else {
|
| 299 | this.useDefaultActivity();
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | private void setActiveActivity(final Activity activity) {
|
| 304 | if (!this.isActive(activity)) {
|
| 305 | this.eraseMemoriesForOtherActivitesThan(activity);
|
| 306 | this.activeActivities.clear();
|
| 307 | this.activeActivities.addAll(this.coreActivities);
|
| 308 | this.activeActivities.add(activity);
|
| 309 | }
|
| 310 | }
|
| 311 |
|
| 312 | private void eraseMemoriesForOtherActivitesThan(final Activity activity) {
|
| 313 | for (Activity oldActivity : this.activeActivities) {
|
| 314 | if (oldActivity != activity) {
|
| 315 | Set<MemoryModuleType<?>> memoryModuleTypes = this.activityMemoriesToEraseWhenStopped.get(oldActivity);
|
| 316 | if (memoryModuleTypes != null) {
|
| 317 | for (MemoryModuleType<?> memoryModuleType : memoryModuleTypes) {
|
| 318 | this.eraseMemory(memoryModuleType);
|
| 319 | }
|
| 320 | }
|
| 321 | }
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | public void updateActivityFromSchedule(final EnvironmentAttributeSystem environmentAttributes, final long gameTime, final Vec3 pos) {
|
| 326 | if (gameTime - this.lastScheduleUpdate > 20L) {
|
| 327 | this.lastScheduleUpdate = gameTime;
|
| 328 | Activity scheduledActivity = this.schedule != null ? environmentAttributes.getValue(this.schedule, pos) : Activity.IDLE;
|
| 329 | if (!this.activeActivities.contains(scheduledActivity)) {
|
| 330 | this.setActiveActivityIfPossible(scheduledActivity);
|
| 331 | }
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | public void setActiveActivityToFirstValid(final List<Activity> activities) {
|
| 336 | for (Activity activity : activities) {
|
| 337 | if (this.activityRequirementsAreMet(activity)) {
|
| 338 | this.setActiveActivity(activity);
|
| 339 | break;
|
| 340 | }
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | public void setDefaultActivity(final Activity activity) {
|
| 345 | this.defaultActivity = activity;
|
| 346 | }
|
| 347 |
|
| 348 | public void addActivity(
|
| 349 | final Activity activity,
|
| 350 | final ImmutableList<? extends Pair<Integer, ? extends BehaviorControl<? super E>>> behaviorPriorityPairs,
|
| 351 | final Set<Pair<MemoryModuleType<?>, MemoryStatus>> conditions,
|
| 352 | final Set<MemoryModuleType<?>> memoriesToEraseWhenStopped
|
| 353 | ) {
|
| 354 | this.activityRequirements.put(activity, conditions);
|
| 355 | if (!memoriesToEraseWhenStopped.isEmpty()) {
|
| 356 | this.activityMemoriesToEraseWhenStopped.put(activity, memoriesToEraseWhenStopped);
|
| 357 | }
|
| 358 |
|
| 359 | for (Pair<Integer, ? extends BehaviorControl<? super E>> pair : behaviorPriorityPairs) {
|
| 360 | BehaviorControl<? super E> behavior = (BehaviorControl<? super E>)pair.getSecond();
|
| 361 |
|
| 362 | for (MemoryModuleType<?> requiredMemory : behavior.getRequiredMemories()) {
|
| 363 | this.registerMemory(requiredMemory);
|
| 364 | }
|
| 365 |
|
| 366 | this.availableBehaviorsByPriority
|
| 367 | .computeIfAbsent(pair.getFirst(), key -> Maps.newHashMap())
|
| 368 | .computeIfAbsent(activity, key -> Sets.newLinkedHashSet())
|
| 369 | .add(behavior);
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | @VisibleForTesting
|
| 374 | public void removeAllBehaviors() {
|
| 375 | this.availableBehaviorsByPriority.clear();
|
| 376 | }
|
| 377 |
|
| 378 | public boolean isActive(final Activity activity) {
|
| 379 | return this.activeActivities.contains(activity);
|
| 380 | }
|
| 381 |
|
| 382 | public void tick(final ServerLevel level, final E body) {
|
| 383 | this.forgetOutdatedMemories();
|
| 384 | this.tickSensors(level, body);
|
| 385 | this.startEachNonRunningBehavior(level, body);
|
| 386 | this.tickEachRunningBehavior(level, body);
|
| 387 | }
|
| 388 |
|
| 389 | private void tickSensors(final ServerLevel level, final E body) {
|
| 390 | for (Sensor<? super E> sensor : this.sensors.values()) {
|
| 391 | sensor.tick(level, body);
|
| 392 | }
|
| 393 | }
|
| 394 |
|
| 395 | private void forgetOutdatedMemories() {
|
| 396 | this.memories.values().forEach(MemorySlot::tick);
|
| 397 | }
|
| 398 |
|
| 399 | public void stopAll(final ServerLevel level, final E body) {
|
| 400 | long timestamp = body.level().getGameTime();
|
| 401 |
|
| 402 | for (BehaviorControl<? super E> behavior : this.getRunningBehaviors()) {
|
| 403 | behavior.doStop(level, body, timestamp);
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | private void startEachNonRunningBehavior(final ServerLevel level, final E body) {
|
| 408 | long time = level.getGameTime();
|
| 409 |
|
| 410 | for (Map<Activity, Set<BehaviorControl<? super E>>> behavioursByActivities : this.availableBehaviorsByPriority.values()) {
|
| 411 | for (Entry<Activity, Set<BehaviorControl<? super E>>> behavioursForActivity : behavioursByActivities.entrySet()) {
|
| 412 | Activity activity = behavioursForActivity.getKey();
|
| 413 | if (this.activeActivities.contains(activity)) {
|
| 414 | for (BehaviorControl<? super E> behavior : behavioursForActivity.getValue()) {
|
| 415 | if (behavior.getStatus() == Behavior.Status.STOPPED) {
|
| 416 | behavior.tryStart(level, body, time);
|
| 417 | }
|
| 418 | }
|
| 419 | }
|
| 420 | }
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | private void tickEachRunningBehavior(final ServerLevel level, final E body) {
|
| 425 | long timestamp = level.getGameTime();
|
| 426 |
|
| 427 | for (BehaviorControl<? super E> behavior : this.getRunningBehaviors()) {
|
| 428 | behavior.tickOrStop(level, body, timestamp);
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | private boolean activityRequirementsAreMet(final Activity activity) {
|
| 433 | if (!this.activityRequirements.containsKey(activity)) {
|
| 434 | return false;
|
| 435 | }
|
| 436 |
|
| 437 | for (Pair<MemoryModuleType<?>, MemoryStatus> memoryRequirement : this.activityRequirements.get(activity)) {
|
| 438 | MemoryModuleType<?> memoryType = memoryRequirement.getFirst();
|
| 439 | MemoryStatus memoryStatus = memoryRequirement.getSecond();
|
| 440 | if (!this.checkMemory(memoryType, memoryStatus)) {
|
| 441 | return false;
|
| 442 | }
|
| 443 | }
|
| 444 |
|
| 445 | return true;
|
| 446 | }
|
| 447 |
|
| 448 | private static boolean isEmptyCollection(final Object object) {
|
| 449 | return object instanceof Collection<?> collection && collection.isEmpty();
|
| 450 | }
|
| 451 |
|
| 452 | public boolean isBrainDead() {
|
| 453 | return this.memories.isEmpty() && this.sensors.isEmpty() && this.availableBehaviorsByPriority.isEmpty();
|
| 454 | }
|
| 455 |
|
| 456 | @FunctionalInterface
|
| 457 | public interface ActivitySupplier<E extends LivingEntity> {
|
| 458 | List<ActivityData<E>> createActivities(E body);
|
| 459 | }
|
| 460 |
|
| 461 | public record Packed(MemoryMap memories) {
|
| 462 | public static final Brain.Packed EMPTY = new Brain.Packed(MemoryMap.EMPTY);
|
| 463 | public static final Codec<Brain.Packed> CODEC = RecordCodecBuilder.create(
|
| 464 | i -> i.group(MemoryMap.CODEC.fieldOf("memories").forGetter(Brain.Packed::memories)).apply(i, Brain.Packed::new)
|
| 465 | );
|
| 466 | }
|
| 467 |
|
| 468 | public static final class Provider<E extends LivingEntity> {
|
| 469 | private final Collection<? extends MemoryModuleType<?>> memoryTypes;
|
| 470 | private final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes;
|
| 471 | private final Brain.ActivitySupplier<E> activities;
|
| 472 |
|
| 473 | private Provider(
|
| 474 | final Collection<? extends MemoryModuleType<?>> memoryTypes,
|
| 475 | final Collection<? extends SensorType<? extends Sensor<? super E>>> sensorTypes,
|
| 476 | final Brain.ActivitySupplier<E> activities
|
| 477 | ) {
|
| 478 | this.memoryTypes = memoryTypes;
|
| 479 | this.sensorTypes = sensorTypes;
|
| 480 | this.activities = activities;
|
| 481 | }
|
| 482 |
|
| 483 | public Brain<E> makeBrain(final E body, final Brain.Packed packed) {
|
| 484 | List<ActivityData<E>> activities = this.activities.createActivities(body);
|
| 485 | return new Brain<>(this.memoryTypes, this.sensorTypes, activities, packed.memories, body.getRandom());
|
| 486 | }
|
| 487 | }
|
| 488 |
|
| 489 | public interface Visitor {
|
| 490 | <U> void acceptEmpty(MemoryModuleType<U> type);
|
| 491 |
|
| 492 | <U> void accept(MemoryModuleType<U> type, U value);
|
| 493 |
|
| 494 | <U> void accept(MemoryModuleType<U> type, U value, long timeToLive);
|
| 495 | }
|
| 496 | }
|
| 497 |
|