Skip to content

Commit

Permalink
Implement Spider
Browse files Browse the repository at this point in the history
And some other changes
  • Loading branch information
IvanCraft623 committed Apr 5, 2024
1 parent a621a17 commit 73abd98
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 87 deletions.
5 changes: 5 additions & 0 deletions src/IvanCraft623/MobPlugin/MobPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use IvanCraft623\MobPlugin\entity\monster\Enderman;
use IvanCraft623\MobPlugin\entity\monster\Endermite;
use IvanCraft623\MobPlugin\entity\monster\Slime;
use IvanCraft623\MobPlugin\entity\monster\Spider;
use IvanCraft623\MobPlugin\item\ExtraItemRegisterHelper;

use pocketmine\entity\AttributeFactory;
Expand Down Expand Up @@ -119,5 +120,9 @@ private function registerEntities() : void{
$factory->register(Enderman::class, function(World $world, CompoundTag $nbt) : Enderman{
return new Enderman(Helper::parseLocation($nbt, $world), $nbt);
}, ['minecraft:enderman', 'Enderman']);

$factory->register(Spider::class, function(World $world, CompoundTag $nbt) : Spider{
return new Spider(Helper::parseLocation($nbt, $world), $nbt);
}, ['minecraft:spider', 'Spider']);
}
}
13 changes: 7 additions & 6 deletions src/IvanCraft623/MobPlugin/entity/Living.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use IvanCraft623\MobPlugin\entity\ai\Brain;
use IvanCraft623\MobPlugin\inventory\MobInventory;
use IvanCraft623\MobPlugin\MobPlugin;
use IvanCraft623\MobPlugin\utils\Utils;

use pocketmine\block\Block;
use pocketmine\block\Liquid;
Expand All @@ -38,24 +39,20 @@
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\inventory\CallbackInventoryListener;
use pocketmine\inventory\Inventory;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\protocol\MobEquipmentPacket;
use pocketmine\network\mcpe\protocol\types\inventory\ContainerIds;
use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper;
use pocketmine\player\Player;
use pocketmine\Server;
use pocketmine\utils\Random;
use function array_filter;
use function array_key_exists;
use function array_merge;
use function array_values;
use pocketmine\world\Position;
use function count;
use function floor;
use function min;

Expand Down Expand Up @@ -399,4 +396,8 @@ protected function checkBlockIntersections() : void{
public function onInsideBlock(Block $block) : bool{
return false;
}

public function getLightLevelDependentMagicValue() : float{
return Utils::getLightLevelDependentMagicValue(Position::fromObject($this->getEyePos(), $this->location->world));
}
}
14 changes: 13 additions & 1 deletion src/IvanCraft623/MobPlugin/entity/Mob.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
use pocketmine\world\sound\ItemBreakSound;
use pocketmine\world\World;
use function assert;
use function count;
use function lcg_value;
use function max;

