Skip to content

Commit

Permalink
Display Item Duration feature (#29)
Browse files Browse the repository at this point in the history
Server side changes (TFS 1.4.2) Oen44/tfs-new-decay@f72bde7
  • Loading branch information
Oen44 authored Dec 15, 2024
1 parent 73c8be3 commit fbe0dfd
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/gamelib/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ GameMissionId = 125
GameItemCustomAttributes = 126
GameAnimatedTextCustomFont = 127
GameDrawFloorShadow = 128
GameDisplayItemDuration = 129

LastGameFeature = 130

Expand Down
1 change: 1 addition & 0 deletions src/client/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ namespace Otc
GameItemCustomAttributes = 126,
GameAnimatedTextCustomFont = 127,
GameDrawFloorShadow = 128,
GameDisplayItemDuration = 129,

LastGameFeature = 130
};
Expand Down
5 changes: 4 additions & 1 deletion src/client/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ Item::Item() :
m_async(true),
m_quickLootFlags(0),
m_phase(0),
m_lastPhase(0)
m_lastPhase(0),
m_durationTime(0),
m_durationTimePaused(0),
m_durationIsPaused(false)
{
m_animator = std::make_shared<Animator>();
m_idleAnimator = std::make_shared<Animator>();
Expand Down
14 changes: 14 additions & 0 deletions src/client/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class Item : public Thing
void setTooltip(const std::string& str) { m_tooltip = str; }
void setQuickLootFlags(uint32 flags) { m_quickLootFlags = flags; }
void setShader(const std::string& str) { m_shader = str; }
void setDurationTime(uint64 value) { m_durationTime = value; }
void setDurationIsPaused(bool value) {
m_durationIsPaused = value;
if (m_durationIsPaused) {
m_durationTimePaused = stdext::unixtimeMs();
}
}

int getCountOrSubType() { return m_countOrSubType; }
int getSubType();
Expand All @@ -106,6 +113,9 @@ class Item : public Thing
std::string getTooltip() { return m_tooltip; }
uint32 getQuickLootFlags() { return m_quickLootFlags; }
std::string getShader() { return m_shader; }
uint64 getDurationTime() { return m_durationTime; }
uint64 getDurationTimePaused() { return m_durationTimePaused; }
bool isDurationPaused() const { return m_durationIsPaused; }

void unserializeItem(const BinaryTreePtr& in);
void serializeItem(const OutputBinaryTreePtr& out);
Expand Down Expand Up @@ -185,6 +195,10 @@ class Item : public Thing
uint8 m_phase;
ticks_t m_lastPhase;

uint64 m_durationTime;
uint64 m_durationTimePaused;
bool m_durationIsPaused;

stdext::packed_storage<uint16> m_customAttribs;
};

Expand Down
10 changes: 10 additions & 0 deletions src/client/protocolgameparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3628,6 +3628,16 @@ ItemPtr ProtocolGame::getItem(const InputMessagePtr& msg, int id, bool hasDescri
}
}

if (g_game.getFeature(Otc::GameDisplayItemDuration)) {
uint8_t hasDuration = msg->getU8();
if (hasDuration) {
uint64_t duration = msg->getU64();
bool stopTime = msg->getU8() == 1;
item->setDurationTime(duration);
item->setDurationIsPaused(stopTime);
}
}

return item;
}

Expand Down
20 changes: 20 additions & 0 deletions src/client/uiitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ UIItem::UIItem()
m_draggable = true;
m_color = Color(231, 231, 231);
m_itemColor = Color::white;
m_lastDecayUpdate = 0;
m_decayColor = Color(127, 255, 212);
m_decayPausedColor = Color(222, 109, 109);
}

void UIItem::drawSelf(Fw::DrawPane drawPane)
Expand Down Expand Up @@ -64,6 +67,18 @@ void UIItem::drawSelf(Fw::DrawPane drawPane)
if (m_showId) {
g_drawQueue->addText(m_font, std::to_string(m_item->getServerId()), drawRect, Fw::AlignBottomRight, m_color);
}

if (g_game.getFeature(Otc::GameDisplayItemDuration)) {
if (m_item->getDurationTime() > 0) {
auto isPaused = m_item->isDurationPaused();
if (m_lastDecayUpdate + 1000 < stdext::millis()) {
uint64 duration = m_item->getDurationTime() - (isPaused ? m_item->getDurationTimePaused() : stdext::unixtimeMs());
m_decayText = stdext::secondsToDuration(duration / 1000);
m_lastDecayUpdate = stdext::millis();
}
g_drawQueue->addText(m_font, m_decayText, drawRect, Fw::AlignBottomRight, isPaused ? m_decayPausedColor : m_decayColor);
}
}
}

drawBorder(m_rect);
Expand All @@ -86,6 +101,8 @@ void UIItem::setItemId(int id)
if (m_item)
m_item->setShader(m_shader);

m_lastDecayUpdate = 0;

callLuaField("onItemChange");
}

Expand All @@ -111,6 +128,9 @@ void UIItem::setItem(const ItemPtr& item)
m_item = item;
if (m_item) {
m_item->setShader(m_shader);

m_lastDecayUpdate = 0;

cacheCountText();
callLuaField("onItemChange");
}
Expand Down
5 changes: 5 additions & 0 deletions src/client/uiitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class UIItem : public UIWidget
stdext::boolean<false> m_showCountAlways;
std::string m_shader;
std::string m_countText;

ticks_t m_lastDecayUpdate;
std::string m_decayText;
Color m_decayColor;
Color m_decayPausedColor;
};

#endif
17 changes: 17 additions & 0 deletions src/framework/stdext/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,21 @@ std::vector<std::string> split(const std::string& str, const std::string& separa
return splitted;
}

std::string secondsToDuration(uint32 totalSeconds) {
int days = totalSeconds / 86400;
int hours = (totalSeconds % 86400) / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = totalSeconds % 60;
if (days > 0) {
return format("%dd", days);
}
else if (hours > 0) {
return format("%0dh", hours);
}
else if (minutes > 0) {
return format("%dm", minutes);
}
return format("%ds", seconds);
}

}
2 changes: 2 additions & 0 deletions src/framework/stdext/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ template<typename T> std::vector<T> split(const std::string& str, const std::str
return results;
}

std::string secondsToDuration(uint32 totalSeconds);

}

#endif
9 changes: 7 additions & 2 deletions src/framework/stdext/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ ticks_t micros() {
void millisleep(size_t ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
};
}

void microsleep(size_t us)
{
std::this_thread::sleep_for(std::chrono::microseconds(us));
};
}

ticks_t unixtimeMs()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

}
1 change: 1 addition & 0 deletions src/framework/stdext/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ticks_t millis();
ticks_t micros();
void millisleep(size_t ms);
void microsleep(size_t us);
ticks_t unixtimeMs();

struct timer {
public:
Expand Down

0 comments on commit fbe0dfd

Please sign in to comment.