From 5eabf6f5189d0aa70441f6434f14bb29b8008686 Mon Sep 17 00:00:00 2001 From: Allan Ference Date: Sat, 17 Aug 2013 23:06:58 +0200 Subject: [PATCH] Optional and configurable zone colors --- src/client/luafunctions.cpp | 13 +++++++++ src/client/map.cpp | 24 ++++++++++++++++ src/client/map.h | 16 +++++++++++ src/client/tile.cpp | 57 ++++++++++++++++++++++--------------- src/client/tile.h | 5 +++- 5 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/client/luafunctions.cpp b/src/client/luafunctions.cpp index 659175b5..ec57d90e 100644 --- a/src/client/luafunctions.cpp +++ b/src/client/luafunctions.cpp @@ -66,6 +66,7 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_things", "getItemType", &ThingTypeManager::getItemType, &g_things); g_lua.bindSingletonFunction("g_things", "getThingTypes", &ThingTypeManager::getThingTypes, &g_things); g_lua.bindSingletonFunction("g_things", "findItemTypeByClientId", &ThingTypeManager::findItemTypeByClientId, &g_things); + g_lua.bindSingletonFunction("g_things", "findItemTypeByName", &ThingTypeManager::findItemTypeByName, &g_things); g_lua.bindSingletonFunction("g_things", "findThingTypeByAttr", &ThingTypeManager::findThingTypeByAttr, &g_things); g_lua.bindSingletonFunction("g_things", "findItemTypeByCategory", &ThingTypeManager::findItemTypeByCategory, &g_things); @@ -122,6 +123,14 @@ void Client::registerLuaFunctions() g_lua.bindSingletonFunction("g_map", "setSpawnFile", &Map::setSpawnFile, &g_map); g_lua.bindSingletonFunction("g_map", "createTile", &Map::createTile, &g_map); g_lua.bindSingletonFunction("g_map", "getSize", &Map::getSize, &g_map); + g_lua.bindSingletonFunction("g_map", "setShowZone", &Map::setShowZone, &g_map); + g_lua.bindSingletonFunction("g_map", "setShowZones", &Map::setShowZones, &g_map); + g_lua.bindSingletonFunction("g_map", "setZoneColor", &Map::setZoneColor, &g_map); + g_lua.bindSingletonFunction("g_map", "setZoneOpacity", &Map::setZoneOpacity, &g_map); + g_lua.bindSingletonFunction("g_map", "getZoneOpacity", &Map::getZoneOpacity, &g_map); + g_lua.bindSingletonFunction("g_map", "getZoneColor", &Map::getZoneColor, &g_map); + g_lua.bindSingletonFunction("g_map", "showZones", &Map::showZones, &g_map); + g_lua.bindSingletonFunction("g_map", "showZone", &Map::showZone, &g_map); g_lua.registerSingletonClass("g_minimap"); g_lua.bindSingletonFunction("g_minimap", "clean", &Minimap::clean, &g_minimap); @@ -529,6 +538,10 @@ void Client::registerLuaFunctions() g_lua.bindClassMemberFunction("isClickable", &Tile::isClickable); g_lua.bindClassMemberFunction("isPathable", &Tile::isPathable); g_lua.bindClassMemberFunction("overwriteMinimapColor", &Tile::overwriteMinimapColor); + g_lua.bindClassMemberFunction("setFlag", &Tile::setFlag); + g_lua.bindClassMemberFunction("setFlags", &Tile::setFlags); + g_lua.bindClassMemberFunction("getFlags", &Tile::getFlags); + g_lua.bindClassMemberFunction("hasFlag", &Tile::hasFlag); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return UIItemPtr(new UIItem); }); diff --git a/src/client/map.cpp b/src/client/map.cpp index c41076e4..6badea78 100644 --- a/src/client/map.cpp +++ b/src/client/map.cpp @@ -305,6 +305,30 @@ void Map::cleanTile(const Position& pos) } } +void Map::setShowZone(tileflags_t zone, bool show) +{ + if(show) + m_zoneFlags |= (uint32)zone; + else + m_zoneFlags &= ~(uint32)zone; +} + +void Map::setShowZones(bool show) +{ + static constexpr uint32 defaultZoneFlags + = TILESTATE_HOUSE | TILESTATE_PROTECTIONZONE; + if(!show) + m_zoneFlags = 0; + else if(m_zoneFlags == 0) + m_zoneFlags = defaultZoneFlags; +} + +void Map::setZoneColor(tileflags_t zone, const Color& color) +{ + if((m_zoneFlags & zone) == zone) + m_zoneColors[zone] = color; +} + void Map::addCreature(const CreaturePtr& creature) { m_knownCreatures[creature->getId()] = creature; diff --git a/src/client/map.h b/src/client/map.h index 4022911d..367008fa 100644 --- a/src/client/map.h +++ b/src/client/map.h @@ -179,6 +179,18 @@ public: const TilePtr& getTile(const Position& pos); void cleanTile(const Position& pos); + // tile zone related + void setShowZone(tileflags_t zone, bool show); + void setShowZones(bool show); + void setZoneColor(tileflags_t flag, const Color& color); + void setZoneOpacity(float opacity) { m_zoneOpacity = opacity; } + + float getZoneOpacity() { return m_zoneOpacity; } + Color getZoneColor(tileflags_t flag) { return m_zoneColors[flag]; } + tileflags_t getZoneFlags() { return (tileflags_t)m_zoneFlags; } + bool showZones() { return m_zoneFlags != 0; } + bool showZone(tileflags_t zone) { return (m_zoneFlags & zone) == zone; } + // known creature related void addCreature(const CreaturePtr& creature); CreaturePtr getCreatureById(uint32 id); @@ -223,6 +235,10 @@ private: std::vector m_mapViews; std::unordered_map m_waypoints; + uint32 m_zoneFlags; + std::array m_zoneColors; + float m_zoneOpacity; + Light m_light; Position m_centralPosition; Rect m_tilesRect; diff --git a/src/client/tile.cpp b/src/client/tile.cpp index fad210a1..0961b09b 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -43,15 +43,11 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags, LightView * { bool animate = drawFlags & Otc::DrawAnimations; - // Added for MapEditor purposes. - // This check will and must evaluate to false if using - // normal client, unless some flag error. - // Save last color - Color lastColor = g_painter->getColor(); - if((m_flags & TILESTATE_HOUSE) == TILESTATE_HOUSE) - g_painter->setColor(Color::blue); - else if((m_flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE) - g_painter->setColor(Color::green); + /* Flags to be checked for. */ + static const tileflags_t flags[] = { + TILESTATE_HOUSE, + TILESTATE_PROTECTIONZONE, + }; // first bottom items if(drawFlags & (Otc::DrawGround | Otc::DrawGroundBorders | Otc::DrawOnBottom)) { @@ -60,10 +56,31 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags, LightView * if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom()) break; + Color prevColor = g_painter->getColor(); + float prevOpacity = g_painter->getOpacity(); + bool restore = false; + + if(g_map.showZones() && thing->isGround()) { + for(unsigned int i = 0; i < sizeof(flags) / sizeof(tileflags_t); ++i) { + tileflags_t flag = flags[i]; + if(hasFlag(flag) && g_map.showZone(flag)) { + g_painter->setOpacity(g_map.getZoneOpacity()); + g_painter->setColor(g_map.getZoneColor(flag)); + restore = true; + break; + } + } + } + if((thing->isGround() && drawFlags & Otc::DrawGround) || (thing->isGroundBorder() && drawFlags & Otc::DrawGroundBorders) || (thing->isOnBottom() && drawFlags & Otc::DrawOnBottom)) { thing->draw(dest - m_drawElevation*scaleFactor, scaleFactor, animate, lightView); + + if(restore) { + g_painter->setOpacity(prevOpacity); + g_painter->setColor(prevColor); + } } m_drawElevation += thing->getElevation(); @@ -131,20 +148,15 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags, LightView * } // effects - if(drawFlags & Otc::DrawEffects) { - for(const EffectPtr& effect : m_effects){ + if(drawFlags & Otc::DrawEffects) + for(const EffectPtr& effect : m_effects) effect->draw(dest - m_drawElevation*scaleFactor, scaleFactor, animate, lightView); - } - } // top items - if(drawFlags & Otc::DrawOnTop) { - for(const ThingPtr& thing : m_things) { - if(thing->isOnTop()){ - thing->draw(dest, scaleFactor, animate, lightView); - } - } - } + if(drawFlags & Otc::DrawOnTop) + for(const ThingPtr& thing : m_things) + if(thing->isOnTop()) + thing->draw(dest, scaleFactor, animate, lightView); // draw translucent light (for tiles beneath holes) if(hasTranslucentLight() && lightView) { @@ -152,9 +164,6 @@ void Tile::draw(const Point& dest, float scaleFactor, int drawFlags, LightView * light.intensity = 1; lightView->addLightSource(dest + Point(16,16) * scaleFactor, scaleFactor, light); } - - // Restore color - g_painter->setColor(lastColor); } void Tile::clean() @@ -652,3 +661,5 @@ void Tile::checkTranslucentLight() else tile->m_flags = tile->m_flags & ~TILESTATE_TRANSLUECENT_LIGHT; } + +/* vim: set ts=8 sw=4 tw=0 et :*/ diff --git a/src/client/tile.h b/src/client/tile.h index cc5e8cba..a44775d0 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -48,7 +48,9 @@ enum tileflags_t TILESTATE_TRASHHOLDER = 1 << 20, TILESTATE_BED = 1 << 21, TILESTATE_DEPOT = 1 << 22, - TILESTATE_TRANSLUECENT_LIGHT = 1 << 23 + TILESTATE_TRANSLUECENT_LIGHT = 1 << 23, + + TILESTATE_LAST = 1 << 24 }; class Tile : public LuaObject @@ -112,6 +114,7 @@ public: void setFlag(tileflags_t flag) { m_flags |= (uint32)flag; } void setFlags(tileflags_t flags) { m_flags = (uint32)flags; } + bool hasFlag(tileflags_t flag) { return (m_flags & flag) == flag; } uint32 getFlags() { return m_flags; } void setHouseId(uint32 hid) { m_houseId = hid; }