| @@ -0,0 +1,1004 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "thingtype.h" | |
| 24 | + | #include "game.h" | |
| 25 | + | #include "lightview.h" | |
| 26 | + | #include "spritemanager.h" | |
| 27 | + | ||
| 28 | + | #include <framework/core/filestream.h> | |
| 29 | + | #include <framework/graphics/framebuffermanager.h> | |
| 30 | + | #include <framework/graphics/graphics.h> | |
| 31 | + | #include <framework/graphics/image.h> | |
| 32 | + | #include <framework/graphics/shadermanager.h> | |
| 33 | + | #include <framework/graphics/texture.h> | |
| 34 | + | #include <framework/graphics/texturemanager.h> | |
| 35 | + | #include <framework/otml/otml.h> | |
| 36 | + | ||
| 37 | + | ThingType::ThingType() { | |
| 38 | + | m_category = ThingInvalidCategory; | |
| 39 | + | m_id = 0; | |
| 40 | + | m_null = true; | |
| 41 | + | m_exactSize = 0; | |
| 42 | + | m_realSize = 0; | |
| 43 | + | m_animator = nullptr; | |
| 44 | + | m_numPatternX = m_numPatternY = m_numPatternZ = 0; | |
| 45 | + | m_animationPhases = 0; | |
| 46 | + | m_layers = 0; | |
| 47 | + | m_elevation = 0; | |
| 48 | + | m_opacity = 1.0f; | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | void ThingType::serialize(const FileStreamPtr &fin) { | |
| 52 | + | for (int i = 0; i < ThingLastAttr; ++i) { | |
| 53 | + | if (!hasAttr((ThingAttr)i)) | |
| 54 | + | continue; | |
| 55 | + | ||
| 56 | + | int attr = i; | |
| 57 | + | if (g_game.getClientVersion() >= 780) { | |
| 58 | + | if (attr == ThingAttrChargeable) | |
| 59 | + | attr = ThingAttrWritable; | |
| 60 | + | else if (attr >= ThingAttrWritable) | |
| 61 | + | attr += 1; | |
| 62 | + | } else if (g_game.getClientVersion() >= 1000) { | |
| 63 | + | if (attr == ThingAttrNoMoveAnimation) | |
| 64 | + | attr = 16; | |
| 65 | + | else if (attr >= ThingAttrPickupable) | |
| 66 | + | attr += 1; | |
| 67 | + | } | |
| 68 | + | ||
| 69 | + | fin->addU8(attr); | |
| 70 | + | switch (attr) { | |
| 71 | + | case ThingAttrDisplacement: { | |
| 72 | + | fin->addU16(m_displacement.x); | |
| 73 | + | fin->addU16(m_displacement.y); | |
| 74 | + | break; | |
| 75 | + | } | |
| 76 | + | case ThingAttrLight: { | |
| 77 | + | Light light = m_attribs.get<Light>(attr); | |
| 78 | + | fin->addU16(light.intensity); | |
| 79 | + | fin->addU16(light.color); | |
| 80 | + | break; | |
| 81 | + | } | |
| 82 | + | case ThingAttrMarket: { | |
| 83 | + | MarketData market = m_attribs.get<MarketData>(attr); | |
| 84 | + | fin->addU16(market.category); | |
| 85 | + | fin->addU16(market.tradeAs); | |
| 86 | + | fin->addU16(market.showAs); | |
| 87 | + | fin->addString(market.name); | |
| 88 | + | fin->addU16(market.restrictVocation); | |
| 89 | + | fin->addU16(market.requiredLevel); | |
| 90 | + | break; | |
| 91 | + | } | |
| 92 | + | case ThingAttrUsable: | |
| 93 | + | case ThingAttrElevation: | |
| 94 | + | case ThingAttrGround: | |
| 95 | + | case ThingAttrWritable: | |
| 96 | + | case ThingAttrWritableOnce: | |
| 97 | + | case ThingAttrMinimapColor: | |
| 98 | + | case ThingAttrCloth: | |
| 99 | + | case ThingAttrLensHelp: | |
| 100 | + | fin->addU16(m_attribs.get<uint16>(attr)); | |
| 101 | + | break; | |
| 102 | + | default: | |
| 103 | + | break; | |
| 104 | + | }; | |
| 105 | + | } | |
| 106 | + | fin->addU8(ThingLastAttr); | |
| 107 | + | ||
| 108 | + | fin->addU8(m_size.width()); | |
| 109 | + | fin->addU8(m_size.height()); | |
| 110 | + | ||
| 111 | + | if (m_size.width() > 1 || m_size.height() > 1) | |
| 112 | + | fin->addU8(m_realSize); | |
| 113 | + | ||
| 114 | + | fin->addU8(m_layers); | |
| 115 | + | fin->addU8(m_numPatternX); | |
| 116 | + | fin->addU8(m_numPatternY); | |
| 117 | + | fin->addU8(m_numPatternZ); | |
| 118 | + | fin->addU8(m_animationPhases); | |
| 119 | + | ||
| 120 | + | if (g_game.getFeature(Otc::GameEnhancedAnimations)) { | |
| 121 | + | if (m_animationPhases > 1 && m_animator != nullptr) { | |
| 122 | + | m_animator->serialize(fin); | |
| 123 | + | } | |
| 124 | + | } | |
| 125 | + | ||
| 126 | + | for (uint i = 0; i < m_spritesIndex.size(); i++) { | |
| 127 | + | if (g_game.getFeature(Otc::GameSpritesU32)) | |
| 128 | + | fin->addU32(m_spritesIndex[i]); | |
| 129 | + | else | |
| 130 | + | fin->addU16(m_spritesIndex[i]); | |
| 131 | + | } | |
| 132 | + | } | |
| 133 | + | ||
| 134 | + | void ThingType::unserialize(uint16 clientId, ThingCategory category, | |
| 135 | + | const FileStreamPtr &fin) { | |
| 136 | + | m_null = false; | |
| 137 | + | m_id = clientId; | |
| 138 | + | m_category = category; | |
| 139 | + | ||
| 140 | + | int count = 0, attr = -1; | |
| 141 | + | bool done = false; | |
| 142 | + | for (int i = 0; i < ThingLastAttr; ++i) { | |
| 143 | + | count++; | |
| 144 | + | attr = fin->getU8(); | |
| 145 | + | if (attr == ThingLastAttr) { | |
| 146 | + | done = true; | |
| 147 | + | break; | |
| 148 | + | } | |
| 149 | + | ||
| 150 | + | if (g_game.getClientVersion() >= 1000) { | |
| 151 | + | /* In 10.10+ all attributes from 16 and up were | |
| 152 | + | * incremented by 1 to make space for 16 as | |
| 153 | + | * "No Movement Animation" flag. | |
| 154 | + | */ | |
| 155 | + | if (attr == 16) | |
| 156 | + | attr = ThingAttrNoMoveAnimation; | |
| 157 | + | else if (attr > 16) | |
| 158 | + | attr -= 1; | |
| 159 | + | } else if (g_game.getClientVersion() >= 860) { | |
| 160 | + | /* Default attribute values follow | |
| 161 | + | * the format of 8.6-9.86. | |
| 162 | + | * Therefore no changes here. | |
| 163 | + | */ | |
| 164 | + | } else if (g_game.getClientVersion() >= 780) { | |
| 165 | + | /* In 7.80-8.54 all attributes from 8 and higher were | |
| 166 | + | * incremented by 1 to make space for 8 as | |
| 167 | + | * "Item Charges" flag. | |
| 168 | + | */ | |
| 169 | + | if (attr == 8) { | |
| 170 | + | m_attribs.set(ThingAttrChargeable, true); | |
| 171 | + | continue; | |
| 172 | + | } else if (attr > 8) | |
| 173 | + | attr -= 1; | |
| 174 | + | } else if (g_game.getClientVersion() >= 755) { | |
| 175 | + | /* In 7.55-7.72 attributes 23 is "Floor Change". */ | |
| 176 | + | if (attr == 23) | |
| 177 | + | attr = ThingAttrFloorChange; | |
| 178 | + | } else if (g_game.getClientVersion() >= 740) { | |
| 179 | + | /* In 7.4-7.5 attribute "Ground Border" did not exist | |
| 180 | + | * attributes 1-15 have to be adjusted. | |
| 181 | + | * Several other changes in the format. | |
| 182 | + | */ | |
| 183 | + | if (attr > 0 && attr <= 15) | |
| 184 | + | attr += 1; | |
| 185 | + | else if (attr == 16) | |
| 186 | + | attr = ThingAttrLight; | |
| 187 | + | else if (attr == 17) | |
| 188 | + | attr = ThingAttrFloorChange; | |
| 189 | + | else if (attr == 18) | |
| 190 | + | attr = ThingAttrFullGround; | |
| 191 | + | else if (attr == 19) | |
| 192 | + | attr = ThingAttrElevation; | |
| 193 | + | else if (attr == 20) | |
| 194 | + | attr = ThingAttrDisplacement; | |
| 195 | + | else if (attr == 22) | |
| 196 | + | attr = ThingAttrMinimapColor; | |
| 197 | + | else if (attr == 23) | |
| 198 | + | attr = ThingAttrRotateable; | |
| 199 | + | else if (attr == 24) | |
| 200 | + | attr = ThingAttrLyingCorpse; | |
| 201 | + | else if (attr == 25) | |
| 202 | + | attr = ThingAttrHangable; | |
| 203 | + | else if (attr == 26) | |
| 204 | + | attr = ThingAttrHookSouth; | |
| 205 | + | else if (attr == 27) | |
| 206 | + | attr = ThingAttrHookEast; | |
| 207 | + | else if (attr == 28) | |
| 208 | + | attr = ThingAttrAnimateAlways; | |
| 209 | + | ||
| 210 | + | /* "Multi Use" and "Force Use" are swapped */ | |
| 211 | + | if (attr == ThingAttrMultiUse) | |
| 212 | + | attr = ThingAttrForceUse; | |
| 213 | + | else if (attr == ThingAttrForceUse) | |
| 214 | + | attr = ThingAttrMultiUse; | |
| 215 | + | } | |
| 216 | + | ||
| 217 | + | switch (attr) { | |
| 218 | + | case ThingAttrDisplacement: { | |
| 219 | + | if (g_game.getClientVersion() >= 755) { | |
| 220 | + | m_displacement.x = fin->getU16(); | |
| 221 | + | m_displacement.y = fin->getU16(); | |
| 222 | + | } else { | |
| 223 | + | m_displacement.x = 8; | |
| 224 | + | m_displacement.y = 8; | |
| 225 | + | } | |
| 226 | + | m_attribs.set(attr, true); | |
| 227 | + | break; | |
| 228 | + | } | |
| 229 | + | case ThingAttrLight: { | |
| 230 | + | Light light; | |
| 231 | + | light.intensity = fin->getU16(); | |
| 232 | + | light.color = fin->getU16(); | |
| 233 | + | m_attribs.set(attr, light); | |
| 234 | + | break; | |
| 235 | + | } | |
| 236 | + | case ThingAttrMarket: { | |
| 237 | + | MarketData market; | |
| 238 | + | market.category = fin->getU16(); | |
| 239 | + | market.tradeAs = fin->getU16(); | |
| 240 | + | market.showAs = fin->getU16(); | |
| 241 | + | market.name = fin->getString(); | |
| 242 | + | market.restrictVocation = fin->getU16(); | |
| 243 | + | market.requiredLevel = fin->getU16(); | |
| 244 | + | m_attribs.set(attr, market); | |
| 245 | + | break; | |
| 246 | + | } | |
| 247 | + | case ThingAttrElevation: { | |
| 248 | + | m_elevation = fin->getU16(); | |
| 249 | + | m_attribs.set(attr, m_elevation); | |
| 250 | + | break; | |
| 251 | + | } | |
| 252 | + | case ThingAttrUsable: | |
| 253 | + | case ThingAttrGround: | |
| 254 | + | case ThingAttrWritable: | |
| 255 | + | case ThingAttrWritableOnce: | |
| 256 | + | case ThingAttrMinimapColor: | |
| 257 | + | case ThingAttrCloth: | |
| 258 | + | case ThingAttrLensHelp: | |
| 259 | + | m_attribs.set(attr, fin->getU16()); | |
| 260 | + | break; | |
| 261 | + | case ThingAttrBones: { | |
| 262 | + | m_bones.resize(4); | |
| 263 | + | uint16 x = fin->getU16(), y = fin->getU16(); | |
| 264 | + | m_bones[Otc::North] = Point(x, y); | |
| 265 | + | x = fin->getU16(), y = fin->getU16(); | |
| 266 | + | m_bones[Otc::South] = Point(x, y); | |
| 267 | + | x = fin->getU16(), y = fin->getU16(); | |
| 268 | + | m_bones[Otc::East] = Point(x, y); | |
| 269 | + | x = fin->getU16(), y = fin->getU16(); | |
| 270 | + | m_bones[Otc::West] = Point(x, y); | |
| 271 | + | m_attribs.set(attr, true); | |
| 272 | + | break; | |
| 273 | + | } | |
| 274 | + | default: | |
| 275 | + | m_attribs.set(attr, true); | |
| 276 | + | break; | |
| 277 | + | }; | |
| 278 | + | } | |
| 279 | + | ||
| 280 | + | if (!done) | |
| 281 | + | stdext::throw_exception(stdext::format( | |
| 282 | + | "corrupt data (id: %d, category: %d, count: %d, lastAttr: %d)", m_id, | |
| 283 | + | m_category, count, attr)); | |
| 284 | + | ||
| 285 | + | bool hasFrameGroups = (category == ThingCategoryCreature && | |
| 286 | + | g_game.getFeature(Otc::GameIdleAnimations)); | |
| 287 | + | uint8 groupCount = hasFrameGroups ? fin->getU8() : 1; | |
| 288 | + | ||
| 289 | + | m_animationPhases = 0; | |
| 290 | + | int totalSpritesCount = 0; | |
| 291 | + | ||
| 292 | + | std::vector<Size> sizes; | |
| 293 | + | std::vector<int> total_sprites; | |
| 294 | + | ||
| 295 | + | for (int i = 0; i < groupCount; ++i) { | |
| 296 | + | uint8 frameGroupType = FrameGroupDefault; | |
| 297 | + | if (hasFrameGroups) | |
| 298 | + | frameGroupType = fin->getU8(); | |
| 299 | + | ||
| 300 | + | uint8 width = fin->getU8(); | |
| 301 | + | uint8 height = fin->getU8(); | |
| 302 | + | m_size = Size(width, height); | |
| 303 | + | sizes.push_back(m_size); | |
| 304 | + | if (width > 1 || height > 1) { | |
| 305 | + | m_realSize = fin->getU8(); | |
| 306 | + | m_exactSize = std::min<int>( | |
| 307 | + | m_realSize, std::max<int>(width * g_sprites.spriteSize(), | |
| 308 | + | height * g_sprites.spriteSize())); | |
| 309 | + | } else | |
| 310 | + | m_exactSize = g_sprites.spriteSize(); | |
| 311 | + | ||
| 312 | + | m_layers = fin->getU8(); | |
| 313 | + | m_numPatternX = fin->getU8(); | |
| 314 | + | m_numPatternY = fin->getU8(); | |
| 315 | + | if (g_game.getClientVersion() >= 755) | |
| 316 | + | m_numPatternZ = fin->getU8(); | |
| 317 | + | else | |
| 318 | + | m_numPatternZ = 1; | |
| 319 | + | ||
| 320 | + | int groupAnimationsPhases = fin->getU8(); | |
| 321 | + | m_animationPhases += groupAnimationsPhases; | |
| 322 | + | ||
| 323 | + | if (groupAnimationsPhases > 1 && | |
| 324 | + | g_game.getFeature(Otc::GameEnhancedAnimations)) { | |
| 325 | + | AnimatorPtr animator = AnimatorPtr(new Animator); | |
| 326 | + | animator->unserialize(groupAnimationsPhases, fin); | |
| 327 | + | ||
| 328 | + | switch (frameGroupType) { | |
| 329 | + | case FrameGroupIdle: | |
| 330 | + | m_idleAnimator = animator; | |
| 331 | + | break; | |
| 332 | + | case FrameGroupMoving: | |
| 333 | + | m_animator = animator; | |
| 334 | + | break; | |
| 335 | + | } | |
| 336 | + | } | |
| 337 | + | ||
| 338 | + | int totalSprites = m_size.area() * m_layers * m_numPatternX * | |
| 339 | + | m_numPatternY * m_numPatternZ * groupAnimationsPhases; | |
| 340 | + | total_sprites.push_back(totalSprites); | |
| 341 | + | ||
| 342 | + | if ((totalSpritesCount + totalSprites) > 12288) | |
| 343 | + | stdext::throw_exception("a thing type has more than 12288 sprites"); | |
| 344 | + | ||
| 345 | + | m_spritesIndex.resize((totalSpritesCount + totalSprites)); | |
| 346 | + | for (int i = totalSpritesCount; i < (totalSpritesCount + totalSprites); i++) | |
| 347 | + | m_spritesIndex[i] = g_game.getFeature(Otc::GameSpritesU32) | |
| 348 | + | ? fin->getU32() | |
| 349 | + | : fin->getU16(); | |
| 350 | + | ||
| 351 | + | totalSpritesCount += totalSprites; | |
| 352 | + | } | |
| 353 | + | ||
| 354 | + | if (sizes.size() > 1) { | |
| 355 | + | // correction for some sprites | |
| 356 | + | for (auto &s : sizes) { | |
| 357 | + | m_size.setWidth(std::max<int>(m_size.width(), s.width())); | |
| 358 | + | m_size.setHeight(std::max<int>(m_size.height(), s.height())); | |
| 359 | + | } | |
| 360 | + | size_t expectedSize = m_size.area() * m_layers * m_numPatternX * | |
| 361 | + | m_numPatternY * m_numPatternZ * m_animationPhases; | |
| 362 | + | if (expectedSize != m_spritesIndex.size()) { | |
| 363 | + | std::vector<int> sprites(std::move(m_spritesIndex)); | |
| 364 | + | m_spritesIndex.clear(); | |
| 365 | + | m_spritesIndex.reserve(expectedSize); | |
| 366 | + | for (size_t i = 0, idx = 0; i < sizes.size(); ++i) { | |
| 367 | + | int totalSprites = total_sprites[i]; | |
| 368 | + | if (m_size == sizes[i]) { | |
| 369 | + | for (int j = 0; j < totalSprites; ++j) { | |
| 370 | + | m_spritesIndex.push_back(sprites[idx++]); | |
| 371 | + | } | |
| 372 | + | continue; | |
| 373 | + | } | |
| 374 | + | size_t patterns = (totalSprites / sizes[i].area()); | |
| 375 | + | for (size_t p = 0; p < patterns; ++p) { | |
| 376 | + | for (int x = 0; x < m_size.width(); ++x) { | |
| 377 | + | for (int y = 0; y < m_size.height(); ++y) { | |
| 378 | + | if (x < sizes[i].width() && y < sizes[i].height()) { | |
| 379 | + | m_spritesIndex.push_back(sprites[idx++]); | |
| 380 | + | continue; | |
| 381 | + | } | |
| 382 | + | m_spritesIndex.push_back(0); | |
| 383 | + | } | |
| 384 | + | } | |
| 385 | + | } | |
| 386 | + | } | |
| 387 | + | // if (m_spritesIndex.size() != expectedSize) { | |
| 388 | + | // g_logger.warning(stdext::format("Wrong thingtype: %i - %i - %i", | |
| 389 | + | // clientId, m_spritesIndex.size(), expectedSize)); | |
| 390 | + | // } | |
| 391 | + | } | |
| 392 | + | } | |
| 393 | + | ||
| 394 | + | if (m_idleAnimator && !m_animator) { | |
| 395 | + | m_animator = m_idleAnimator; | |
| 396 | + | m_idleAnimator = nullptr; | |
| 397 | + | } | |
| 398 | + | ||
| 399 | + | m_textures.resize(m_animationPhases); | |
| 400 | + | m_texturesFramesRects.resize(m_animationPhases); | |
| 401 | + | m_texturesFramesOriginRects.resize(m_animationPhases); | |
| 402 | + | m_texturesFramesOffsets.resize(m_animationPhases); | |
| 403 | + | ||
| 404 | + | m_lastUsage = g_clock.seconds(); | |
| 405 | + | } | |
| 406 | + | ||
| 407 | + | void ThingType::exportImage(std::string fileName) { | |
| 408 | + | if (m_null) | |
| 409 | + | stdext::throw_exception("cannot export null thingtype"); | |
| 410 | + | ||
| 411 | + | if (m_spritesIndex.size() == 0) | |
| 412 | + | stdext::throw_exception("cannot export thingtype without sprites"); | |
| 413 | + | ||
| 414 | + | size_t spriteSize = g_sprites.spriteSize(); | |
| 415 | + | ||
| 416 | + | ImagePtr image( | |
| 417 | + | new Image(Size(spriteSize * m_size.width() * m_layers * m_numPatternX, | |
| 418 | + | spriteSize * m_size.height() * m_animationPhases * | |
| 419 | + | m_numPatternY * m_numPatternZ))); | |
| 420 | + | for (int z = 0; z < m_numPatternZ; ++z) { | |
| 421 | + | for (int y = 0; y < m_numPatternY; ++y) { | |
| 422 | + | for (int x = 0; x < m_numPatternX; ++x) { | |
| 423 | + | for (int l = 0; l < m_layers; ++l) { | |
| 424 | + | for (int a = 0; a < m_animationPhases; ++a) { | |
| 425 | + | for (int w = 0; w < m_size.width(); ++w) { | |
| 426 | + | for (int h = 0; h < m_size.height(); ++h) { | |
| 427 | + | image->blit( | |
| 428 | + | Point(spriteSize * | |
| 429 | + | (m_size.width() - w - 1 + m_size.width() * x + | |
| 430 | + | m_size.width() * m_numPatternX * l), | |
| 431 | + | spriteSize * | |
| 432 | + | (m_size.height() - h - 1 + m_size.height() * y + | |
| 433 | + | m_size.height() * m_numPatternY * a + | |
| 434 | + | m_size.height() * m_numPatternY * | |
| 435 | + | m_animationPhases * z)), | |
| 436 | + | g_sprites.getSpriteImage( | |
| 437 | + | m_spritesIndex[getSpriteIndex(w, h, l, x, y, z, a)])); | |
| 438 | + | } | |
| 439 | + | } | |
| 440 | + | } | |
| 441 | + | } | |
| 442 | + | } | |
| 443 | + | } | |
| 444 | + | } | |
| 445 | + | ||
| 446 | + | image->savePNG(fileName); | |
| 447 | + | } | |
| 448 | + | ||
| 449 | + | void ThingType::replaceSprites(std::map<uint32_t, ImagePtr> &replacements, | |
| 450 | + | std::string fileName) { | |
| 451 | + | if (m_null) | |
| 452 | + | stdext::throw_exception("cannot export null thingtype"); | |
| 453 | + | ||
| 454 | + | if (m_spritesIndex.size() == 0) | |
| 455 | + | stdext::throw_exception("cannot export thingtype without sprites"); | |
| 456 | + | ||
| 457 | + | size_t spriteSize = g_sprites.spriteSize(); | |
| 458 | + | ||
| 459 | + | ImagePtr image = Image::loadPNG(fileName); | |
| 460 | + | if (!image) | |
| 461 | + | stdext::throw_exception( | |
| 462 | + | stdext::format("can't load image from %s", fileName)); | |
| 463 | + | ||
| 464 | + | for (int z = 0; z < m_numPatternZ; ++z) { | |
| 465 | + | for (int y = 0; y < m_numPatternY; ++y) { | |
| 466 | + | for (int x = 0; x < m_numPatternX; ++x) { | |
| 467 | + | for (int l = 0; l < m_layers; ++l) { | |
| 468 | + | for (int a = 0; a < m_animationPhases; ++a) { | |
| 469 | + | for (int w = 0; w < m_size.width(); ++w) { | |
| 470 | + | for (int h = 0; h < m_size.height(); ++h) { | |
| 471 | + | uint32_t sprite = | |
| 472 | + | m_spritesIndex[getSpriteIndex(w, h, l, x, y, z, a)]; | |
| 473 | + | ImagePtr orgSprite = g_sprites.getSpriteImage( | |
| 474 | + | m_spritesIndex[getSpriteIndex(w, h, l, x, y, z, a)]); | |
| 475 | + | if (!orgSprite) | |
| 476 | + | continue; | |
| 477 | + | Point src(spriteSize * | |
| 478 | + | (m_size.width() - w - 1 + m_size.width() * x + | |
| 479 | + | m_size.width() * m_numPatternX * l), | |
| 480 | + | spriteSize * | |
| 481 | + | (m_size.height() - h - 1 + m_size.height() * y + | |
| 482 | + | m_size.height() * m_numPatternY * a + | |
| 483 | + | m_size.height() * m_numPatternY * | |
| 484 | + | m_animationPhases * z)); | |
| 485 | + | src = src * 2; | |
| 486 | + | ImagePtr newSprite(new Image(Size(orgSprite->getSize() * 2))); | |
| 487 | + | for (int x = 0; x < newSprite->getSize().width(); ++x) { | |
| 488 | + | for (int y = 0; y < newSprite->getSize().height(); ++y) { | |
| 489 | + | newSprite->setPixel(x, y, | |
| 490 | + | image->getPixel(src.x + x, src.y + y)); | |
| 491 | + | } | |
| 492 | + | } | |
| 493 | + | replacements[sprite] = newSprite; | |
| 494 | + | } | |
| 495 | + | } | |
| 496 | + | } | |
| 497 | + | } | |
| 498 | + | } | |
| 499 | + | } | |
| 500 | + | } | |
| 501 | + | } | |
| 502 | + | ||
| 503 | + | void ThingType::unserializeOtml(const OTMLNodePtr &node) { | |
| 504 | + | for (const OTMLNodePtr &node2 : node->children()) { | |
| 505 | + | if (node2->tag() == "opacity") | |
| 506 | + | m_opacity = node2->value<float>(); | |
| 507 | + | else if (node2->tag() == "notprewalkable") | |
| 508 | + | m_attribs.set(ThingAttrNotPreWalkable, node2->value<bool>()); | |
| 509 | + | else if (node2->tag() == "image") | |
| 510 | + | m_customImage = node2->value(); | |
| 511 | + | else if (node2->tag() == "full-ground") { | |
| 512 | + | if (node2->value<bool>()) | |
| 513 | + | m_attribs.set(ThingAttrFullGround, true); | |
| 514 | + | else | |
| 515 | + | m_attribs.remove(ThingAttrFullGround); | |
| 516 | + | } | |
| 517 | + | } | |
| 518 | + | } | |
| 519 | + | ||
| 520 | + | void ThingType::unload() { | |
| 521 | + | m_textures.clear(); | |
| 522 | + | m_texturesFramesRects.clear(); | |
| 523 | + | m_texturesFramesOriginRects.clear(); | |
| 524 | + | m_texturesFramesOffsets.clear(); | |
| 525 | + | ||
| 526 | + | m_textures.resize(m_animationPhases); | |
| 527 | + | m_texturesFramesRects.resize(m_animationPhases); | |
| 528 | + | m_texturesFramesOriginRects.resize(m_animationPhases); | |
| 529 | + | m_texturesFramesOffsets.resize(m_animationPhases); | |
| 530 | + | ||
| 531 | + | m_loaded = false; | |
| 532 | + | } | |
| 533 | + | ||
| 534 | + | DrawQueueItem *ThingType::draw(const Point &dest, int layer, int xPattern, | |
| 535 | + | int yPattern, int zPattern, int animationPhase, | |
| 536 | + | Color color, LightView *lightView) { | |
| 537 | + | if (m_null) | |
| 538 | + | return nullptr; | |
| 539 | + | ||
| 540 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 541 | + | return nullptr; | |
| 542 | + | ||
| 543 | + | const TexturePtr &texture = getTexture( | |
| 544 | + | animationPhase); // texture might not exists, neither its rects. | |
| 545 | + | if (!texture) | |
| 546 | + | return nullptr; | |
| 547 | + | ||
| 548 | + | uint frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 549 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size()) | |
| 550 | + | return nullptr; | |
| 551 | + | ||
| 552 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 553 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 554 | + | ||
| 555 | + | // FIX: If the textureRect is empty (size 0), all sprites for this frame were | |
| 556 | + | // ID=0 (transparent/no sprite). Skip drawing so the tile shows transparency | |
| 557 | + | // (floor below), not a black quad. | |
| 558 | + | if (!textureRect.isValid() || textureRect.isEmpty()) | |
| 559 | + | return nullptr; | |
| 560 | + | ||
| 561 | + | Rect screenRect( | |
| 562 | + | dest + (textureOffset - m_displacement * g_sprites.getOffsetFactor() - | |
| 563 | + | (m_size.toPoint() - Point(1, 1)) * g_sprites.spriteSize()), | |
| 564 | + | textureRect.size()); | |
| 565 | + | ||
| 566 | + | bool useOpacity = m_opacity < 1.0f; | |
| 567 | + | if (useOpacity) | |
| 568 | + | color.setAlpha(m_opacity); | |
| 569 | + | ||
| 570 | + | if (lightView && hasLight()) | |
| 571 | + | lightView->addLight(screenRect.center(), getLight()); | |
| 572 | + | ||
| 573 | + | return g_drawQueue->addTexturedRect(screenRect, texture, textureRect, color); | |
| 574 | + | } | |
| 575 | + | ||
| 576 | + | DrawQueueItem *ThingType::draw(const Rect &dest, int layer, int xPattern, | |
| 577 | + | int yPattern, int zPattern, int animationPhase, | |
| 578 | + | Color color) { | |
| 579 | + | if (m_null) | |
| 580 | + | return nullptr; | |
| 581 | + | ||
| 582 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 583 | + | return nullptr; | |
| 584 | + | ||
| 585 | + | const TexturePtr &texture = getTexture( | |
| 586 | + | animationPhase); // texture might not exists, neither its rects. | |
| 587 | + | if (!texture) | |
| 588 | + | return nullptr; | |
| 589 | + | ||
| 590 | + | uint frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 591 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size()) | |
| 592 | + | return nullptr; | |
| 593 | + | ||
| 594 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 595 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 596 | + | ||
| 597 | + | bool useOpacity = m_opacity < 1.0f; | |
| 598 | + | if (useOpacity) | |
| 599 | + | color.setAlpha(m_opacity); | |
| 600 | + | ||
| 601 | + | Size size = m_size * g_sprites.spriteSize(); | |
| 602 | + | if (!size.isValid()) | |
| 603 | + | return nullptr; | |
| 604 | + | ||
| 605 | + | // size correction for some too big items | |
| 606 | + | if ((m_size.width() > 1 || m_size.height() > 1) && | |
| 607 | + | textureRect.width() <= g_sprites.spriteSize() && | |
| 608 | + | textureRect.height() <= g_sprites.spriteSize()) { | |
| 609 | + | size = Size(g_sprites.spriteSize(), g_sprites.spriteSize()); | |
| 610 | + | textureOffset = Point( | |
| 611 | + | (g_sprites.spriteSize() - textureRect.width()) / m_size.width(), | |
| 612 | + | (g_sprites.spriteSize() - textureRect.height()) / m_size.height()); | |
| 613 | + | } | |
| 614 | + | ||
| 615 | + | float scale = std::min<float>((float)dest.width() / size.width(), | |
| 616 | + | (float)dest.height() / size.height()); | |
| 617 | + | return g_drawQueue->addTexturedRect( | |
| 618 | + | Rect(dest.topLeft() + (textureOffset * scale), | |
| 619 | + | textureRect.size() * scale), | |
| 620 | + | texture, textureRect, color); | |
| 621 | + | } | |
| 622 | + | ||
| 623 | + | std::shared_ptr<DrawOutfitParams> | |
| 624 | + | ThingType::drawOutfit(const Point &dest, int maskLayer, int xPattern, | |
| 625 | + | int yPattern, int zPattern, int animationPhase, | |
| 626 | + | Color color, LightView *lightView) { | |
| 627 | + | if (m_null) | |
| 628 | + | return nullptr; | |
| 629 | + | ||
| 630 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 631 | + | return nullptr; | |
| 632 | + | ||
| 633 | + | const TexturePtr &texture = getTexture( | |
| 634 | + | animationPhase); // texture might not exists, neither its rects. | |
| 635 | + | if (!texture) | |
| 636 | + | return nullptr; | |
| 637 | + | ||
| 638 | + | uint frameIndex = getTextureIndex(0, xPattern, yPattern, zPattern); | |
| 639 | + | uint frameIndex2 = getTextureIndex(maskLayer, xPattern, yPattern, zPattern); | |
| 640 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size() || | |
| 641 | + | frameIndex2 >= m_texturesFramesRects[animationPhase].size()) | |
| 642 | + | return nullptr; | |
| 643 | + | ||
| 644 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 645 | + | Point textureOffset2 = m_texturesFramesOffsets[animationPhase][frameIndex2]; | |
| 646 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 647 | + | Rect textureRect2 = m_texturesFramesRects[animationPhase][frameIndex2]; | |
| 648 | + | Size size = textureRect.size(); | |
| 649 | + | if (!size.isValid()) | |
| 650 | + | return nullptr; | |
| 651 | + | Rect screenRect( | |
| 652 | + | dest + (textureOffset - m_displacement * g_sprites.getOffsetFactor() - | |
| 653 | + | (m_size.toPoint() - Point(1, 1)) * g_sprites.spriteSize()), | |
| 654 | + | textureRect.size()); | |
| 655 | + | ||
| 656 | + | bool useOpacity = m_opacity < 1.0f; | |
| 657 | + | if (useOpacity) | |
| 658 | + | color.setAlpha(m_opacity); | |
| 659 | + | ||
| 660 | + | if (lightView && hasLight()) | |
| 661 | + | lightView->addLight(screenRect.center(), getLight()); | |
| 662 | + | ||
| 663 | + | Point offset = textureOffset - textureOffset2; | |
| 664 | + | offset += textureRect2.topLeft() - textureRect.topLeft(); | |
| 665 | + | ||
| 666 | + | return std::shared_ptr<DrawOutfitParams>( | |
| 667 | + | new DrawOutfitParams{screenRect, texture, textureRect, offset, color}); | |
| 668 | + | } | |
| 669 | + | ||
| 670 | + | Rect ThingType::getDrawSize(const Point &dest, int layer, int xPattern, | |
| 671 | + | int yPattern, int zPattern, int animationPhase) { | |
| 672 | + | if (m_null) | |
| 673 | + | return Rect(0, 0, 1, 1); | |
| 674 | + | ||
| 675 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 676 | + | return Rect(0, 0, 1, 1); | |
| 677 | + | ||
| 678 | + | const TexturePtr &texture = getTexture( | |
| 679 | + | animationPhase); // texture might not exists, neither its rects. | |
| 680 | + | if (!texture) | |
| 681 | + | return Rect(0, 0, 1, 1); | |
| 682 | + | ||
| 683 | + | uint frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 684 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size()) | |
| 685 | + | return Rect(0, 0, 1, 1); | |
| 686 | + | ||
| 687 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 688 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 689 | + | return Rect(dest + textureOffset - m_displacement - | |
| 690 | + | (m_size.toPoint() - Point(1, 1)) * g_sprites.spriteSize(), | |
| 691 | + | textureRect.size()); | |
| 692 | + | } | |
| 693 | + | ||
| 694 | + | void ThingType::drawWithShader(const Point &dest, int layer, int xPattern, | |
| 695 | + | int yPattern, int zPattern, int animationPhase, | |
| 696 | + | const std::string &shader, Color color, | |
| 697 | + | LightView *lightView) { | |
| 698 | + | if (m_null) | |
| 699 | + | return; | |
| 700 | + | ||
| 701 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 702 | + | return; | |
| 703 | + | ||
| 704 | + | const TexturePtr &texture = getTexture( | |
| 705 | + | animationPhase); // texture might not exists, neither its rects. | |
| 706 | + | if (!texture) | |
| 707 | + | return; | |
| 708 | + | ||
| 709 | + | uint frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 710 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size()) | |
| 711 | + | return; | |
| 712 | + | ||
| 713 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 714 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 715 | + | ||
| 716 | + | Rect screenRect( | |
| 717 | + | dest + (textureOffset - m_displacement * g_sprites.getOffsetFactor() - | |
| 718 | + | (m_size.toPoint() - Point(1, 1)) * g_sprites.spriteSize()), | |
| 719 | + | textureRect.size()); | |
| 720 | + | ||
| 721 | + | bool useOpacity = m_opacity < 1.0f; | |
| 722 | + | if (useOpacity) | |
| 723 | + | color.setAlpha(m_opacity); | |
| 724 | + | ||
| 725 | + | if (lightView && hasLight()) | |
| 726 | + | lightView->addLight(screenRect.center(), getLight()); | |
| 727 | + | ||
| 728 | + | DrawQueueItemTexturedRect *thing = new DrawQueueItemThingWithShader( | |
| 729 | + | screenRect, texture, textureRect, textureOffset, screenRect.center(), 0, | |
| 730 | + | shader); | |
| 731 | + | g_drawQueue->add(thing); | |
| 732 | + | ||
| 733 | + | // return g_drawQueue->addTexturedRect(screenRect, texture, textureRect, | |
| 734 | + | // color); | |
| 735 | + | } | |
| 736 | + | ||
| 737 | + | void ThingType::drawWithShader(const Rect &dest, int layer, int xPattern, | |
| 738 | + | int yPattern, int zPattern, int animationPhase, | |
| 739 | + | const std::string &shader, Color color) { | |
| 740 | + | if (m_null) | |
| 741 | + | return; | |
| 742 | + | ||
| 743 | + | if (animationPhase < 0 || animationPhase >= m_animationPhases) | |
| 744 | + | return; | |
| 745 | + | ||
| 746 | + | const TexturePtr &texture = getTexture( | |
| 747 | + | animationPhase); // texture might not exists, neither its rects. | |
| 748 | + | if (!texture) | |
| 749 | + | return; | |
| 750 | + | ||
| 751 | + | uint frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 752 | + | if (frameIndex >= m_texturesFramesRects[animationPhase].size()) | |
| 753 | + | return; | |
| 754 | + | ||
| 755 | + | Point textureOffset = m_texturesFramesOffsets[animationPhase][frameIndex]; | |
| 756 | + | Rect textureRect = m_texturesFramesRects[animationPhase][frameIndex]; | |
| 757 | + | ||
| 758 | + | bool useOpacity = m_opacity < 1.0f; | |
| 759 | + | if (useOpacity) | |
| 760 | + | color.setAlpha(m_opacity); | |
| 761 | + | ||
| 762 | + | Size size = m_size * g_sprites.spriteSize(); | |
| 763 | + | if (!size.isValid()) | |
| 764 | + | return; | |
| 765 | + | ||
| 766 | + | // size correction for some too big items | |
| 767 | + | if ((m_size.width() > 1 || m_size.height() > 1) && | |
| 768 | + | textureRect.width() <= g_sprites.spriteSize() && | |
| 769 | + | textureRect.height() <= g_sprites.spriteSize()) { | |
| 770 | + | size = Size(g_sprites.spriteSize(), g_sprites.spriteSize()); | |
| 771 | + | textureOffset = Point( | |
| 772 | + | (g_sprites.spriteSize() - textureRect.width()) / m_size.width(), | |
| 773 | + | (g_sprites.spriteSize() - textureRect.height()) / m_size.height()); | |
| 774 | + | } | |
| 775 | + | ||
| 776 | + | float scale = std::min<float>((float)dest.width() / size.width(), | |
| 777 | + | (float)dest.height() / size.height()); | |
| 778 | + | ||
| 779 | + | Rect screenRect = Rect(dest.topLeft() + (textureOffset * scale), | |
| 780 | + | textureRect.size() * scale); | |
| 781 | + | DrawQueueItemTexturedRect *thing = new DrawQueueItemThingWithShader( | |
| 782 | + | screenRect, texture, textureRect, textureOffset, screenRect.center(), 0, | |
| 783 | + | shader); | |
| 784 | + | g_drawQueue->add(thing); | |
| 785 | + | ||
| 786 | + | // return g_drawQueue->addTexturedRect(Rect(dest.topLeft() + (textureOffset * | |
| 787 | + | // scale), textureRect.size() * scale), texture, textureRect, color); | |
| 788 | + | } | |
| 789 | + | ||
| 790 | + | const TexturePtr &ThingType::getTexture(int animationPhase) { | |
| 791 | + | m_lastUsage = g_clock.seconds(); | |
| 792 | + | ||
| 793 | + | int spriteSize = g_sprites.spriteSize(); | |
| 794 | + | TexturePtr &animationPhaseTexture = m_textures[animationPhase]; | |
| 795 | + | if (!animationPhaseTexture) { | |
| 796 | + | bool useCustomImage = false; | |
| 797 | + | if (animationPhase == 0 && !m_customImage.empty()) | |
| 798 | + | useCustomImage = true; | |
| 799 | + | ||
| 800 | + | // we don't need layers in common items, they will be pre-drawn | |
| 801 | + | int textureLayers = 1; | |
| 802 | + | int numLayers = m_layers; | |
| 803 | + | if (m_category == ThingCategoryCreature && numLayers >= 2) { | |
| 804 | + | // otcv8 optimization from 5 to 2 layers | |
| 805 | + | textureLayers = 2; | |
| 806 | + | numLayers = 2; | |
| 807 | + | } | |
| 808 | + | ||
| 809 | + | int indexSize = | |
| 810 | + | textureLayers * m_numPatternX * m_numPatternY * m_numPatternZ; | |
| 811 | + | Size textureSize = | |
| 812 | + | getBestTextureDimension(m_size.width(), m_size.height(), indexSize); | |
| 813 | + | ImagePtr fullImage; | |
| 814 | + | ||
| 815 | + | if (useCustomImage) | |
| 816 | + | fullImage = Image::load(m_customImage); | |
| 817 | + | else | |
| 818 | + | fullImage = ImagePtr(new Image(textureSize * spriteSize)); | |
| 819 | + | ||
| 820 | + | m_texturesFramesRects[animationPhase].resize(indexSize); | |
| 821 | + | m_texturesFramesOriginRects[animationPhase].resize(indexSize); | |
| 822 | + | m_texturesFramesOffsets[animationPhase].resize(indexSize); | |
| 823 | + | ||
| 824 | + | for (int z = 0; z < m_numPatternZ; ++z) { | |
| 825 | + | for (int y = 0; y < m_numPatternY; ++y) { | |
| 826 | + | for (int x = 0; x < m_numPatternX; ++x) { | |
| 827 | + | for (int l = 0; l < numLayers; ++l) { | |
| 828 | + | bool spriteMask = (m_category == ThingCategoryCreature && l > 0); | |
| 829 | + | int frameIndex = getTextureIndex(l % textureLayers, x, y, z); | |
| 830 | + | Point framePos = | |
| 831 | + | Point(frameIndex % (textureSize.width() / m_size.width()) * | |
| 832 | + | m_size.width(), | |
| 833 | + | frameIndex / (textureSize.width() / m_size.width()) * | |
| 834 | + | m_size.height()) * | |
| 835 | + | spriteSize; | |
| 836 | + | ||
| 837 | + | if (!useCustomImage) { | |
| 838 | + | for (int h = 0; h < m_size.height(); ++h) { | |
| 839 | + | for (int w = 0; w < m_size.width(); ++w) { | |
| 840 | + | uint spriteIndex = getSpriteIndex(w, h, spriteMask ? 1 : l, x, | |
| 841 | + | y, z, animationPhase); | |
| 842 | + | ImagePtr spriteImage = | |
| 843 | + | g_sprites.getSpriteImage(m_spritesIndex[spriteIndex]); | |
| 844 | + | if (!spriteImage) { | |
| 845 | + | continue; | |
| 846 | + | } | |
| 847 | + | Point spritePos = | |
| 848 | + | Point(m_size.width() - w - 1, m_size.height() - h - 1) * | |
| 849 | + | spriteSize; | |
| 850 | + | fullImage->blit(framePos + spritePos, spriteImage); | |
| 851 | + | } | |
| 852 | + | } | |
| 853 | + | } | |
| 854 | + | ||
| 855 | + | Rect drawRect( | |
| 856 | + | framePos + Point(m_size.width(), m_size.height()) * spriteSize - | |
| 857 | + | Point(1, 1), | |
| 858 | + | framePos); | |
| 859 | + | for (int x = framePos.x; | |
| 860 | + | x < framePos.x + m_size.width() * spriteSize; ++x) { | |
| 861 | + | for (int y = framePos.y; | |
| 862 | + | y < framePos.y + m_size.height() * spriteSize; ++y) { | |
| 863 | + | uint8 *p = fullImage->getPixel(x, y); | |
| 864 | + | if (p[3] != 0x00) { | |
| 865 | + | drawRect.setTop(std::min<int>(y, (int)drawRect.top())); | |
| 866 | + | drawRect.setLeft(std::min<int>(x, (int)drawRect.left())); | |
| 867 | + | drawRect.setBottom(std::max<int>(y, (int)drawRect.bottom())); | |
| 868 | + | drawRect.setRight(std::max<int>(x, (int)drawRect.right())); | |
| 869 | + | } | |
| 870 | + | } | |
| 871 | + | } | |
| 872 | + | ||
| 873 | + | // FIX: If drawRect was never updated (all sprites are ID=0 / fully | |
| 874 | + | // transparent), it remains in an inverted invalid state (top > | |
| 875 | + | // bottom, left > right). An invalid rect causes draw() to render a | |
| 876 | + | // black quad instead of nothing. We reset it to an empty rect so | |
| 877 | + | // draw() correctly skips rendering this frame. | |
| 878 | + | if (!drawRect.isValid()) { | |
| 879 | + | drawRect = Rect(framePos, Size(0, 0)); | |
| 880 | + | } | |
| 881 | + | ||
| 882 | + | m_texturesFramesRects[animationPhase][frameIndex] = drawRect; | |
| 883 | + | m_texturesFramesOriginRects[animationPhase][frameIndex] = | |
| 884 | + | Rect(framePos, Size(m_size.width(), m_size.height()) * | |
| 885 | + | spriteSize); // *0.5; | |
| 886 | + | m_texturesFramesOffsets[animationPhase][frameIndex] = | |
| 887 | + | (drawRect.isValid() ? drawRect.topLeft() - framePos | |
| 888 | + | : Point(0, 0)); | |
| 889 | + | } | |
| 890 | + | } | |
| 891 | + | } | |
| 892 | + | } | |
| 893 | + | animationPhaseTexture = | |
| 894 | + | TexturePtr(new Texture(fullImage, true, false, true)); | |
| 895 | + | m_loaded = true; | |
| 896 | + | } | |
| 897 | + | return animationPhaseTexture; | |
| 898 | + | } | |
| 899 | + | ||
| 900 | + | Size ThingType::getBestTextureDimension(int w, int h, int count) { | |
| 901 | + | const int MAX = g_sprites.spriteSize(); | |
| 902 | + | ||
| 903 | + | int k = 1; | |
| 904 | + | while (k < w) | |
| 905 | + | k <<= 1; | |
| 906 | + | w = k; | |
| 907 | + | ||
| 908 | + | k = 1; | |
| 909 | + | while (k < h) | |
| 910 | + | k <<= 1; | |
| 911 | + | h = k; | |
| 912 | + | ||
| 913 | + | int numSprites = w * h * count; | |
| 914 | + | VALIDATE(numSprites <= MAX * MAX); | |
| 915 | + | VALIDATE(w <= MAX); | |
| 916 | + | VALIDATE(h <= MAX); | |
| 917 | + | ||
| 918 | + | Size bestDimension = Size(MAX, MAX); | |
| 919 | + | for (int i = w; i <= MAX; i <<= 1) { | |
| 920 | + | for (int j = h; j <= MAX; j <<= 1) { | |
| 921 | + | Size candidateDimension = Size(i, j); | |
| 922 | + | if (candidateDimension.area() < numSprites) | |
| 923 | + | continue; | |
| 924 | + | if ((candidateDimension.area() < bestDimension.area()) || | |
| 925 | + | (candidateDimension.area() == bestDimension.area() && | |
| 926 | + | candidateDimension.width() + candidateDimension.height() < | |
| 927 | + | bestDimension.width() + bestDimension.height())) | |
| 928 | + | bestDimension = candidateDimension; | |
| 929 | + | } | |
| 930 | + | } | |
| 931 | + | ||
| 932 | + | return bestDimension; | |
| 933 | + | } | |
| 934 | + | ||
| 935 | + | uint ThingType::getSpriteIndex(int w, int h, int l, int x, int y, int z, | |
| 936 | + | int a) { | |
| 937 | + | uint index = | |
| 938 | + | ((((((a % m_animationPhases) * m_numPatternZ + z) * m_numPatternY + y) * | |
| 939 | + | m_numPatternX + | |
| 940 | + | x) * | |
| 941 | + | m_layers + | |
| 942 | + | l) * | |
| 943 | + | m_size.height() + | |
| 944 | + | h) * | |
| 945 | + | m_size.width() + | |
| 946 | + | w; | |
| 947 | + | VALIDATE(index < m_spritesIndex.size()); | |
| 948 | + | return index; | |
| 949 | + | } | |
| 950 | + | ||
| 951 | + | uint ThingType::getTextureIndex(int l, int x, int y, int z) { | |
| 952 | + | return ((l * m_numPatternZ + z) * m_numPatternY + y) * m_numPatternX + x; | |
| 953 | + | } | |
| 954 | + | ||
| 955 | + | int ThingType::getExactSize(int layer, int xPattern, int yPattern, int zPattern, | |
| 956 | + | int animationPhase) { | |
| 957 | + | if (m_null) | |
| 958 | + | return 0; | |
| 959 | + | ||
| 960 | + | getTexture(animationPhase); // we must calculate it anyway. | |
| 961 | + | int frameIndex = getTextureIndex(layer, xPattern, yPattern, zPattern); | |
| 962 | + | Size size = m_texturesFramesOriginRects[animationPhase][frameIndex].size() - | |
| 963 | + | m_texturesFramesOffsets[animationPhase][frameIndex].toSize(); | |
| 964 | + | return std::max<int>(size.width(), size.height()); | |
| 965 | + | } | |
| 966 | + | ||
| 967 | + | void ThingType::setPathable(bool var) { | |
| 968 | + | if (var == true) | |
| 969 | + | m_attribs.remove(ThingAttrNotPathable); | |
| 970 | + | else | |
| 971 | + | m_attribs.set(ThingAttrNotPathable, true); | |
| 972 | + | } | |
| 973 | + | ||
| 974 | + | void DrawQueueItemThingWithShader::draw() { | |
| 975 | + | if (!m_texture) | |
| 976 | + | return; | |
| 977 | + | PainterShaderProgramPtr shader = g_shaders.getShader(m_shader); | |
| 978 | + | if (!shader) | |
| 979 | + | return DrawQueueItemTexturedRect::draw(); | |
| 980 | + | bool useFramebuffer = m_dest.size() != m_src.size(); | |
| 981 | + | ||
| 982 | + | if (useFramebuffer) { | |
| 983 | + | g_framebuffers.getTemporaryFrameBuffer()->resize(m_src.size()); | |
| 984 | + | g_framebuffers.getTemporaryFrameBuffer()->bind(); | |
| 985 | + | g_painter->clear(Color::alpha); | |
| 986 | + | } | |
| 987 | + | ||
| 988 | + | g_painter->setShaderProgram(shader); | |
| 989 | + | g_painter->setOffset(m_offset); | |
| 990 | + | shader->setCenter(m_center); | |
| 991 | + | shader->bindMultiTextures(); | |
| 992 | + | if (useFramebuffer) { | |
| 993 | + | g_painter->drawTexturedRect(Rect(0, 0, m_src.size()), m_texture, m_src); | |
| 994 | + | } else { | |
| 995 | + | g_painter->drawTexturedRect(m_dest, m_texture, m_src); | |
| 996 | + | } | |
| 997 | + | g_painter->resetShaderProgram(); | |
| 998 | + | ||
| 999 | + | if (useFramebuffer) { | |
| 1000 | + | g_framebuffers.getTemporaryFrameBuffer()->release(); | |
| 1001 | + | g_painter->resetColor(); | |
| 1002 | + | g_framebuffers.getTemporaryFrameBuffer()->draw(m_dest); | |
| 1003 | + | } | |
| 1004 | + | } |
| @@ -0,0 +1,323 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef THINGTYPE_H | |
| 24 | + | #define THINGTYPE_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | #include "animator.h" | |
| 28 | + | ||
| 29 | + | #include <framework/core/declarations.h> | |
| 30 | + | #include <framework/otml/declarations.h> | |
| 31 | + | #include <framework/graphics/texture.h> | |
| 32 | + | #include <framework/graphics/coordsbuffer.h> | |
| 33 | + | #include <framework/graphics/drawqueue.h> | |
| 34 | + | #include <framework/luaengine/luaobject.h> | |
| 35 | + | #include <framework/net/server.h> | |
| 36 | + | ||
| 37 | + | enum NewDrawType : uint8 { | |
| 38 | + | NewDrawNormal = 0, | |
| 39 | + | NewDrawMount = 5, | |
| 40 | + | NewDrawOutfit = 6, | |
| 41 | + | NewDrawOutfitLayers = 7, | |
| 42 | + | NewDrawMissle = 10 | |
| 43 | + | }; | |
| 44 | + | ||
| 45 | + | enum FrameGroupType : uint8 { | |
| 46 | + | FrameGroupDefault = 0, | |
| 47 | + | FrameGroupIdle = FrameGroupDefault, | |
| 48 | + | FrameGroupMoving | |
| 49 | + | }; | |
| 50 | + | ||
| 51 | + | enum ThingCategory : uint8 { | |
| 52 | + | ThingCategoryItem = 0, | |
| 53 | + | ThingCategoryCreature, | |
| 54 | + | ThingCategoryEffect, | |
| 55 | + | ThingCategoryMissile, | |
| 56 | + | ThingInvalidCategory, | |
| 57 | + | ThingLastCategory = ThingInvalidCategory | |
| 58 | + | }; | |
| 59 | + | ||
| 60 | + | enum ThingAttr : uint8 { | |
| 61 | + | ThingAttrGround = 0, | |
| 62 | + | ThingAttrGroundBorder = 1, | |
| 63 | + | ThingAttrOnBottom = 2, | |
| 64 | + | ThingAttrOnTop = 3, | |
| 65 | + | ThingAttrContainer = 4, | |
| 66 | + | ThingAttrStackable = 5, | |
| 67 | + | ThingAttrForceUse = 6, | |
| 68 | + | ThingAttrMultiUse = 7, | |
| 69 | + | ThingAttrWritable = 8, | |
| 70 | + | ThingAttrWritableOnce = 9, | |
| 71 | + | ThingAttrFluidContainer = 10, | |
| 72 | + | ThingAttrSplash = 11, | |
| 73 | + | ThingAttrNotWalkable = 12, | |
| 74 | + | ThingAttrNotMoveable = 13, | |
| 75 | + | ThingAttrBlockProjectile = 14, | |
| 76 | + | ThingAttrNotPathable = 15, | |
| 77 | + | ThingAttrPickupable = 16, | |
| 78 | + | ThingAttrHangable = 17, | |
| 79 | + | ThingAttrHookSouth = 18, | |
| 80 | + | ThingAttrHookEast = 19, | |
| 81 | + | ThingAttrRotateable = 20, | |
| 82 | + | ThingAttrLight = 21, | |
| 83 | + | ThingAttrDontHide = 22, | |
| 84 | + | ThingAttrTranslucent = 23, | |
| 85 | + | ThingAttrDisplacement = 24, | |
| 86 | + | ThingAttrElevation = 25, | |
| 87 | + | ThingAttrLyingCorpse = 26, | |
| 88 | + | ThingAttrAnimateAlways = 27, | |
| 89 | + | ThingAttrMinimapColor = 28, | |
| 90 | + | ThingAttrLensHelp = 29, | |
| 91 | + | ThingAttrFullGround = 30, | |
| 92 | + | ThingAttrLook = 31, | |
| 93 | + | ThingAttrCloth = 32, | |
| 94 | + | ThingAttrMarket = 33, | |
| 95 | + | ThingAttrUsable = 34, | |
| 96 | + | ThingAttrWrapable = 35, | |
| 97 | + | ThingAttrUnwrapable = 36, | |
| 98 | + | ThingAttrTopEffect = 37, | |
| 99 | + | ThingAttrBones = 38, | |
| 100 | + | ||
| 101 | + | // additional | |
| 102 | + | ThingAttrOpacity = 100, | |
| 103 | + | ThingAttrNotPreWalkable = 101, | |
| 104 | + | ||
| 105 | + | ThingAttrFloorChange = 252, | |
| 106 | + | ThingAttrNoMoveAnimation = 253, // 10.10: real value is 16, but we need to do this for backwards compatibility | |
| 107 | + | ThingAttrChargeable = 254, // deprecated | |
| 108 | + | ThingLastAttr = 255 | |
| 109 | + | }; | |
| 110 | + | ||
| 111 | + | enum SpriteMask { | |
| 112 | + | SpriteMask = 1, | |
| 113 | + | }; | |
| 114 | + | ||
| 115 | + | struct MarketData { | |
| 116 | + | std::string name; | |
| 117 | + | int category; | |
| 118 | + | uint16 requiredLevel; | |
| 119 | + | uint16 restrictVocation; | |
| 120 | + | uint16 showAs; | |
| 121 | + | uint16 tradeAs; | |
| 122 | + | }; | |
| 123 | + | ||
| 124 | + | struct StoreCategory { | |
| 125 | + | std::string name; | |
| 126 | + | std::string description; | |
| 127 | + | int state; | |
| 128 | + | std::string icon; | |
| 129 | + | std::string parent; | |
| 130 | + | }; | |
| 131 | + | ||
| 132 | + | struct StoreOffer { | |
| 133 | + | int id; | |
| 134 | + | std::string name; | |
| 135 | + | std::string description; | |
| 136 | + | int price; | |
| 137 | + | int state; | |
| 138 | + | std::string icon; | |
| 139 | + | }; | |
| 140 | + | ||
| 141 | + | struct Imbuement { | |
| 142 | + | int id; | |
| 143 | + | std::string name; | |
| 144 | + | std::string description; | |
| 145 | + | std::string group; | |
| 146 | + | int imageId; | |
| 147 | + | int duration; | |
| 148 | + | bool premiumOnly; | |
| 149 | + | std::vector<std::pair<ItemPtr, std::string>> sources; | |
| 150 | + | int cost; | |
| 151 | + | int successRate; | |
| 152 | + | int protectionCost; | |
| 153 | + | }; | |
| 154 | + | ||
| 155 | + | struct Light { | |
| 156 | + | Point pos; | |
| 157 | + | uint8_t color = 215; | |
| 158 | + | uint8_t intensity = 0; | |
| 159 | + | }; | |
| 160 | + | ||
| 161 | + | struct DrawOutfitParams { | |
| 162 | + | Rect dest; | |
| 163 | + | TexturePtr texture; | |
| 164 | + | Rect src; | |
| 165 | + | Point offset; | |
| 166 | + | Color color; | |
| 167 | + | }; | |
| 168 | + | ||
| 169 | + | class ThingType : public LuaObject | |
| 170 | + | { | |
| 171 | + | public: | |
| 172 | + | ThingType(); | |
| 173 | + | ||
| 174 | + | void unserialize(uint16 clientId, ThingCategory category, const FileStreamPtr& fin); | |
| 175 | + | void unserializeOtml(const OTMLNodePtr& node); | |
| 176 | + | void unload(); | |
| 177 | + | ||
| 178 | + | void serialize(const FileStreamPtr& fin); | |
| 179 | + | void exportImage(std::string fileName); | |
| 180 | + | void replaceSprites(std::map<uint32_t, ImagePtr>& replacements, std::string fileName); | |
| 181 | + | ||
| 182 | + | DrawQueueItem* draw(const Point& dest, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, Color color = Color::white, LightView* lightView = nullptr); | |
| 183 | + | DrawQueueItem* draw(const Rect& dest, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, Color color = Color::white); | |
| 184 | + | std::shared_ptr<DrawOutfitParams> drawOutfit(const Point& dest, int maskLayer, int xPattern, int yPattern, int zPattern, int animationPhase, Color color = Color::white, LightView* lightView = nullptr); | |
| 185 | + | Rect getDrawSize(const Point& dest, int layer, int xPattern, int yPattern, int zPattern, int animationPhase); | |
| 186 | + | void drawWithShader(const Point& dest, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, const std::string& shader, Color color = Color::white, LightView* lightView = nullptr); | |
| 187 | + | void drawWithShader(const Rect& dest, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, const std::string& shader, Color color = Color::white); | |
| 188 | + | ||
| 189 | + | uint16 getId() { return m_id; } | |
| 190 | + | ThingCategory getCategory() { return m_category; } | |
| 191 | + | bool isNull() { return m_null; } | |
| 192 | + | bool hasAttr(ThingAttr attr) { return m_attribs.has(attr); } | |
| 193 | + | bool isLoaded() { return m_loaded; } | |
| 194 | + | ticks_t getLastUsage() { return m_lastUsage; } | |
| 195 | + | ||
| 196 | + | Size getSize() { return m_size; } | |
| 197 | + | int getWidth() { return m_size.width(); } | |
| 198 | + | int getHeight() { return m_size.height(); } | |
| 199 | + | int getExactSize(int layer = 0, int xPattern = 0, int yPattern = 0, int zPattern = 0, int animationPhase = 0); | |
| 200 | + | int getRealSize() { return m_realSize; } | |
| 201 | + | int getLayers() { return m_layers; } | |
| 202 | + | int getNumPatternX() { return m_numPatternX; } | |
| 203 | + | int getNumPatternY() { return m_numPatternY; } | |
| 204 | + | int getNumPatternZ() { return m_numPatternZ; } | |
| 205 | + | int getAnimationPhases() { return m_animationPhases; } | |
| 206 | + | AnimatorPtr getAnimator() { return m_animator; } | |
| 207 | + | AnimatorPtr getIdleAnimator() { return m_idleAnimator; } | |
| 208 | + | Point getDisplacement() { return m_displacement; } | |
| 209 | + | int getDisplacementX() { return getDisplacement().x; } | |
| 210 | + | int getDisplacementY() { return getDisplacement().y; } | |
| 211 | + | int getElevation() { return m_elevation; } | |
| 212 | + | const Point& getBones(int direction) { return m_bones[direction]; } | |
| 213 | + | ||
| 214 | + | int getGroundSpeed() { return m_attribs.get<uint16>(ThingAttrGround); } | |
| 215 | + | int getMaxTextLength() { return m_attribs.has(ThingAttrWritableOnce) ? m_attribs.get<uint16>(ThingAttrWritableOnce) : m_attribs.get<uint16>(ThingAttrWritable); } | |
| 216 | + | Light getLight() { return m_attribs.get<Light>(ThingAttrLight); } | |
| 217 | + | int getMinimapColor() { return m_attribs.get<uint16>(ThingAttrMinimapColor); } | |
| 218 | + | int getLensHelp() { return m_attribs.get<uint16>(ThingAttrLensHelp); } | |
| 219 | + | int getClothSlot() { return m_attribs.get<uint16>(ThingAttrCloth); } | |
| 220 | + | MarketData getMarketData() { return m_attribs.get<MarketData>(ThingAttrMarket); } | |
| 221 | + | bool isGround() { return m_attribs.has(ThingAttrGround); } | |
| 222 | + | bool isGroundBorder() { return m_attribs.has(ThingAttrGroundBorder); } | |
| 223 | + | bool isOnBottom() { return m_attribs.has(ThingAttrOnBottom); } | |
| 224 | + | bool isOnTop() { return m_attribs.has(ThingAttrOnTop); } | |
| 225 | + | bool isContainer() { return m_attribs.has(ThingAttrContainer); } | |
| 226 | + | bool isStackable() { return m_attribs.has(ThingAttrStackable); } | |
| 227 | + | bool isForceUse() { return m_attribs.has(ThingAttrForceUse); } | |
| 228 | + | bool isMultiUse() { return m_attribs.has(ThingAttrMultiUse); } | |
| 229 | + | bool isWritable() { return m_attribs.has(ThingAttrWritable); } | |
| 230 | + | bool isChargeable() { return m_attribs.has(ThingAttrChargeable); } | |
| 231 | + | bool isWritableOnce() { return m_attribs.has(ThingAttrWritableOnce); } | |
| 232 | + | bool isFluidContainer() { return m_attribs.has(ThingAttrFluidContainer); } | |
| 233 | + | bool isSplash() { return m_attribs.has(ThingAttrSplash); } | |
| 234 | + | bool isNotWalkable() { return m_attribs.has(ThingAttrNotWalkable); } | |
| 235 | + | bool isNotMoveable() { return m_attribs.has(ThingAttrNotMoveable); } | |
| 236 | + | bool blockProjectile() { return m_attribs.has(ThingAttrBlockProjectile); } | |
| 237 | + | bool isNotPathable() { return m_attribs.has(ThingAttrNotPathable); } | |
| 238 | + | bool isPickupable() { return m_attribs.has(ThingAttrPickupable); } | |
| 239 | + | bool isHangable() { return m_attribs.has(ThingAttrHangable); } | |
| 240 | + | bool isHookSouth() { return m_attribs.has(ThingAttrHookSouth); } | |
| 241 | + | bool isHookEast() { return m_attribs.has(ThingAttrHookEast); } | |
| 242 | + | bool isRotateable() { return m_attribs.has(ThingAttrRotateable); } | |
| 243 | + | bool hasLight() { return m_attribs.has(ThingAttrLight); } | |
| 244 | + | bool isDontHide() { return m_attribs.has(ThingAttrDontHide); } | |
| 245 | + | bool isTranslucent() { return m_attribs.has(ThingAttrTranslucent); } | |
| 246 | + | bool hasDisplacement() { return m_attribs.has(ThingAttrDisplacement); } | |
| 247 | + | bool hasElevation() { return m_attribs.has(ThingAttrElevation); } | |
| 248 | + | bool isLyingCorpse() { return m_attribs.has(ThingAttrLyingCorpse); } | |
| 249 | + | bool isAnimateAlways() { return m_attribs.has(ThingAttrAnimateAlways); } | |
| 250 | + | bool hasMiniMapColor() { return m_attribs.has(ThingAttrMinimapColor); } | |
| 251 | + | bool hasLensHelp() { return m_attribs.has(ThingAttrLensHelp); } | |
| 252 | + | bool isFullGround() { return m_attribs.has(ThingAttrFullGround); } | |
| 253 | + | bool isIgnoreLook() { return m_attribs.has(ThingAttrLook); } | |
| 254 | + | bool isCloth() { return m_attribs.has(ThingAttrCloth); } | |
| 255 | + | bool isMarketable() { return m_attribs.has(ThingAttrMarket); } | |
| 256 | + | bool isUsable() { return m_attribs.has(ThingAttrUsable); } | |
| 257 | + | bool isWrapable() { return m_attribs.has(ThingAttrWrapable); } | |
| 258 | + | bool isUnwrapable() { return m_attribs.has(ThingAttrUnwrapable); } | |
| 259 | + | bool isTopEffect() { return m_attribs.has(ThingAttrTopEffect); } | |
| 260 | + | bool hasBones() { return m_attribs.has(ThingAttrBones); } | |
| 261 | + | ||
| 262 | + | std::vector<int> getSprites() { return m_spritesIndex; } | |
| 263 | + | ||
| 264 | + | // additional | |
| 265 | + | float getOpacity() { return m_opacity; } | |
| 266 | + | bool isNotPreWalkable() { return m_attribs.has(ThingAttrNotPreWalkable); } | |
| 267 | + | void setPathable(bool var); | |
| 268 | + | ||
| 269 | + | private: | |
| 270 | + | const TexturePtr& getTexture(int animationPhase); | |
| 271 | + | Size getBestTextureDimension(int w, int h, int count); | |
| 272 | + | uint getSpriteIndex(int w, int h, int l, int x, int y, int z, int a); | |
| 273 | + | uint getTextureIndex(int l, int x, int y, int z); | |
| 274 | + | ||
| 275 | + | ThingCategory m_category; | |
| 276 | + | uint16 m_id; | |
| 277 | + | bool m_null; | |
| 278 | + | stdext::dynamic_storage<uint8> m_attribs; | |
| 279 | + | ||
| 280 | + | Size m_size; | |
| 281 | + | Point m_displacement; | |
| 282 | + | AnimatorPtr m_animator; | |
| 283 | + | AnimatorPtr m_idleAnimator; | |
| 284 | + | std::vector<Point> m_bones; | |
| 285 | + | int m_animationPhases; | |
| 286 | + | int m_exactSize; | |
| 287 | + | int m_realSize; | |
| 288 | + | int m_numPatternX, m_numPatternY, m_numPatternZ; | |
| 289 | + | int m_layers; | |
| 290 | + | int m_elevation; | |
| 291 | + | float m_opacity; | |
| 292 | + | std::string m_customImage; | |
| 293 | + | ||
| 294 | + | std::vector<int> m_spritesIndex; | |
| 295 | + | std::vector<TexturePtr> m_textures; | |
| 296 | + | std::vector<std::vector<Rect>> m_texturesFramesRects; | |
| 297 | + | std::vector<std::vector<Rect>> m_texturesFramesOriginRects; | |
| 298 | + | std::vector<std::vector<Point>> m_texturesFramesOffsets; | |
| 299 | + | ||
| 300 | + | bool m_loaded = false; | |
| 301 | + | time_t m_lastUsage; | |
| 302 | + | }; | |
| 303 | + | ||
| 304 | + | struct DrawQueueItemThingWithShader : public DrawQueueItemTexturedRect { | |
| 305 | + | DrawQueueItemThingWithShader(const Rect& rect, const TexturePtr& texture, const Rect& src, const Point& offset, const Point& center, int32_t colors, const std::string& shader) : | |
| 306 | + | DrawQueueItemTexturedRect(rect, texture, src, Color::white), m_offset(offset), m_center(center), m_colors(colors), m_shader(shader) | |
| 307 | + | {}; | |
| 308 | + | ||
| 309 | + | void draw() override; | |
| 310 | + | void draw(const Point& pos) override | |
| 311 | + | {} | |
| 312 | + | bool cache() override | |
| 313 | + | { | |
| 314 | + | return false; | |
| 315 | + | } | |
| 316 | + | ||
| 317 | + | Point m_offset; | |
| 318 | + | Point m_center; | |
| 319 | + | int32_t m_colors; | |
| 320 | + | std::string m_shader; | |
| 321 | + | }; | |
| 322 | + | ||
| 323 | + | #endif |
| @@ -0,0 +1,497 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "thingtypemanager.h" | |
| 24 | + | #include "spritemanager.h" | |
| 25 | + | #include "thing.h" | |
| 26 | + | #include "thingtype.h" | |
| 27 | + | #include "itemtype.h" | |
| 28 | + | #include "creature.h" | |
| 29 | + | #include "creatures.h" | |
| 30 | + | #include "game.h" | |
| 31 | + | ||
| 32 | + | #include <framework/core/resourcemanager.h> | |
| 33 | + | #include <framework/core/filestream.h> | |
| 34 | + | #include <framework/core/binarytree.h> | |
| 35 | + | #include <framework/xml/tinyxml.h> | |
| 36 | + | #include <framework/otml/otml.h> | |
| 37 | + | #include <framework/util/stats.h> | |
| 38 | + | ||
| 39 | + | ThingTypeManager g_things; | |
| 40 | + | ||
| 41 | + | void ThingTypeManager::init() | |
| 42 | + | { | |
| 43 | + | m_nullThingType = ThingTypePtr(new ThingType); | |
| 44 | + | m_nullItemType = ItemTypePtr(new ItemType); | |
| 45 | + | m_datSignature = 0; | |
| 46 | + | m_contentRevision = 0; | |
| 47 | + | m_otbMinorVersion = 0; | |
| 48 | + | m_otbMajorVersion = 0; | |
| 49 | + | m_datLoaded = false; | |
| 50 | + | m_xmlLoaded = false; | |
| 51 | + | m_otbLoaded = false; | |
| 52 | + | for (int i = 0; i < ThingLastCategory; ++i) { | |
| 53 | + | m_thingTypes[i].resize(1, m_nullThingType); | |
| 54 | + | m_checkIndex[i] = 0; | |
| 55 | + | } | |
| 56 | + | m_itemTypes.resize(1, m_nullItemType); | |
| 57 | + | ||
| 58 | + | check(); | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | void ThingTypeManager::terminate() | |
| 62 | + | { | |
| 63 | + | for(int i = 0; i < ThingLastCategory; ++i) | |
| 64 | + | m_thingTypes[i].clear(); | |
| 65 | + | m_itemTypes.clear(); | |
| 66 | + | m_reverseItemTypes.clear(); | |
| 67 | + | m_marketCategories.clear(); | |
| 68 | + | m_nullThingType = nullptr; | |
| 69 | + | m_nullItemType = nullptr; | |
| 70 | + | ||
| 71 | + | if (m_checkEvent) { | |
| 72 | + | m_checkEvent->cancel(); | |
| 73 | + | m_checkEvent = nullptr; | |
| 74 | + | } | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | void ThingTypeManager::check() | |
| 78 | + | { | |
| 79 | + | // removes unused textures from memory after 60s, 500 checks / s | |
| 80 | + | m_checkEvent = g_dispatcher.scheduleEvent(std::bind(&ThingTypeManager::check, &g_things), 1000); | |
| 81 | + | ||
| 82 | + | for (size_t i = 0; i < ThingLastCategory; ++i) { | |
| 83 | + | size_t limit = std::min<size_t>(m_checkIndex[i] + 100, m_thingTypes[i].size()); | |
| 84 | + | for (size_t j = m_checkIndex[i]; j < limit; ++j) { | |
| 85 | + | if (m_thingTypes[i][j]->isLoaded() && m_thingTypes[i][j]->getLastUsage() + 60 < g_clock.seconds()) { | |
| 86 | + | m_thingTypes[i][j]->unload(); | |
| 87 | + | } | |
| 88 | + | } | |
| 89 | + | m_checkIndex[i] = limit; | |
| 90 | + | if (m_checkIndex[i] >= m_thingTypes[i].size()) { | |
| 91 | + | m_checkIndex[i] = 0; | |
| 92 | + | } | |
| 93 | + | } | |
| 94 | + | } | |
| 95 | + | ||
| 96 | + | #ifdef WITH_ENCRYPTION | |
| 97 | + | void ThingTypeManager::saveDat(std::string fileName) | |
| 98 | + | { | |
| 99 | + | if(!m_datLoaded) | |
| 100 | + | stdext::throw_exception("failed to save, dat is not loaded"); | |
| 101 | + | ||
| 102 | + | try { | |
| 103 | + | FileStreamPtr fin = g_resources.createFile(fileName); | |
| 104 | + | if(!fin) | |
| 105 | + | stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName)); | |
| 106 | + | ||
| 107 | + | fin->addU32(m_datSignature); | |
| 108 | + | ||
| 109 | + | for(int category = 0; category < ThingLastCategory; ++category) | |
| 110 | + | fin->addU16(m_thingTypes[category].size() - 1); | |
| 111 | + | ||
| 112 | + | for(int category = 0; category < ThingLastCategory; ++category) { | |
| 113 | + | uint16 firstId = 1; | |
| 114 | + | if(category == ThingCategoryItem) | |
| 115 | + | firstId = 100; | |
| 116 | + | ||
| 117 | + | for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) | |
| 118 | + | m_thingTypes[category][id]->serialize(fin); | |
| 119 | + | } | |
| 120 | + | ||
| 121 | + | ||
| 122 | + | fin->flush(); | |
| 123 | + | fin->close(); | |
| 124 | + | } catch(std::exception& e) { | |
| 125 | + | g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what())); | |
| 126 | + | } | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | void ThingTypeManager::dumpTextures(std::string dir) | |
| 130 | + | { | |
| 131 | + | if (dir.empty()) { | |
| 132 | + | g_logger.error("Empty dir for sprites dump"); | |
| 133 | + | return; | |
| 134 | + | } | |
| 135 | + | g_resources.makeDir(dir); | |
| 136 | + | for (int category = 0; category < ThingLastCategory; ++category) { | |
| 137 | + | g_resources.makeDir(dir + "/" + std::to_string((int)category)); | |
| 138 | + | ||
| 139 | + | uint16 firstId = 1; | |
| 140 | + | if (category == ThingCategoryItem) | |
| 141 | + | firstId = 100; | |
| 142 | + | ||
| 143 | + | for (uint16 id = firstId; id < m_thingTypes[category].size(); ++id) | |
| 144 | + | m_thingTypes[category][id]->exportImage(dir + "/" + std::to_string((int)category) + "/" + std::to_string(id) + ".png"); | |
| 145 | + | } | |
| 146 | + | } | |
| 147 | + | ||
| 148 | + | void ThingTypeManager::replaceTextures(std::string dir) { | |
| 149 | + | if (dir.empty()) { | |
| 150 | + | g_logger.error("Empty dir for sprites dump"); | |
| 151 | + | return; | |
| 152 | + | } | |
| 153 | + | ||
| 154 | + | std::map<uint32_t, ImagePtr> replacements; | |
| 155 | + | for (int category = 0; category < ThingLastCategory; ++category) { | |
| 156 | + | uint16 firstId = 1; | |
| 157 | + | if (category == ThingCategoryItem) | |
| 158 | + | firstId = 100; | |
| 159 | + | ||
| 160 | + | for (uint16 id = firstId; id < m_thingTypes[category].size(); ++id) { | |
| 161 | + | std::string fileName = dir + "/" + std::to_string((int)category) + "/" + std::to_string(id) + "_[][x2.000000].png"; | |
| 162 | + | m_thingTypes[category][id]->replaceSprites(replacements, fileName); | |
| 163 | + | } | |
| 164 | + | } | |
| 165 | + | //g_sprites.saveReplacedSpr(dir + "/sprites.spr", replacements); | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | #endif | |
| 169 | + | ||
| 170 | + | bool ThingTypeManager::loadDat(std::string file) | |
| 171 | + | { | |
| 172 | + | m_datLoaded = false; | |
| 173 | + | m_datSignature = 0; | |
| 174 | + | m_contentRevision = 0; | |
| 175 | + | try { | |
| 176 | + | file = g_resources.guessFilePath(file, "dat"); | |
| 177 | + | ||
| 178 | + | FileStreamPtr fin = g_resources.openFile(file, g_game.getFeature(Otc::GameDontCacheFiles)); | |
| 179 | + | ||
| 180 | + | m_datSignature = fin->getU32(); | |
| 181 | + | m_contentRevision = static_cast<uint16_t>(m_datSignature); | |
| 182 | + | ||
| 183 | + | for(int category = 0; category < ThingLastCategory; ++category) { | |
| 184 | + | int count = fin->getU16() + 1; | |
| 185 | + | m_thingTypes[category].clear(); | |
| 186 | + | m_thingTypes[category].resize(count, m_nullThingType); | |
| 187 | + | } | |
| 188 | + | ||
| 189 | + | m_marketCategories.clear(); | |
| 190 | + | for(int category = 0; category < ThingLastCategory; ++category) { | |
| 191 | + | uint16 firstId = 1; | |
| 192 | + | if(category == ThingCategoryItem) | |
| 193 | + | firstId = 100; | |
| 194 | + | for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) { | |
| 195 | + | ThingTypePtr type(new ThingType); | |
| 196 | + | type->unserialize(id, (ThingCategory)category, fin); | |
| 197 | + | m_thingTypes[category][id] = type; | |
| 198 | + | if (type->isMarketable()) { | |
| 199 | + | auto marketData = type->getMarketData(); | |
| 200 | + | m_marketCategories.insert(marketData.category); | |
| 201 | + | } | |
| 202 | + | } | |
| 203 | + | } | |
| 204 | + | ||
| 205 | + | m_datLoaded = true; | |
| 206 | + | g_lua.callGlobalField("g_things", "onLoadDat", file); | |
| 207 | + | return true; | |
| 208 | + | } catch(stdext::exception& e) { | |
| 209 | + | g_logger.error(stdext::format("Failed to read dat '%s': %s'", file, e.what())); | |
| 210 | + | return false; | |
| 211 | + | } | |
| 212 | + | } | |
| 213 | + | ||
| 214 | + | bool ThingTypeManager::loadOtml(std::string file) | |
| 215 | + | { | |
| 216 | + | try { | |
| 217 | + | file = g_resources.guessFilePath(file, "otml"); | |
| 218 | + | ||
| 219 | + | OTMLDocumentPtr doc = OTMLDocument::parse(file); | |
| 220 | + | for(const OTMLNodePtr& node : doc->children()) { | |
| 221 | + | ThingCategory category; | |
| 222 | + | if(node->tag() == "creatures") | |
| 223 | + | category = ThingCategoryCreature; | |
| 224 | + | else if(node->tag() == "items") | |
| 225 | + | category = ThingCategoryItem; | |
| 226 | + | else if(node->tag() == "effects") | |
| 227 | + | category = ThingCategoryEffect; | |
| 228 | + | else if(node->tag() == "missiles") | |
| 229 | + | category = ThingCategoryMissile; | |
| 230 | + | else { | |
| 231 | + | throw OTMLException(node, "not a valid thing category"); | |
| 232 | + | } | |
| 233 | + | ||
| 234 | + | for(const OTMLNodePtr& node2 : node->children()) { | |
| 235 | + | uint16 id = stdext::safe_cast<uint16>(node2->tag()); | |
| 236 | + | ThingTypePtr type = getThingType(id, category); | |
| 237 | + | if(!type) | |
| 238 | + | throw OTMLException(node2, "thing not found"); | |
| 239 | + | type->unserializeOtml(node2); | |
| 240 | + | } | |
| 241 | + | } | |
| 242 | + | return true; | |
| 243 | + | } catch(std::exception& e) { | |
| 244 | + | g_logger.error(stdext::format("Failed to read dat otml '%s': %s'", file, e.what())); | |
| 245 | + | return false; | |
| 246 | + | } | |
| 247 | + | } | |
| 248 | + | ||
| 249 | + | void ThingTypeManager::loadOtb(const std::string& file) | |
| 250 | + | { | |
| 251 | + | try { | |
| 252 | + | FileStreamPtr fin = g_resources.openFile(file, g_game.getFeature(Otc::GameDontCacheFiles)); | |
| 253 | + | ||
| 254 | + | uint signature = fin->getU32(); | |
| 255 | + | if (signature != 0) | |
| 256 | + | stdext::throw_exception("invalid otb file"); | |
| 257 | + | ||
| 258 | + | BinaryTreePtr root = fin->getBinaryTree(); | |
| 259 | + | root->skip(1); // otb first byte is always 0 | |
| 260 | + | ||
| 261 | + | signature = root->getU32(); | |
| 262 | + | if (signature != 0) | |
| 263 | + | stdext::throw_exception("invalid otb file"); | |
| 264 | + | ||
| 265 | + | uint8 rootAttr = root->getU8(); | |
| 266 | + | if (rootAttr == 0x01) { // OTB_ROOT_ATTR_VERSION | |
| 267 | + | uint16 size = root->getU16(); | |
| 268 | + | if (size != 4 + 4 + 4 + 128) | |
| 269 | + | stdext::throw_exception("invalid otb root attr version size"); | |
| 270 | + | ||
| 271 | + | m_otbMajorVersion = root->getU32(); | |
| 272 | + | m_otbMinorVersion = root->getU32(); | |
| 273 | + | root->skip(4); // buildNumber | |
| 274 | + | root->skip(128); // description | |
| 275 | + | } | |
| 276 | + | ||
| 277 | + | BinaryTreeVec children = root->getChildren(); | |
| 278 | + | m_reverseItemTypes.clear(); | |
| 279 | + | m_itemTypes.resize(children.size() + 1, m_nullItemType); | |
| 280 | + | m_reverseItemTypes.resize(children.size() + 1, m_nullItemType); | |
| 281 | + | ||
| 282 | + | for (const BinaryTreePtr& node : children) { | |
| 283 | + | ItemTypePtr itemType(new ItemType); | |
| 284 | + | itemType->unserialize(node); | |
| 285 | + | addItemType(itemType); | |
| 286 | + | ||
| 287 | + | uint16 clientId = itemType->getClientId(); | |
| 288 | + | if (unlikely(clientId >= m_reverseItemTypes.size())) | |
| 289 | + | m_reverseItemTypes.resize(clientId + 1); | |
| 290 | + | m_reverseItemTypes[clientId] = itemType; | |
| 291 | + | } | |
| 292 | + | ||
| 293 | + | m_otbLoaded = true; | |
| 294 | + | g_lua.callGlobalField("g_things", "onLoadOtb", file); | |
| 295 | + | } catch (std::exception& e) { | |
| 296 | + | g_logger.error(stdext::format("Failed to load '%s' (OTB file): %s", file, e.what())); | |
| 297 | + | } | |
| 298 | + | } | |
| 299 | + | ||
| 300 | + | ||
| 301 | + | void ThingTypeManager::loadXml(const std::string& file) | |
| 302 | + | { | |
| 303 | + | try { | |
| 304 | + | if(!isOtbLoaded()) | |
| 305 | + | stdext::throw_exception("OTB must be loaded before XML"); | |
| 306 | + | ||
| 307 | + | TiXmlDocument doc; | |
| 308 | + | doc.Parse(g_resources.readFileContents(file).c_str()); | |
| 309 | + | if(doc.Error()) | |
| 310 | + | stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc())); | |
| 311 | + | ||
| 312 | + | TiXmlElement* root = doc.FirstChildElement(); | |
| 313 | + | if(!root || root->ValueTStr() != "items") | |
| 314 | + | stdext::throw_exception("invalid root tag name"); | |
| 315 | + | ||
| 316 | + | for(TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) { | |
| 317 | + | if(unlikely(element->ValueTStr() != "item")) | |
| 318 | + | continue; | |
| 319 | + | ||
| 320 | + | uint16 id = element->readType<uint16>("id"); | |
| 321 | + | if(id != 0) { | |
| 322 | + | std::vector<std::string> s_ids = stdext::split(element->Attribute("id"), ";"); | |
| 323 | + | for(const std::string& s : s_ids) { | |
| 324 | + | std::vector<int32> ids = stdext::split<int32>(s, "-"); | |
| 325 | + | if(ids.size() > 1) { | |
| 326 | + | int32 i = ids[0]; | |
| 327 | + | while(i <= ids[1]) | |
| 328 | + | parseItemType(i++, element); | |
| 329 | + | } else | |
| 330 | + | parseItemType(atoi(s.c_str()), element); | |
| 331 | + | } | |
| 332 | + | } else { | |
| 333 | + | std::vector<int32> begin = stdext::split<int32>(element->Attribute("fromid"), ";"); | |
| 334 | + | std::vector<int32> end = stdext::split<int32>(element->Attribute("toid"), ";"); | |
| 335 | + | if(begin[0] && begin.size() == end.size()) { | |
| 336 | + | size_t size = begin.size(); | |
| 337 | + | for(size_t i = 0; i < size; ++i) | |
| 338 | + | while(begin[i] <= end[i]) | |
| 339 | + | parseItemType(begin[i]++, element); | |
| 340 | + | } | |
| 341 | + | } | |
| 342 | + | } | |
| 343 | + | ||
| 344 | + | doc.Clear(); | |
| 345 | + | m_xmlLoaded = true; | |
| 346 | + | g_logger.debug("items.xml read successfully."); | |
| 347 | + | } catch(std::exception& e) { | |
| 348 | + | g_logger.error(stdext::format("Failed to load '%s' (XML file): %s", file, e.what())); | |
| 349 | + | } | |
| 350 | + | } | |
| 351 | + | ||
| 352 | + | void ThingTypeManager::parseItemType(uint16 serverId, TiXmlElement* elem) | |
| 353 | + | { | |
| 354 | + | ItemTypePtr itemType = nullptr; | |
| 355 | + | ||
| 356 | + | bool s; | |
| 357 | + | int d; | |
| 358 | + | ||
| 359 | + | if(g_game.getClientVersion() < 960) { | |
| 360 | + | s = serverId > 20000 && serverId < 20100; | |
| 361 | + | d = 20000; | |
| 362 | + | } else { | |
| 363 | + | s = serverId > 30000 && serverId < 30100; | |
| 364 | + | d = 30000; | |
| 365 | + | } | |
| 366 | + | ||
| 367 | + | if(s) { | |
| 368 | + | serverId -= d; | |
| 369 | + | itemType = ItemTypePtr(new ItemType); | |
| 370 | + | itemType->setServerId(serverId); | |
| 371 | + | addItemType(itemType); | |
| 372 | + | } else | |
| 373 | + | itemType = getItemType(serverId); | |
| 374 | + | ||
| 375 | + | itemType->setName(elem->Attribute("name")); | |
| 376 | + | for(TiXmlElement* attrib = elem->FirstChildElement(); attrib; attrib = attrib->NextSiblingElement()) { | |
| 377 | + | std::string key = attrib->Attribute("key"); | |
| 378 | + | if(key.empty()) | |
| 379 | + | continue; | |
| 380 | + | ||
| 381 | + | stdext::tolower(key); | |
| 382 | + | if(key == "description") | |
| 383 | + | itemType->setDesc(attrib->Attribute("value")); | |
| 384 | + | else if(key == "weapontype") | |
| 385 | + | itemType->setCategory(ItemCategoryWeapon); | |
| 386 | + | else if(key == "ammotype") | |
| 387 | + | itemType->setCategory(ItemCategoryAmmunition); | |
| 388 | + | else if(key == "armor") | |
| 389 | + | itemType->setCategory(ItemCategoryArmor); | |
| 390 | + | else if(key == "charges") | |
| 391 | + | itemType->setCategory(ItemCategoryCharges); | |
| 392 | + | else if(key == "type") { | |
| 393 | + | std::string value = attrib->Attribute("value"); | |
| 394 | + | stdext::tolower(value); | |
| 395 | + | ||
| 396 | + | if(value == "key") | |
| 397 | + | itemType->setCategory(ItemCategoryKey); | |
| 398 | + | else if(value == "magicfield") | |
| 399 | + | itemType->setCategory(ItemCategoryMagicField); | |
| 400 | + | else if(value == "teleport") | |
| 401 | + | itemType->setCategory(ItemCategoryTeleport); | |
| 402 | + | else if(value == "door") | |
| 403 | + | itemType->setCategory(ItemCategoryDoor); | |
| 404 | + | } | |
| 405 | + | } | |
| 406 | + | } | |
| 407 | + | ||
| 408 | + | void ThingTypeManager::addItemType(const ItemTypePtr& itemType) | |
| 409 | + | { | |
| 410 | + | uint16 id = itemType->getServerId(); | |
| 411 | + | if(unlikely(id >= m_itemTypes.size())) | |
| 412 | + | m_itemTypes.resize(id + 1, m_nullItemType); | |
| 413 | + | m_itemTypes[id] = itemType; | |
| 414 | + | } | |
| 415 | + | ||
| 416 | + | const ItemTypePtr& ThingTypeManager::findItemTypeByClientId(uint16 id) | |
| 417 | + | { | |
| 418 | + | if(id == 0 || id >= m_reverseItemTypes.size()) | |
| 419 | + | return m_nullItemType; | |
| 420 | + | ||
| 421 | + | if(m_reverseItemTypes[id]) | |
| 422 | + | return m_reverseItemTypes[id]; | |
| 423 | + | else | |
| 424 | + | return m_nullItemType; | |
| 425 | + | } | |
| 426 | + | ||
| 427 | + | const ItemTypePtr& ThingTypeManager::findItemTypeByName(std::string name) | |
| 428 | + | { | |
| 429 | + | for(const ItemTypePtr& it : m_itemTypes) | |
| 430 | + | if(it->getName() == name) | |
| 431 | + | return it; | |
| 432 | + | return m_nullItemType; | |
| 433 | + | } | |
| 434 | + | ||
| 435 | + | ItemTypeList ThingTypeManager::findItemTypesByName(std::string name) | |
| 436 | + | { | |
| 437 | + | ItemTypeList ret; | |
| 438 | + | for(const ItemTypePtr& it : m_itemTypes) | |
| 439 | + | if(it->getName() == name) | |
| 440 | + | ret.push_back(it); | |
| 441 | + | return ret; | |
| 442 | + | } | |
| 443 | + | ||
| 444 | + | ItemTypeList ThingTypeManager::findItemTypesByString(std::string name) | |
| 445 | + | { | |
| 446 | + | ItemTypeList ret; | |
| 447 | + | for(const ItemTypePtr& it : m_itemTypes) | |
| 448 | + | if(it->getName().find(name) != std::string::npos) | |
| 449 | + | ret.push_back(it); | |
| 450 | + | return ret; | |
| 451 | + | } | |
| 452 | + | ||
| 453 | + | const ThingTypePtr& ThingTypeManager::getThingType(uint16 id, ThingCategory category) | |
| 454 | + | { | |
| 455 | + | if(category >= ThingLastCategory || id >= m_thingTypes[category].size()) { | |
| 456 | + | g_logger.error(stdext::format("invalid thing type client id %d in category %d", id, category)); | |
| 457 | + | return m_nullThingType; | |
| 458 | + | } | |
| 459 | + | return m_thingTypes[category][id]; | |
| 460 | + | } | |
| 461 | + | ||
| 462 | + | const ItemTypePtr& ThingTypeManager::getItemType(uint16 id) | |
| 463 | + | { | |
| 464 | + | if(id >= m_itemTypes.size() || m_itemTypes[id] == m_nullItemType) { | |
| 465 | + | g_logger.error(stdext::format("invalid thing type, server id: %d", id)); | |
| 466 | + | return m_nullItemType; | |
| 467 | + | } | |
| 468 | + | return m_itemTypes[id]; | |
| 469 | + | } | |
| 470 | + | ||
| 471 | + | ThingTypeList ThingTypeManager::findThingTypeByAttr(ThingAttr attr, ThingCategory category) | |
| 472 | + | { | |
| 473 | + | ThingTypeList ret; | |
| 474 | + | for(const ThingTypePtr& type : m_thingTypes[category]) | |
| 475 | + | if(type->hasAttr(attr)) | |
| 476 | + | ret.push_back(type); | |
| 477 | + | return ret; | |
| 478 | + | } | |
| 479 | + | ||
| 480 | + | ItemTypeList ThingTypeManager::findItemTypeByCategory(ItemCategory category) | |
| 481 | + | { | |
| 482 | + | ItemTypeList ret; | |
| 483 | + | for(const ItemTypePtr& type : m_itemTypes) | |
| 484 | + | if(type->getCategory() == category) | |
| 485 | + | ret.push_back(type); | |
| 486 | + | return ret; | |
| 487 | + | } | |
| 488 | + | ||
| 489 | + | const ThingTypeList& ThingTypeManager::getThingTypes(ThingCategory category) | |
| 490 | + | { | |
| 491 | + | ThingTypeList ret; | |
| 492 | + | if(category >= ThingLastCategory) | |
| 493 | + | stdext::throw_exception(stdext::format("invalid thing type category %d", category)); | |
| 494 | + | return m_thingTypes[category]; | |
| 495 | + | } | |
| 496 | + | ||
| 497 | + | /* vim: set ts=4 sw=4 et: */ |
| @@ -0,0 +1,119 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef THINGTYPEMANAGER_H | |
| 24 | + | #define THINGTYPEMANAGER_H | |
| 25 | + | ||
| 26 | + | #include <framework/global.h> | |
| 27 | + | #include <framework/core/declarations.h> | |
| 28 | + | #include <framework/core/eventdispatcher.h> | |
| 29 | + | ||
| 30 | + | #include "thingtype.h" | |
| 31 | + | #include "itemtype.h" | |
| 32 | + | ||
| 33 | + | class ThingTypeManager | |
| 34 | + | { | |
| 35 | + | public: | |
| 36 | + | void init(); | |
| 37 | + | void terminate(); | |
| 38 | + | void check(); | |
| 39 | + | ||
| 40 | + | bool loadDat(std::string file); | |
| 41 | + | bool loadOtml(std::string file); | |
| 42 | + | void loadOtb(const std::string& file); | |
| 43 | + | void loadXml(const std::string& file); | |
| 44 | + | void parseItemType(uint16 id, TiXmlElement *elem); | |
| 45 | + | ||
| 46 | + | #ifdef WITH_ENCRYPTION | |
| 47 | + | void saveDat(std::string fileName); | |
| 48 | + | void dumpTextures(std::string dir); | |
| 49 | + | void replaceTextures(std::string dir); | |
| 50 | + | #endif | |
| 51 | + | ||
| 52 | + | void addItemType(const ItemTypePtr& itemType); | |
| 53 | + | const ItemTypePtr& findItemTypeByClientId(uint16 id); | |
| 54 | + | const ItemTypePtr& findItemTypeByName(std::string name); | |
| 55 | + | ItemTypeList findItemTypesByName(std::string name); | |
| 56 | + | ItemTypeList findItemTypesByString(std::string str); | |
| 57 | + | ||
| 58 | + | std::set<int> getMarketCategories() | |
| 59 | + | { | |
| 60 | + | return m_marketCategories; | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | const ThingTypePtr& getNullThingType() { return m_nullThingType; } | |
| 64 | + | const ItemTypePtr& getNullItemType() { return m_nullItemType; } | |
| 65 | + | ||
| 66 | + | const ThingTypePtr& getThingType(uint16 id, ThingCategory category); | |
| 67 | + | const ItemTypePtr& getItemType(uint16 id); | |
| 68 | + | ThingType* rawGetThingType(uint16 id, ThingCategory category) { | |
| 69 | + | VALIDATE(id < m_thingTypes[category].size()); | |
| 70 | + | return m_thingTypes[category][id].get(); | |
| 71 | + | } | |
| 72 | + | ItemType* rawGetItemType(uint16 id) { | |
| 73 | + | VALIDATE(id < m_itemTypes.size()); | |
| 74 | + | return m_itemTypes[id].get(); | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | ThingTypeList findThingTypeByAttr(ThingAttr attr, ThingCategory category); | |
| 78 | + | ItemTypeList findItemTypeByCategory(ItemCategory category); | |
| 79 | + | ||
| 80 | + | const ThingTypeList& getThingTypes(ThingCategory category); | |
| 81 | + | const ItemTypeList& getItemTypes() { return m_itemTypes; } | |
| 82 | + | ||
| 83 | + | uint32 getDatSignature() { return m_datSignature; } | |
| 84 | + | uint32 getOtbMajorVersion() { return m_otbMajorVersion; } | |
| 85 | + | uint32 getOtbMinorVersion() { return m_otbMinorVersion; } | |
| 86 | + | uint16 getContentRevision() { return m_contentRevision; } | |
| 87 | + | ||
| 88 | + | bool isDatLoaded() { return m_datLoaded; } | |
| 89 | + | bool isXmlLoaded() { return m_xmlLoaded; } | |
| 90 | + | bool isOtbLoaded() { return m_otbLoaded; } | |
| 91 | + | ||
| 92 | + | bool isValidDatId(uint16 id, ThingCategory category) { return id >= 1 && id < m_thingTypes[category].size(); } | |
| 93 | + | bool isValidOtbId(uint16 id) { return id >= 1 && id < m_itemTypes.size(); } | |
| 94 | + | ||
| 95 | + | private: | |
| 96 | + | ThingTypeList m_thingTypes[ThingLastCategory]; | |
| 97 | + | ItemTypeList m_reverseItemTypes; | |
| 98 | + | ItemTypeList m_itemTypes; | |
| 99 | + | std::set<int> m_marketCategories; | |
| 100 | + | ||
| 101 | + | ThingTypePtr m_nullThingType; | |
| 102 | + | ItemTypePtr m_nullItemType; | |
| 103 | + | ||
| 104 | + | bool m_datLoaded; | |
| 105 | + | bool m_xmlLoaded; | |
| 106 | + | bool m_otbLoaded; | |
| 107 | + | ||
| 108 | + | uint32 m_otbMinorVersion; | |
| 109 | + | uint32 m_otbMajorVersion; | |
| 110 | + | uint32 m_datSignature; | |
| 111 | + | uint16 m_contentRevision; | |
| 112 | + | ||
| 113 | + | ScheduledEventPtr m_checkEvent; | |
| 114 | + | size_t m_checkIndex[ThingLastCategory]; | |
| 115 | + | }; | |
| 116 | + | ||
| 117 | + | extern ThingTypeManager g_things; | |
| 118 | + | ||
| 119 | + | #endif |
| @@ -0,0 +1,912 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "tile.h" | |
| 24 | + | #include "item.h" | |
| 25 | + | #include "thingtypemanager.h" | |
| 26 | + | #include "map.h" | |
| 27 | + | #include "game.h" | |
| 28 | + | #include "localplayer.h" | |
| 29 | + | #include "effect.h" | |
| 30 | + | #include "protocolgame.h" | |
| 31 | + | #include "lightview.h" | |
| 32 | + | #include "spritemanager.h" | |
| 33 | + | #include <framework/graphics/fontmanager.h> | |
| 34 | + | #include <framework/util/extras.h> | |
| 35 | + | #include <framework/core/adaptiverenderer.h> | |
| 36 | + | ||
| 37 | + | Tile::Tile(const Position& position) : | |
| 38 | + | m_position(position), | |
| 39 | + | m_drawElevation(0), | |
| 40 | + | m_minimapColor(0), | |
| 41 | + | m_flags(0) | |
| 42 | + | { | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | void Tile::drawGround(const Point& dest, LightView* lightView) | |
| 46 | + | { | |
| 47 | + | m_topDraws = 0; | |
| 48 | + | m_drawElevation = 0; | |
| 49 | + | if (m_fill != Color::alpha) { | |
| 50 | + | g_drawQueue->addFilledRect(Rect(dest, g_sprites.spriteSize(), g_sprites.spriteSize()), m_fill); | |
| 51 | + | return; | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | // ground | |
| 55 | + | for (const ThingPtr& thing : m_things) { | |
| 56 | + | if (!thing->isGround() && !thing->isGroundBorder() && (g_game.getFeature(Otc::GameMapDrawGroundFirst) || !thing->isOnBottom())) | |
| 57 | + | break; | |
| 58 | + | if (thing->isHidden()) | |
| 59 | + | continue; | |
| 60 | + | ||
| 61 | + | thing->draw(dest - m_drawElevation * g_sprites.getOffsetFactor(), true, lightView); | |
| 62 | + | m_drawElevation = std::min<uint8_t>(m_drawElevation + thing->getElevation(), Otc::MAX_ELEVATION); | |
| 63 | + | } | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | void Tile::drawBottom(const Point& dest, LightView* lightView) | |
| 67 | + | { | |
| 68 | + | if (m_fill != Color::alpha) | |
| 69 | + | return; | |
| 70 | + | ||
| 71 | + | // bottom things, only when GameMapDrawGroundFirst is active | |
| 72 | + | if (g_game.getFeature(Otc::GameMapDrawGroundFirst)) { | |
| 73 | + | bool afterBottom = false; | |
| 74 | + | for (const ThingPtr& thing : m_things) { | |
| 75 | + | if (thing->isOnBottom()) | |
| 76 | + | afterBottom = true; | |
| 77 | + | if (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom()) | |
| 78 | + | break; | |
| 79 | + | if (thing->isHidden() || !afterBottom) | |
| 80 | + | continue; | |
| 81 | + | ||
| 82 | + | thing->draw(dest - m_drawElevation * g_sprites.getOffsetFactor(), true, lightView); | |
| 83 | + | m_drawElevation = std::min<uint8_t>(m_drawElevation + thing->getElevation(), Otc::MAX_ELEVATION); | |
| 84 | + | } | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | // common items, reverse order | |
| 88 | + | int redrawPreviousTopW = 0, redrawPreviousTopH = 0; | |
| 89 | + | for (auto it = m_things.rbegin(); it != m_things.rend(); ++it) { | |
| 90 | + | const ThingPtr& thing = *it; | |
| 91 | + | if (thing->isOnTop() || thing->isOnBottom() || thing->isGroundBorder() || thing->isGround() || thing->isCreature()) | |
| 92 | + | break; | |
| 93 | + | if (thing->isHidden()) | |
| 94 | + | continue; | |
| 95 | + | ||
| 96 | + | thing->draw(dest - m_drawElevation * g_sprites.getOffsetFactor() , true, lightView); | |
| 97 | + | m_drawElevation = std::min<uint8_t>(m_drawElevation + thing->getElevation(), Otc::MAX_ELEVATION); | |
| 98 | + | ||
| 99 | + | if (thing->isLyingCorpse()) { | |
| 100 | + | redrawPreviousTopW = std::max<int>(thing->getWidth() - 1, redrawPreviousTopW); | |
| 101 | + | redrawPreviousTopH = std::max<int>(thing->getHeight() - 1, redrawPreviousTopH); | |
| 102 | + | } | |
| 103 | + | } | |
| 104 | + | ||
| 105 | + | if (!g_game.getFeature(Otc::GameMapIgnoreCorpseCorrection)) { | |
| 106 | + | for (int x = -redrawPreviousTopW; x <= 0; ++x) { | |
| 107 | + | for (int y = -redrawPreviousTopH; y <= 0; ++y) { | |
| 108 | + | if (x == 0 && y == 0) | |
| 109 | + | continue; | |
| 110 | + | if (const TilePtr& tile = g_map.getTile(m_position.translated(x, y))) { | |
| 111 | + | tile->drawCreatures(dest + Point(x * g_sprites.spriteSize(), y * g_sprites.spriteSize()), lightView); | |
| 112 | + | tile->drawTop(dest + Point(x * g_sprites.spriteSize(), y * g_sprites.spriteSize()), lightView); | |
| 113 | + | } | |
| 114 | + | } | |
| 115 | + | } | |
| 116 | + | } | |
| 117 | + | ||
| 118 | + | if (lightView && hasTranslucentLight()) { | |
| 119 | + | lightView->addLight(dest + Point(16, 16), 215, 1); | |
| 120 | + | } | |
| 121 | + | } | |
| 122 | + | ||
| 123 | + | void Tile::drawCreatures(const Point& dest, LightView* lightView) | |
| 124 | + | { | |
| 125 | + | if (m_fill != Color::alpha) | |
| 126 | + | return; | |
| 127 | + | if (m_topDraws < m_topCorrection) | |
| 128 | + | return; | |
| 129 | + | ||
| 130 | + | // walking creatures | |
| 131 | + | for (const CreaturePtr& creature : m_walkingCreatures) { | |
| 132 | + | if (creature->isHidden()) | |
| 133 | + | continue; | |
| 134 | + | Point creatureDest(dest.x + ((creature->getPrewalkingPosition().x - m_position.x) * g_sprites.spriteSize() - m_drawElevation * g_sprites.getOffsetFactor()), | |
| 135 | + | dest.y + ((creature->getPrewalkingPosition().y - m_position.y) * g_sprites.spriteSize() - m_drawElevation * g_sprites.getOffsetFactor())); | |
| 136 | + | creature->draw(creatureDest, true, lightView); | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | // creatures | |
| 140 | + | std::vector<CreaturePtr> creaturesToDraw; | |
| 141 | + | int limit = g_adaptiveRenderer.creaturesLimit(); | |
| 142 | + | for (auto& thing : m_things) { | |
| 143 | + | if (!thing->isCreature() || thing->isHidden()) | |
| 144 | + | continue; | |
| 145 | + | if (limit-- <= 0) | |
| 146 | + | break; | |
| 147 | + | CreaturePtr creature = thing->static_self_cast<Creature>(); | |
| 148 | + | if (!creature || creature->isWalking()) | |
| 149 | + | continue; | |
| 150 | + | creature->draw(dest - m_drawElevation * g_sprites.getOffsetFactor(), true, lightView); | |
| 151 | + | } | |
| 152 | + | } | |
| 153 | + | ||
| 154 | + | void Tile::drawTop(const Point& dest, LightView* lightView) | |
| 155 | + | { | |
| 156 | + | if (m_fill != Color::alpha) | |
| 157 | + | return; | |
| 158 | + | if (m_topDraws++ < m_topCorrection) | |
| 159 | + | return; | |
| 160 | + | ||
| 161 | + | // walking creatures | |
| 162 | + | for (const CreaturePtr& creature : m_walkingCreatures) { | |
| 163 | + | if (creature->isHidden()) | |
| 164 | + | continue; | |
| 165 | + | Point creatureDest(dest.x + ((creature->getPrewalkingPosition().x - m_position.x) * g_sprites.spriteSize() - m_drawElevation * g_sprites.getOffsetFactor()), | |
| 166 | + | dest.y + ((creature->getPrewalkingPosition().y - m_position.y) * g_sprites.spriteSize() - m_drawElevation * g_sprites.getOffsetFactor())); | |
| 167 | + | creature->draw(creatureDest, true, lightView); | |
| 168 | + | } | |
| 169 | + | ||
| 170 | + | // creatures | |
| 171 | + | std::vector<CreaturePtr> creaturesToDraw; | |
| 172 | + | int limit = g_adaptiveRenderer.creaturesLimit(); | |
| 173 | + | for (auto& thing : m_things) { | |
| 174 | + | if (!thing->isCreature() || thing->isHidden()) | |
| 175 | + | continue; | |
| 176 | + | if (limit-- <= 0) | |
| 177 | + | break; | |
| 178 | + | CreaturePtr creature = thing->static_self_cast<Creature>(); | |
| 179 | + | if (!creature || creature->isWalking()) | |
| 180 | + | continue; | |
| 181 | + | creature->draw(dest - m_drawElevation * g_sprites.getOffsetFactor(), true, lightView); | |
| 182 | + | } | |
| 183 | + | ||
| 184 | + | // effects | |
| 185 | + | limit = std::min<int>((int)m_effects.size() - 1, g_adaptiveRenderer.effetsLimit()); | |
| 186 | + | for (int i = limit; i >= 0; --i) { | |
| 187 | + | if (m_effects[i]->isHidden()) | |
| 188 | + | continue; | |
| 189 | + | m_effects[i]->draw(dest - m_drawElevation * g_sprites.getOffsetFactor(), m_position.x - g_map.getCentralPosition().x, m_position.y - g_map.getCentralPosition().y, true, lightView); | |
| 190 | + | } | |
| 191 | + | ||
| 192 | + | // top | |
| 193 | + | for (const ThingPtr& thing : m_things) { | |
| 194 | + | if (!thing->isOnTop() || thing->isHidden()) | |
| 195 | + | continue; | |
| 196 | + | thing->draw(dest, true, lightView); | |
| 197 | + | } | |
| 198 | + | } | |
| 199 | + | ||
| 200 | + | ||
| 201 | + | void Tile::calculateCorpseCorrection() { | |
| 202 | + | m_topCorrection = 0; | |
| 203 | + | ||
| 204 | + | if (g_game.getFeature(Otc::GameMapIgnoreCorpseCorrection)) | |
| 205 | + | return; | |
| 206 | + | ||
| 207 | + | int redrawPreviousTopW = 0, redrawPreviousTopH = 0; | |
| 208 | + | for(auto it = m_things.rbegin(); it != m_things.rend(); ++it) { | |
| 209 | + | const ThingPtr& thing = *it; | |
| 210 | + | if(!thing->isLyingCorpse()) { | |
| 211 | + | continue; | |
| 212 | + | } | |
| 213 | + | if (thing->isHidden()) | |
| 214 | + | continue; | |
| 215 | + | redrawPreviousTopW = std::max<int>(thing->getWidth() - 1, redrawPreviousTopW); | |
| 216 | + | redrawPreviousTopH = std::max<int>(thing->getHeight() - 1, redrawPreviousTopH); | |
| 217 | + | } | |
| 218 | + | ||
| 219 | + | for (int x = -redrawPreviousTopW; x <= 0; ++x) { | |
| 220 | + | for (int y = -redrawPreviousTopH; y <= 0; ++y) { | |
| 221 | + | if (x == 0 && y == 0) | |
| 222 | + | continue; | |
| 223 | + | if (const TilePtr& tile = g_map.getTile(m_position.translated(x, y))) | |
| 224 | + | tile->m_topCorrection += 1; | |
| 225 | + | } | |
| 226 | + | } | |
| 227 | + | } | |
| 228 | + | ||
| 229 | + | void Tile::drawTexts(Point dest) | |
| 230 | + | { | |
| 231 | + | if (m_timerText && g_clock.millis() < m_timer) { | |
| 232 | + | if (m_text && m_text->hasText()) | |
| 233 | + | dest.y -= 8; | |
| 234 | + | m_timerText->setText(stdext::format("%.01f", (m_timer - g_clock.millis()) / 1000.)); | |
| 235 | + | m_timerText->drawText(dest, Rect(dest.x - 64, dest.y - 64, 128, 128)); | |
| 236 | + | dest.y += 16; | |
| 237 | + | } | |
| 238 | + | ||
| 239 | + | if (m_text && m_text->hasText()) { | |
| 240 | + | m_text->drawText(dest, Rect(dest.x - 64, dest.y - 64, 128, 128)); | |
| 241 | + | } | |
| 242 | + | } | |
| 243 | + | ||
| 244 | + | void Tile::drawWidget(Point dest) | |
| 245 | + | { | |
| 246 | + | if (!m_widget) | |
| 247 | + | return; | |
| 248 | + | ||
| 249 | + | Rect dest_rect = m_widget->getRect(); | |
| 250 | + | dest.x += m_widget->getMarginLeft(); | |
| 251 | + | dest.x -= m_widget->getMarginRight(); | |
| 252 | + | dest.y += m_widget->getMarginTop(); | |
| 253 | + | dest.y -= m_widget->getMarginBottom(); | |
| 254 | + | dest_rect = Rect(dest - Point(dest_rect.width() / 2 - g_sprites.spriteSize(), dest_rect.height() / 2 - g_sprites.spriteSize()), dest_rect.width(), dest_rect.height()); | |
| 255 | + | m_widget->setRect(dest_rect); | |
| 256 | + | m_widget->draw(dest_rect, Fw::ForegroundPane); | |
| 257 | + | } | |
| 258 | + | ||
| 259 | + | void Tile::clean() | |
| 260 | + | { | |
| 261 | + | while(!m_things.empty()) | |
| 262 | + | removeThing(m_things.front()); | |
| 263 | + | ||
| 264 | + | if (m_widget) { | |
| 265 | + | m_widget->destroy(); | |
| 266 | + | m_widget = nullptr; | |
| 267 | + | } | |
| 268 | + | } | |
| 269 | + | ||
| 270 | + | void Tile::addWalkingCreature(const CreaturePtr& creature) | |
| 271 | + | { | |
| 272 | + | m_walkingCreatures.push_back(creature); | |
| 273 | + | } | |
| 274 | + | ||
| 275 | + | void Tile::removeWalkingCreature(const CreaturePtr& creature) | |
| 276 | + | { | |
| 277 | + | auto it = std::find(m_walkingCreatures.begin(), m_walkingCreatures.end(), creature); | |
| 278 | + | if(it != m_walkingCreatures.end()) | |
| 279 | + | m_walkingCreatures.erase(it); | |
| 280 | + | } | |
| 281 | + | ||
| 282 | + | void Tile::addThing(const ThingPtr& thing, int stackPos) | |
| 283 | + | { | |
| 284 | + | if(!thing) | |
| 285 | + | return; | |
| 286 | + | ||
| 287 | + | if(thing->isEffect()) { | |
| 288 | + | if(thing->isTopEffect()) | |
| 289 | + | m_effects.insert(m_effects.begin(), thing->static_self_cast<Effect>()); | |
| 290 | + | else | |
| 291 | + | m_effects.push_back(thing->static_self_cast<Effect>()); | |
| 292 | + | } else { | |
| 293 | + | // priority 854 | |
| 294 | + | // 0 - ground, --> --> | |
| 295 | + | // 1 - ground borders --> --> | |
| 296 | + | // 2 - bottom (walls), --> --> | |
| 297 | + | // 3 - on top (doors) --> --> | |
| 298 | + | // 4 - creatures, from top to bottom <-- --> | |
| 299 | + | // 5 - items, from top to bottom <-- <-- | |
| 300 | + | if(stackPos < 0 || stackPos == 255) { | |
| 301 | + | int priority = thing->getStackPriority(); | |
| 302 | + | ||
| 303 | + | // -1 or 255 => auto detect position | |
| 304 | + | // -2 => append | |
| 305 | + | ||
| 306 | + | bool append; | |
| 307 | + | if(stackPos == -2) | |
| 308 | + | append = true; | |
| 309 | + | else { | |
| 310 | + | append = (priority <= 3); | |
| 311 | + | ||
| 312 | + | // newer protocols does not store creatures in reverse order | |
| 313 | + | if(g_game.getClientVersion() >= 854 && priority == 4) | |
| 314 | + | append = !append; | |
| 315 | + | } | |
| 316 | + | ||
| 317 | + | for(stackPos = 0; stackPos < (int)m_things.size(); ++stackPos) { | |
| 318 | + | int otherPriority = m_things[stackPos]->getStackPriority(); | |
| 319 | + | if((append && otherPriority > priority) || (!append && otherPriority >= priority)) | |
| 320 | + | break; | |
| 321 | + | } | |
| 322 | + | } else if(stackPos > (int)m_things.size()) | |
| 323 | + | stackPos = m_things.size(); | |
| 324 | + | ||
| 325 | + | m_things.insert(m_things.begin() + stackPos, thing); | |
| 326 | + | ||
| 327 | + | if(!g_game.getFeature(Otc::GameNewCreatureStacking) && m_things.size() > MAX_THINGS) | |
| 328 | + | removeThing(m_things[MAX_THINGS]); | |
| 329 | + | ||
| 330 | + | /* | |
| 331 | + | // check stack priorities | |
| 332 | + | // this code exists to find stackpos bugs faster | |
| 333 | + | int lastPriority = 0; | |
| 334 | + | for(const ThingPtr& thing : m_things) { | |
| 335 | + | int priority = thing->getStackPriority(); | |
| 336 | + | VALIDATE(lastPriority <= priority); | |
| 337 | + | lastPriority = priority; | |
| 338 | + | } | |
| 339 | + | */ | |
| 340 | + | } | |
| 341 | + | ||
| 342 | + | thing->setPosition(m_position); | |
| 343 | + | thing->onAppear(); | |
| 344 | + | ||
| 345 | + | if(thing->isTranslucent()) | |
| 346 | + | checkTranslucentLight(); | |
| 347 | + | ||
| 348 | + | if(g_game.isTileThingLuaCallbackEnabled()) | |
| 349 | + | callLuaField("onAddThing", thing); | |
| 350 | + | } | |
| 351 | + | ||
| 352 | + | bool Tile::removeThing(ThingPtr thing) | |
| 353 | + | { | |
| 354 | + | if(!thing) | |
| 355 | + | return false; | |
| 356 | + | ||
| 357 | + | bool removed = false; | |
| 358 | + | ||
| 359 | + | if(thing->isEffect()) { | |
| 360 | + | EffectPtr effect = thing->static_self_cast<Effect>(); | |
| 361 | + | auto it = std::find(m_effects.begin(), m_effects.end(), effect); | |
| 362 | + | if(it != m_effects.end()) { | |
| 363 | + | m_effects.erase(it); | |
| 364 | + | removed = true; | |
| 365 | + | } | |
| 366 | + | } else { | |
| 367 | + | auto it = std::find(m_things.begin(), m_things.end(), thing); | |
| 368 | + | if(it != m_things.end()) { | |
| 369 | + | m_things.erase(it); | |
| 370 | + | removed = true; | |
| 371 | + | } | |
| 372 | + | } | |
| 373 | + | ||
| 374 | + | if (thing->isCreature()) { | |
| 375 | + | m_lastCreature = thing->getId(); | |
| 376 | + | } | |
| 377 | + | ||
| 378 | + | thing->onDisappear(); | |
| 379 | + | ||
| 380 | + | if(thing->isTranslucent()) | |
| 381 | + | checkTranslucentLight(); | |
| 382 | + | ||
| 383 | + | if (g_game.isTileThingLuaCallbackEnabled() && removed) { | |
| 384 | + | callLuaField("onRemoveThing", thing); | |
| 385 | + | } | |
| 386 | + | ||
| 387 | + | return removed; | |
| 388 | + | } | |
| 389 | + | ||
| 390 | + | ThingPtr Tile::getThing(int stackPos) | |
| 391 | + | { | |
| 392 | + | if(stackPos >= 0 && stackPos < (int)m_things.size()) | |
| 393 | + | return m_things[stackPos]; | |
| 394 | + | return nullptr; | |
| 395 | + | } | |
| 396 | + | ||
| 397 | + | EffectPtr Tile::getEffect(uint16 id) | |
| 398 | + | { | |
| 399 | + | for(const EffectPtr& effect : m_effects) | |
| 400 | + | if(effect->getId() == id) | |
| 401 | + | return effect; | |
| 402 | + | return nullptr; | |
| 403 | + | } | |
| 404 | + | ||
| 405 | + | bool Tile::hasThing(const ThingPtr& thing) | |
| 406 | + | { | |
| 407 | + | return std::find(m_things.begin(), m_things.end(), thing) != m_things.end(); | |
| 408 | + | } | |
| 409 | + | ||
| 410 | + | int Tile::getThingStackPos(const ThingPtr& thing) | |
| 411 | + | { | |
| 412 | + | for(uint stackpos = 0; stackpos < m_things.size(); ++stackpos) | |
| 413 | + | if(thing == m_things[stackpos]) | |
| 414 | + | return stackpos; | |
| 415 | + | return -1; | |
| 416 | + | } | |
| 417 | + | ||
| 418 | + | ThingPtr Tile::getTopThing() | |
| 419 | + | { | |
| 420 | + | if(isEmpty()) | |
| 421 | + | return nullptr; | |
| 422 | + | for(const ThingPtr& thing : m_things) | |
| 423 | + | if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature()) | |
| 424 | + | return thing; | |
| 425 | + | return m_things[m_things.size() - 1]; | |
| 426 | + | } | |
| 427 | + | ||
| 428 | + | std::vector<ItemPtr> Tile::getItems() | |
| 429 | + | { | |
| 430 | + | std::vector<ItemPtr> items; | |
| 431 | + | for(const ThingPtr& thing : m_things) { | |
| 432 | + | if(!thing->isItem()) | |
| 433 | + | continue; | |
| 434 | + | ItemPtr item = thing->static_self_cast<Item>(); | |
| 435 | + | items.push_back(item); | |
| 436 | + | } | |
| 437 | + | return items; | |
| 438 | + | } | |
| 439 | + | ||
| 440 | + | std::vector<CreaturePtr> Tile::getCreatures() | |
| 441 | + | { | |
| 442 | + | std::vector<CreaturePtr> creatures; | |
| 443 | + | for(const ThingPtr& thing : m_things) { | |
| 444 | + | if(thing->isCreature()) | |
| 445 | + | creatures.push_back(thing->static_self_cast<Creature>()); | |
| 446 | + | } | |
| 447 | + | return creatures; | |
| 448 | + | } | |
| 449 | + | ||
| 450 | + | ItemPtr Tile::getGround() | |
| 451 | + | { | |
| 452 | + | ThingPtr firstObject = getThing(0); | |
| 453 | + | if(!firstObject) | |
| 454 | + | return nullptr; | |
| 455 | + | if(firstObject->isGround() && firstObject->isItem()) | |
| 456 | + | return firstObject->static_self_cast<Item>(); | |
| 457 | + | return nullptr; | |
| 458 | + | } | |
| 459 | + | ||
| 460 | + | int Tile::getGroundSpeed() | |
| 461 | + | { | |
| 462 | + | if (m_speed) | |
| 463 | + | return m_speed; | |
| 464 | + | int groundSpeed = 100; | |
| 465 | + | if(ItemPtr ground = getGround()) | |
| 466 | + | groundSpeed = ground->getGroundSpeed(); | |
| 467 | + | return groundSpeed; | |
| 468 | + | } | |
| 469 | + | ||
| 470 | + | uint8 Tile::getMinimapColorByte() | |
| 471 | + | { | |
| 472 | + | uint8 color = 255; // alpha | |
| 473 | + | if(m_minimapColor != 0) | |
| 474 | + | return m_minimapColor; | |
| 475 | + | ||
| 476 | + | for(const ThingPtr& thing : m_things) { | |
| 477 | + | if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()) | |
| 478 | + | break; | |
| 479 | + | uint8 c = thing->getMinimapColor(); | |
| 480 | + | if(c != 0) | |
| 481 | + | color = c; | |
| 482 | + | } | |
| 483 | + | return color; | |
| 484 | + | } | |
| 485 | + | ||
| 486 | + | ThingPtr Tile::getTopLookThing() | |
| 487 | + | { | |
| 488 | + | if(isEmpty()) | |
| 489 | + | return nullptr; | |
| 490 | + | ||
| 491 | + | for(uint i = 0; i < m_things.size(); ++i) { | |
| 492 | + | ThingPtr thing = m_things[i]; | |
| 493 | + | if(!thing->isIgnoreLook() && (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop())) | |
| 494 | + | return thing; | |
| 495 | + | } | |
| 496 | + | ||
| 497 | + | return m_things[0]; | |
| 498 | + | } | |
| 499 | + | ||
| 500 | + | ThingPtr Tile::getTopLookThingEx(Point offset) | |
| 501 | + | { | |
| 502 | + | auto creature = getTopCreatureEx(offset); | |
| 503 | + | if (creature) | |
| 504 | + | return creature; | |
| 505 | + | ||
| 506 | + | if (isEmpty()) | |
| 507 | + | return nullptr; | |
| 508 | + | ||
| 509 | + | for (uint i = 0; i < m_things.size(); ++i) { | |
| 510 | + | ThingPtr thing = m_things[i]; | |
| 511 | + | if (!thing->isIgnoreLook() && (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature())) | |
| 512 | + | return thing; | |
| 513 | + | } | |
| 514 | + | ||
| 515 | + | return m_things[0]; | |
| 516 | + | } | |
| 517 | + | ||
| 518 | + | ThingPtr Tile::getTopUseThing() | |
| 519 | + | { | |
| 520 | + | if(isEmpty()) | |
| 521 | + | return nullptr; | |
| 522 | + | ||
| 523 | + | for(uint i = 0; i < m_things.size(); ++i) { | |
| 524 | + | ThingPtr thing = m_things[i]; | |
| 525 | + | if (thing->isForceUse() || (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature() && !thing->isSplash())) | |
| 526 | + | return thing; | |
| 527 | + | } | |
| 528 | + | ||
| 529 | + | for (uint i = m_things.size() - 1; i > 0; --i) { | |
| 530 | + | ThingPtr thing = m_things[i]; | |
| 531 | + | if (!thing->isSplash() && !thing->isCreature()) | |
| 532 | + | return thing; | |
| 533 | + | } | |
| 534 | + | ||
| 535 | + | return m_things[0]; | |
| 536 | + | } | |
| 537 | + | ||
| 538 | + | CreaturePtr Tile::getTopCreature() | |
| 539 | + | { | |
| 540 | + | CreaturePtr creature; | |
| 541 | + | for(uint i = 0; i < m_things.size(); ++i) { | |
| 542 | + | ThingPtr thing = m_things[i]; | |
| 543 | + | if(thing->isLocalPlayer()) // return local player if there is no other creature | |
| 544 | + | creature = thing->static_self_cast<Creature>(); | |
| 545 | + | else if(thing->isCreature() && !thing->isLocalPlayer()) | |
| 546 | + | return thing->static_self_cast<Creature>(); | |
| 547 | + | } | |
| 548 | + | if(!creature && !m_walkingCreatures.empty()) | |
| 549 | + | creature = m_walkingCreatures.back(); | |
| 550 | + | ||
| 551 | + | // check for walking creatures in tiles around | |
| 552 | + | if(!creature) { | |
| 553 | + | for(int xi=-1;xi<=1;++xi) { | |
| 554 | + | for(int yi=-1;yi<=1;++yi) { | |
| 555 | + | Position pos = m_position.translated(xi, yi); | |
| 556 | + | if(pos == m_position) | |
| 557 | + | continue; | |
| 558 | + | ||
| 559 | + | const TilePtr& tile = g_map.getTile(pos); | |
| 560 | + | if(tile) { | |
| 561 | + | for(const CreaturePtr& c : tile->getCreatures()) { | |
| 562 | + | if(c->isWalking() && c->getLastStepFromPosition() == m_position && c->getStepProgress() < 0.75f) { | |
| 563 | + | creature = c; | |
| 564 | + | } | |
| 565 | + | } | |
| 566 | + | } | |
| 567 | + | } | |
| 568 | + | } | |
| 569 | + | } | |
| 570 | + | return creature; | |
| 571 | + | } | |
| 572 | + | ||
| 573 | + | CreaturePtr Tile::getTopCreatureEx(Point offset) | |
| 574 | + | { | |
| 575 | + | static const int cords[][2] = { {1,1}, {0,1}, {1, 0}, {-1, 1}, {0, 0}, {1, -1}, {-1, 0}, {0, -1}, {-1, -1} }; | |
| 576 | + | ||
| 577 | + | CreaturePtr localPlayer = nullptr; | |
| 578 | + | Point localPlayerOffset; | |
| 579 | + | ||
| 580 | + | for (auto& xy : cords) { | |
| 581 | + | Position pos = m_position.translated(xy[0], xy[1]); | |
| 582 | + | const TilePtr& tile = g_map.getTile(pos); | |
| 583 | + | if (!tile) continue; | |
| 584 | + | for (const CreaturePtr& c : tile->getCreatures()) { | |
| 585 | + | if (c->isLocalPlayer()) { | |
| 586 | + | localPlayer = c; | |
| 587 | + | localPlayerOffset = Point(offset.x - xy[0] * g_sprites.spriteSize(), offset.y - xy[1] * g_sprites.spriteSize()); | |
| 588 | + | continue; | |
| 589 | + | } | |
| 590 | + | if (c->isInsideOffset(Point(offset.x - xy[0] * g_sprites.spriteSize(), offset.y - xy[1] * g_sprites.spriteSize()))) | |
| 591 | + | return c; | |
| 592 | + | } | |
| 593 | + | } | |
| 594 | + | ||
| 595 | + | if (localPlayer && localPlayer->isInsideOffset(localPlayerOffset)) | |
| 596 | + | return localPlayer; | |
| 597 | + | ||
| 598 | + | return nullptr; | |
| 599 | + | } | |
| 600 | + | ||
| 601 | + | ThingPtr Tile::getTopMoveThing() | |
| 602 | + | { | |
| 603 | + | if(isEmpty()) | |
| 604 | + | return nullptr; | |
| 605 | + | ||
| 606 | + | for(uint i = 0; i < m_things.size(); ++i) { | |
| 607 | + | ThingPtr thing = m_things[i]; | |
| 608 | + | if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature()) { | |
| 609 | + | if(i > 0 && thing->isNotMoveable()) | |
| 610 | + | return m_things[i-1]; | |
| 611 | + | return thing; | |
| 612 | + | } | |
| 613 | + | } | |
| 614 | + | ||
| 615 | + | for(const ThingPtr& thing : m_things) { | |
| 616 | + | if(thing->isCreature()) | |
| 617 | + | return thing; | |
| 618 | + | } | |
| 619 | + | ||
| 620 | + | return m_things[0]; | |
| 621 | + | } | |
| 622 | + | ||
| 623 | + | ThingPtr Tile::getTopMultiUseThing() | |
| 624 | + | { | |
| 625 | + | if (isEmpty()) | |
| 626 | + | return nullptr; | |
| 627 | + | ||
| 628 | + | if (CreaturePtr topCreature = getTopCreature()) | |
| 629 | + | return topCreature; | |
| 630 | + | ||
| 631 | + | for (uint i = 0; i < m_things.size(); ++i) { | |
| 632 | + | ThingPtr thing = m_things[i]; | |
| 633 | + | if (thing->isForceUse()) | |
| 634 | + | return thing; | |
| 635 | + | } | |
| 636 | + | ||
| 637 | + | for (uint i = 0; i < m_things.size(); ++i) { | |
| 638 | + | ThingPtr thing = m_things[i]; | |
| 639 | + | if (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()) { | |
| 640 | + | if (i > 0 && thing->isSplash()) | |
| 641 | + | return m_things[i - 1]; | |
| 642 | + | return thing; | |
| 643 | + | } | |
| 644 | + | } | |
| 645 | + | ||
| 646 | + | return m_things.back(); | |
| 647 | + | } | |
| 648 | + | ||
| 649 | + | ThingPtr Tile::getTopMultiUseThingEx(Point offset) | |
| 650 | + | { | |
| 651 | + | if (CreaturePtr topCreature = getTopCreatureEx(offset)) | |
| 652 | + | return topCreature; | |
| 653 | + | ||
| 654 | + | if (isEmpty()) | |
| 655 | + | return nullptr; | |
| 656 | + | ||
| 657 | + | for (uint i = 0; i < m_things.size(); ++i) { | |
| 658 | + | ThingPtr thing = m_things[i]; | |
| 659 | + | if (thing->isForceUse() && !thing->isCreature()) | |
| 660 | + | return thing; | |
| 661 | + | } | |
| 662 | + | ||
| 663 | + | for (uint i = 0; i < m_things.size(); ++i) { | |
| 664 | + | ThingPtr thing = m_things[i]; | |
| 665 | + | if (!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop() && !thing->isCreature()) { | |
| 666 | + | if (i > 0 && thing->isSplash()) | |
| 667 | + | return m_things[i - 1]; | |
| 668 | + | return thing; | |
| 669 | + | } | |
| 670 | + | } | |
| 671 | + | ||
| 672 | + | for (uint i = m_things.size() - 1; i > 0; --i) { | |
| 673 | + | ThingPtr thing = m_things[i]; | |
| 674 | + | if (!thing->isCreature()) | |
| 675 | + | return thing; | |
| 676 | + | } | |
| 677 | + | ||
| 678 | + | return m_things[0]; | |
| 679 | + | } | |
| 680 | + | ||
| 681 | + | bool Tile::isWalkable(bool ignoreCreatures) | |
| 682 | + | { | |
| 683 | + | if(!getGround()) | |
| 684 | + | return false; | |
| 685 | + | ||
| 686 | + | for(const ThingPtr& thing : m_things) { | |
| 687 | + | if(thing->isNotWalkable()) | |
| 688 | + | return false; | |
| 689 | + | ||
| 690 | + | if(!ignoreCreatures) { | |
| 691 | + | if(thing->isCreature()) { | |
| 692 | + | CreaturePtr creature = thing->static_self_cast<Creature>(); | |
| 693 | + | if(!creature->isPassable() && creature->canBeSeen() && !creature->isLocalPlayer()) | |
| 694 | + | return false; | |
| 695 | + | } | |
| 696 | + | } | |
| 697 | + | } | |
| 698 | + | return true; | |
| 699 | + | } | |
| 700 | + | ||
| 701 | + | bool Tile::isPathable() | |
| 702 | + | { | |
| 703 | + | for(const ThingPtr& thing : m_things) | |
| 704 | + | if(thing->isNotPathable()) | |
| 705 | + | return false; | |
| 706 | + | return true; | |
| 707 | + | } | |
| 708 | + | ||
| 709 | + | bool Tile::isFullGround() | |
| 710 | + | { | |
| 711 | + | ItemPtr ground = getGround(); | |
| 712 | + | if(ground && ground->isFullGround()) | |
| 713 | + | return true; | |
| 714 | + | return false; | |
| 715 | + | } | |
| 716 | + | ||
| 717 | + | bool Tile::isFullyOpaque() | |
| 718 | + | { | |
| 719 | + | ThingPtr firstObject = getThing(0); | |
| 720 | + | return firstObject && firstObject->isFullGround(); | |
| 721 | + | } | |
| 722 | + | ||
| 723 | + | bool Tile::isSingleDimension() | |
| 724 | + | { | |
| 725 | + | if(!m_walkingCreatures.empty()) | |
| 726 | + | return false; | |
| 727 | + | for(const ThingPtr& thing : m_things) | |
| 728 | + | if(thing->getHeight() != 1 || thing->getWidth() != 1) | |
| 729 | + | return false; | |
| 730 | + | return true; | |
| 731 | + | } | |
| 732 | + | ||
| 733 | + | bool Tile::isLookPossible() | |
| 734 | + | { | |
| 735 | + | for (const ThingPtr& thing : m_things) | |
| 736 | + | if (thing->blockProjectile()) | |
| 737 | + | return false; | |
| 738 | + | return true; | |
| 739 | + | } | |
| 740 | + | ||
| 741 | + | bool Tile::isBlockingProjectile() | |
| 742 | + | { | |
| 743 | + | for (const ThingPtr& thing : m_things) | |
| 744 | + | if (thing->blockProjectile()) | |
| 745 | + | return true; | |
| 746 | + | return false; | |
| 747 | + | } | |
| 748 | + | ||
| 749 | + | bool Tile::isClickable() | |
| 750 | + | { | |
| 751 | + | bool hasGround = false; | |
| 752 | + | bool hasOnBottom = false; | |
| 753 | + | bool hasIgnoreLook = false; | |
| 754 | + | for(const ThingPtr& thing : m_things) { | |
| 755 | + | if(thing->isGround()) | |
| 756 | + | hasGround = true; | |
| 757 | + | if(thing->isOnBottom()) | |
| 758 | + | hasOnBottom = true; | |
| 759 | + | if((hasGround || hasOnBottom) && !hasIgnoreLook) | |
| 760 | + | return true; | |
| 761 | + | } | |
| 762 | + | return false; | |
| 763 | + | } | |
| 764 | + | ||
| 765 | + | bool Tile::isEmpty() | |
| 766 | + | { | |
| 767 | + | return m_things.size() == 0; | |
| 768 | + | } | |
| 769 | + | ||
| 770 | + | bool Tile::isDrawable() | |
| 771 | + | { | |
| 772 | + | return !m_things.empty() || !m_walkingCreatures.empty() || !m_effects.empty(); | |
| 773 | + | } | |
| 774 | + | ||
| 775 | + | bool Tile::mustHookEast() | |
| 776 | + | { | |
| 777 | + | for(const ThingPtr& thing : m_things) | |
| 778 | + | if(thing->isHookEast()) | |
| 779 | + | return true; | |
| 780 | + | return false; | |
| 781 | + | } | |
| 782 | + | ||
| 783 | + | bool Tile::mustHookSouth() | |
| 784 | + | { | |
| 785 | + | for(const ThingPtr& thing : m_things) | |
| 786 | + | if(thing->isHookSouth()) | |
| 787 | + | return true; | |
| 788 | + | return false; | |
| 789 | + | } | |
| 790 | + | ||
| 791 | + | bool Tile::hasCreature() | |
| 792 | + | { | |
| 793 | + | for(const ThingPtr& thing : m_things) | |
| 794 | + | if(thing->isCreature()) | |
| 795 | + | return true; | |
| 796 | + | return false; | |
| 797 | + | } | |
| 798 | + | ||
| 799 | + | bool Tile::hasBlockingCreature() | |
| 800 | + | { | |
| 801 | + | for (const ThingPtr& thing : m_things) | |
| 802 | + | if (thing->isCreature() && !thing->static_self_cast<Creature>()->isPassable() && !thing->isLocalPlayer()) | |
| 803 | + | return true; | |
| 804 | + | return false; | |
| 805 | + | } | |
| 806 | + | ||
| 807 | + | bool Tile::limitsFloorsView(bool isFreeView) | |
| 808 | + | { | |
| 809 | + | // ground and walls limits the view | |
| 810 | + | ThingPtr firstThing = getThing(0); | |
| 811 | + | ||
| 812 | + | if(isFreeView) { | |
| 813 | + | if(firstThing && !firstThing->isDontHide() && (firstThing->isGround() || firstThing->isOnBottom())) | |
| 814 | + | return true; | |
| 815 | + | } else if(firstThing && !firstThing->isDontHide() && (firstThing->isGround() || (firstThing->isOnBottom() && firstThing->blockProjectile()))) | |
| 816 | + | return true; | |
| 817 | + | return false; | |
| 818 | + | } | |
| 819 | + | ||
| 820 | + | ||
| 821 | + | bool Tile::canErase() | |
| 822 | + | { | |
| 823 | + | return m_walkingCreatures.empty() && m_effects.empty() && m_things.empty() && m_flags == 0 && m_minimapColor == 0; | |
| 824 | + | } | |
| 825 | + | ||
| 826 | + | int Tile::getElevation() | |
| 827 | + | { | |
| 828 | + | int elevation = 0; | |
| 829 | + | for(const ThingPtr& thing : m_things) | |
| 830 | + | if(thing->getElevation() > 0) | |
| 831 | + | elevation++; | |
| 832 | + | return elevation; | |
| 833 | + | } | |
| 834 | + | ||
| 835 | + | bool Tile::hasElevation(int elevation) | |
| 836 | + | { | |
| 837 | + | return getElevation() >= elevation; | |
| 838 | + | } | |
| 839 | + | ||
| 840 | + | void Tile::checkTranslucentLight() | |
| 841 | + | { | |
| 842 | + | if(m_position.z != Otc::SEA_FLOOR) | |
| 843 | + | return; | |
| 844 | + | ||
| 845 | + | Position downPos = m_position; | |
| 846 | + | if(!downPos.down()) | |
| 847 | + | return; | |
| 848 | + | ||
| 849 | + | TilePtr tile = g_map.getOrCreateTile(downPos); | |
| 850 | + | if(!tile) | |
| 851 | + | return; | |
| 852 | + | ||
| 853 | + | bool translucent = false; | |
| 854 | + | for(const ThingPtr& thing : m_things) { | |
| 855 | + | if(thing->isTranslucent() || thing->hasLensHelp()) { | |
| 856 | + | translucent = true; | |
| 857 | + | break; | |
| 858 | + | } | |
| 859 | + | } | |
| 860 | + | ||
| 861 | + | if(translucent) | |
| 862 | + | tile->m_flags |= TILESTATE_TRANSLUECENT_LIGHT; | |
| 863 | + | else | |
| 864 | + | tile->m_flags &= ~TILESTATE_TRANSLUECENT_LIGHT; | |
| 865 | + | } | |
| 866 | + | ||
| 867 | + | void Tile::setText(const std::string& text, Color color) | |
| 868 | + | { | |
| 869 | + | if (!m_text) { | |
| 870 | + | m_text = StaticTextPtr(new StaticText()); | |
| 871 | + | } | |
| 872 | + | m_text->setText(text); | |
| 873 | + | m_text->setColor(color); | |
| 874 | + | } | |
| 875 | + | ||
| 876 | + | std::string Tile::getText() | |
| 877 | + | { | |
| 878 | + | return m_text ? m_text->getCachedText().getText() : ""; | |
| 879 | + | } | |
| 880 | + | ||
| 881 | + | void Tile::setTimer(int time, Color color) | |
| 882 | + | { | |
| 883 | + | if (time > 60000) { | |
| 884 | + | g_logger.warning("Max tile timer value is 300000 (300s)!"); | |
| 885 | + | return; | |
| 886 | + | } | |
| 887 | + | m_timer = time + g_clock.millis(); | |
| 888 | + | if (!m_timerText) { | |
| 889 | + | m_timerText = StaticTextPtr(new StaticText()); | |
| 890 | + | } | |
| 891 | + | m_timerText->setColor(color); | |
| 892 | + | } | |
| 893 | + | ||
| 894 | + | int Tile::getTimer() | |
| 895 | + | { | |
| 896 | + | return m_timerText ? std::max<int>(0, m_timer - g_clock.millis()) : 0; | |
| 897 | + | } | |
| 898 | + | ||
| 899 | + | void Tile::setFill(Color color) | |
| 900 | + | { | |
| 901 | + | m_fill = color; | |
| 902 | + | } | |
| 903 | + | ||
| 904 | + | bool Tile::canShoot(int distance) | |
| 905 | + | { | |
| 906 | + | auto player = g_game.getLocalPlayer(); | |
| 907 | + | if (!player) return false; | |
| 908 | + | auto playerPos = player->getPrewalkingPosition(); | |
| 909 | + | if(distance > 0 && std::max<int>(std::abs<int>(m_position.x - playerPos.x), std::abs<int>(m_position.y - playerPos.y)) > distance) | |
| 910 | + | return false; | |
| 911 | + | return g_map.isSightClear(playerPos, m_position); | |
| 912 | + | } |