Skip to content

Commit

Permalink
Merge branch 'main' into bot
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 18, 2023
2 parents d93ee20 + 4190e86 commit b9fbc81
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 115 deletions.
1 change: 1 addition & 0 deletions modules/game_attachedeffects/attachedeffects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ controller = Controller:new()
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(1))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(2))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(3))
g_game.getLocalPlayer():getTile():attachEffect(g_attachedEffects.getById(1))
end
function controller:onGameEnd()
Expand Down
1 change: 1 addition & 0 deletions modules/game_attachedeffects/effects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
speed, disableWalkAnimation, shader, drawOnUI, opacity
duration, loop, transform, hideOwner, size{width, height}
offset{x, y, onTop}, dirOffset[dir]{x, y, onTop},
light { color, intensity}, drawOrder(only for tiles),
onAttach, onDetach
}
]] --
Expand Down
11 changes: 11 additions & 0 deletions modules/game_attachedeffects/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ local executeConfig = function(attachedEffect, config)
attachedEffect:setOpacity(config.opacity)
end

if config.light then
attachedEffect:setLight({
color = config.light.color or 0,
intensity = config.light.intensity or 0
})
end

if config.drawOrder then
attachedEffect:setDrawOrder(config.drawOrder)
end

if config.duration ~= nil and config.duration > 0 then
attachedEffect:setDuration(config.duration)
end
Expand Down
1 change: 1 addition & 0 deletions modules/gamelib/gamelib.otmod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Module
dofile 'textmessages'
dofile 'thing'
dofile 'spells'
dofile 'tile'

dofile 'eventcontroller'
dofile 'controller'
Expand Down
4 changes: 4 additions & 0 deletions modules/gamelib/thing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ SpriteMaskRed = 1
SpriteMaskGreen = 2
SpriteMaskBlue = 3
SpriteMaskYellow = 4

function Thing:isTile()
return false
end
39 changes: 39 additions & 0 deletions modules/gamelib/tile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Tile:isTile()
return true
end

function Tile:isCreature()
return false
end

function Tile:isLocalPlayer()
return false
end

function Tile:isNpc()
return false
end

function Tile:isMonster()
return false
end

function Tile:isPlayer()
return false
end

function Tile:isEffect()
return false
end

function Tile:isMissile()
return false
end

function Tile:isItem()
return false
end

