tibia-rme/source/table_brush.cpp

265 lines
7.7 KiB
C++
Raw Permalink Normal View History

//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
2020-07-30 11:42:28 -03:00
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "table_brush.h"
#include "items.h"
#include "basemap.h"
uint32_t TableBrush::table_types[256];
//=============================================================================
// Table brush
TableBrush::TableBrush() :
2023-10-09 19:12:16 -07:00
look_id(0) {
2015-12-06 11:42:37 -03:00
////
}
2023-10-09 19:12:16 -07:00
TableBrush::~TableBrush() {
2015-12-06 11:42:37 -03:00
////
}
2023-10-09 19:12:16 -07:00
bool TableBrush::load(pugi::xml_node node, wxArrayString &warnings) {
if (const pugi::xml_attribute attribute = node.attribute("server_lookid")) {
feat: synchronizing commits with the official rme repository (#36) * Show indicators for pickupable and moveable items * Fix: drawing always refreshing ui * Fix crash on invalid friend for wallbrush * Option to remove empty spawns * Draws position indicator, some code cleanup * Teleport copy/paste improvements * Fix flood fill * Add function to get minimap/8bit color * Small code cleanup * Cleanup Position * Code cleanup and small optimizations * Code cleanup * Code cleanup and small optimizations (#406) * Cleanup and cast functions. * Avoid adding a new Unique ID if it already exists * Code cleanup and small optimizations * More changes and cleanup * Changes and cleanup * Some changes * Fix copy position. * Only show uid/aid alert if it really changed * Add actions history panel * Does not draw tooltips on minimap mode * Use constexpr * Replace items fix (#408) * Draw grid small optimization * Small change in selection box and Fix #409 * Fix some xpm * Ingame box improvements, add lights support. * Fix go to previous position (#410) * Fix depot crash (#411) * Fix XPMs * Export minimap as .otmm (otclient format) or .png (#413) * Fix glitch after drawing secondary map * fix * Update about_window.cpp * sonar * fix Minimap and progress bar * fix erro load items.xml * fix * fix doodad brush * fix: slightly more accurate house size estimation * feat: update items.otb and items.xml * fix: bad merge * Revert "feat: update items.otb and items.xml" This reverts commit 40f1edc70f17c423282d546a41541a8fe9b92dcc. --------- Co-authored-by: Nailson <Mignari@users.noreply.github.com> Co-authored-by: wtver <51377408+maattch@users.noreply.github.com> Co-authored-by: Majesty <32709570+majestyotbr@users.noreply.github.com> Co-authored-by: Luan Santos <github@luan.sh>
2023-10-09 23:02:37 -03:00
look_id = g_items.getItemType(attribute.as_uint()).clientID;
2023-10-09 19:12:16 -07:00
}
2023-10-09 19:12:16 -07:00
if (look_id == 0) {
look_id = node.attribute("lookid").as_uint();
}
2023-10-09 19:12:16 -07:00
for (pugi::xml_node childNode = node.first_child(); childNode; childNode = childNode.next_sibling()) {
if (as_lower_str(childNode.name()) != "table") {
continue;
}
2023-10-09 19:12:16 -07:00
const std::string &alignString = childNode.attribute("align").as_string();
if (alignString.empty()) {
2016-11-05 15:28:14 +01:00
warnings.push_back("Could not read type tag of table node\n");
continue;
}
uint32_t alignment;
2023-10-09 19:12:16 -07:00
if (alignString == "vertical") {
alignment = TABLE_VERTICAL;
2023-10-09 19:12:16 -07:00
} else if (alignString == "horizontal") {
alignment = TABLE_HORIZONTAL;
2023-10-09 19:12:16 -07:00
} else if (alignString == "south") {
alignment = TABLE_SOUTH_END;
2023-10-09 19:12:16 -07:00
} else if (alignString == "east") {
alignment = TABLE_EAST_END;
2023-10-09 19:12:16 -07:00
} else if (alignString == "north") {
alignment = TABLE_NORTH_END;
2023-10-09 19:12:16 -07:00
} else if (alignString == "west") {
alignment = TABLE_WEST_END;
2023-10-09 19:12:16 -07:00
} else if (alignString == "alone") {
alignment = TABLE_ALONE;
} else {
2016-11-05 15:28:14 +01:00
warnings.push_back("Unknown table alignment '" + wxstr(alignString) + "'\n");
continue;
}
2023-10-09 19:12:16 -07:00
for (pugi::xml_node subChildNode = childNode.first_child(); subChildNode; subChildNode = subChildNode.next_sibling()) {
if (as_lower_str(subChildNode.name()) != "item") {
continue;
}
uint16_t id = subChildNode.attribute("id").as_uint();
2023-10-09 19:12:16 -07:00
if (id == 0) {
2016-11-05 15:28:14 +01:00
warnings.push_back("Could not read id tag of item node\n");
break;
}
2024-06-07 18:16:45 -03:00
auto type = g_items.getRawItemType(id);
2023-10-09 19:12:16 -07:00
if (!type) {
2016-11-05 15:28:14 +01:00
warnings.push_back("There is no itemtype with id " + std::to_string(id));
return false;
2023-10-09 19:12:16 -07:00
} else if (type->brush && type->brush != this) {
2016-11-05 15:28:14 +01:00
warnings.push_back("Itemtype id " + std::to_string(id) + " already has a brush");
return false;
}
feat: synchronizing commits with the official rme repository (#36) * Show indicators for pickupable and moveable items * Fix: drawing always refreshing ui * Fix crash on invalid friend for wallbrush * Option to remove empty spawns * Draws position indicator, some code cleanup * Teleport copy/paste improvements * Fix flood fill * Add function to get minimap/8bit color * Small code cleanup * Cleanup Position * Code cleanup and small optimizations * Code cleanup * Code cleanup and small optimizations (#406) * Cleanup and cast functions. * Avoid adding a new Unique ID if it already exists * Code cleanup and small optimizations * More changes and cleanup * Changes and cleanup * Some changes * Fix copy position. * Only show uid/aid alert if it really changed * Add actions history panel * Does not draw tooltips on minimap mode * Use constexpr * Replace items fix (#408) * Draw grid small optimization * Small change in selection box and Fix #409 * Fix some xpm * Ingame box improvements, add lights support. * Fix go to previous position (#410) * Fix depot crash (#411) * Fix XPMs * Export minimap as .otmm (otclient format) or .png (#413) * Fix glitch after drawing secondary map * fix * Update about_window.cpp * sonar * fix Minimap and progress bar * fix erro load items.xml * fix * fix doodad brush * fix: slightly more accurate house size estimation * feat: update items.otb and items.xml * fix: bad merge * Revert "feat: update items.otb and items.xml" This reverts commit 40f1edc70f17c423282d546a41541a8fe9b92dcc. --------- Co-authored-by: Nailson <Mignari@users.noreply.github.com> Co-authored-by: wtver <51377408+maattch@users.noreply.github.com> Co-authored-by: Majesty <32709570+majestyotbr@users.noreply.github.com> Co-authored-by: Luan Santos <github@luan.sh>
2023-10-09 23:02:37 -03:00
type->isTable = true;
type->brush = this;
TableType tt;
tt.item_id = id;
tt.chance = subChildNode.attribute("chance").as_int();
table_items[alignment].total_chance += tt.chance;
table_items[alignment].items.push_back(tt);
}
}
return true;
}
2023-10-09 19:12:16 -07:00
bool TableBrush::canDraw(BaseMap* map, const Position &position) const {
return true;
}
2023-10-09 19:12:16 -07:00
void TableBrush::undraw(BaseMap* map, Tile* t) {
ItemVector::iterator it = t->items.begin();
2023-10-09 19:12:16 -07:00
while (it != t->items.end()) {
if ((*it)->isTable()) {
TableBrush* tb = (*it)->getTableBrush();
2023-10-09 19:12:16 -07:00
if (tb == this) {
delete *it;
it = t->items.erase(it);
} else {
++it;
}
} else {
++it;
}
}
}
2023-10-09 19:12:16 -07:00
void TableBrush::draw(BaseMap* map, Tile* tile, void* parameter) {
undraw(map, tile); // Remove old
2023-10-09 19:12:16 -07:00
TableNode &tn = table_items[0];
if (tn.total_chance <= 0) {
return;
}
int chance = random(1, tn.total_chance);
uint16_t type = 0;
2023-10-09 19:12:16 -07:00
for (std::vector<TableType>::const_iterator table_iter = tn.items.begin(); table_iter != tn.items.end(); ++table_iter) {
if (chance <= table_iter->chance) {
type = table_iter->item_id;
break;
}
chance -= table_iter->chance;
}
2023-10-09 19:12:16 -07:00
if (type != 0) {
tile->addItem(Item::Create(type));
}
}
2023-10-09 19:12:16 -07:00
bool hasMatchingTableBrushAtTile(BaseMap* map, TableBrush* table_brush, uint32_t x, uint32_t y, uint32_t z) {
Tile* t = map->getTile(x, y, z);
2023-10-09 19:12:16 -07:00
if (!t) {
return false;
}
ItemVector::const_iterator it = t->items.begin();
2023-10-09 19:12:16 -07:00
for (; it != t->items.end(); ++it) {
TableBrush* tb = (*it)->getTableBrush();
2023-10-09 19:12:16 -07:00
if (tb == table_brush) {
return true;
}
}
return false;
}
2023-10-09 19:12:16 -07:00
void TableBrush::doTables(BaseMap* map, Tile* tile) {
ASSERT(tile);
2023-10-09 19:12:16 -07:00
if (!tile->hasTable()) {
return;
}
2023-10-09 19:12:16 -07:00
const Position &position = tile->getPosition();
2014-03-01 14:44:11 +01:00
int32_t x = position.x;
int32_t y = position.y;
int32_t z = position.z;
2023-10-09 19:12:16 -07:00
for (Item* item : tile->items) {
ASSERT(item);
2014-03-01 14:44:11 +01:00
TableBrush* table_brush = item->getTableBrush();
2023-10-09 19:12:16 -07:00
if (!table_brush) {
continue;
}
bool neighbours[8];
2023-10-09 19:12:16 -07:00
if (x == 0) {
if (y == 0) {
neighbours[0] = false;
neighbours[1] = false;
neighbours[2] = false;
neighbours[3] = false;
neighbours[4] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y, z);
neighbours[5] = false;
2023-10-09 19:12:16 -07:00
neighbours[6] = hasMatchingTableBrushAtTile(map, table_brush, x, y + 1, z);
neighbours[7] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y + 1, z);
} else {
neighbours[0] = false;
2023-10-09 19:12:16 -07:00
neighbours[1] = hasMatchingTableBrushAtTile(map, table_brush, x, y - 1, z);
neighbours[2] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y - 1, z);
neighbours[3] = false;
neighbours[4] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y, z);
neighbours[5] = false;
2023-10-09 19:12:16 -07:00
neighbours[6] = hasMatchingTableBrushAtTile(map, table_brush, x, y + 1, z);
neighbours[7] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y + 1, z);
}
2023-10-09 19:12:16 -07:00
} else if (y == 0) {
neighbours[0] = false;
neighbours[1] = false;
neighbours[2] = false;
neighbours[3] = hasMatchingTableBrushAtTile(map, table_brush, x - 1, y, z);
neighbours[4] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y, z);
neighbours[5] = hasMatchingTableBrushAtTile(map, table_brush, x - 1, y + 1, z);
2023-10-09 19:12:16 -07:00
neighbours[6] = hasMatchingTableBrushAtTile(map, table_brush, x, y + 1, z);
neighbours[7] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y + 1, z);
} else {
neighbours[0] = hasMatchingTableBrushAtTile(map, table_brush, x - 1, y - 1, z);
2023-10-09 19:12:16 -07:00
neighbours[1] = hasMatchingTableBrushAtTile(map, table_brush, x, y - 1, z);
neighbours[2] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y - 1, z);
neighbours[3] = hasMatchingTableBrushAtTile(map, table_brush, x - 1, y, z);
neighbours[4] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y, z);
neighbours[5] = hasMatchingTableBrushAtTile(map, table_brush, x - 1, y + 1, z);
2023-10-09 19:12:16 -07:00
neighbours[6] = hasMatchingTableBrushAtTile(map, table_brush, x, y + 1, z);
neighbours[7] = hasMatchingTableBrushAtTile(map, table_brush, x + 1, y + 1, z);
}
uint32_t tiledata = 0;
2023-10-09 19:12:16 -07:00
for (int32_t i = 0; i < 8; ++i) {
if (neighbours[i]) {
// Same table as this one, calculate what border
tiledata |= 1 << i;
}
}
2014-03-01 14:44:11 +01:00
BorderType bt = static_cast<BorderType>(table_types[tiledata]);
2023-10-09 19:12:16 -07:00
TableNode &tn = table_brush->table_items[static_cast<int32_t>(bt)];
if (tn.total_chance == 0) {
return;
}
2014-03-01 14:44:11 +01:00
int32_t chance = random(1, tn.total_chance);
uint16_t id = 0;
2014-03-01 14:44:11 +01:00
2023-10-09 19:12:16 -07:00
for (const TableType &tableType : tn.items) {
if (chance <= tableType.chance) {
2014-03-01 14:44:11 +01:00
id = tableType.item_id;
break;
}
2014-03-01 14:44:11 +01:00
chance -= tableType.chance;
}
2014-03-01 14:44:11 +01:00
2023-10-09 19:12:16 -07:00
if (id != 0) {
item->setID(id);
}
}
}