#include "CWeightAgent.h"
#include <string>
#include <functional>
namespace Gothic_II_Addon
{
float CWeightAgent::GetWeight(const zSTRING& text)
{
zSTRING& str = const_cast<zSTRING&>(text);
std::string numStr = "0";
static zSTRING prefix = parser->GetSymbol("NAME_05KG")->stringdata[0];
for (int i = 0; i < str.Length(); i++)
{
if (i >= 4)
{
if ((str[i] >= '0') && (str[i] <= '9') || str[i] == '.')
{
numStr += str[i];
continue;
}
else
{
break;
}
}
if (str[i] != prefix[i])
{
return 0;
}
}
return atof(numStr.c_str());
}
float CWeightAgent::GetWeight(oCItem& item)
{
auto it = cachedWeights.find(item.GetInstance());
if (it != cachedWeights.end())
{
return item.amount * it->second;
}
return item.amount * (cachedWeights[item.GetInstance()] = GetWeight(item.GetText(5)));
}
float CWeightAgent::GetWeight(oCNpcInventory& inv)
{
inv.UnpackAllItems();
float sum = 0;
for (zCListSort<oCItem>* list = inv.inventory.GetNextInList(); list; list = list->GetNextInList())
{
sum += GetWeight(*list->data);
}
return sum;
}
float CWeightAgent::GetMaxWeight()
{
return 27 + parser->GetInt(parser->GetIndex("ADD_WGHT"), 0) + player->level / 2 + player->GetAttribute(NPC_ATR_STRENGTH) / 3 + 1;
}
void CWeightAgent::AttachBar()
{
barPos[0] = ogame->hpBar->vposx;
barPos[1] = ogame->hpBar->vposy;
barSize[0] = ogame->hpBar->vsizex;
barSize[1] = ogame->hpBar->vsizey;
zVEC2 sizeFactor(0.9f, 0.5f);
barPos[0] += barSize[0] * (1 - sizeFactor[0]) / 2;
barSize *= sizeFactor;
barPos[1] -= barSize[1] * 1.25f;
}
void CWeightAgent::HideNotifications()
{
parser->GetSymbol("я55098")->stringdata[0] = "";
parser->GetSymbol("я55099")->stringdata[0] = "";
parser->GetSymbol("я55100")->stringdata[0] = "";
}
void CWeightAgent::Process()
{
float time = ztimer->totalTimeFloatSecs;
if (lastUpdate < 0)
{
HideNotifications();
}
if (lastUpdate < 0 || (time - lastUpdate) * fps >= 1)
{
lastUpdate = time;
AttachBar();
if (ogame->hpBar->ondesk)
{
float weight = GetWeight(player->inventory2);
float maxWeight = GetMaxWeight();
bar->Update(weight / maxWeight, time);
}
}
TryDraw();
}
void CWeightAgent::TryDraw()
{
if (ogame->hpBar->ondesk)
{
bar->Draw(*screen, barPos, barSize);
}
}
CWeightAgent::CWeightAgent(IBar* bar, float fps):
bar(bar),
fps(fps),
lastUpdate(-1)
{
CGlobalEventSubscription onLoop(CGlobalEventPublisher::LOOP, std::bind(&CWeightAgent::Process, this));
subscriptions.push_back(std::move(onLoop));
}
}