-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
210 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ok-bukkit/src/main/java/org/enginehub/craftbook/mechanics/betterai/AttackPassiveGoal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* CraftBook Copyright (C) me4502 <https://matthewmiller.dev/> | ||
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/> | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program. If not, | ||
* see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.enginehub.craftbook.mechanics.betterai; | ||
|
||
import com.destroystokyo.paper.entity.ai.Goal; | ||
import com.destroystokyo.paper.entity.ai.GoalKey; | ||
import com.destroystokyo.paper.entity.ai.GoalType; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.entity.Animals; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.bukkit.entity.Monster; | ||
import org.enginehub.craftbook.bukkit.CraftBookPlugin; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.EnumSet; | ||
|
||
class AttackPassiveGoal implements Goal<Monster> { | ||
private static final GoalKey<Monster> key = GoalKey.of( | ||
Monster.class, | ||
new NamespacedKey("craftbook", "attack_passive") | ||
); | ||
|
||
private final Monster monster; | ||
private LivingEntity target; | ||
|
||
public AttackPassiveGoal(Monster monster) { | ||
this.monster = monster; | ||
} | ||
|
||
@Override | ||
public boolean shouldActivate() { | ||
return !monster.isDead(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
tick(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
this.target = null; | ||
monster.setTarget(null); | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
if (target != null && !target.isDead()) { | ||
if (target.getWorld().equals(monster.getWorld()) | ||
&& target.getLocation().distanceSquared(monster.getLocation()) < 15*15) { | ||
return; | ||
} | ||
} | ||
|
||
Animals closest = null; | ||
double closestDist = Double.MAX_VALUE; | ||
|
||
for (Entity ent : monster.getNearbyEntities(10D, 10D, 10D)) { | ||
if (ent instanceof Animals && monster.hasLineOfSight(ent)) { | ||
double dist = ent.getLocation().distanceSquared(monster.getLocation()); | ||
if (dist < closestDist) { | ||
closest = (Animals) ent; | ||
closestDist = dist; | ||
} | ||
} | ||
} | ||
|
||
if (closest != null) { | ||
target = closest; | ||
monster.setTarget(this.target); | ||
CraftBookPlugin.logDebugMessage("Setting target to entity: " + closest.getType().name(), "ai-mechanics.entity-target.attack-passive"); | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull GoalKey<Monster> getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public @NotNull EnumSet<GoalType> getTypes() { | ||
return EnumSet.of(GoalType.TARGET); | ||
} | ||
} |
Oops, something went wrong.