Skip to content

Commit

Permalink
Preliminary API for 'new' brains AI system for mobs
Browse files Browse the repository at this point in the history
Would fix PaperMC#10443 if done.

This is a rough proposal for abstracting over the brains AI system for mobs
for plugins to make use of, similar to the Goals API. I don't think it's feasible
to attempt to make the new AI conform to the old interfaces.

At the moment I understand the rough skeleton and arteries of the system, though I'm
still trying to piece together what does what in user-facing terms.
'get the mob to walk somewhere' has been unsuccessful so far.

As far as API stability concerns, as long as the exposed set of tasks isn't too 1:1 with what
Mojang has, then we should be fine (until the next Big Rewrite:tm: whenever that is) considering
the general stability of the system across versions since its introductions.

There are still some unanswered questions in the API design:
- do events make sense to have here? I'd say no considering the amount of lambdas in the Minecraft implementation
  that make any real introspection API unfeasible.
- do we care to expose sensors/make them modifiable? It could probably be done in a followup PR to make custom
  tasks & sensors a thing, as I'm mostly concerned about exposing what exists in Vanilla at the moment.
- is the arrangement of the entrypoints to the new API sensical?

Test plugin is included in PR to ease testing of the new API until it's ready to ship.
  • Loading branch information
pontaoski committed Apr 27, 2024
1 parent f4c7d37 commit 94a6917
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Janet Blackquill <uhhadd@gmail.com>
Date: Fri, 26 Apr 2024 23:45:11 -0400
Subject: [PATCH] Add preliminary API for working with 'new' mob AI system


