1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "stdafx.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.ai.control.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.level.pathfinder.h"
#include "ControlledByPlayerGoal.h"
ControlledByPlayerGoal::ControlledByPlayerGoal(Mob *mob, float maxSpeed, float walkSpeed)
{
this->mob = mob;
this->maxSpeed = maxSpeed;
this->walkSpeed = walkSpeed;
speed = 0;
boosting = false;
boostTime = 0;
boostTimeTotal = 0;
setRequiredControlFlags(Control::MoveControlFlag | Control::JumpControlFlag | Control::LookControlFlag);
}
void ControlledByPlayerGoal::start()
{
speed = 0;
// 4J Stu - Need to initialise this otherwise the pig will never move if you jump on before another goal has made it move and set the speed
if(mob->getSpeed() < walkSpeed) mob->setSpeed(walkSpeed);
}
void ControlledByPlayerGoal::stop()
{
boosting = false;
speed = 0;
}
bool ControlledByPlayerGoal::canUse()
{
return mob->isAlive() && mob->rider.lock() != nullptr && mob->rider.lock()->instanceof(eTYPE_PLAYER) && (boosting || mob->canBeControlledByRider());
}
void ControlledByPlayerGoal::tick()
{
shared_ptr<Player> player = dynamic_pointer_cast<Player>(mob->rider.lock());
PathfinderMob *pig = static_cast<PathfinderMob *>(mob);
float yrd = Mth::wrapDegrees(player->yRot - mob->yRot) * 0.5f;
if (yrd > 5) yrd = 5;
if (yrd < -5) yrd = -5;
mob->yRot = Mth::wrapDegrees(mob->yRot + yrd);
if (speed < maxSpeed) speed += (maxSpeed - speed) * 0.01f;
if (speed > maxSpeed) speed = maxSpeed;
int x = Mth::floor(mob->x);
int y = Mth::floor(mob->y);
int z = Mth::floor(mob->z);
float moveSpeed = speed;
if (boosting)
{
if (boostTime++ > boostTimeTotal)
{
boosting = false;
}
moveSpeed += moveSpeed * 1.15f * Mth::sin(static_cast<float>(boostTime) / boostTimeTotal * PI);
}
float friction = 0.91f;
if (mob->onGround)
{
friction = 0.6f * 0.91f;
int t = mob->level->getTile(x,y,z);
if (t > 0)
{
friction = Tile::tiles[t]->friction * 0.91f;
}
}
float friction2 = (0.6f * 0.6f * 0.91f * 0.91f * 0.6f * 0.91f) / (friction * friction * friction);
float sin = Mth::sin(pig->yRot * PI / 180);
float cos = Mth::cos(pig->yRot * PI / 180);
float aproxSpeed = pig->getSpeed() * friction2;
float dist = max((int)moveSpeed, 1);
dist = aproxSpeed / dist;
float normMoveSpeed = moveSpeed * dist;
float xa = -(normMoveSpeed * sin);
float za = normMoveSpeed * cos;
if (Mth::abs(xa) > Mth::abs(za))
{
if (xa < 0) xa -= mob->bbWidth / 2.0f;
if (xa > 0) xa += mob->bbWidth / 2.0f;
za = 0;
}
else
{
xa = 0;
if (za < 0) za -= mob->bbWidth / 2.0f;
if (za > 0) za += mob->bbWidth / 2.0f;
}
int xt = Mth::floor(mob->x + xa);
int zt = Mth::floor(mob->z + za);
Node *size = new Node(Mth::floor(mob->bbWidth + 1), Mth::floor(mob->bbHeight + player->bbHeight + 1), Mth::floor(mob->bbWidth + 1));
if (x != xt || z != zt)
{
if (PathFinder::isFree(mob, xt, y, zt, size, false, false, true) == PathFinder::TYPE_BLOCKED
&& PathFinder::isFree(mob, x, y + 1, z, size, false, false, true) == PathFinder::TYPE_OPEN
&& PathFinder::isFree(mob, xt, y + 1, zt, size, false, false, true) == PathFinder::TYPE_OPEN)
{
pig->getJumpControl()->jump();
}
}
if (!player->abilities.instabuild && speed >= maxSpeed * 0.5f && mob->getRandom()->nextFloat() < 0.006f && !boosting)
{
shared_ptr<ItemInstance> carriedItem = player->getCarriedItem();
if (carriedItem != nullptr && carriedItem->id == Item::carrotOnAStick_Id)
{
carriedItem->hurtAndBreak(1, player);
if (carriedItem->count == 0)
{
shared_ptr<ItemInstance> replacement = std::make_shared<ItemInstance>(Item::fishingRod);
replacement->setTag(carriedItem->tag);
player->inventory->items[player->inventory->selected] = replacement;
}
}
}
mob->travel(0, moveSpeed);
}
bool ControlledByPlayerGoal::isNoJumpTile(int tile)
{
return Tile::tiles[tile] != nullptr && (Tile::tiles[tile]->getRenderShape() == Tile::SHAPE_STAIRS || (dynamic_cast<HalfSlabTile *>(Tile::tiles[tile]) != nullptr) );
}
bool ControlledByPlayerGoal::isBoosting()
{
return boosting;
}
void ControlledByPlayerGoal::boost()
{
boosting = true;
boostTime = 0;
boostTimeTotal = mob->getRandom()->nextInt(MAX_BOOST_TIME + MIN_BOOST_TIME + 1) + MIN_BOOST_TIME;
}
bool ControlledByPlayerGoal::canBoost()
{
return !isBoosting() && speed > maxSpeed * 0.3f;
}
|