Skip to content

Commit

Permalink
Merge pull request #38 from mrianura/master
Browse files Browse the repository at this point in the history
HDMod - cwm enchanced
  • Loading branch information
OTCv8 committed Sep 23, 2021
2 parents 56bebdd + c820efa commit 72af7c4
Show file tree
Hide file tree
Showing 22 changed files with 405 additions and 141 deletions.
1 change: 1 addition & 0 deletions init.lua
Expand Up @@ -57,6 +57,7 @@ elseif settings:exists('layout') then
end
g_resources.setLayout(layout)
g_game.enableFeature(Game64Textures)
-- load mods
g_modules.discoverModules()
g_modules.ensureModuleLoaded("corelib")
Expand Down
32 changes: 24 additions & 8 deletions modules/client_entergame/entergame.lua
Expand Up @@ -132,10 +132,18 @@ local function onTibia12HTTPResult(session, playdata)
account.subStatus = math.floor((session["premiumuntil"] - g_clock.seconds()) / 86400)
end

local things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/Tibia.spr", ""},
}
local things
if not g_game.getFeature(Game64Textures) then
things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/Tibia.spr", ""},
}
else
things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/TibiaHD.cwm", ""},
}
end

local incorrectThings = validateThings(things)
if #incorrectThings > 0 then
Expand Down Expand Up @@ -465,10 +473,18 @@ function EnterGame.doLogin()
return EnterGame.onError("Invalid server, it should be in format IP:PORT or it should be http url to login script")
end

local things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/Tibia.spr", ""},
}
local things
if not g_game.getFeature(Game64Textures) then
things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/Tibia.spr", ""},
}
else
things = {
data = {G.clientVersion .. "/Tibia.dat", ""},
sprites = {G.clientVersion .. "/TibiaHD.cwm", ""},
}
end

local incorrectThings = validateThings(things)
if #incorrectThings > 0 then
Expand Down
15 changes: 11 additions & 4 deletions modules/game_things/things.lua
Expand Up @@ -12,8 +12,10 @@ end
function load()
local version = g_game.getClientVersion()
local things = g_settings.getNode('things')
g_game.enableFeature(Game64Textures)

local datPath, sprPath
local datPath, sprPath, hdPath
local isHdMod = false
if things and things["data"] ~= nil and things["sprites"] ~= nil then
datPath = '/things/' .. things["data"]
sprPath = '/things/' .. things["sprites"]
Expand All @@ -26,6 +28,11 @@ function load()
sprPath = resolvepath('/things/' .. version .. '/Tibia')
end
end

if g_game.getFeature(Game64Textures) then
hdPath = resolvepath('/things/' .. version .. '/TibiaHD')
isHdMod = true
end

local errorMessage = ''
if not g_things.loadDat(datPath) then
Expand All @@ -38,10 +45,10 @@ function load()
errorMessage = errorMessage .. tr("Unable to load dat file, please place a valid dat in '%s'", datPath) .. '\n'
end
end
if not g_sprites.loadSpr(sprPath, false) then
errorMessage = errorMessage .. tr("Unable to load spr file, please place a valid spr in '%s'", sprPath)

if not g_sprites.loadSpr(sprPath, isHdMod) then
errorMessage = errorMessage .. tr("Unable to load spr file, please place a valid spr in '%s'", sprPath)
end

loaded = (errorMessage:len() == 0)

