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