Files
LegacyRenouveau/Minecraft.World/Cow.cpp
T
Anaël d8d65bcd49 game loop: Refactor the tooltips loop
Moved the whole logic to a new file (TooltipManager.cpp), and rewrote most of the logic to profit from object oriented programming. The side-effect is that the changes are scattered across entities, tiles and items because moving everything to OOP requires to add the tooltip to every .cpp file for every tile/item/entities (or their class, on plants for example which all use the tooltip IDS_TOOLTIPS_PLANT)

The issue with the default tooltips loop written by 4J is that:

    Its running in the main Minecraft::tick loop and is more than a thousand lines long, polluting the function.
    Not using C++'s full potential with object oriented programming, 4J decided to use a big switch case (probably because it could run better on hardware without branch prediction like the WiiU or Xbox 360 ?)
    And seems to be better off in its own file than in the main Minecraft.cpp file
2026-10-04 20:54:24 +02:00

192 lines
4.6 KiB
C++

#include "stdafx.h"
#include "com.mojang.nbt.h"
#include "net.minecraft.world.entity.ai.attributes.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.entity.ai.goal.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.entity.monster.h"
#include "net.minecraft.stats.h"
#include "Cow.h"
#include "../Minecraft.Client/Textures.h"
#include "MobCategory.h"
#include "SoundTypes.h"
Cow::Cow(Level *level) : Animal( level )
{
// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
// the derived version of the function is called
this->defineSynchedData();
registerAttributes();
setHealth(getMaxHealth());
this->setSize(0.9f, 1.3f);
getNavigation()->setAvoidWater(true);
goalSelector.addGoal(0, new FloatGoal(this));
goalSelector.addGoal(1, new PanicGoal(this, 2.0f));
goalSelector.addGoal(2, new BreedGoal(this, 1.0f));
goalSelector.addGoal(3, new TemptGoal(this, 1.25f, Item::wheat_Id, false));
goalSelector.addGoal(4, new FollowParentGoal(this, 1.25f));
goalSelector.addGoal(5, new RandomStrollGoal(this, 1.0f));
goalSelector.addGoal(6, new LookAtPlayerGoal(this, typeid(Player), 6));
goalSelector.addGoal(7, new RandomLookAroundGoal(this));
}
bool Cow::useNewAi()
{
return true;
}
void Cow::registerAttributes()
{
Animal::registerAttributes();
getAttribute(SharedMonsterAttributes::MAX_HEALTH)->setBaseValue(10);
getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED)->setBaseValue(0.2f);
}
int Cow::getAmbientSound()
{
return eSoundType_MOB_COW_AMBIENT;
}
int Cow::getHurtSound()
{
return eSoundType_MOB_COW_HURT;
}
int Cow::getDeathSound()
{
return eSoundType_MOB_COW_HURT;
}
void Cow::playStepSound(int xt, int yt, int zt, int t)
{
playSound(eSoundType_MOB_COW_STEP, 0.15f, 1);
}
float Cow::getSoundVolume()
{
return 1.f;
}
int Cow::getDeathLoot()
{
return Item::leather->id;
}
/*
void Cow::dropDeathLoot(bool wasKilledByPlayer, int playerBonusLevel)
{
// drop some leather
int count = random->nextInt(3) + random->nextInt(1 + playerBonusLevel);
for (int i = 0; i < count; i++)
{
spawnAtLocation(Item::leather_Id, 1);
}
// and some meat
count = random->nextInt(3) + 1 + random->nextInt(1 + playerBonusLevel);
for (int i = 0; i < count; i++)
{
if (isOnFire())
{
spawnAtLocation(Item::cooked_beef_Id, 1);
}
else
{
spawnAtLocation(Item::beef_Id, 1);
}
}
}
*/
bool Cow::mobInteract(shared_ptr<Player> player)
{
shared_ptr<ItemInstance> item = player->inventory->getSelected();
if (item != nullptr && item->id == Item::bucket->id && !player->abilities.instabuild)
{
player->awardStat(GenericStats::cowsMilked(),GenericStats::param_cowsMilked());
playSound(eSoundType_ENTITY_COW_MILK, 1.0f, 1.0f);
if (item->count-- == 0)
{
player->inventory->setItem(player->inventory->selected, std::make_shared<ItemInstance>(Item::milk_bucket));
}
else if (!player->inventory->add(std::make_shared<ItemInstance>(Item::milk_bucket)))
{
player->drop(std::make_shared<ItemInstance>(Item::milk_bucket));
}
return true;
}
return Animal::mobInteract(player);
}
shared_ptr<AgableMob> Cow::getBreedOffspring(shared_ptr<AgableMob> target)
{
// 4J - added limit to number of animals that can be bred
if( level->canCreateMore( GetType(), Level::eSpawnType_Breed) )
{
return std::make_shared<Cow>(level);
}
else
{
return nullptr;
}
}
int Cow::getAttackTooltipId(std::shared_ptr<Player> player)
{
if (player->isAllowedToAttackAnimals())
{
return IDS_TOOLTIPS_HIT;
}
}
int Cow::getInteractTooltipId(Level *level,
std::shared_ptr<Player> player,
std::shared_ptr<ItemInstance> heldItem)
{
int heldItemId = -1;
if (heldItem != nullptr && heldItem->getItem() != nullptr)
{
heldItemId = heldItem->getItem()->id;
}
if (this->isLeashed() && this->getLeashHolder() == player)
return IDS_TOOLTIPS_UNLEASH;
switch (heldItemId)
{
case Item::name_tag_Id:
return IDS_TOOLTIPS_NAME;
case Item::lead_Id:
if (!this->isLeashed())
return IDS_TOOLTIPS_LEASH;
case Item::bucket_Id:
return IDS_TOOLTIPS_MILK;
default:
{
if (!this->isBaby() && !this->isInLove() && (this->getAge() == 0) && this->isFood(heldItem))
{
return IDS_TOOLTIPS_LOVEMODE;
}
}
break;
case -1:
break; // 4J-JEV: Empty hand.
}
return 0;
}