diff --git a/src/main/java/io/papermc/paper/entity/ai/Activities.java b/src/main/java/io/papermc/paper/entity/ai/Activities.java
new file mode 100644
index 0000000000000000000000000000000000000000..d03d81256a8ba9a07e1a6d3990f1d3654ea608d2
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/ai/Activities.java
@@ -0,0 +1,32 @@
+package io.papermc.paper.entity.ai;
+
+import org.jetbrains.annotations.NotNull;
+
+public interface Activities {
+ public @NotNull Activity getAdmireItem();
+ public @NotNull Activity getAvoid();
+ public @NotNull Activity getCelebrate();
+ public @NotNull Activity getCore();
+ public @NotNull Activity getDig();
+ public @NotNull Activity getEmerge();
+ public @NotNull Activity getFight();
+ public @NotNull Activity getHide();
+ public @NotNull Activity getIdle();
+ public @NotNull Activity getInvestigate();
+ public @NotNull Activity getLaySpawn();
+ public @NotNull Activity getLongJump();
+ public @NotNull Activity getMeet();
+ public @NotNull Activity getPanic();
+ public @NotNull Activity getPlay();
+ public @NotNull Activity getPlayDead();
+ public @NotNull Activity getPreRaid();
+ public @NotNull Activity getRaid();
+ public @NotNull Activity getRam();
+ public @NotNull Activity getRest();
+ public @NotNull Activity getRide();
+ public @NotNull Activity getRoar();
+ public @NotNull Activity getSniff();
+ public @NotNull Activity getSwim();
+ public @NotNull Activity getTongue();
+ public @NotNull Activity getWork();
+}
diff --git a/src/main/java/io/papermc/paper/entity/ai/Activity.java b/src/main/java/io/papermc/paper/entity/ai/Activity.java
new file mode 100644
index 0000000000000000000000000000000000000000..5ceec10a4bdae2155b7d793ed01ea316cb8f751e
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/ai/Activity.java
@@ -0,0 +1,7 @@
+package io.papermc.paper.entity.ai;
+
+/**
+ * An Activity is a key to a list of tasks in a Brain.
+ */
+public interface Activity {
+}
diff --git a/src/main/java/io/papermc/paper/entity/ai/Brain.java b/src/main/java/io/papermc/paper/entity/ai/Brain.java
new file mode 100644
index 0000000000000000000000000000000000000000..b359ece13fece6d89b1a1cbf55a78af8152a93de
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/ai/Brain.java
@@ -0,0 +1,49 @@
+package io.papermc.paper.entity.ai;
+
+import java.util.List;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.entity.Entity;
+import org.bukkit.Location;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A brain is the central AI for some modern Minecraft entities such as
+ * villagers and sniffers.
+ */
+public interface Brain<E extends LivingEntity> {
+ /**
+ * If the tasks in the given `activity` can be run, the brain will switch to
+ * the given `activity`.
+ */
+ public void useActivityIfPossible(@NotNull Activity activity);
+ /**
+ * Makes the brain switch to the default activity.
+ */
+ public void useDefaultActivity();
+ /**
+ * Sets the tasks for the given `activity` to the provided list of `tasks`.
+ */
+ ////// TODO: what is that int parameter doing
+ public void setTasksForActivity(@NotNull Activity activity, int begin, @NotNull List<Task<E>> tasks);
+ /**
+ * Clears all tasks for activities associated with this brain.
+ */
+ public void clearActivities();
+ /**
+ * Sets the default activity for this brain.
+ */
+ public void setDefaultActivity(@NotNull Activity activity);
+ /**
+ * Checks whether the given activity is active.
+ */
+ public boolean isActive(@NotNull Activity activity);
+ /**
+ * Sets the entity's current walk target to the given location.
+ */
+ public void setWalkTarget(@NotNull Location location, float speed, int completeWithinDistance);
+ /**
+ * Sets the entity's current walk target to the given entity.
+ */
+ public void setWalkTarget(@NotNull Entity entity, float speed, int completeWithinDistance);
+}
+
diff --git a/src/main/java/io/papermc/paper/entity/ai/Task.java b/src/main/java/io/papermc/paper/entity/ai/Task.java
new file mode 100644
index 0000000000000000000000000000000000000000..8ee58cbb3a520a473e28e48e378c8a9fcb7d24eb
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/ai/Task.java
@@ -0,0 +1,10 @@
+package io.papermc.paper.entity.ai;
+
+import org.bukkit.entity.LivingEntity;
+
+/**
+ * A task can be associated with an Activity in a Brain in order
+ * to instruct the entity to do something.
+ */
+public interface Task<E extends LivingEntity> {
+}
\ No newline at end of file
diff --git a/src/main/java/io/papermc/paper/entity/ai/Tasks.java b/src/main/java/io/papermc/paper/entity/ai/Tasks.java
new file mode 100644
index 0000000000000000000000000000000000000000..6edea608966a88f1b16d3fb96daf0de52498bc6a
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/ai/Tasks.java
@@ -0,0 +1,21 @@
+package io.papermc.paper.entity.ai;
+
+import org.bukkit.Location;
+import org.bukkit.entity.Mob;
+import org.bukkit.entity.memory.MemoryKey;
+import org.jetbrains.annotations.NotNull;
+
+public interface Tasks {
+ /**
+ * Instructs the entity to get within N blocks of the block location.
+ */
+ public <Entity extends Mob> @NotNull Task<Entity> walkToWalkTarget(int minRunTime, int maxRunTime);
+ /**
+ * Instructs the entity to swim if it is in water.
+ */
+ public <Entity extends Mob> @NotNull Task<Entity> swimIfInWater(float chance);
+ /**
+ * Instructs the entity to panic if it is hit, freezing, or on fire.
+ */
+ public <Entity extends Mob> @NotNull Task<Entity> panicOnDamage(float speed);
+}
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 4ff1b38eb65f97344257204cf018f176f247ed36..8fed012655bd568a38e572f6382e8d5b5092d2cc 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2518,4 +2518,14 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
*/
boolean isOwnedByCurrentRegion(@NotNull Entity entity);
// Paper end - Folia region threading API
+ // Paper start - activities and tasks API
+ @NotNull
+ io.papermc.paper.entity.ai.Tasks getTasks();
+
+ @NotNull
+ io.papermc.paper.entity.ai.Activities getActivities();
+
+ @NotNull
+ <E extends org.bukkit.entity.LivingEntity> io.papermc.paper.entity.ai.Brain<E> getBrain(@NotNull E entity);
+ // Paper end
}

0 comments on commit 94a6917

Please sign in to comment.