1
0
cuberite-2a/src/Protocol/RecipeMapper.cpp

129 lines
3.1 KiB
C++
Raw Normal View History

Introduce recipe book functionality (#4493) * Introduce recipe book functionality The recipe book helps especially new players. Missing it gives the impression that cuberite is not as advanced as it is. The handling of the recipe book uses the following functions: - Unlock Recipes (https://wiki.vg/index.php?title=Protocol&oldid=14204#Unlock_Recipes) to make recipes available and show the notification for new recipes. Initialization is done on player login for known ones, the update is done when new items are discovered. - Craft Recipe Request (https://wiki.vg/index.php?title=Protocol&oldid=14204#Craft_Recipe_Request) when the user selects a recipe from the recipe book to fill the slots. Known recipes are initialized on player login via `Unlock Recipes` with `Action` 0. As soon as a new recipe is discovered this is added via `Unlock Recipes` with `Action` 1. To be able to know and recognize new recipes the player class is extended with `KnownItems` and `KnownRecipes`. As soon as a player touches an item this is compared to the list of `KnownItems`, if the item is unknown the recipes are checked for this item and the other ingredients are checked with the list of `KnownItems`. If a full match is discovered the recipe is unlocked with the client and stored in the `KnownRecipes`. To unlock recipes the recipe ID is sent to the client. A mapping file (for protocol 1.12.2) translated the minecraft recipe names to ids. The crafting.txt is extended with and minecraft recipe names is possible. Limitations: Only a single recipe is added to the crafting area. Multiple clicks or shift click does not increase the number of builds. Co-authored-by: peterbell10 <peterbell10@live.co.uk> * Address first issues mentioned by @peterbell10 - Some linting - Extract loading of recipe specific protocol mapping into a function - Build `RecipeNameMap` only once - Use `std::optional` - Extract `LoadRecipe` from `Window` * Start to implement new suggestions * Update with suggestions from @peterbell10 * Some minor cleanup * Update protocol packet IDs * Remove unused include * Include header in cmake * Change a vector to integer counter * Change dromedaryCase method names to PascalCase * Address suggestions from @madmaxoft * Read Protocol subdirectories to load recipe books To load all recipebooks iterate over the `Protocol` subdirectories to find mapping files. Co-authored-by: peterbell10 <peterbell10@live.co.uk>
2020-07-14 12:56:42 -04:00
#include "Globals.h"
#include "RecipeMapper.h"
#include "../Root.h"
cRecipeMapper::cRecipeMapper(void)
{
AString path = "Protocol";
auto contents = cFile::GetFolderContents(path);
for (const auto & content: contents)
{
auto fullName = path + cFile::PathSeparator() + content;
if (cFile::IsFolder(fullName))
{
loadRecipes(content);
}
}
}
void cRecipeMapper::loadRecipes(const AString & a_ProtocolVersion)
{
cFile f;
if (!f.Open("Protocol/" + a_ProtocolVersion + "/base.recipes.txt", cFile::fmRead))
{
LOGWARNING("Cannot open file \"Protocol/%s/base.recipes.txt\", no recipe book recipes will be available!", a_ProtocolVersion);
return;
}
AString Everything;
if (!f.ReadRestOfFile(Everything))
{
LOGWARNING("Cannot read file \"Protocol/%s/base.recipes.txt\", no recipe book recipes will be available!", a_ProtocolVersion);
return;
}
f.Close();
// Split it into lines, then process each line as a single recipe:
AStringVector Split = StringSplit(Everything, "\n");
m_ProtocolVersionMap[a_ProtocolVersion] = {};
const auto & RecipeNameMap = cRoot::Get()->GetCraftingRecipes()->GetRecipeNameMap();
int LineNum = 1;
for (AStringVector::const_iterator itr = Split.begin(); itr != Split.end(); ++itr, ++LineNum)
{
// Remove anything after a '#' sign and trim away the whitespace:
AString Recipe = TrimString(itr->substr(0, itr->find('#')));
if (Recipe.empty())
{
// Empty recipe
continue;
}
AddRecipeLine(a_ProtocolVersion, LineNum, Recipe, RecipeNameMap);
}
LOG("Loaded %s %zu recipe book", a_ProtocolVersion, m_ProtocolVersionMap[a_ProtocolVersion].size());
}
cRecipeMapper::~cRecipeMapper()
{
}
void cRecipeMapper::AddRecipeLine(const AString & a_ProtocolVersion, int a_LineNum, const AString & a_RecipeLine, const std::map<AString, UInt32> & a_RecipeNameMap)
{
AStringVector Sides = StringSplit(a_RecipeLine, " ");
UInt32 Id;
if (Sides.size() != 2)
{
LOGINFO("Recipe incompletely configured %s", a_RecipeLine);
return;
}
StringToInteger<UInt32>(Sides[0], Id);
auto RecipeIndex = a_RecipeNameMap.find(Sides[1]);
if (RecipeIndex == a_RecipeNameMap.end())
{
return;
}
m_ProtocolVersionMap[a_ProtocolVersion].emplace(Id, RecipeIndex->second);
}
std::optional<UInt32> cRecipeMapper::GetProtocolRecipeId(UInt32 a_RecipeId, UInt32 a_ProtocolVersion)
{
auto ProtocolMap = m_ProtocolVersionMap.find(cRoot::Get()->GetProtocolVersionTextFromInt(static_cast<int>(a_ProtocolVersion)));
if (ProtocolMap == m_ProtocolVersionMap.end())
{
return {};
}
for (const auto & item: ProtocolMap->second)
{
if (item.second == a_RecipeId)
{
return item.first;
}
}
return {};
}
std::optional<UInt32> cRecipeMapper::GetCuberiteRecipeId(UInt32 a_ProtocolRecipeId, UInt32 a_ProtocolVersion)
{
auto ProtocolMap = m_ProtocolVersionMap.find(cRoot::Get()->GetProtocolVersionTextFromInt(static_cast<int>(a_ProtocolVersion)));
if (ProtocolMap == m_ProtocolVersionMap.end())
{
return {};
}
auto Element = ProtocolMap->second.find(a_ProtocolRecipeId);
if (Element != ProtocolMap->second.end())
{
return Element->second;
}
return {};
}