abstract class Mob extends Living {
Expand Down Expand Up @@ -144,7 +146,7 @@ protected function addAttributes() : void{
}

public function createNavigation() : PathNavigation{
return new GroundPathNavigation($this, $this->getWorld());
return new GroundPathNavigation($this);
}

public function getLookControl() : LookControl {
Expand Down Expand Up @@ -380,9 +382,19 @@ public function tickAi() : void{
public function travel(Vector3 $movementInput) : void{
// TODO: More complex movement suff :P
$motion = Utils::movementInputToMotion($movementInput, $this->location->yaw, $this->getMovementSpeed());

//Climb stuff
if ($this->isCollidedHorizontally && $this->onClimbable()) {
$motion->y = 0.2 - $this->motion->y;
}

$this->addMotion($motion->x, $motion->y, $motion->z);
}

protected function onClimbable() : bool{
return false;
}

protected function updateControlFlags() : void{
// TODO!
}
Expand Down
1 change: 0 additions & 1 deletion src/IvanCraft623/MobPlugin/entity/NeutralMobTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use pocketmine\entity\Human;
use pocketmine\entity\Living as PMLiving;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\world\World;

trait NeutralMobTrait {

Expand Down
6 changes: 3 additions & 3 deletions src/IvanCraft623/MobPlugin/entity/ai/Brain.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,17 @@ public function addActivityWithBehaviorPairs(Activity $activity, array $behavior
}

/**
* @param Behavior[] $behaviors
* @param Behavior[] $behaviors
* @param Pair<MemoryModuleType, MemoryStatus>[] $conditions
*/
public function addActivityWithConditions(Activity $activity, int $startPriority, array $behaviors, array $conditions) : void {
$this->addActivityAndRemoveMemoriesWhenStopped($activity, $this->createPriorityPairs($startPriority, $behaviors), $conditions, []);
}

/**
* @param Pair<int, Behavior>[] $behaviorPairs
* @param Pair<int, Behavior>[] $behaviorPairs
* @param Pair<MemoryModuleType, MemoryStatus>[] $conditions
* @param MemoryModuleType[] $memoryModules
* @param MemoryModuleType[] $memoryModules
*/
public function addActivityAndRemoveMemoriesWhenStopped(Activity $activity, array $behaviorPairs, array $conditions, array $memoryModules) : void {
$this->activityRequirements[$activity->id()] = $conditions;
Expand Down
86 changes: 86 additions & 0 deletions src/IvanCraft623/MobPlugin/entity/ai/goal/LeapAtTargetGoal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* __ __ _ _____ _ _
* | \/ | | | | __ \| | (_)
* | \ / | ___ | |__ | |__) | |_ _ __ _ _ _ __
* | |\/| |/ _ \| '_ \| ___/| | | | |/ _` | | '_ \
* | | | | (_) | |_) | | | | |_| | (_| | | | | |
* |_| |_|\___/|_.__/|_| |_|\__,_|\__, |_|_| |_|
* __/ |
* |___/
*
* A PocketMine-MP plugin that implements mobs AI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* @author IvanCraft623
*/

declare(strict_types=1);

namespace IvanCraft623\MobPlugin\entity\ai\goal;

use IvanCraft623\MobPlugin\entity\Mob;
use pocketmine\entity\Entity;
use pocketmine\math\Vector3;

class LeapAtTargetGoal extends Goal {

protected Entity $target;

public function __construct(
protected Mob $mob,
protected float $yMotion
) {
$this->setFlags(Goal::FLAG_JUMP, Goal::FLAG_MOVE);
}

public function canUse() : bool{
//TODO: Check there is no passanger controlling movement

if (!$this->mob->isOnGround()) {
return false;
}

$target = $this->mob->getTargetEntity();
if ($target === null) {
return false;
}

$distanceSquared = $this->mob->getLocation()->distanceSquared($target->getLocation());
if ($distanceSquared < 2 ** 2 || $distanceSquared > 4 ** 2) {
return false;
}

if ($this->mob->getRandom()->nextBoundedInt($this->reducedTickDelay(5)) === 0) {
$this->target = $target;
return true;
}

return false;
}

public function canContinueToUse() : bool{
return !$this->mob->isOnGround();
}

public function start() : void{
$position = $this->mob->getLocation();
$targetPosition = $this->target->getLocation();

$leap = new Vector3($targetPosition->x - $position->x, 0.0, $targetPosition->z - $position->z);
if ($leap->lengthSquared() > 0.0000001) {
$leap = $leap->normalize()->multiply(0.4)->addVector($this->mob->getMotion()->multiply(0.2));
}
$leap->y = $this->yMotion;

$this->mob->setMotion($leap);
}

public function stop() : void{
unset($this->target);
}
}
10 changes: 4 additions & 6 deletions src/IvanCraft623/MobPlugin/entity/ai/goal/RandomTeleportGoal.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@

namespace IvanCraft623\MobPlugin\entity\ai\goal;

use IvanCraft623\MobPlugin\entity\ai\goal\Goal;
use IvanCraft623\MobPlugin\entity\Mob;

use pocketmine\player\Player;
use pocketmine\entity\Living;
use pocketmine\block\Liquid;
use pocketmine\math\Vector3;
use pocketmine\world\Position;
use pocketmine\world\World;
use pocketmine\block\Liquid;
use function min;
use function mt_rand;

class RandomTeleportGoal extends Goal {

Expand Down Expand Up @@ -138,7 +136,7 @@ public function getRandomTeleportPosition(Position $origin, Vector3 $teleportRan
}

//TODO: Use $entity->canStandAt() instead of this, but is somehow broken
for ($extraY = 1; $extraY <= 3; $extraY++) {
for ($extraY = 1; $extraY <= 3; $extraY++) {
if (($block = $world->getBlockAt($x, $y + $extraY, $z))->isSolid() || $block instanceof Liquid) {
continue 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use IvanCraft623\MobPlugin\entity\monster\Enderman;

use pocketmine\player\Player;
use pocketmine\entity\Living;

class FreezeWhenLookedAt extends Goal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@
use IvanCraft623\MobPlugin\entity\ai\goal\Goal;
use IvanCraft623\MobPlugin\entity\monster\Enderman;

use pocketmine\player\Player;
use pocketmine\entity\Living;
use pocketmine\world\World;
use pocketmine\block\BlockTypeIds;
use pocketmine\block\Block;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3;
use function floor;

class LeaveBlockGoal extends Goal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@
namespace IvanCraft623\MobPlugin\entity\ai\goal\enderman;

use IvanCraft623\MobPlugin\entity\ai\goal\target\TargetGoal;
use IvanCraft623\MobPlugin\entity\ai\goal\Goal;
use IvanCraft623\MobPlugin\entity\monster\Enderman;
use IvanCraft623\MobPlugin\entity\ai\targeting\TargetingConditions;
use IvanCraft623\MobPlugin\entity\monster\Enderman;

use pocketmine\entity\Entity;
use pocketmine\math\AxisAlignedBB;
use pocketmine\player\Player;
use pocketmine\entity\Living as PMLiving;

use Closure;
use function array_reduce;

class LookForStaringPlayerGoal extends TargetGoal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@
use IvanCraft623\MobPlugin\entity\ai\goal\Goal;
use IvanCraft623\MobPlugin\entity\monster\Enderman;

use pocketmine\player\Player;
use pocketmine\entity\Living;
use pocketmine\math\Vector3;
use pocketmine\world\World;
use pocketmine\world\BlockTransaction;
use pocketmine\block\VanillaBlocks;
use pocketmine\block\BlockTypeIds;
use pocketmine\math\AxisAlignedBB;
use pocketmine\block\VanillaBlocks;
use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace;
use function floor;

class TakeBlockGoal extends Goal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,32 @@ public function getTempMobPosition() : Vector3 {
}

public function createPathToPosition(Vector3 $position, int $maxVisitedNodes, ?float $range = null) : ?Path{
if ($this->world->getBlock($position)->getTypeId() === BlockTypeIds::AIR) {
$world = $this->getWorld();
if ($world->getBlock($position)->getTypeId() === BlockTypeIds::AIR) {
$currentPos = $position->down();

while ($currentPos->y > World::Y_MIN && $this->world->getBlock($currentPos)->getTypeId() === BlockTypeIds::AIR) {
while ($currentPos->y > World::Y_MIN && $world->getBlock($currentPos)->getTypeId() === BlockTypeIds::AIR) {
$currentPos = $currentPos->down();
}

if ($currentPos->getY() > World::Y_MIN) {
return parent::createPathToPosition($currentPos->up(), $maxVisitedNodes, $range);
}

while($currentPos->getY() < World::Y_MAX && $this->world->getBlock($currentPos)->getTypeId() === BlockTypeIds::AIR) {
while($currentPos->getY() < World::Y_MAX && $world->getBlock($currentPos)->getTypeId() === BlockTypeIds::AIR) {
$currentPos = $currentPos->up();
}

$position = $currentPos;
}

if (!$this->world->getBlock($position)->isSolid()) {
if (!$world->getBlock($position)->isSolid()) {
return parent::createPathToPosition($position, $maxVisitedNodes, $range);
}

$currentPos = $position->up();

while($currentPos->getY() < World::Y_MAX && $this->world->getBlock($currentPos)->isSolid()) {
while($currentPos->getY() < World::Y_MAX && $world->getBlock($currentPos)->isSolid()) {
$currentPos = $currentPos->up();
}

Expand All @@ -91,11 +92,12 @@ public function getSurfaceY() : int{
$mobPos = $this->mob->getPosition();
if ($this->mob->isInWater() && $this->canFloat()) {
$y = (int) $mobPos->getY();
$block = $this->world->getBlock($mobPos);
$world = $this->getWorld();
$block = $world->getBlock($mobPos);
$distDiff = 0;

while ($block instanceof Water) {
$block = $this->world->getBlockAt((int) floor($mobPos->x), ++$y, (int) floor($mobPos->z));
$block = $world->getBlockAt((int) floor($mobPos->x), ++$y, (int) floor($mobPos->z));
if (++$distDiff > 16) {
return (int) $mobPos->getY();
}
Expand All @@ -116,13 +118,14 @@ protected function trimPath() : void{

if ($this->avoidSun) {
$mobPos = $this->mob->getPosition();
if ($this->world->getRealBlockSkyLightAt((int) floor($mobPos->x), (int) floor($mobPos->y + 0.5), (int) floor($mobPos->z)) >= 15) {
$world = $this->getWorld();
if ($world->getRealBlockSkyLightAt((int) floor($mobPos->x), (int) floor($mobPos->y + 0.5), (int) floor($mobPos->z)) >= 15) {
return;
}

for($i = 0; $i < $this->path->getNodeCount(); ++$i) {
$node = $this->path->getNode($i);
if ($this->world->getRealBlockSkyLightAt($node->x(), $node->y(), $node->z()) >= 15) {
if ($world->getRealBlockSkyLightAt($node->x(), $node->y(), $node->z()) >= 15) {
$this->path->truncateNodes($i);
return;
}
Expand Down
Loading

0 comments on commit 73abd98

Please sign in to comment.