the whole game

This commit is contained in:
Kolyah35 2026-03-02 22:04:18 +03:00
parent 816e9060b4
commit f0617a5d22
2069 changed files with 581500 additions and 0 deletions

View file

@ -0,0 +1,346 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI__PathNavigation_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI__PathNavigation_H__
//package net.minecraft.world.entity.ai;
#include "../Mob.h"
#include "../../level/Level.h"
#include "../../level/material/Material.h"
#include "../../level/tile/Tile.h"
#include "../../phys/Vec3.h"
#include <cmath>
#include "../../level/pathfinder/Path.h"
#include "control/MoveControl.h"
#include "../../../util/MemUtils.h"
class PathNavigation
{
public:
PathNavigation(Mob* mob, Level* level, float maxDist)
: mob(mob),
level(level),
maxDist(maxDist),
_tick(0),
avoidSun(false),
avoidWater(false),
canOpenDoors(false),
lastStuckCheckPos(0, 0, 0),
speed(0),
lastStuckCheck(0),
path(NULL),
deletePath(false)
{
}
~PathNavigation() {
deletePathIfNeeded();
}
void setSpeed(float speed) {
this->speed = speed;
}
Path* createPath(float x, float y, float z) {
//LOGI("can update? %d\n", canUpdatePath());
if (!canUpdatePath())
return NULL;
Path* p = new Path();
level->findPath(p, mob, Mth::floor(x), (int) y, Mth::floor(z), maxDist, canOpenDoors, avoidWater);
return p;
}
bool moveTo(float x, float y, float z, float speed) {
Path* newPath = createPath(x, y, z);
return moveTo(newPath, speed, true);
}
Path* createPath(Mob* target) {
//LOGI("can update? %d\n", canUpdatePath());
if (!canUpdatePath())
return NULL;
Path* p = new Path();
level->findPath(p, mob, target, maxDist, canOpenDoors, avoidWater);
return p;
}
bool moveTo(Mob* target, float speed) {
Path* newPath = createPath(target);
//if (!newPath->isEmpty()) return moveTo(newPath, speed);
if (newPath) return moveTo(newPath, speed, true);
else return false;
}
bool moveTo(Path* newPath, float speed, bool navIsPathOwner) {
if (newPath == NULL) {
deletePathIfNeeded();
return false;
}
//if (newPath.isEmpty()) {
// path.destroy();
// return false;
//}
Node* last = newPath->last();
if (last) {
//LOGI("> %d, %d, %d in %d steps\n", last->x, last->y, last->z, newPath->getSize());
}
/*if (!newPath->sameAs(path))*/ {
// Delete old path if we are owner
deletePathIfNeeded();
path = newPath;
deletePath = navIsPathOwner;
}
if (avoidSun) trimPathFromSun();
if (path->getSize() == 0) return false;
this->speed = speed;
Vec3 mobPos = getTempMobPos();
lastStuckCheck = _tick;
lastStuckCheckPos.x = mobPos.x;
lastStuckCheckPos.y = mobPos.y;
lastStuckCheckPos.z = mobPos.z;
return true;
}
//Ref<Path>* getPath() {
Path* getPath() {
return path;
}
void tick() {
++_tick;
//LOGI("hehe %d\n", _tick);
if (isDone()) return;
//LOGI("hehe2 %d %d\n", _tick, canUpdatePath());
if (canUpdatePath()) updatePath();
//LOGI("hehe3 %d\n", _tick);
if (isDone()) {
//LOGI("done!\n");
return;
}
Vec3 target = path->currentPos(mob);
//LOGI("hehe4 %d\n", _tick);
mob->getMoveControl()->setWantedPosition(target.x, target.y, target.z, speed);
}
bool isDone() {
return !path || path->isDone();// path == NULL || path.isDone();
}
void stop() {
deletePathIfNeeded();
//if (path) {
// path->destroy(); //@?
// path = NULL;
//}
}
private:
void deletePathIfNeeded() {
//return;
if (deletePath && path) {
LOGI("nav-deleting %p (%d)\n", path, path->id);
delete path;
//deletePath = false;
}
path = NULL;
}
void updatePath() {
Vec3 mobPos = getTempMobPos();
Path& path = *this->path;
// find first elevations in path
int firstElevation = path.getSize();
for (int i = path.getIndex(); i < path.getSize(); ++i)
if (path.get(i)->y != (int) mobPos.y) {
firstElevation = i;
break;
}
// remove those within way point radius (this is not optimal, should
// check canWalkDirectly also) possibly only check next as well
float waypointRadiusSqr = mob->bbWidth * mob->bbWidth;
for (int i = path.getIndex(); i < firstElevation; ++i) {
if (mobPos.distanceToSqr(path.getPos(mob, i)) < waypointRadiusSqr) {
path.setIndex(i + 1);
}
}
// smooth remaining on same elevation
int sx = (int)ceil(mob->bbWidth);
int sy = (int) mob->bbHeight + 1;
int sz = sx;
for (int i = firstElevation - 1; i >= path.getIndex(); --i) {
if (canMoveDirectly(mobPos, path.getPos(mob, i), sx, sy, sz)) {
path.setIndex(i);
break;
}
}
// stuck detection (probably pushed off path)
if (_tick - lastStuckCheck > 100) {
if (mobPos.distanceToSqr(lastStuckCheckPos) < 1.5 * 1.5) stop();
lastStuckCheck = _tick;
lastStuckCheckPos.x = mobPos.x;
lastStuckCheckPos.y = mobPos.y;
lastStuckCheckPos.z = mobPos.z;
}
}
Vec3 getTempMobPos() {
return Vec3(mob->x, (float)getSurfaceY(), mob->z);
}
int getSurfaceY() {
if (!mob->isInWater()) return (int) (mob->bb.y0 + 0.5);
int surface = (int) (mob->bb.y0);
int tileId = level->getTile(Mth::floor(mob->x), surface, Mth::floor(mob->z));
int steps = 0;
while (tileId == Tile::water->id || tileId == Tile::calmWater->id) {
++surface;
tileId = level->getTile(Mth::floor(mob->x), surface, Mth::floor(mob->z));
if (++steps > 16) return (int) (mob->bb.y0);
}
return surface;
}
bool canUpdatePath() {
return mob->onGround || isInLiquid();
}
bool isInLiquid() {
return mob->isInWater() || mob->isInLava();
}
void trimPathFromSun() {
if (level->canSeeSky(Mth::floor(mob->x), (int) (mob->bb.y0 + 0.5), Mth::floor(mob->z)))
return;
Path& path = *this->path;
for (int i = 0; i < path.getSize(); ++i) {
Node* n = path.get(i);
if (level->canSeeSky(n->x, n->y, n->z)) {
path.setSize(i - 1);
return;
}
}
}
bool canMoveDirectly(const Vec3& startPos, const Vec3& stopPos, int sx, int sy, int sz) {
int gridPosX = Mth::floor(startPos.x);
int gridPosZ = Mth::floor(startPos.z);
float dirX = stopPos.x - startPos.x;
float dirZ = stopPos.z - startPos.z;
float distSqr = dirX * dirX + dirZ * dirZ;
if (distSqr < 0.00001f) return false;
float nf = Mth::invSqrt(distSqr);
dirX *= nf;
dirZ *= nf;
sx += 2;
sz += 2;
if (!canWalkOn(gridPosX, (int) startPos.y, gridPosZ, sx, sy, sz, startPos, dirX, dirZ)) return false;
sx -= 2;
sz -= 2;
float deltaX = 1 / Mth::abs(dirX);
float deltaZ = 1 / Mth::abs(dirZ);
float maxX = gridPosX * 1 - startPos.x;
float maxZ = gridPosZ * 1 - startPos.z;
if (dirX >= 0) maxX += 1;
if (dirZ >= 0) maxZ += 1;
maxX /= dirX;
maxZ /= dirZ;
int stepX = dirX < 0 ? -1 : 1;
int stepZ = dirZ < 0 ? -1 : 1;
int gridGoalX = Mth::floor(stopPos.x);
int gridGoalZ = Mth::floor(stopPos.z);
int currentDirX = gridGoalX - gridPosX;
int currentDirZ = gridGoalZ - gridPosZ;
while (currentDirX * stepX > 0 || currentDirZ * stepZ > 0) {
if (maxX < maxZ) {
maxX += deltaX;
gridPosX += stepX;
currentDirX = gridGoalX - gridPosX;
} else {
maxZ += deltaZ;
gridPosZ += stepZ;
currentDirZ = gridGoalZ - gridPosZ;
}
if (!canWalkOn(gridPosX, (int) startPos.y, gridPosZ, sx, sy, sz, startPos, dirX, dirZ)) return false;
}
return true;
}
bool canWalkOn(int x, int y, int z, int sx, int sy, int sz, const Vec3& startPos, float goalDirX, float goalDirZ) {
int startX = x - sx / 2;
int startZ = z - sz / 2;
if (!canWalkAbove(startX, y, startZ, sx, sy, sz, startPos, goalDirX, goalDirZ)) return false;
// lava or water or air under
for (int xx = startX; xx < startX + sx; xx++) {
for (int zz = startZ; zz < startZ + sz; zz++) {
float dirX = xx + 0.5f - startPos.x;
float dirZ = zz + 0.5f - startPos.z;
if (dirX * goalDirX + dirZ * goalDirZ < 0) continue;
int tile = level->getTile(xx, y - 1, zz);
if (tile <= 0) return false;
const Material* m = Tile::tiles[tile]->material;
if (m == Material::water && !mob->isInWater()) return false;
if (m == Material::lava) return false;
}
}
return true;
}
bool canWalkAbove(int startX, int startY, int startZ, int sx, int sy, int sz, const Vec3& startPos, float goalDirX, float goalDirZ) {
for (int xx = startX; xx < startX + sx; xx++) {
for (int yy = startY; yy < startY + sy; yy++) {
for (int zz = startZ; zz < startZ + sz; zz++) {
float dirX = xx + 0.5f - startPos.x;
float dirZ = zz + 0.5f - startPos.z;
if (dirX * goalDirX + dirZ * goalDirZ < 0) continue;
int tile = level->getTile(xx, yy, zz);
if (tile <= 0) continue;
if (Tile::tiles[tile]->material->blocksMotion()) return false;
//if (!Tile::tiles[tile]->isPathfindable(level, xx, yy, zz)) return false; //@todo
}
}
}
return true;
}
private:
Mob* mob;
Level* level;
Path* path;
bool deletePath;
Ref<Path>* refPath;
float speed;
float maxDist;
int _tick;
int lastStuckCheck;
Vec3 lastStuckCheckPos;
public:
bool avoidWater;
bool avoidSun;
bool canOpenDoors;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI__PathNavigation_H__*/

38
src/world/entity/ai/Sensing.h Executable file
View file

@ -0,0 +1,38 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_SENSING__Sensing_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_SENSING__Sensing_H__
//package net.minecraft.world.entity.ai.sensing;
#include <set>
class Sensing {
typedef std::set<Entity*> EntitySet;
public:
Sensing(Mob* mob)
: mob(mob)
{}
void tick() {
seen.clear();
unseen.clear();
}
bool canSee(Entity* target) {
EntitySet::const_iterator cSeen = seen.find(target);
if (cSeen != seen.end()) return true;
EntitySet::const_iterator cUnseen = unseen.find(target);
if (cUnseen != unseen.end()) return false;
bool canSee = mob->canSee(target);
if (canSee) seen.insert(target);
else unseen.insert(target);
return canSee;
}
private:
Mob* mob;
EntitySet seen;
EntitySet unseen;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_SENSING__Sensing_H__*/

View file

@ -0,0 +1,13 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__Control_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__Control_H__
//package net.minecraft.world.entity->ai.control;
class Control {
public:
static const int MoveControlFlag = 1;
static const int LookControlFlag = 2;
static const int JumpControlFlag = 4;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__Control_H__*/

View file

@ -0,0 +1,31 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__JumpControl_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__JumpControl_H__
//package net.minecraft.world.entity.ai.control;
#include "Control.h"
#include "../../Mob.h"
class JumpControl: public Control
{
public:
JumpControl(Mob* mob)
: mob(mob),
_jump(false)
{
}
void jump() {
_jump = true;
}
void tick() {
mob->setJumping(_jump);
_jump = false;
}
private:
Mob* mob;
bool _jump;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__JumpControl_H__*/

View file

@ -0,0 +1,70 @@
#include "MoveControl.h"
#include "JumpControl.h"
const float MoveControl::MAX_TURN = 30;
const float MoveControl::MIN_SPEED = 0.0005f;
const float MoveControl::MIN_SPEED_SQR = MIN_SPEED * MIN_SPEED;
MoveControl::MoveControl( Mob* mob )
: mob(mob),
wantedX(mob->x),
wantedY(mob->y),
wantedZ(mob->z),
_hasWanted(false)
{
}
bool MoveControl::hasWanted() {
return _hasWanted;
}
float MoveControl::getSpeed() {
return speed;
}
void MoveControl::setWantedPosition( float x, float y, float z, float speed ) {
//LOGI("> %f, %f, %f\n", x, y, z);
wantedX = x;
wantedY = y;
wantedZ = z;
this->speed = speed;
_hasWanted = true;
}
void MoveControl::tick() {
mob->setYya(0);
if (!_hasWanted) return;
_hasWanted = false;
int yFloor = Mth::floor(mob->bb.y0 + 0.5f);
float xd = wantedX - mob->x;
float zd = wantedZ - mob->z;
float yd = wantedY - yFloor;
float dd = xd * xd + yd * yd + zd * zd;
if (dd < MIN_SPEED_SQR) return;
float yRotD = (float) (std::atan2(zd, xd) * 180 / Mth::PI) - 90;
mob->yRot = rotlerp(mob->yRot, yRotD, MAX_TURN);
mob->setSpeed(speed);
if (yd > 0 && xd * xd + zd * zd < 1) mob->getJumpControl()->jump();
}
float MoveControl::rotlerp( float a, float b, float max ) {
float diff = b - a;
while (diff < -180)
diff += 360;
while (diff >= 180)
diff -= 360;
if (diff > max) {
diff = max;
}
if (diff < -max) {
diff = -max;
}
return a + diff;
}

View file

@ -0,0 +1,38 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__MoveControl_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__MoveControl_H__
//package net.minecraft.world.entity.ai.control;
#include "Control.h"
#include "../../Mob.h"
#include "../../../../util/Mth.h"
class MoveControl: public Control
{
static const float MAX_TURN;
public:
static const float MIN_SPEED;
static const float MIN_SPEED_SQR;
MoveControl(Mob* mob);
bool hasWanted();
float getSpeed();
void setWantedPosition(float x, float y, float z, float speed);
void tick();
private:
float rotlerp(float a, float b, float max);
Mob* mob;
float wantedX;
float wantedY;
float wantedZ;
float speed;
bool _hasWanted;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_CONTROL__MoveControl_H__*/

View file

@ -0,0 +1,57 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__BreakDoorGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__BreakDoorGoal_H__
//package net.minecraft.world.entity->ai.goal;
#include "DoorInteractGoal.h"
#include "../../Entity.h"
#include "../../../level/tile/LevelEvent.h"
#include "../../../level/Level.h"
#include "../../../level/tile/DoorTile.h"
#include "../../../../SharedConstants.h"
class BreakDoorGoal: public DoorInteractGoal
{
typedef DoorInteractGoal super;
public:
BreakDoorGoal(Monster* mob)
: super(mob)
{}
bool canUse() {
if (!super::canUse()) return false;
return !doorTile->isOpen(mob->level, doorX, doorY, doorZ);
}
void start() {
super::start();
breakTime = SharedConstants::TicksPerSecond * 12;
}
bool canContinueToUse() {
float d = mob->distanceToSqr((float)doorX, (float)doorY, (float)doorZ);
return breakTime >= 0 && !doorTile->isOpen(mob->level, doorX, doorY, doorZ) && d < 2 * 2;
}
void tick() {
super::tick();
if (mob->random.nextInt(20) == 0) {
//mob->level->levelEvent(LevelEvent::SOUND_ZOMBIE_WOODEN_DOOR, doorX, doorY, doorZ, 0);
}
//LOGI("time: %d\n", breakTime);
if (--breakTime == 0) {
/*if (mob->level->difficulty == Difficulty.HARD)*/ {
mob->level->setTile(doorX, doorY, doorZ, 0);
//mob->level->levelEvent(LevelEvent::SOUND_ZOMBIE_DOOR_CRASH, doorX, doorY, doorZ, 0);
//mob->level->levelEvent(LevelEvent::PARTICLES_DESTROY_BLOCK, doorX, doorY, doorZ, doorTile->id);
}
}
}
private:
int breakTime;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__BreakDoorGoal_H__*/

View file

@ -0,0 +1,79 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__DoorInteractGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__DoorInteractGoal_H__
//package net.minecraft.world.entity.ai.goal;
#include "Goal.h"
#include "../PathNavigation.h"
#include "../../monster/Monster.h"
#include "../../../level/pathfinder/Path.h"
#include "../../../level/tile/DoorTile.h"
class DoorInteractGoal: public Goal
{
public:
DoorInteractGoal(Monster* mob)
: mob(mob)
{}
bool canUse() {
if (!mob->horizontalCollision) return false;
PathNavigation* pathNav = mob->getNavigation();
Path* path = pathNav->getPath();
if (path == NULL || path->isDone() || !pathNav->canOpenDoors)
return false;
for (int i = 0; i < Mth::Min(path->getIndex() + 2, path->getSize()); ++i) {
Node* n = path->get(i);
doorX = n->x;
doorY = n->y + 1;
doorZ = n->z;
if (mob->distanceToSqr((float)doorX, mob->y, (float)doorZ) > 1.5f * 1.5f) continue;
doorTile = getDoorTile(doorX, doorY, doorZ);
if (doorTile == NULL) continue;
return true;
}
doorX = Mth::floor(mob->x);
doorY = Mth::floor(mob->y + 1);
doorZ = Mth::floor(mob->z);
doorTile = getDoorTile(doorX, doorY, doorZ);
return doorTile != NULL;
}
bool canContinueToUse() {
return !passed;
}
void start() {
passed = false;
doorOpenDirX = doorX + 0.5f - mob->x;
doorOpenDirZ = doorZ + 0.5f - mob->z;
}
void tick() {
float newDoorDirX = doorX + 0.5f - mob->x;
float newDoorDirZ = doorZ + 0.5f - mob->z;
float dot = doorOpenDirX * newDoorDirX + doorOpenDirZ * newDoorDirZ;
if (dot < 0) {
passed = true;
}
}
private:
DoorTile* getDoorTile(int x, int y, int z) {
int tileId = mob->level->getTile(x, y, z);
if (tileId != Tile::door_wood->id) return NULL;
DoorTile* doorTile = (DoorTile*) Tile::tiles[tileId];
return doorTile;
}
protected:
Monster* mob;
int doorX, doorY, doorZ;
DoorTile* doorTile;
private:
bool passed;
float doorOpenDirX, doorOpenDirZ;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__DoorInteractGoal_H__*/

44
src/world/entity/ai/goal/Goal.h Executable file
View file

@ -0,0 +1,44 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__Goal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__Goal_H__
//package net.minecraft.world.entity.ai.goal;
class Goal
{
public:
Goal()
: _requiredControlFlags(0)
{}
virtual ~Goal() {}
virtual bool canUse() = 0;
virtual bool canContinueToUse() {
return canUse();
}
virtual bool canInterrupt() {
return true;
}
virtual void start() {
}
virtual void stop() {
}
virtual void tick() {
}
void setRequiredControlFlags(int requiredControlFlags) {
_requiredControlFlags = requiredControlFlags;
}
int getRequiredControlFlags() {
return _requiredControlFlags;
}
private:
int _requiredControlFlags;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__Goal_H__*/

View file

@ -0,0 +1,104 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__GoalSelector_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__GoalSelector_H__
//package net.minecraft.world.entity.ai.goal;
#include "Goal.h"
#include <vector>
class GoalSelector
{
class InternalGoal {
public:
InternalGoal(short prio, Goal* goal)
: prio(prio),
goal(goal),
isUsing(false)
{}
Goal* goal;
short prio;
bool isUsing;
};
public:
~GoalSelector() {
for (unsigned int i = 0; i < goals.size(); ++i) {
delete goals[i].goal;
}
}
void addGoal(int prio, Goal* goal) {
goals.push_back(InternalGoal(prio, goal));
}
void tick() {
std::vector<InternalGoal> toStart;
for (std::vector<InternalGoal>::iterator it = goals.begin(); it != goals.end(); ++it) {
InternalGoal& ig = *it;
bool isUsing = ig.isUsing; //usingGoals.contains(ig);
if (isUsing) {
if (!canUseInSystem(ig) || !ig.goal->canContinueToUse()) {
ig.goal->stop();
ig.isUsing = false;
//usingGoals.remove(ig);
} else continue;
}
if (!canUseInSystem(ig) || !ig.goal->canUse()) {
continue;
}
toStart.push_back(ig);
//usingGoals.add(ig);
ig.isUsing = true;
}
//bool debug = false;
//if (debug && toStart.size() > 0) printf("Starting: ");
for (std::vector<InternalGoal>::iterator it = toStart.begin(); it != toStart.end(); ++it) {
InternalGoal& ig = *it;
//if (debug) printf(" %s, ", ig.goal.toString() + ", ");
ig.goal->start();
}
//if (debug && usingGoals.size() > 0) printf("Running: ");
for (std::vector<InternalGoal>::iterator it = goals.begin(); it != goals.end(); ++it) {
InternalGoal& ig = *it;
if (ig.isUsing) {
//if (debug) printf(" %s\n", ig.goal.toString());
ig.goal->tick();
}
}
}
//std::vector<InternalGoal>& getRunningGoals() {
// return usingGoals;
//}
private:
bool canUseInSystem(InternalGoal& goal) {
for (std::vector<InternalGoal>::iterator it = goals.begin(); it != goals.end(); ++it) {
InternalGoal& ig = *it;
if (ig.goal == goal.goal && ig.prio == goal.prio)
continue;
if (goal.prio >= ig.prio) {
if (ig.isUsing && !canCoExist(goal, ig))
return false;
} else if (ig.isUsing && !ig.goal->canInterrupt())
return false;
}
return true;
}
bool canCoExist(InternalGoal& goalA, InternalGoal& goalB) {
return (goalA.goal->getRequiredControlFlags() & goalB.goal->getRequiredControlFlags()) == 0;
}
std::vector<InternalGoal> goals;
//std::vector<InternalGoal> usingGoals;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__GoalSelector_H__*/

View file

@ -0,0 +1,94 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__MeleeAttackGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__MeleeAttackGoal_H__
//package net.minecraft.world.entity.ai.goal;
#include "Goal.h"
#include "../control/Control.h"
#include "../../monster/Monster.h"
#include "../../../level/Level.h"
#include "../../../level/pathfinder/Path.h"
#include "../Sensing.h"
class MeleeAttackGoal: public Goal
{
public:
MeleeAttackGoal(Monster* mob, float speed, bool trackTarget, int attackType = 0)
: mob(mob),
level(mob->level),
speed(speed),
trackTarget(trackTarget),
attackTime(0),
attackType(attackType),
path(NULL)
{
setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
}
~MeleeAttackGoal() {
if (path) {
LOGI("mag-deleting %p (%d)\n", path, path->id);
delete path;
}
}
/*@Override*/
bool canUse() {
Mob* bestTarget = mob->getTarget();
if (bestTarget == NULL) return false;
if (attackType != 0 && !mob->isPlayer()) return false; //!attackType.isAssignableFrom(bestTarget.getClass())) return false;
target = bestTarget;
if (path) {
LOGI("mag-canuse-deleting %p (%d)\n", path, path->id);
delete path;
}
path = mob->getNavigation()->createPath(target);
return path != NULL;
}
bool canContinueToUse() {
Mob* bestTarget = mob->getTarget();
if (bestTarget == NULL) return false;
if (attackType != 0 && !mob->isPlayer()) return false;//!attackType.isAssignableFrom(bestTarget.getClass())) return false;
target = bestTarget;
if (!trackTarget) return !mob->getNavigation()->isDone();
return true;
}
void start() {
mob->getNavigation()->moveTo(path, speed, false);
}
void stop() {
target = NULL;
mob->getNavigation()->stop();
}
void tick() {
//mob->getLookControl().setLookAt(target, 30, 30);
if (trackTarget || mob->sensing->canSee(target)) {
//LOGI("target: %p @ %f, %f, %f\n", target, target->x, target->y, target->z);
mob->getNavigation()->moveTo(target, speed);
}
attackTime = Mth::Max(attackTime - 1, 0);
float meleeRadiusSqr = (mob->bbWidth * 2) * (mob->bbWidth * 2);
if (mob->distanceToSqr(target->x, target->bb.y0, target->z) > meleeRadiusSqr) return;
if (attackTime > 0) return;
attackTime = 20;
mob->doHurtTarget(target);
}
private:
Level* level;
Monster* mob;
Mob* target;
int attackTime;
float speed;
Path* path;
int attackType;
bool trackTarget;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__MeleeAttackGoal_H__*/

View file

@ -0,0 +1,54 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__RandomStrollGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__RandomStrollGoal_H__
//package net.minecraft.world.entity.ai.goal;
#include "Goal.h"
#include "../../../../SharedConstants.h"
#include "../../PathfinderMob.h"
#include "../control/Control.h"
#include "../util/RandomPos.h"
#include "../../../phys/Vec3.h"
#include "../util/RandomPos.h"
class RandomStrollGoal: public Goal
{
public:
RandomStrollGoal(PathfinderMob* mob, float speed)
: mob(mob),
speed(speed)
{
setRequiredControlFlags(Control::MoveControlFlag);
}
/*@Override*/
bool canUse() {
if (mob->getNoActionTime() >= SharedConstants::TicksPerSecond * 5) return false;
if (mob->random.nextInt(120) != 0) return false;
Vec3 pos;
if (!RandomPos::getPos(pos, mob, 10, 7)) return false;
wantedX = pos.x;
wantedY = pos.y;
wantedZ = pos.z;
return true;
}
/*@Override*/
bool canContinueToUse() {
return !mob->getNavigation()->isDone();
}
/*@Override*/
void start() {
mob->getNavigation()->moveTo(wantedX, wantedY, wantedZ, speed);
}
private:
PathfinderMob* mob;
float wantedX, wantedY, wantedZ;
float speed;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL__RandomStrollGoal_H__*/

View file

@ -0,0 +1,41 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__HurtByTargetGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__HurtByTargetGoal_H__
//package net.minecraft.world.entity->ai.goal.target;
#include "TargetGoal.h"
#include "../../../../phys/AABB.h"
class HurtByTargetGoal: public TargetGoal
{
typedef TargetGoal super;
public:
HurtByTargetGoal(Monster* mob, bool alertSameType)
: super(mob, 16, false),
alertSameType(alertSameType)
{
setRequiredControlFlags(TargetGoal::TargetFlag);
}
bool canUse() {
return canAttack(mob->getLastHurtByMob(), true);
}
void start() {
mob->setTarget(mob->getLastHurtByMob());
// if (alertSameType) {
// List<Entity> nearby = mob.level->getEntitiesOfClass(mob.getClass(), AABB.newTemp(mob.x, mob.y, mob.z, mob.x + 1, mob.y + 1, mob.z + 1).grow(within, 4, within));
// for (Entity* ent : nearby) {
// Mob other = (Mob) ent;
// if (this->mob == other) continue;
// if (other.getTarget() != NULL) continue;
// other.setTarget(mob.getLastHurtByMob());
// }
// }
}
private:
bool alertSameType;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__HurtByTargetGoal_H__*/

View file

@ -0,0 +1,42 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__NearestAttackableTargetGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__NearestAttackableTargetGoal_H__
//package net.minecraft.world.entity->ai.goal.target;
/* import net.minecraft.world.entity->* */
#include "TargetGoal.h"
#include "../../../player/Player.h"
class NearestAttackableTargetGoal: public TargetGoal
{
typedef TargetGoal super;
public:
NearestAttackableTargetGoal(Monster* mob, int targetType, float within, int randomInterval, bool mustSee)
: super(mob, within, mustSee),
targetType(targetType),
randomInterval(randomInterval)
{
setRequiredControlFlags(TargetGoal::TargetFlag);
}
bool canUse() {
if (randomInterval > 0 && mob->random.nextInt(randomInterval) != 0) return false;
Mob* potentialTarget = NULL;
if (targetType == 1) potentialTarget = mob->level->getNearestPlayer(mob, within); //@todo: targetType
//else potentialTarget = (Mob*) mob->level->getClosestEntityOfClass(targetType, mob->bb.grow(within, 4, within), mob);
if (!canAttack(potentialTarget, false)) return false;
target = potentialTarget;
return true;
}
void start() {
mob->setTarget(target);
}
private:
Mob* target;
int targetType;
int randomInterval;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__NearestAttackableTargetGoal_H__*/

View file

@ -0,0 +1,57 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__TargetGoal_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__TargetGoal_H__
//package net.minecraft.world.entity->ai.goal.target;
/* import net.minecraft.world.entity->* */
#include "../Goal.h"
/* import net.minecraft.world.entity->monster.* */
#include "../../../player/Player.h"
class Monster;
class TargetGoal: public Goal
{
public:
static const int TargetFlag = 1;
TargetGoal(Monster* mob, float within, bool mustSee)
: mob(mob),
within(within),
mustSee(mustSee)
{}
bool canContinueToUse() {
Mob* target = mob->getTarget();
if (target == NULL) return false;
if (!target->isAlive()) return false;
if (mob->distanceToSqr(target) > within * within) return false;
if (mustSee && !mob->sensing->canSee(target)) return false;
return true;
}
void stop() {
mob->setTarget(NULL);
}
protected:
bool canAttack(Mob* target, bool allowInvulnerable) {
if (target == NULL) return false;
if (target == mob) return false;
if (!target->isAlive()) return false;
if (target->bb.y1 <= mob->bb.y0 || target->bb.y0 >= mob->bb.y1) return false;
//if (target instanceof Creeper || target instanceof Ghast) return false;
if (target->isPlayer()) {
if (!allowInvulnerable && ((Player*) target)->abilities.invulnerable) return false;
}
if (mustSee && !mob->sensing->canSee(target)) return false;
return true;
}
Monster* mob;
float within;
bool mustSee;
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_GOAL_TARGET__TargetGoal_H__*/

View file

@ -0,0 +1,71 @@
#ifndef NET_MINECRAFT_WORLD_ENTITY_AI_UTIL__RandomPos_H__
#define NET_MINECRAFT_WORLD_ENTITY_AI_UTIL__RandomPos_H__
//package net.minecraft.world.entity.ai.util;
#include "../../PathfinderMob.h"
#include "../../../phys/Vec3.h"
class RandomPos
{
public:
static bool getPos(Vec3& outPos, PathfinderMob* mob, int xzDist, int yDist) {
return generateRandomPos(outPos, mob, xzDist, yDist, NULL);
}
static bool getPosTowards(Vec3& outPos, PathfinderMob* mob, int xzDist, int yDist, Vec3* towardsPos) {
Vec3 tempDir(towardsPos->x, towardsPos->y, towardsPos->z);
tempDir.subSelf(mob->x, mob->y, mob->z);
return generateRandomPos(outPos, mob, xzDist, yDist, &tempDir);
}
static bool getPosAvoid(Vec3& outPos, PathfinderMob* mob, int xzDist, int yDist, Vec3* avoidPos) {
Vec3 tempDir(mob->x, mob->y, mob->z);
tempDir.subSelf(avoidPos->x, avoidPos->y, avoidPos->z);
return generateRandomPos(outPos, mob, xzDist, yDist, &tempDir);
}
private:
static bool generateRandomPos(Vec3& outPos, PathfinderMob* mob, int xzDist, int yDist, Vec3* dir) {
Random& random = mob->random;
bool hasBest = false;
int xBest = 0, yBest = 0, zBest = 0;
float best = -99999;
// bool restrict;
// if (mob->hasRestriction()) {
// float restDist = mob->getRestrictCenter().dist((int) Math.floor(mob->x), (int) Math.floor(mob->y), (int) Math.floor(mob->z)) + 4;
// restrict = restDist < mob->getRestrictRadius() + xzDist;
// } else restrict = false;
for (int i = 0; i < 10; i++) {
int xt = random.nextInt(2 * xzDist) - xzDist;
int yt = random.nextInt(2 * yDist) - yDist;
int zt = random.nextInt(2 * xzDist) - xzDist;
if (dir != NULL && xt * dir->x + zt * dir->z < 0) continue;
xt += Mth::floor(mob->x);
yt += Mth::floor(mob->y);
zt += Mth::floor(mob->z);
//if (restrict && !mob->isWithinRestriction(xt, yt, zt)) continue;
float value = mob->getWalkTargetValue(xt, yt, zt);
if (value > best) {
best = value;
xBest = xt;
yBest = yt;
zBest = zt;
hasBest = true;
}
}
if (hasBest) {
outPos.set((float)xBest, (float)yBest, (float)zBest);
return true;
}
return false;
}
};
#endif /*NET_MINECRAFT_WORLD_ENTITY_AI_UTIL__RandomPos_H__*/