if errorMessage:len() > 0 then
Expand Down
1 change: 0 additions & 1 deletion src/client/const.h
Expand Up @@ -28,7 +28,6 @@
namespace Otc
{
enum : int {
TILE_PIXELS = 32, // change to 64 if you want to use 64x64 HD sprites
MAX_ELEVATION = 24,

SEA_FLOOR = 7,
Expand Down
73 changes: 42 additions & 31 deletions src/client/creature.cpp
Expand Up @@ -84,20 +84,21 @@ void Creature::draw(const Point& dest, bool animate, LightView* lightView)
if (!canBeSeen())
return;

const int sprSize = g_sprites.spriteSize();
Point jumpOffset = Point(m_jumpOffset.x, m_jumpOffset.y);
Point creatureCenter = dest - jumpOffset + m_walkOffset - getDisplacement() + Point(Otc::TILE_PIXELS / 2, Otc::TILE_PIXELS / 2);
Point creatureCenter = dest - jumpOffset + m_walkOffset - getDisplacement() + Point(sprSize / 2, sprSize / 2);
drawBottomWidgets(creatureCenter, m_walking ? m_walkDirection : m_direction);

Point animationOffset = animate ? m_walkOffset : Point(0, 0);
if (m_outfit.getCategory() != ThingCategoryCreature)
animationOffset -= getDisplacement();

if (m_showTimedSquare && animate) {
g_drawQueue->addBoundingRect(Rect(dest - jumpOffset + (animationOffset - getDisplacement() + 2), Size(28, 28)), 2, m_timedSquareColor);
g_drawQueue->addBoundingRect(Rect(dest - jumpOffset + (animationOffset - getDisplacement() + 2 * g_sprites.getOffsetFactor()), Size(sprSize - 4, sprSize - 4)), 2 * g_sprites.getOffsetFactor(), m_timedSquareColor);
}

if (m_showStaticSquare && animate) {
g_drawQueue->addBoundingRect(Rect(dest - jumpOffset + (animationOffset - getDisplacement()), Size(Otc::TILE_PIXELS, Otc::TILE_PIXELS)), 2, m_staticSquareColor);
g_drawQueue->addBoundingRect(Rect(dest - jumpOffset + (animationOffset - getDisplacement()), Size(sprSize, sprSize)), 2 * g_sprites.getOffsetFactor(), m_staticSquareColor);
}

size_t drawQueueSize = g_drawQueue->size();
Expand Down Expand Up @@ -144,7 +145,12 @@ void Creature::drawInformation(const Point& point, bool useGray, const Rect& par
fillColor = m_informationColor;

// calculate main rects
Rect backgroundRect = Rect(point.x + m_informationOffset.x - (13.5), point.y + m_informationOffset.y, 27, 4);
Rect backgroundRect;
if (g_sprites.isHdMod() == false) {
backgroundRect = Rect(point.x + m_informationOffset.x - (13.5), point.y + m_informationOffset.y, 27, 4);
}else {
backgroundRect = Rect(point.x + m_informationOffset.x * g_sprites.getOffsetFactor(), point.y + m_informationOffset.y * g_sprites.getOffsetFactor(), 27, 4);
}
backgroundRect.bind(parentRect);

//debug
Expand All @@ -159,7 +165,12 @@ void Creature::drawInformation(const Point& point, bool useGray, const Rect& par
}

Size nameSize = m_nameCache.getTextSize();
Rect textRect = Rect(point.x + m_informationOffset.x - nameSize.width() / 2.0, point.y + m_informationOffset.y - 12, nameSize);
Rect textRect;
if (g_sprites.isHdMod() == false) {
textRect = Rect(point.x + m_informationOffset.x - nameSize.width() / 2.0, point.y + m_informationOffset.y - 12, nameSize);
} else {
textRect = Rect(point.x + m_informationOffset.x - nameSize.width() / 2.0 + 14.5, point.y + m_informationOffset.y - 13, nameSize);
}
textRect.bind(parentRect);

// distance them
Expand Down Expand Up @@ -263,7 +274,7 @@ void Creature::drawInformation(const Point& point, bool useGray, const Rect& par

if (m_text) {
auto extraTextSize = m_text->getCachedText().getTextSize();
Rect extraTextRect = Rect(point.x + m_informationOffset.x - extraTextSize.width() / 2.0, point.y + m_informationOffset.y + 15, extraTextSize);
Rect extraTextRect = Rect(point.x + m_informationOffset.x * g_sprites.getOffsetFactor() - extraTextSize.width() / 2.0, point.y + (m_informationOffset.y + 15) * g_sprites.getOffsetFactor(), extraTextSize);
m_text->drawText(extraTextRect.center(), extraTextRect);
}
}
Expand Down Expand Up @@ -297,7 +308,7 @@ bool Creature::isInsideOffset(Point offset)
{
// for worse precision:
// Rect rect(getDrawOffset() - (m_walking ? m_walkOffset : Point(0,0)), Size(Otc::TILE_PIXELS - getDisplacementY(), Otc::TILE_PIXELS - getDisplacementX()));
Rect rect(getDrawOffset() - getDisplacement(), Size(Otc::TILE_PIXELS, Otc::TILE_PIXELS));
Rect rect(getDrawOffset() - getDisplacement(), Size(g_sprites.spriteSize(), g_sprites.spriteSize()));
return rect.contains(offset);
}

Expand Down Expand Up @@ -493,15 +504,15 @@ void Creature::updateWalkAnimation(uint8 totalPixelsWalked)

if (footAnimPhases == 0) {
m_walkAnimationPhase = 0;
} else if (g_clock.millis() >= m_footLastStep + footDelay && totalPixelsWalked < Otc::TILE_PIXELS) {
} else if (g_clock.millis() >= m_footLastStep + footDelay && totalPixelsWalked < g_sprites.spriteSize()) {
m_footStep++;
m_walkAnimationPhase = 1 + (m_footStep % footAnimPhases);
m_footLastStep = (g_clock.millis() - m_footLastStep) > footDelay * 1.5 ? g_clock.millis() : m_footLastStep + footDelay;
} else if (m_walkAnimationPhase == 0 && totalPixelsWalked < Otc::TILE_PIXELS) {
} else if (m_walkAnimationPhase == 0 && totalPixelsWalked < g_sprites.spriteSize()) {
m_walkAnimationPhase = 1 + (m_footStep % footAnimPhases);
}

if (totalPixelsWalked == Otc::TILE_PIXELS && !m_walkFinishAnimEvent) {
if (totalPixelsWalked == g_sprites.spriteSize() && !m_walkFinishAnimEvent) {
auto self = static_self_cast<Creature>();
m_walkFinishAnimEvent = g_dispatcher.scheduleEvent([self] {
self->m_footStep = 0;
Expand All @@ -517,26 +528,26 @@ void Creature::updateWalkOffset(uint8 totalPixelsWalked, bool inNextFrame)
Point& walkOffset = inNextFrame ? m_walkOffsetInNextFrame : m_walkOffset;
walkOffset = Point(0, 0);
if (m_walkDirection == Otc::North || m_walkDirection == Otc::NorthEast || m_walkDirection == Otc::NorthWest)
walkOffset.y = Otc::TILE_PIXELS - totalPixelsWalked;
walkOffset.y = g_sprites.spriteSize() - totalPixelsWalked;
else if (m_walkDirection == Otc::South || m_walkDirection == Otc::SouthEast || m_walkDirection == Otc::SouthWest)
walkOffset.y = totalPixelsWalked - Otc::TILE_PIXELS;
walkOffset.y = totalPixelsWalked - g_sprites.spriteSize();

if (m_walkDirection == Otc::East || m_walkDirection == Otc::NorthEast || m_walkDirection == Otc::SouthEast)
walkOffset.x = totalPixelsWalked - Otc::TILE_PIXELS;
walkOffset.x = totalPixelsWalked - g_sprites.spriteSize();
else if (m_walkDirection == Otc::West || m_walkDirection == Otc::NorthWest || m_walkDirection == Otc::SouthWest)
walkOffset.x = Otc::TILE_PIXELS - totalPixelsWalked;
walkOffset.x = g_sprites.spriteSize() - totalPixelsWalked;
}

void Creature::updateWalkingTile()
{
// determine new walking tile
TilePtr newWalkingTile;
Rect virtualCreatureRect(Otc::TILE_PIXELS + (m_walkOffset.x - getDisplacementX()),
Otc::TILE_PIXELS + (m_walkOffset.y - getDisplacementY()),
Otc::TILE_PIXELS, Otc::TILE_PIXELS);
Rect virtualCreatureRect(g_sprites.spriteSize() + (m_walkOffset.x - getDisplacementX()),
g_sprites.spriteSize() + (m_walkOffset.y - getDisplacementY()),
g_sprites.spriteSize(), g_sprites.spriteSize());
for (int xi = -1; xi <= 1 && !newWalkingTile; ++xi) {
for (int yi = -1; yi <= 1 && !newWalkingTile; ++yi) {
Rect virtualTileRect((xi + 1) * Otc::TILE_PIXELS, (yi + 1) * Otc::TILE_PIXELS, Otc::TILE_PIXELS, Otc::TILE_PIXELS);
Rect virtualTileRect((xi + 1) * g_sprites.spriteSize(), (yi + 1) * g_sprites.spriteSize(), g_sprites.spriteSize(), g_sprites.spriteSize());

// only render creatures where bottom right is inside tile rect
if (virtualTileRect.contains(virtualCreatureRect.bottomRight())) {
Expand Down Expand Up @@ -575,15 +586,15 @@ void Creature::nextWalkUpdate()
self->m_walkUpdateEvent = nullptr;
self->nextWalkUpdate();
}, g_game.getFeature(Otc::GameNewUpdateWalk) && isLocalPlayer() ?
std::ceil<uint16>(((float)getStepDuration(true) / g_app.getFps()) * 2) : (float)getStepDuration() / Otc::TILE_PIXELS);
std::ceil<uint16>(((float)getStepDuration(true) / g_app.getFps()) * 2) : (float)getStepDuration() / g_sprites.spriteSize());
}
}

void Creature::updateWalk()
{
float walkTicksPerPixel = ((float)(getStepDuration(true) + (g_game.getFeature(Otc::GameNewUpdateWalk) ? 0 : 10))) / (float)Otc::TILE_PIXELS;
uint8 totalPixelsWalked = std::min<uint8>(m_walkTimer.ticksElapsed() / walkTicksPerPixel, Otc::TILE_PIXELS);
uint8 totalPixelsWalkedInNextFrame = std::min<uint8>((m_walkTimer.ticksElapsed() + (g_game.getFeature(Otc::GameNewUpdateWalk) ? std::max(1000.f / g_app.getFps(), 1.0f) : 15)) / walkTicksPerPixel, Otc::TILE_PIXELS);
float walkTicksPerPixel = ((float)(getStepDuration(true) + (g_game.getFeature(Otc::GameNewUpdateWalk) ? 0 : 10))) / (float)g_sprites.spriteSize();
uint8 totalPixelsWalked = std::min<uint8>(m_walkTimer.ticksElapsed() / walkTicksPerPixel, g_sprites.spriteSize());
uint8 totalPixelsWalkedInNextFrame = std::min<uint8>((m_walkTimer.ticksElapsed() + (g_game.getFeature(Otc::GameNewUpdateWalk) ? std::max(1000.f / g_app.getFps(), 1.0f) : 15)) / walkTicksPerPixel, g_sprites.spriteSize());

// needed for paralyze effect
m_walkedPixels = std::max<uint8>(m_walkedPixels, totalPixelsWalked);
Expand Down Expand Up @@ -927,48 +938,48 @@ uint16 Creature::getStepDuration(bool ignoreDiagonal, Otc::Direction dir)
Point Creature::getDisplacement()
{
if (m_outfit.getCategory() == ThingCategoryEffect)
return Point(8, 8);
return Point(8, 8) * g_sprites.getOffsetFactor();
else if (m_outfit.getCategory() == ThingCategoryItem)
return Point(0, 0);

if (m_outfit.getMount() != 0) {
auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
return datType->getDisplacement();
return datType->getDisplacement() * g_sprites.getOffsetFactor();
}

return Thing::getDisplacement();
return Thing::getDisplacement() * g_sprites.getOffsetFactor();
}

int Creature::getDisplacementX()
{
if (m_outfit.getCategory() == ThingCategoryEffect)
return 8;
return 8 * g_sprites.getOffsetFactor();
else if (m_outfit.getCategory() == ThingCategoryItem)
return 0;

if (m_outfit.getMount() != 0) {
auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
return datType->getDisplacementX();
return datType->getDisplacementX() * g_sprites.getOffsetFactor();
}

return Thing::getDisplacementX();
return Thing::getDisplacementX() * g_sprites.getOffsetFactor();
}

int Creature::getDisplacementY()
{
if (m_outfit.getCategory() == ThingCategoryEffect)
return 8;
return 8 * g_sprites.getOffsetFactor();
else if (m_outfit.getCategory() == ThingCategoryItem)
return 0;

if (m_outfit.getMount() != 0) {
auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
if (datType) {
return datType->getDisplacementY();
return datType->getDisplacementY() * g_sprites.getOffsetFactor();
}
}

return Thing::getDisplacementY();
return Thing::getDisplacementY() * g_sprites.getOffsetFactor();
}

int Creature::getExactSize(int layer, int xPattern, int yPattern, int zPattern, int animationPhase)
Expand Down
11 changes: 6 additions & 5 deletions src/client/lightview.cpp
Expand Up @@ -21,6 +21,7 @@
*/

#include "lightview.h"
#include "spritemanager.h"
#include <framework/graphics/painter.h>

void LightView::addLight(const Point& pos, uint8_t color, uint8_t intensity)
Expand All @@ -37,7 +38,7 @@ void LightView::addLight(const Point& pos, uint8_t color, uint8_t intensity)

void LightView::setFieldBrightness(const Point& pos, size_t start, uint8_t color)
{
size_t index = (pos.y / Otc::TILE_PIXELS) * m_mapSize.width() + (pos.x / Otc::TILE_PIXELS);
size_t index = (pos.y / g_sprites.spriteSize()) * m_mapSize.width() + (pos.x / g_sprites.spriteSize());
if (index >= m_tiles.size()) return;
m_tiles[index].start = start;
m_tiles[index].color = color;
Expand All @@ -52,7 +53,7 @@ void LightView::draw() // render thread

for (int x = 0; x < m_mapSize.width(); ++x) {
for (int y = 0; y < m_mapSize.height(); ++y) {
Point pos(x * Otc::TILE_PIXELS + Otc::TILE_PIXELS / 2, y * Otc::TILE_PIXELS + Otc::TILE_PIXELS / 2);
Point pos(x * g_sprites.spriteSize() + g_sprites.spriteSize() / 2, y * g_sprites.spriteSize() + g_sprites.spriteSize() / 2);
int index = (y * m_mapSize.width() + x);
int colorIndex = index * 4;
buffer[colorIndex] = m_globalLight.r();
Expand All @@ -63,7 +64,7 @@ void LightView::draw() // render thread
Light& light = m_lights[i];
float distance = std::sqrt((pos.x - light.pos.x) * (pos.x - light.pos.x) +
(pos.y - light.pos.y) * (pos.y - light.pos.y));
distance /= Otc::TILE_PIXELS;
distance /= g_sprites.spriteSize();
float intensity = (-distance + light.intensity) * 0.2f;
if (intensity < 0.01f) continue;
if (intensity > 1.0f) intensity = 1.0f;
Expand All @@ -83,8 +84,8 @@ void LightView::draw() // render thread
Size size = m_src.size();
CoordsBuffer coords;
coords.addRect(RectF(m_dest.left(), m_dest.top(), m_dest.width(), m_dest.height()),
RectF((float)offset.x / Otc::TILE_PIXELS, (float)offset.y / Otc::TILE_PIXELS,
(float)size.width() / Otc::TILE_PIXELS, (float)size.height() / Otc::TILE_PIXELS));
RectF((float)offset.x / g_sprites.spriteSize(), (float)offset.y / g_sprites.spriteSize(),
(float)size.width() / g_sprites.spriteSize(), (float)size.height() / g_sprites.spriteSize()));

g_painter->resetColor();
g_painter->setCompositionMode(Painter::CompositionMode_Multiply);
Expand Down
10 changes: 5 additions & 5 deletions src/client/mapview.cpp
Expand Up @@ -161,7 +161,7 @@ void MapView::drawFloor(short floor, const Position& cameraPosition, const TileP
tile->drawBottom(tileDrawPos, m_lightView.get());

if (m_crosshair && tile == crosshairTile) {
g_drawQueue->addTexturedRect(Rect(tileDrawPos, tileDrawPos + Otc::TILE_PIXELS - 1),
g_drawQueue->addTexturedRect(Rect(tileDrawPos, tileDrawPos + g_sprites.spriteSize() - 1),
m_crosshair, Rect(0, 0, m_crosshair->getSize()));
}

Expand All @@ -185,7 +185,7 @@ void MapView::drawFloor(short floor, const Position& cameraPosition, const TileP
tile->drawBottom(tileDrawPos, m_lightView.get());

if (m_crosshair && tile == crosshairTile) {
g_drawQueue->addTexturedRect(Rect(tileDrawPos, tileDrawPos + Otc::TILE_PIXELS - 1),
g_drawQueue->addTexturedRect(Rect(tileDrawPos, tileDrawPos + g_sprites.spriteSize() - 1),
m_crosshair, Rect(0, 0, m_crosshair->getSize()));
}

Expand Down Expand Up @@ -451,7 +451,7 @@ Position MapView::getPosition(const Point& point, const Size& mapSize)

Point framebufferPos = Point(point.x * sh, point.y * sv);
Point realPos = (framebufferPos + srcRect.topLeft());
Point centerOffset = realPos / Otc::TILE_PIXELS;
Point centerOffset = realPos / g_sprites.spriteSize();

Point tilePos2D = getVisibleCenterOffset() - m_drawDimension.toPoint() + centerOffset + Point(2,2);
if(tilePos2D.x + cameraPosition.x < 0 && tilePos2D.y + cameraPosition.y < 0)
Expand Down Expand Up @@ -479,7 +479,7 @@ Point MapView::getPositionOffset(const Point& point, const Size& mapSize)

Point framebufferPos = Point(point.x * sh, point.y * sv);
Point realPos = (framebufferPos + srcRect.topLeft());
return Point(realPos.x % Otc::TILE_PIXELS, realPos.y % Otc::TILE_PIXELS);
return Point(realPos.x % g_sprites.spriteSize(), realPos.y % g_sprites.spriteSize());
}

void MapView::move(int x, int y)
Expand Down Expand Up @@ -508,7 +508,7 @@ void MapView::move(int x, int y)

Rect MapView::calcFramebufferSource(const Size& destSize, bool inNextFrame)
{
float scaleFactor = g_sprites.spriteSize()/(float)Otc::TILE_PIXELS;
float scaleFactor = g_sprites.spriteSize()/(float)g_sprites.spriteSize();
Point drawOffset = ((m_drawDimension - m_visibleDimension - Size(1,1)).toPoint()/2) * g_sprites.spriteSize();
if(isFollowingCreature())
drawOffset += m_followingCreature->getWalkOffset(inNextFrame) * scaleFactor;
Expand Down

0 comments on commit 72af7c4

Please sign in to comment.