function Tile:isContainer()
return false
end
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ set(SOURCE_FILES

client/animatedtext.cpp
client/animator.cpp
client/attachableobject.cpp
client/attachedeffect.cpp
client/attachedeffectmanager.cpp
client/client.cpp
Expand Down Expand Up @@ -378,7 +379,6 @@ set(SOURCE_FILES
client/uiminimap.cpp
client/uiprogressrect.cpp
client/uisprite.cpp
client/uigraph.cpp

protobuf/appearances.pb.cc
main.cpp
Expand Down
97 changes: 97 additions & 0 deletions src/client/attachableobject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2010-2022 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "attachableobject.h"

#include <framework/core/eventdispatcher.h>

void AttachableObject::attachEffect(const AttachedEffectPtr& obj) {
if (!obj)
return;

onStartAttachEffect(obj);

if (obj->isHidedOwner())
++m_ownerHidden;

if (obj->getDuration() > 0) {
g_dispatcher.scheduleEvent([self = std::static_pointer_cast<AttachableObject>(shared_from_this()), effectId = obj->getId()]() {
self->detachEffectById(effectId);
}, obj->getDuration());
}

m_attachedEffects.emplace_back(obj);
g_dispatcher.addEvent([effect = obj, self = std::static_pointer_cast<AttachableObject>(shared_from_this())] {
self->onDispatcherAttachEffect(effect);
effect->callLuaField("onAttach", self->attachedObjectToLuaObject());
});
}

bool AttachableObject::detachEffectById(uint16_t id) {
const auto it = std::find_if(m_attachedEffects.begin(), m_attachedEffects.end(),
[id](const AttachedEffectPtr& obj) { return obj->getId() == id; });

if (it == m_attachedEffects.end())
return false;

onDetachEffect(*it);
m_attachedEffects.erase(it);

return true;
}

void AttachableObject::onDetachEffect(const AttachedEffectPtr& effect) {
if (effect->isHidedOwner())
--m_ownerHidden;

onStartDetachEffect(effect);

effect->callLuaField("onDetach", attachedObjectToLuaObject());
}

void AttachableObject::clearAttachedEffects() {
for (const auto& e : m_attachedEffects)
onDetachEffect(e);
m_attachedEffects.clear();
}

AttachedEffectPtr AttachableObject::getAttachedEffectById(uint16_t id) {
const auto it = std::find_if(m_attachedEffects.begin(), m_attachedEffects.end(),
[id](const AttachedEffectPtr& obj) { return obj->getId() == id; });

if (it == m_attachedEffects.end())
return nullptr;

return *it;
}

void AttachableObject::drawAttachedEffect(const Point& dest, LightView* lightView, bool isOnTop)
{
for (const auto& effect : m_attachedEffects) {
effect->draw(dest, isOnTop, lightView);
if (effect->getLoop() == 0) {
g_dispatcher.addEvent([self = std::static_pointer_cast<AttachableObject>(shared_from_this()), effectId = effect->getId()]() {
self->detachEffectById(effectId);
});
}
}
}
53 changes: 53 additions & 0 deletions src/client/attachableobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2010-2022 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once
#include "attachedeffect.h"
#include <framework/luaengine/luaobject.h>

class AttachableObject : public LuaObject
{
public:
AttachableObject() = default;
virtual ~AttachableObject() = default;

void attachEffect(const AttachedEffectPtr& obj);
void clearAttachedEffects();
bool detachEffectById(uint16_t id);
AttachedEffectPtr getAttachedEffectById(uint16_t id);

virtual LuaObjectPtr attachedObjectToLuaObject() = 0;
virtual void onStartAttachEffect(const AttachedEffectPtr& effect) { };
virtual void onDispatcherAttachEffect(const AttachedEffectPtr& effect) { };
virtual void onStartDetachEffect(const AttachedEffectPtr& effect) { };

bool isOwnerHidden() { return m_ownerHidden > 0; }

const std::vector<AttachedEffectPtr>& getAttachedEffects() { return m_attachedEffects; };

protected:
void drawAttachedEffect(const Point& dest, LightView* lightView, bool isOnTop);
void onDetachEffect(const AttachedEffectPtr& effect);

std::vector<AttachedEffectPtr> m_attachedEffects;
uint8_t m_ownerHidden{ 0 };
};
10 changes: 8 additions & 2 deletions src/client/attachedeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "attachedeffect.h"
#include "shadermanager.h"
#include "gameconfig.h"
#include "lightview.h"

#include <framework/core/clock.h>
#include <framework/graphics/animatedtexture.h>

Expand Down Expand Up @@ -60,12 +62,16 @@ void AttachedEffect::draw(const Point& dest, bool isOnTop, LightView* lightView)
if (m_opacity < 100) g_drawPool.setOpacity(getOpacity(), true);

const auto& point = dest - (dirControl.offset * g_drawPool.getScaleFactor());
if (lightView && m_light.intensity > 0)
lightView->addLightSource(dest, m_light);

if (m_texture) {
const auto& size = (m_size.isUnset() ? m_texture->getSize() : m_size) * g_drawPool.getScaleFactor();
g_drawPool.addTexturedRect(Rect(point, size), m_texture->get(m_frame, m_animationTimer));
const auto& texture = m_texture->get(m_frame, m_animationTimer);
const auto& rect = Rect(Point(), texture->getSize());
g_drawPool.addTexturedRect(Rect(point, size), texture, rect, Color::white, { .order = getDrawOrder() });
} else {
m_thingType->draw(point, 0, m_direction, 0, 0, animation, Color::white, true, lightView);
m_thingType->draw(point, 0, m_direction, 0, 0, animation, Color::white, true, lightView, {.order = getDrawOrder()});
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/client/attachedeffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class AttachedEffect : public LuaObject

void attachEffect(const AttachedEffectPtr& e) { m_effects.emplace_back(e); }

DrawOrder getDrawOrder() { return m_drawOrder; }
void setDrawOrder(DrawOrder drawOrder) { m_drawOrder = drawOrder; }
const Light& getLight() const { return m_light; }
void setLight(const Light& light) { m_light = light; }

private:
int getCurrentAnimationPhase();

Expand All @@ -86,6 +91,7 @@ class AttachedEffect : public LuaObject
uint8_t m_speed{ 100 };
uint8_t m_opacity{ 100 };
uint8_t m_lastAnimation{ 0 };
DrawOrder m_drawOrder{ DrawOrder::FIRST };

uint16_t m_id{ 0 };
uint16_t m_duration{ 0 };
Expand All @@ -98,6 +104,7 @@ class AttachedEffect : public LuaObject
bool m_disableWalkAnimation{ false };

Outfit m_outfitOwner;
Light m_light;

uint16_t m_thingId{ 0 };
ThingCategory m_thingCategory{ ThingInvalidCategory };
Expand Down
19 changes: 12 additions & 7 deletions src/client/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "uiprogressrect.h"
#include "uisprite.h"
#include "uigraph.h"
#include "attachableobject.h"

#ifdef FRAMEWORK_EDITOR
#include "houses.h"
Expand Down Expand Up @@ -405,7 +406,14 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<Container>("getSize", &Container::getSize);
g_lua.bindClassMemberFunction<Container>("getFirstIndex", &Container::getFirstIndex);

g_lua.registerClass<Thing>();
g_lua.registerClass<AttachableObject>();
g_lua.bindClassMemberFunction<AttachableObject>("getAttachedEffects", &AttachableObject::getAttachedEffects);
g_lua.bindClassMemberFunction<AttachableObject>("attachEffect", &AttachableObject::attachEffect);
g_lua.bindClassMemberFunction<AttachableObject>("detachEffectById", &AttachableObject::detachEffectById);
g_lua.bindClassMemberFunction<AttachableObject>("getAttachedEffectById", &AttachableObject::getAttachedEffectById);
g_lua.bindClassMemberFunction<AttachableObject>("clearAttachedEffects", &AttachableObject::clearAttachedEffects);

g_lua.registerClass<Thing, AttachableObject>();
g_lua.bindClassMemberFunction<Thing>("setId", &Thing::setId);
g_lua.bindClassMemberFunction<Thing>("setShader", &Thing::setShader);
g_lua.bindClassMemberFunction<Thing>("setPosition", &Thing::setPosition);
Expand Down Expand Up @@ -448,11 +456,6 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<Thing>("isTopEffect", &Thing::isTopEffect);
g_lua.bindClassMemberFunction<Thing>("isLyingCorpse", &Thing::isLyingCorpse);
g_lua.bindClassMemberFunction<Thing>("getDefaultAction", &Thing::getDefaultAction);
g_lua.bindClassMemberFunction<Thing>("getAttachedEffects", &Thing::getAttachedEffects);
g_lua.bindClassMemberFunction<Thing>("attachEffect", &Thing::attachEffect);
g_lua.bindClassMemberFunction<Thing>("detachEffectById", &Thing::detachEffectById);
g_lua.bindClassMemberFunction<Thing>("getAttachedEffectById", &Thing::getAttachedEffectById);
g_lua.bindClassMemberFunction<Thing>("clearAttachedEffects", &Thing::clearAttachedEffects);
g_lua.bindClassMemberFunction<Thing>("getClassification", &Thing::getClassification);

#ifdef FRAMEWORK_EDITOR
Expand Down Expand Up @@ -715,6 +718,8 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<AttachedEffect>("canDrawOnUI", &AttachedEffect::canDrawOnUI);
g_lua.bindClassMemberFunction<AttachedEffect>("setCanDrawOnUI", &AttachedEffect::setCanDrawOnUI);
g_lua.bindClassMemberFunction<AttachedEffect>("attachEffect", &AttachedEffect::attachEffect);
g_lua.bindClassMemberFunction<AttachedEffect>("setDrawOrder", &AttachedEffect::setDrawOrder);
g_lua.bindClassMemberFunction<AttachedEffect>("setLight", &AttachedEffect::setLight);

g_lua.registerClass<StaticText>();
g_lua.bindClassStaticFunction<StaticText>("create", [] { return std::make_shared<StaticText>(); });
Expand Down Expand Up @@ -784,7 +789,7 @@ void Client::registerLuaFunctions()
g_lua.bindClassMemberFunction<LocalPlayer>("setResourceBalance", &LocalPlayer::setResourceBalance);
g_lua.bindClassMemberFunction<LocalPlayer>("getTotalMoney", &LocalPlayer::getTotalMoney);

g_lua.registerClass<Tile>();
g_lua.registerClass<Tile, AttachableObject>();
g_lua.bindClassMemberFunction<Tile>("clean", &Tile::clean);
g_lua.bindClassMemberFunction<Tile>("addThing", &Tile::addThing);
g_lua.bindClassMemberFunction<Tile>("getThing", &Tile::getThing);
Expand Down
Loading

0 comments on commit b9fbc81

Please sign in to comment.