| @@ -0,0 +1,1672 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "configmanager.h" | |
| 23 | + | #include "creature.h" | |
| 24 | + | #include "game.h" | |
| 25 | + | #include "monster.h" | |
| 26 | + | #include "scheduler.h" | |
| 27 | + | ||
| 28 | + | double Creature::speedA = 857.36; | |
| 29 | + | double Creature::speedB = 261.29; | |
| 30 | + | double Creature::speedC = -4795.01; | |
| 31 | + | ||
| 32 | + | extern Game g_game; | |
| 33 | + | extern ConfigManager g_config; | |
| 34 | + | extern CreatureEvents *g_creatureEvents; | |
| 35 | + | ||
| 36 | + | Creature::Creature() { onIdleStatus(); } | |
| 37 | + | ||
| 38 | + | Creature::~Creature() { | |
| 39 | + | for (Creature *summon : summons) { | |
| 40 | + | summon->setAttackedCreature(nullptr); | |
| 41 | + | summon->removeMaster(); | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | for (Condition *condition : conditions) { | |
| 45 | + | condition->endCondition(this); | |
| 46 | + | delete condition; | |
| 47 | + | } | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | bool Creature::canSee(const Position &myPos, const Position &pos, | |
| 51 | + | int32_t viewRangeX, int32_t viewRangeY) { | |
| 52 | + | if (myPos.z <= 7) { | |
| 53 | + | // we are on ground level or above (7 -> 0) | |
| 54 | + | // view is from 7 -> 0 | |
| 55 | + | if (pos.z > 7) { | |
| 56 | + | return false; | |
| 57 | + | } | |
| 58 | + | } else if (myPos.z >= 8) { | |
| 59 | + | // we are underground (8 -> 15) | |
| 60 | + | // view is +/- 2 from the floor we stand on | |
| 61 | + | if (Position::getDistanceZ(myPos, pos) > 2) { | |
| 62 | + | return false; | |
| 63 | + | } | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | const int_fast32_t offsetz = myPos.getZ() - pos.getZ(); | |
| 67 | + | return (pos.getX() >= myPos.getX() - viewRangeX + offsetz) && | |
| 68 | + | (pos.getX() <= myPos.getX() + viewRangeX + offsetz) && | |
| 69 | + | (pos.getY() >= myPos.getY() - viewRangeY + offsetz) && | |
| 70 | + | (pos.getY() <= myPos.getY() + viewRangeY + offsetz); | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | bool Creature::canSee(const Position &pos) const { | |
| 74 | + | return canSee(getPosition(), pos, Map::maxViewportX, Map::maxViewportY); | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | bool Creature::canSeeCreature(const Creature *creature) const { | |
| 78 | + | if (!canSeeInvisibility() && creature->isInvisible()) { | |
| 79 | + | return false; | |
| 80 | + | } | |
| 81 | + | return true; | |
| 82 | + | } | |
| 83 | + | ||
| 84 | + | void Creature::setSkull(Skulls_t newSkull) { | |
| 85 | + | skull = newSkull; | |
| 86 | + | g_game.updateCreatureSkull(this); | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | int64_t Creature::getTimeSinceLastMove() const { | |
| 90 | + | if (lastStep) { | |
| 91 | + | return OTSYS_TIME() - lastStep; | |
| 92 | + | } | |
| 93 | + | return std::numeric_limits<int64_t>::max(); | |
| 94 | + | } | |
| 95 | + | ||
| 96 | + | int32_t Creature::getWalkDelay(Direction dir) const { | |
| 97 | + | if (lastStep == 0) { | |
| 98 | + | return 0; | |
| 99 | + | } | |
| 100 | + | ||
| 101 | + | int64_t ct = OTSYS_TIME(); | |
| 102 | + | int64_t stepDuration = getStepDuration(dir); | |
| 103 | + | return stepDuration - (ct - lastStep); | |
| 104 | + | } | |
| 105 | + | ||
| 106 | + | int32_t Creature::getWalkDelay() const { | |
| 107 | + | // Used for auto-walking | |
| 108 | + | if (lastStep == 0) { | |
| 109 | + | return 0; | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | int64_t ct = OTSYS_TIME(); | |
| 113 | + | int64_t stepDuration = getStepDuration() * lastStepCost; | |
| 114 | + | return stepDuration - (ct - lastStep); | |
| 115 | + | } | |
| 116 | + | ||
| 117 | + | void Creature::onThink(uint32_t interval) { | |
| 118 | + | if (!isMapLoaded && useCacheMap()) { | |
| 119 | + | isMapLoaded = true; | |
| 120 | + | updateMapCache(); | |
| 121 | + | } | |
| 122 | + | ||
| 123 | + | if (followCreature && master != followCreature && | |
| 124 | + | !canSeeCreature(followCreature)) { | |
| 125 | + | onCreatureDisappear(followCreature, false); | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | if (attackedCreature && master != attackedCreature && | |
| 129 | + | !canSeeCreature(attackedCreature)) { | |
| 130 | + | onCreatureDisappear(attackedCreature, false); | |
| 131 | + | } | |
| 132 | + | ||
| 133 | + | blockTicks += interval; | |
| 134 | + | if (blockTicks >= 1000) { | |
| 135 | + | blockCount = std::min<uint32_t>(blockCount + 1, 2); | |
| 136 | + | blockTicks = 0; | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | if (followCreature) { | |
| 140 | + | walkUpdateTicks += interval; | |
| 141 | + | if (forceUpdateFollowPath || walkUpdateTicks >= 2000) { | |
| 142 | + | walkUpdateTicks = 0; | |
| 143 | + | forceUpdateFollowPath = false; | |
| 144 | + | isUpdatingPath = true; | |
| 145 | + | } | |
| 146 | + | } | |
| 147 | + | ||
| 148 | + | if (isUpdatingPath) { | |
| 149 | + | isUpdatingPath = false; | |
| 150 | + | goToFollowCreature(); | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | // scripting event - onThink | |
| 154 | + | const CreatureEventList &thinkEvents = | |
| 155 | + | getCreatureEvents(CREATURE_EVENT_THINK); | |
| 156 | + | for (CreatureEvent *thinkEvent : thinkEvents) { | |
| 157 | + | thinkEvent->executeOnThink(this, interval); | |
| 158 | + | } | |
| 159 | + | } | |
| 160 | + | ||
| 161 | + | void Creature::onAttacking(uint32_t interval) { | |
| 162 | + | if (!attackedCreature) { | |
| 163 | + | return; | |
| 164 | + | } | |
| 165 | + | ||
| 166 | + | onAttacked(); | |
| 167 | + | attackedCreature->onAttacked(); | |
| 168 | + | ||
| 169 | + | if (g_game.isSightClear(getPosition(), attackedCreature->getPosition(), | |
| 170 | + | true)) { | |
| 171 | + | doAttacking(interval); | |
| 172 | + | } | |
| 173 | + | } | |
| 174 | + | ||
| 175 | + | void Creature::onIdleStatus() { | |
| 176 | + | if (getHealth() > 0) { | |
| 177 | + | damageMap.clear(); | |
| 178 | + | lastHitCreatureId = 0; | |
| 179 | + | } | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | void Creature::onWalk() { | |
| 183 | + | if (getWalkDelay() <= 0) { | |
| 184 | + | // Nota: Hemos eliminado el revert prematuro al caminar para permitir | |
| 185 | + | // que las animaciones de ataque se completen fluidamente (Premium look). | |
| 186 | + | ||
| 187 | + | Player *player = getPlayer(); | |
| 188 | + | Direction dir; | |
| 189 | + | uint32_t flags = FLAG_IGNOREFIELDDAMAGE; | |
| 190 | + | if (getNextStep(dir, flags)) { | |
| 191 | + | ReturnValue ret = g_game.internalMoveCreature(this, dir, flags); | |
| 192 | + | if (player && | |
| 193 | + | ret != RETURNVALUE_NOERROR) { // it's player and walk path is invalid | |
| 194 | + | if (tryToFixAutoWalk(dir)) { // correction for auto walking | |
| 195 | + | ret = g_game.internalMoveCreature(this, dir, flags); | |
| 196 | + | } | |
| 197 | + | } | |
| 198 | + | if (ret != RETURNVALUE_NOERROR) { | |
| 199 | + | if (player) { | |
| 200 | + | player->sendCancelMessage(ret); | |
| 201 | + | player->sendCancelWalk(); | |
| 202 | + | player->sendNewCancelWalk(); | |
| 203 | + | } | |
| 204 | + | ||
| 205 | + | forceUpdateFollowPath = true; | |
| 206 | + | } | |
| 207 | + | } else { | |
| 208 | + | if (listWalkDir.empty()) { | |
| 209 | + | onWalkComplete(); | |
| 210 | + | } | |
| 211 | + | ||
| 212 | + | stopEventWalk(); | |
| 213 | + | } | |
| 214 | + | } | |
| 215 | + | ||
| 216 | + | if (cancelNextWalk) { | |
| 217 | + | listWalkDir.clear(); | |
| 218 | + | onWalkAborted(); | |
| 219 | + | cancelNextWalk = false; | |
| 220 | + | } | |
| 221 | + | ||
| 222 | + | if (eventWalk != 0) { | |
| 223 | + | eventWalk = 0; | |
| 224 | + | addEventWalk(); | |
| 225 | + | } | |
| 226 | + | } | |
| 227 | + | ||
| 228 | + | void Creature::onWalk(Direction &dir) { | |
| 229 | + | if (hasCondition(CONDITION_DRUNK)) { | |
| 230 | + | uint32_t r = uniform_random(0, 20); | |
| 231 | + | if (r <= DIRECTION_DIAGONAL_MASK) { | |
| 232 | + | if (r < DIRECTION_DIAGONAL_MASK) { | |
| 233 | + | dir = static_cast<Direction>(r); | |
| 234 | + | } | |
| 235 | + | g_game.internalCreatureSay(this, TALKTYPE_MONSTER_SAY, "Hicks!", false); | |
| 236 | + | } | |
| 237 | + | } | |
| 238 | + | } | |
| 239 | + | ||
| 240 | + | bool Creature::getNextStep(Direction &dir, uint32_t &) { | |
| 241 | + | if (listWalkDir.empty()) { | |
| 242 | + | return false; | |
| 243 | + | } | |
| 244 | + | ||
| 245 | + | dir = listWalkDir.front(); | |
| 246 | + | listWalkDir.pop_front(); | |
| 247 | + | onWalk(dir); | |
| 248 | + | return true; | |
| 249 | + | } | |
| 250 | + | ||
| 251 | + | void Creature::startAutoWalk(const std::list<Direction> &listDir) { | |
| 252 | + | listWalkDir = listDir; | |
| 253 | + | addEventWalk(); | |
| 254 | + | } | |
| 255 | + | ||
| 256 | + | void Creature::addEventWalk() { | |
| 257 | + | cancelNextWalk = false; | |
| 258 | + | ||
| 259 | + | if (getStepSpeed() <= 0) { | |
| 260 | + | return; | |
| 261 | + | } | |
| 262 | + | ||
| 263 | + | if (eventWalk != 0) { | |
| 264 | + | return; | |
| 265 | + | } | |
| 266 | + | ||
| 267 | + | int64_t ticks = getEventStepTicks(); | |
| 268 | + | if (ticks <= 0) { | |
| 269 | + | return; | |
| 270 | + | } | |
| 271 | + | ||
| 272 | + | // Take first step right away, but still queue the next | |
| 273 | + | if (ticks == 1) { | |
| 274 | + | g_game.checkCreatureWalk(getID()); | |
| 275 | + | } | |
| 276 | + | ||
| 277 | + | eventWalk = g_scheduler.addEvent(createSchedulerTask( | |
| 278 | + | ticks, std::bind(&Game::checkCreatureWalk, &g_game, getID()))); | |
| 279 | + | } | |
| 280 | + | ||
| 281 | + | void Creature::stopEventWalk() { | |
| 282 | + | if (eventWalk != 0) { | |
| 283 | + | g_scheduler.stopEvent(eventWalk); | |
| 284 | + | eventWalk = 0; | |
| 285 | + | } | |
| 286 | + | } | |
| 287 | + | ||
| 288 | + | void Creature::updateMapCache() { | |
| 289 | + | Tile *tile; | |
| 290 | + | const Position &myPos = getPosition(); | |
| 291 | + | Position pos(0, 0, myPos.z); | |
| 292 | + | ||
| 293 | + | for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { | |
| 294 | + | for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { | |
| 295 | + | pos.x = myPos.getX() + x; | |
| 296 | + | pos.y = myPos.getY() + y; | |
| 297 | + | tile = g_game.map.getTile(pos); | |
| 298 | + | updateTileCache(tile, pos); | |
| 299 | + | } | |
| 300 | + | } | |
| 301 | + | } | |
| 302 | + | ||
| 303 | + | void Creature::updateTileCache(const Tile *tile, int32_t dx, int32_t dy) { | |
| 304 | + | if (std::abs(dx) <= maxWalkCacheWidth && std::abs(dy) <= maxWalkCacheHeight) { | |
| 305 | + | localMapCache[maxWalkCacheHeight + dy][maxWalkCacheWidth + dx] = | |
| 306 | + | tile && tile->queryAdd(0, *this, 1, | |
| 307 | + | FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) == | |
| 308 | + | RETURNVALUE_NOERROR; | |
| 309 | + | } | |
| 310 | + | } | |
| 311 | + | ||
| 312 | + | void Creature::updateTileCache(const Tile *tile, const Position &pos) { | |
| 313 | + | const Position &myPos = getPosition(); | |
| 314 | + | if (pos.z == myPos.z) { | |
| 315 | + | int32_t dx = Position::getOffsetX(pos, myPos); | |
| 316 | + | int32_t dy = Position::getOffsetY(pos, myPos); | |
| 317 | + | updateTileCache(tile, dx, dy); | |
| 318 | + | } | |
| 319 | + | } | |
| 320 | + | ||
| 321 | + | int32_t Creature::getWalkCache(const Position &pos) const { | |
| 322 | + | if (!useCacheMap()) { | |
| 323 | + | return 2; | |
| 324 | + | } | |
| 325 | + | ||
| 326 | + | const Position &myPos = getPosition(); | |
| 327 | + | if (myPos.z != pos.z) { | |
| 328 | + | return 0; | |
| 329 | + | } | |
| 330 | + | ||
| 331 | + | if (pos == myPos) { | |
| 332 | + | return 1; | |
| 333 | + | } | |
| 334 | + | ||
| 335 | + | int32_t dx = Position::getOffsetX(pos, myPos); | |
| 336 | + | if (std::abs(dx) <= maxWalkCacheWidth) { | |
| 337 | + | int32_t dy = Position::getOffsetY(pos, myPos); | |
| 338 | + | if (std::abs(dy) <= maxWalkCacheHeight) { | |
| 339 | + | if (localMapCache[maxWalkCacheHeight + dy][maxWalkCacheWidth + dx]) { | |
| 340 | + | return 1; | |
| 341 | + | } else { | |
| 342 | + | return 0; | |
| 343 | + | } | |
| 344 | + | } | |
| 345 | + | } | |
| 346 | + | ||
| 347 | + | // out of range | |
| 348 | + | return 2; | |
| 349 | + | } | |
| 350 | + | ||
| 351 | + | void Creature::onAddTileItem(const Tile *tile, const Position &pos) { | |
| 352 | + | if (isMapLoaded && pos.z == getPosition().z) { | |
| 353 | + | updateTileCache(tile, pos); | |
| 354 | + | } | |
| 355 | + | } | |
| 356 | + | ||
| 357 | + | void Creature::onUpdateTileItem(const Tile *tile, const Position &pos, | |
| 358 | + | const Item *, const ItemType &oldType, | |
| 359 | + | const Item *, const ItemType &newType) { | |
| 360 | + | if (!isMapLoaded) { | |
| 361 | + | return; | |
| 362 | + | } | |
| 363 | + | ||
| 364 | + | if (oldType.blockSolid || oldType.blockPathFind || newType.blockPathFind || | |
| 365 | + | newType.blockSolid) { | |
| 366 | + | if (pos.z == getPosition().z) { | |
| 367 | + | updateTileCache(tile, pos); | |
| 368 | + | } | |
| 369 | + | } | |
| 370 | + | } | |
| 371 | + | ||
| 372 | + | void Creature::onRemoveTileItem(const Tile *tile, const Position &pos, | |
| 373 | + | const ItemType &iType, const Item *) { | |
| 374 | + | if (!isMapLoaded) { | |
| 375 | + | return; | |
| 376 | + | } | |
| 377 | + | ||
| 378 | + | if (iType.blockSolid || iType.blockPathFind || iType.isGroundTile()) { | |
| 379 | + | if (pos.z == getPosition().z) { | |
| 380 | + | updateTileCache(tile, pos); | |
| 381 | + | } | |
| 382 | + | } | |
| 383 | + | } | |
| 384 | + | ||
| 385 | + | void Creature::onCreatureAppear(Creature *creature, bool isLogin) { | |
| 386 | + | if (creature == this) { | |
| 387 | + | if (useCacheMap()) { | |
| 388 | + | isMapLoaded = true; | |
| 389 | + | updateMapCache(); | |
| 390 | + | } | |
| 391 | + | ||
| 392 | + | if (isLogin) { | |
| 393 | + | setLastPosition(getPosition()); | |
| 394 | + | } | |
| 395 | + | } else if (isMapLoaded) { | |
| 396 | + | if (creature->getPosition().z == getPosition().z) { | |
| 397 | + | updateTileCache(creature->getTile(), creature->getPosition()); | |
| 398 | + | } | |
| 399 | + | } | |
| 400 | + | } | |
| 401 | + | ||
| 402 | + | void Creature::onRemoveCreature(Creature *creature, bool) { | |
| 403 | + | onCreatureDisappear(creature, true); | |
| 404 | + | if (creature == this) { | |
| 405 | + | if (master && !master->isRemoved()) { | |
| 406 | + | setMaster(nullptr); | |
| 407 | + | } | |
| 408 | + | } else if (isMapLoaded) { | |
| 409 | + | if (creature->getPosition().z == getPosition().z) { | |
| 410 | + | updateTileCache(creature->getTile(), creature->getPosition()); | |
| 411 | + | } | |
| 412 | + | } | |
| 413 | + | } | |
| 414 | + | ||
| 415 | + | void Creature::onCreatureDisappear(const Creature *creature, bool isLogout) { | |
| 416 | + | if (attackedCreature == creature) { | |
| 417 | + | setAttackedCreature(nullptr); | |
| 418 | + | onAttackedCreatureDisappear(isLogout); | |
| 419 | + | } | |
| 420 | + | ||
| 421 | + | if (followCreature == creature) { | |
| 422 | + | setFollowCreature(nullptr); | |
| 423 | + | onFollowCreatureDisappear(isLogout); | |
| 424 | + | } | |
| 425 | + | } | |
| 426 | + | ||
| 427 | + | void Creature::onChangeZone(ZoneType_t zone) { | |
| 428 | + | if (attackedCreature && zone == ZONE_PROTECTION) { | |
| 429 | + | onCreatureDisappear(attackedCreature, false); | |
| 430 | + | } | |
| 431 | + | } | |
| 432 | + | ||
| 433 | + | void Creature::onAttackedCreatureChangeZone(ZoneType_t zone) { | |
| 434 | + | if (zone == ZONE_PROTECTION) { | |
| 435 | + | onCreatureDisappear(attackedCreature, false); | |
| 436 | + | } | |
| 437 | + | } | |
| 438 | + | ||
| 439 | + | void Creature::onCreatureMove(Creature *creature, const Tile *newTile, | |
| 440 | + | const Position &newPos, const Tile *oldTile, | |
| 441 | + | const Position &oldPos, bool teleport) { | |
| 442 | + | if (creature == this) { | |
| 443 | + | lastStep = OTSYS_TIME(); | |
| 444 | + | lastStepCost = 1; | |
| 445 | + | ||
| 446 | + | if (!teleport) { | |
| 447 | + | if (oldPos.z != newPos.z) { | |
| 448 | + | // floor change extra cost | |
| 449 | + | lastStepCost = 2; | |
| 450 | + | } else if (Position::getDistanceX(newPos, oldPos) >= 1 && | |
| 451 | + | Position::getDistanceY(newPos, oldPos) >= 1) { | |
| 452 | + | // diagonal extra cost | |
| 453 | + | lastStepCost = 3; | |
| 454 | + | } | |
| 455 | + | } else { | |
| 456 | + | stopEventWalk(); | |
| 457 | + | } | |
| 458 | + | ||
| 459 | + | if (!summons.empty()) { | |
| 460 | + | // check if any of our summons is out of range (+/- 2 floors or 30 tiles | |
| 461 | + | // away) | |
| 462 | + | std::forward_list<Creature *> despawnList; | |
| 463 | + | for (Creature *summon : summons) { | |
| 464 | + | const Position &pos = summon->getPosition(); | |
| 465 | + | if (Position::getDistanceZ(newPos, pos) > 2 || | |
| 466 | + | (std::max<int32_t>(Position::getDistanceX(newPos, pos), | |
| 467 | + | Position::getDistanceY(newPos, pos)) > 30)) { | |
| 468 | + | despawnList.push_front(summon); | |
| 469 | + | } | |
| 470 | + | } | |
| 471 | + | ||
| 472 | + | for (Creature *despawnCreature : despawnList) { | |
| 473 | + | g_game.removeCreature(despawnCreature, true); | |
| 474 | + | } | |
| 475 | + | } | |
| 476 | + | ||
| 477 | + | if (newTile->getZone() != oldTile->getZone()) { | |
| 478 | + | onChangeZone(getZone()); | |
| 479 | + | } | |
| 480 | + | ||
| 481 | + | // update map cache | |
| 482 | + | if (isMapLoaded) { | |
| 483 | + | if (teleport || oldPos.z != newPos.z) { | |
| 484 | + | updateMapCache(); | |
| 485 | + | } else { | |
| 486 | + | const Position &myPos = getPosition(); | |
| 487 | + | ||
| 488 | + | if (oldPos.y > newPos.y) { // north | |
| 489 | + | // shift y south | |
| 490 | + | for (int32_t y = mapWalkHeight - 1; --y >= 0;) { | |
| 491 | + | memcpy(localMapCache[y + 1], localMapCache[y], | |
| 492 | + | sizeof(localMapCache[y])); | |
| 493 | + | } | |
| 494 | + | ||
| 495 | + | // update 0 | |
| 496 | + | for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { | |
| 497 | + | Tile *cacheTile = g_game.map.getTile( | |
| 498 | + | myPos.getX() + x, myPos.getY() - maxWalkCacheHeight, myPos.z); | |
| 499 | + | updateTileCache(cacheTile, x, -maxWalkCacheHeight); | |
| 500 | + | } | |
| 501 | + | } else if (oldPos.y < newPos.y) { // south | |
| 502 | + | // shift y north | |
| 503 | + | for (int32_t y = 0; y <= mapWalkHeight - 2; ++y) { | |
| 504 | + | memcpy(localMapCache[y], localMapCache[y + 1], | |
| 505 | + | sizeof(localMapCache[y])); | |
| 506 | + | } | |
| 507 | + | ||
| 508 | + | // update mapWalkHeight - 1 | |
| 509 | + | for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { | |
| 510 | + | Tile *cacheTile = g_game.map.getTile( | |
| 511 | + | myPos.getX() + x, myPos.getY() + maxWalkCacheHeight, myPos.z); | |
| 512 | + | updateTileCache(cacheTile, x, maxWalkCacheHeight); | |
| 513 | + | } | |
| 514 | + | } | |
| 515 | + | ||
| 516 | + | if (oldPos.x < newPos.x) { // east | |
| 517 | + | // shift y west | |
| 518 | + | int32_t starty = 0; | |
| 519 | + | int32_t endy = mapWalkHeight - 1; | |
| 520 | + | int32_t dy = Position::getDistanceY(oldPos, newPos); | |
| 521 | + | ||
| 522 | + | if (dy < 0) { | |
| 523 | + | endy += dy; | |
| 524 | + | } else if (dy > 0) { | |
| 525 | + | starty = dy; | |
| 526 | + | } | |
| 527 | + | ||
| 528 | + | for (int32_t y = starty; y <= endy; ++y) { | |
| 529 | + | for (int32_t x = 0; x <= mapWalkWidth - 2; ++x) { | |
| 530 | + | localMapCache[y][x] = localMapCache[y][x + 1]; | |
| 531 | + | } | |
| 532 | + | } | |
| 533 | + | ||
| 534 | + | // update mapWalkWidth - 1 | |
| 535 | + | for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { | |
| 536 | + | Tile *cacheTile = g_game.map.getTile(myPos.x + maxWalkCacheWidth, | |
| 537 | + | myPos.y + y, myPos.z); | |
| 538 | + | updateTileCache(cacheTile, maxWalkCacheWidth, y); | |
| 539 | + | } | |
| 540 | + | } else if (oldPos.x > newPos.x) { // west | |
| 541 | + | // shift y east | |
| 542 | + | int32_t starty = 0; | |
| 543 | + | int32_t endy = mapWalkHeight - 1; | |
| 544 | + | int32_t dy = Position::getDistanceY(oldPos, newPos); | |
| 545 | + | ||
| 546 | + | if (dy < 0) { | |
| 547 | + | endy += dy; | |
| 548 | + | } else if (dy > 0) { | |
| 549 | + | starty = dy; | |
| 550 | + | } | |
| 551 | + | ||
| 552 | + | for (int32_t y = starty; y <= endy; ++y) { | |
| 553 | + | for (int32_t x = mapWalkWidth - 1; --x >= 0;) { | |
| 554 | + | localMapCache[y][x + 1] = localMapCache[y][x]; | |
| 555 | + | } | |
| 556 | + | } | |
| 557 | + | ||
| 558 | + | // update 0 | |
| 559 | + | for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { | |
| 560 | + | Tile *cacheTile = g_game.map.getTile(myPos.x - maxWalkCacheWidth, | |
| 561 | + | myPos.y + y, myPos.z); | |
| 562 | + | updateTileCache(cacheTile, -maxWalkCacheWidth, y); | |
| 563 | + | } | |
| 564 | + | } | |
| 565 | + | ||
| 566 | + | updateTileCache(oldTile, oldPos); | |
| 567 | + | } | |
| 568 | + | } | |
| 569 | + | } else { | |
| 570 | + | if (isMapLoaded) { | |
| 571 | + | const Position &myPos = getPosition(); | |
| 572 | + | ||
| 573 | + | if (newPos.z == myPos.z) { | |
| 574 | + | updateTileCache(newTile, newPos); | |
| 575 | + | } | |
| 576 | + | ||
| 577 | + | if (oldPos.z == myPos.z) { | |
| 578 | + | updateTileCache(oldTile, oldPos); | |
| 579 | + | } | |
| 580 | + | } | |
| 581 | + | } | |
| 582 | + | ||
| 583 | + | if (creature == followCreature || (creature == this && followCreature)) { | |
| 584 | + | if (hasFollowPath) { | |
| 585 | + | isUpdatingPath = true; | |
| 586 | + | } | |
| 587 | + | ||
| 588 | + | if (newPos.z != oldPos.z || !canSee(followCreature->getPosition())) { | |
| 589 | + | onCreatureDisappear(followCreature, false); | |
| 590 | + | } | |
| 591 | + | } | |
| 592 | + | ||
| 593 | + | if (creature == attackedCreature || (creature == this && attackedCreature)) { | |
| 594 | + | if (newPos.z != oldPos.z || !canSee(attackedCreature->getPosition())) { | |
| 595 | + | onCreatureDisappear(attackedCreature, false); | |
| 596 | + | } else { | |
| 597 | + | if (hasExtraSwing()) { | |
| 598 | + | // our target is moving lets see if we can get in hit | |
| 599 | + | g_dispatcher.addTask(createTask( | |
| 600 | + | std::bind(&Game::checkCreatureAttack, &g_game, getID()))); | |
| 601 | + | } | |
| 602 | + | ||
| 603 | + | if (newTile->getZone() != oldTile->getZone()) { | |
| 604 | + | onAttackedCreatureChangeZone(attackedCreature->getZone()); | |
| 605 | + | } | |
| 606 | + | } | |
| 607 | + | } | |
| 608 | + | } | |
| 609 | + | ||
| 610 | + | void Creature::onDeath() { | |
| 611 | + | bool lastHitUnjustified = false; | |
| 612 | + | bool mostDamageUnjustified = false; | |
| 613 | + | Creature *lastHitCreature = g_game.getCreatureByID(lastHitCreatureId); | |
| 614 | + | Creature *lastHitCreatureMaster; | |
| 615 | + | if (lastHitCreature) { | |
| 616 | + | lastHitUnjustified = lastHitCreature->onKilledCreature(this); | |
| 617 | + | lastHitCreatureMaster = lastHitCreature->getMaster(); | |
| 618 | + | } else { | |
| 619 | + | lastHitCreatureMaster = nullptr; | |
| 620 | + | } | |
| 621 | + | ||
| 622 | + | Creature *mostDamageCreature = nullptr; | |
| 623 | + | ||
| 624 | + | const int64_t timeNow = OTSYS_TIME(); | |
| 625 | + | const uint32_t inFightTicks = g_config.getNumber(ConfigManager::PZ_LOCKED); | |
| 626 | + | int32_t mostDamage = 0; | |
| 627 | + | std::map<Creature *, uint64_t> experienceMap; | |
| 628 | + | for (const auto &it : damageMap) { | |
| 629 | + | if (Creature *attacker = g_game.getCreatureByID(it.first)) { | |
| 630 | + | CountBlock_t cb = it.second; | |
| 631 | + | if ((cb.total > mostDamage && (timeNow - cb.ticks <= inFightTicks))) { | |
| 632 | + | mostDamage = cb.total; | |
| 633 | + | mostDamageCreature = attacker; | |
| 634 | + | } | |
| 635 | + | ||
| 636 | + | if (attacker != this) { | |
| 637 | + | uint64_t gainExp = getGainedExperience(attacker); | |
| 638 | + | if (Player *attackerPlayer = attacker->getPlayer()) { | |
| 639 | + | attackerPlayer->removeAttacked(getPlayer()); | |
| 640 | + | ||
| 641 | + | Party *party = attackerPlayer->getParty(); | |
| 642 | + | if (party && party->getLeader() && | |
| 643 | + | party->isSharedExperienceActive() && | |
| 644 | + | party->isSharedExperienceEnabled()) { | |
| 645 | + | attacker = party->getLeader(); | |
| 646 | + | } | |
| 647 | + | } | |
| 648 | + | ||
| 649 | + | auto tmpIt = experienceMap.find(attacker); | |
| 650 | + | if (tmpIt == experienceMap.end()) { | |
| 651 | + | experienceMap[attacker] = gainExp; | |
| 652 | + | } else { | |
| 653 | + | tmpIt->second += gainExp; | |
| 654 | + | } | |
| 655 | + | } | |
| 656 | + | } | |
| 657 | + | } | |
| 658 | + | ||
| 659 | + | for (const auto &it : experienceMap) { | |
| 660 | + | it.first->onGainExperience(it.second, this); | |
| 661 | + | } | |
| 662 | + | ||
| 663 | + | if (mostDamageCreature) { | |
| 664 | + | if (mostDamageCreature != lastHitCreature && | |
| 665 | + | mostDamageCreature != lastHitCreatureMaster) { | |
| 666 | + | Creature *mostDamageCreatureMaster = mostDamageCreature->getMaster(); | |
| 667 | + | if (lastHitCreature != mostDamageCreatureMaster && | |
| 668 | + | (lastHitCreatureMaster == nullptr || | |
| 669 | + | mostDamageCreatureMaster != lastHitCreatureMaster)) { | |
| 670 | + | mostDamageUnjustified = | |
| 671 | + | mostDamageCreature->onKilledCreature(this, false); | |
| 672 | + | } | |
| 673 | + | } | |
| 674 | + | } | |
| 675 | + | ||
| 676 | + | bool droppedCorpse = dropCorpse(lastHitCreature, mostDamageCreature, | |
| 677 | + | lastHitUnjustified, mostDamageUnjustified); | |
| 678 | + | death(lastHitCreature); | |
| 679 | + | ||
| 680 | + | if (master) { | |
| 681 | + | setMaster(nullptr); | |
| 682 | + | } | |
| 683 | + | ||
| 684 | + | if (droppedCorpse) { | |
| 685 | + | g_game.removeCreature(this, false); | |
| 686 | + | } | |
| 687 | + | } | |
| 688 | + | ||
| 689 | + | bool Creature::dropCorpse(Creature *lastHitCreature, | |
| 690 | + | Creature *mostDamageCreature, bool lastHitUnjustified, | |
| 691 | + | bool mostDamageUnjustified) { | |
| 692 | + | if (!lootDrop && getMonster()) { | |
| 693 | + | if (master) { | |
| 694 | + | // scripting event - onDeath | |
| 695 | + | const CreatureEventList &deathEvents = | |
| 696 | + | getCreatureEvents(CREATURE_EVENT_DEATH); | |
| 697 | + | for (CreatureEvent *deathEvent : deathEvents) { | |
| 698 | + | deathEvent->executeOnDeath(this, nullptr, lastHitCreature, | |
| 699 | + | mostDamageCreature, lastHitUnjustified, | |
| 700 | + | mostDamageUnjustified); | |
| 701 | + | } | |
| 702 | + | } | |
| 703 | + | ||
| 704 | + | g_game.addMagicEffect(getPosition(), CONST_ME_POFF); | |
| 705 | + | } else { | |
| 706 | + | Item *splash; | |
| 707 | + | switch (getRace()) { | |
| 708 | + | case RACE_VENOM: | |
| 709 | + | splash = Item::CreateItem(ITEM_FULLSPLASH, FLUID_SLIME); | |
| 710 | + | break; | |
| 711 | + | ||
| 712 | + | case RACE_BLOOD: | |
| 713 | + | splash = Item::CreateItem(ITEM_FULLSPLASH, FLUID_BLOOD); | |
| 714 | + | break; | |
| 715 | + | ||
| 716 | + | default: | |
| 717 | + | splash = nullptr; | |
| 718 | + | break; | |
| 719 | + | } | |
| 720 | + | ||
| 721 | + | Tile *tile = getTile(); | |
| 722 | + | ||
| 723 | + | if (splash) { | |
| 724 | + | g_game.internalAddItem(tile, splash, INDEX_WHEREEVER, FLAG_NOLIMIT); | |
| 725 | + | g_game.startDecay(splash); | |
| 726 | + | } | |
| 727 | + | ||
| 728 | + | Item *corpse = getCorpse(lastHitCreature, mostDamageCreature); | |
| 729 | + | if (corpse) { | |
| 730 | + | g_game.internalAddItem(tile, corpse, INDEX_WHEREEVER, FLAG_NOLIMIT); | |
| 731 | + | g_game.startDecay(corpse); | |
| 732 | + | } | |
| 733 | + | ||
| 734 | + | // scripting event - onDeath | |
| 735 | + | for (CreatureEvent *deathEvent : getCreatureEvents(CREATURE_EVENT_DEATH)) { | |
| 736 | + | deathEvent->executeOnDeath(this, corpse, lastHitCreature, | |
| 737 | + | mostDamageCreature, lastHitUnjustified, | |
| 738 | + | mostDamageUnjustified); | |
| 739 | + | } | |
| 740 | + | ||
| 741 | + | if (corpse) { | |
| 742 | + | dropLoot(corpse->getContainer(), lastHitCreature); | |
| 743 | + | } | |
| 744 | + | } | |
| 745 | + | ||
| 746 | + | return true; | |
| 747 | + | } | |
| 748 | + | ||
| 749 | + | bool Creature::hasBeenAttacked(uint32_t attackerId) { | |
| 750 | + | auto it = damageMap.find(attackerId); | |
| 751 | + | if (it == damageMap.end()) { | |
| 752 | + | return false; | |
| 753 | + | } | |
| 754 | + | return (OTSYS_TIME() - it->second.ticks) <= | |
| 755 | + | g_config.getNumber(ConfigManager::PZ_LOCKED); | |
| 756 | + | } | |
| 757 | + | ||
| 758 | + | Item *Creature::getCorpse(Creature *, Creature *) { | |
| 759 | + | return Item::CreateItem(getLookCorpse()); | |
| 760 | + | } | |
| 761 | + | ||
| 762 | + | void Creature::changeHealth(int32_t healthChange, | |
| 763 | + | bool sendHealthChange /* = true*/) { | |
| 764 | + | int32_t oldHealth = health; | |
| 765 | + | ||
| 766 | + | if (healthChange > 0) { | |
| 767 | + | health += std::min<int32_t>(healthChange, getMaxHealth() - health); | |
| 768 | + | } else { | |
| 769 | + | health = std::max<int32_t>(0, health + healthChange); | |
| 770 | + | } | |
| 771 | + | ||
| 772 | + | if (sendHealthChange && oldHealth != health) { | |
| 773 | + | g_game.addCreatureHealth(this); | |
| 774 | + | } | |
| 775 | + | } | |
| 776 | + | ||
| 777 | + | void Creature::gainHealth(Creature *healer, int32_t healthGain) { | |
| 778 | + | changeHealth(healthGain); | |
| 779 | + | if (healer) { | |
| 780 | + | healer->onTargetCreatureGainHealth(this, healthGain); | |
| 781 | + | } | |
| 782 | + | } | |
| 783 | + | ||
| 784 | + | void Creature::drainHealth(Creature *attacker, int32_t damage) { | |
| 785 | + | changeHealth(-damage, false); | |
| 786 | + | ||
| 787 | + | if (attacker) { | |
| 788 | + | attacker->onAttackedCreatureDrainHealth(this, damage); | |
| 789 | + | } | |
| 790 | + | } | |
| 791 | + | ||
| 792 | + | BlockType_t Creature::blockHit(Creature *attacker, CombatType_t combatType, | |
| 793 | + | int32_t &damage, bool checkDefense /* = false */, | |
| 794 | + | bool checkArmor /* = false */, | |
| 795 | + | bool /* field = false */) { | |
| 796 | + | BlockType_t blockType = BLOCK_NONE; | |
| 797 | + | ||
| 798 | + | if (isImmune(combatType)) { | |
| 799 | + | damage = 0; | |
| 800 | + | blockType = BLOCK_IMMUNITY; | |
| 801 | + | } else if (checkDefense || checkArmor) { | |
| 802 | + | bool hasDefense = false; | |
| 803 | + | ||
| 804 | + | if (blockCount > 0) { | |
| 805 | + | --blockCount; | |
| 806 | + | hasDefense = true; | |
| 807 | + | } | |
| 808 | + | ||
| 809 | + | if (checkDefense && hasDefense && canUseDefense) { | |
| 810 | + | int32_t defense = getDefense(); | |
| 811 | + | damage -= uniform_random(defense / 2, defense); | |
| 812 | + | if (damage <= 0) { | |
| 813 | + | damage = 0; | |
| 814 | + | blockType = BLOCK_DEFENSE; | |
| 815 | + | checkArmor = false; | |
| 816 | + | } | |
| 817 | + | } | |
| 818 | + | ||
| 819 | + | if (checkArmor) { | |
| 820 | + | int32_t armor = getArmor(); | |
| 821 | + | if (armor > 3) { | |
| 822 | + | damage -= uniform_random(armor / 2, armor - (armor % 2 + 1)); | |
| 823 | + | } else if (armor > 0) { | |
| 824 | + | --damage; | |
| 825 | + | } | |
| 826 | + | ||
| 827 | + | if (damage <= 0) { | |
| 828 | + | damage = 0; | |
| 829 | + | blockType = BLOCK_ARMOR; | |
| 830 | + | } | |
| 831 | + | } | |
| 832 | + | ||
| 833 | + | if (hasDefense && blockType != BLOCK_NONE) { | |
| 834 | + | onBlockHit(); | |
| 835 | + | } | |
| 836 | + | } | |
| 837 | + | ||
| 838 | + | if (attacker) { | |
| 839 | + | attacker->onAttackedCreature(this); | |
| 840 | + | attacker->onAttackedCreatureBlockHit(blockType); | |
| 841 | + | } | |
| 842 | + | ||
| 843 | + | onAttacked(); | |
| 844 | + | return blockType; | |
| 845 | + | } | |
| 846 | + | ||
| 847 | + | bool Creature::setAttackedCreature(Creature *creature) { | |
| 848 | + | if (creature) { | |
| 849 | + | const Position &creaturePos = creature->getPosition(); | |
| 850 | + | if (creaturePos.z != getPosition().z || !canSee(creaturePos)) { | |
| 851 | + | attackedCreature = nullptr; | |
| 852 | + | return false; | |
| 853 | + | } | |
| 854 | + | ||
| 855 | + | attackedCreature = creature; | |
| 856 | + | onAttackedCreature(attackedCreature); | |
| 857 | + | attackedCreature->onAttacked(); | |
| 858 | + | } else { | |
| 859 | + | attackedCreature = nullptr; | |
| 860 | + | if (isAttackPulsing) { | |
| 861 | + | revertAttackOutfit(); | |
| 862 | + | } | |
| 863 | + | } | |
| 864 | + | ||
| 865 | + | for (Creature *summon : summons) { | |
| 866 | + | summon->setAttackedCreature(creature); | |
| 867 | + | } | |
| 868 | + | return true; | |
| 869 | + | } | |
| 870 | + | ||
| 871 | + | void Creature::getPathSearchParams(const Creature *, | |
| 872 | + | FindPathParams &fpp) const { | |
| 873 | + | fpp.fullPathSearch = !hasFollowPath; | |
| 874 | + | fpp.clearSight = true; | |
| 875 | + | fpp.maxSearchDist = 12; | |
| 876 | + | fpp.minTargetDist = 1; | |
| 877 | + | fpp.maxTargetDist = 1; | |
| 878 | + | } | |
| 879 | + | ||
| 880 | + | void Creature::goToFollowCreature() { | |
| 881 | + | if (followCreature) { | |
| 882 | + | FindPathParams fpp; | |
| 883 | + | getPathSearchParams(followCreature, fpp); | |
| 884 | + | ||
| 885 | + | Monster *monster = getMonster(); | |
| 886 | + | if (monster && !monster->getMaster() && | |
| 887 | + | (monster->isFleeing() || fpp.maxTargetDist > 1)) { | |
| 888 | + | Direction dir = DIRECTION_NONE; | |
| 889 | + | ||
| 890 | + | if (monster->isFleeing()) { | |
| 891 | + | monster->getDistanceStep(followCreature->getPosition(), dir, true); | |
| 892 | + | } else { // maxTargetDist > 1 | |
| 893 | + | if (!monster->getDistanceStep(followCreature->getPosition(), dir)) { | |
| 894 | + | // if we can't get anything then let the A* calculate | |
| 895 | + | listWalkDir.clear(); | |
| 896 | + | if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { | |
| 897 | + | hasFollowPath = true; | |
| 898 | + | startAutoWalk(listWalkDir); | |
| 899 | + | } else { | |
| 900 | + | hasFollowPath = false; | |
| 901 | + | } | |
| 902 | + | return; | |
| 903 | + | } | |
| 904 | + | } | |
| 905 | + | ||
| 906 | + | if (dir != DIRECTION_NONE) { | |
| 907 | + | listWalkDir.clear(); | |
| 908 | + | listWalkDir.push_front(dir); | |
| 909 | + | ||
| 910 | + | hasFollowPath = true; | |
| 911 | + | startAutoWalk(listWalkDir); | |
| 912 | + | } | |
| 913 | + | } else { | |
| 914 | + | listWalkDir.clear(); | |
| 915 | + | if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { | |
| 916 | + | hasFollowPath = true; | |
| 917 | + | startAutoWalk(listWalkDir); | |
| 918 | + | } else { | |
| 919 | + | hasFollowPath = false; | |
| 920 | + | } | |
| 921 | + | } | |
| 922 | + | } | |
| 923 | + | ||
| 924 | + | onFollowCreatureComplete(followCreature); | |
| 925 | + | } | |
| 926 | + | ||
| 927 | + | bool Creature::setFollowCreature(Creature *creature) { | |
| 928 | + | if (followCreature == creature) { | |
| 929 | + | return true; | |
| 930 | + | } | |
| 931 | + | ||
| 932 | + | if (creature) { | |
| 933 | + | const Position &creaturePos = creature->getPosition(); | |
| 934 | + | if (creaturePos.z != getPosition().z || !canSee(creaturePos)) { | |
| 935 | + | followCreature = nullptr; | |
| 936 | + | return false; | |
| 937 | + | } | |
| 938 | + | ||
| 939 | + | if (!listWalkDir.empty()) { | |
| 940 | + | listWalkDir.clear(); | |
| 941 | + | onWalkAborted(); | |
| 942 | + | } | |
| 943 | + | ||
| 944 | + | hasFollowPath = false; | |
| 945 | + | forceUpdateFollowPath = false; | |
| 946 | + | followCreature = creature; | |
| 947 | + | isUpdatingPath = true; | |
| 948 | + | } else { | |
| 949 | + | isUpdatingPath = false; | |
| 950 | + | followCreature = nullptr; | |
| 951 | + | } | |
| 952 | + | ||
| 953 | + | onFollowCreature(creature); | |
| 954 | + | return true; | |
| 955 | + | } | |
| 956 | + | ||
| 957 | + | double Creature::getDamageRatio(Creature *attacker) const { | |
| 958 | + | uint32_t totalDamage = 0; | |
| 959 | + | uint32_t attackerDamage = 0; | |
| 960 | + | ||
| 961 | + | for (const auto &it : damageMap) { | |
| 962 | + | const CountBlock_t &cb = it.second; | |
| 963 | + | totalDamage += cb.total; | |
| 964 | + | if (it.first == attacker->getID()) { | |
| 965 | + | attackerDamage += cb.total; | |
| 966 | + | } | |
| 967 | + | } | |
| 968 | + | ||
| 969 | + | if (totalDamage == 0) { | |
| 970 | + | return 0; | |
| 971 | + | } | |
| 972 | + | ||
| 973 | + | return (static_cast<double>(attackerDamage) / totalDamage); | |
| 974 | + | } | |
| 975 | + | ||
| 976 | + | uint64_t Creature::getGainedExperience(Creature *attacker) const { | |
| 977 | + | return std::floor(getDamageRatio(attacker) * getLostExperience()); | |
| 978 | + | } | |
| 979 | + | ||
| 980 | + | void Creature::addDamagePoints(Creature *attacker, int32_t damagePoints) { | |
| 981 | + | if (damagePoints <= 0) { | |
| 982 | + | return; | |
| 983 | + | } | |
| 984 | + | ||
| 985 | + | uint32_t attackerId = attacker->id; | |
| 986 | + | ||
| 987 | + | auto it = damageMap.find(attackerId); | |
| 988 | + | if (it == damageMap.end()) { | |
| 989 | + | CountBlock_t cb; | |
| 990 | + | cb.ticks = OTSYS_TIME(); | |
| 991 | + | cb.total = damagePoints; | |
| 992 | + | damageMap[attackerId] = cb; | |
| 993 | + | } else { | |
| 994 | + | it->second.total += damagePoints; | |
| 995 | + | it->second.ticks = OTSYS_TIME(); | |
| 996 | + | } | |
| 997 | + | ||
| 998 | + | lastHitCreatureId = attackerId; | |
| 999 | + | } | |
| 1000 | + | ||
| 1001 | + | void Creature::onAddCondition(ConditionType_t type) { | |
| 1002 | + | if (type == CONDITION_PARALYZE && hasCondition(CONDITION_HASTE)) { | |
| 1003 | + | removeCondition(CONDITION_HASTE); | |
| 1004 | + | } else if (type == CONDITION_HASTE && hasCondition(CONDITION_PARALYZE)) { | |
| 1005 | + | removeCondition(CONDITION_PARALYZE); | |
| 1006 | + | } | |
| 1007 | + | } | |
| 1008 | + | ||
| 1009 | + | void Creature::onAddCombatCondition(ConditionType_t) { | |
| 1010 | + | // | |
| 1011 | + | } | |
| 1012 | + | ||
| 1013 | + | void Creature::onEndCondition(ConditionType_t) { | |
| 1014 | + | // | |
| 1015 | + | } | |
| 1016 | + | ||
| 1017 | + | void Creature::onTickCondition(ConditionType_t type, bool &bRemove) { | |
| 1018 | + | const MagicField *field = getTile()->getFieldItem(); | |
| 1019 | + | if (!field) { | |
| 1020 | + | return; | |
| 1021 | + | } | |
| 1022 | + | ||
| 1023 | + | switch (type) { | |
| 1024 | + | case CONDITION_FIRE: | |
| 1025 | + | bRemove = (field->getCombatType() != COMBAT_FIREDAMAGE); | |
| 1026 | + | break; | |
| 1027 | + | case CONDITION_ENERGY: | |
| 1028 | + | bRemove = (field->getCombatType() != COMBAT_ENERGYDAMAGE); | |
| 1029 | + | break; | |
| 1030 | + | case CONDITION_POISON: | |
| 1031 | + | bRemove = (field->getCombatType() != COMBAT_EARTHDAMAGE); | |
| 1032 | + | break; | |
| 1033 | + | case CONDITION_FREEZING: | |
| 1034 | + | bRemove = (field->getCombatType() != COMBAT_ICEDAMAGE); | |
| 1035 | + | break; | |
| 1036 | + | case CONDITION_DAZZLED: | |
| 1037 | + | bRemove = (field->getCombatType() != COMBAT_HOLYDAMAGE); | |
| 1038 | + | break; | |
| 1039 | + | case CONDITION_CURSED: | |
| 1040 | + | bRemove = (field->getCombatType() != COMBAT_DEATHDAMAGE); | |
| 1041 | + | break; | |
| 1042 | + | case CONDITION_DROWN: | |
| 1043 | + | bRemove = (field->getCombatType() != COMBAT_DROWNDAMAGE); | |
| 1044 | + | break; | |
| 1045 | + | case CONDITION_BLEEDING: | |
| 1046 | + | bRemove = (field->getCombatType() != COMBAT_PHYSICALDAMAGE); | |
| 1047 | + | break; | |
| 1048 | + | default: | |
| 1049 | + | break; | |
| 1050 | + | } | |
| 1051 | + | } | |
| 1052 | + | ||
| 1053 | + | void Creature::onCombatRemoveCondition(Condition *condition) { | |
| 1054 | + | removeCondition(condition); | |
| 1055 | + | } | |
| 1056 | + | ||
| 1057 | + | void Creature::onAttacked() { | |
| 1058 | + | // | |
| 1059 | + | } | |
| 1060 | + | ||
| 1061 | + | void Creature::onAttackedCreatureDrainHealth(Creature *target, int32_t points) { | |
| 1062 | + | target->addDamagePoints(this, points); | |
| 1063 | + | } | |
| 1064 | + | ||
| 1065 | + | bool Creature::onKilledCreature(Creature *target, bool) { | |
| 1066 | + | if (master) { | |
| 1067 | + | master->onKilledCreature(target); | |
| 1068 | + | } | |
| 1069 | + | ||
| 1070 | + | // scripting event - onKill | |
| 1071 | + | const CreatureEventList &killEvents = getCreatureEvents(CREATURE_EVENT_KILL); | |
| 1072 | + | for (CreatureEvent *killEvent : killEvents) { | |
| 1073 | + | killEvent->executeOnKill(this, target); | |
| 1074 | + | } | |
| 1075 | + | return false; | |
| 1076 | + | } | |
| 1077 | + | ||
| 1078 | + | void Creature::onGainExperience(uint64_t gainExp, Creature *target) { | |
| 1079 | + | if (gainExp == 0 || !master) { | |
| 1080 | + | return; | |
| 1081 | + | } | |
| 1082 | + | ||
| 1083 | + | gainExp /= 2; | |
| 1084 | + | master->onGainExperience(gainExp, target); | |
| 1085 | + | ||
| 1086 | + | SpectatorVec spectators; | |
| 1087 | + | g_game.map.getSpectators(spectators, position, false, true); | |
| 1088 | + | if (spectators.empty()) { | |
| 1089 | + | return; | |
| 1090 | + | } | |
| 1091 | + | ||
| 1092 | + | TextMessage message( | |
| 1093 | + | MESSAGE_EXPERIENCE_OTHERS, | |
| 1094 | + | ucfirst(getNameDescription()) + " gained " + std::to_string(gainExp) + | |
| 1095 | + | (gainExp != 1 ? " experience points." : " experience point.")); | |
| 1096 | + | message.position = position; | |
| 1097 | + | message.primary.color = TEXTCOLOR_WHITE_EXP; | |
| 1098 | + | message.primary.value = gainExp; | |
| 1099 | + | ||
| 1100 | + | for (Creature *spectator : spectators) { | |
| 1101 | + | spectator->getPlayer()->sendTextMessage(message); | |
| 1102 | + | } | |
| 1103 | + | } | |
| 1104 | + | ||
| 1105 | + | bool Creature::setMaster(Creature *newMaster) { | |
| 1106 | + | if (!newMaster && !master) { | |
| 1107 | + | return false; | |
| 1108 | + | } | |
| 1109 | + | ||
| 1110 | + | if (newMaster) { | |
| 1111 | + | incrementReferenceCounter(); | |
| 1112 | + | newMaster->summons.push_back(this); | |
| 1113 | + | } | |
| 1114 | + | ||
| 1115 | + | Creature *oldMaster = master; | |
| 1116 | + | master = newMaster; | |
| 1117 | + | ||
| 1118 | + | if (oldMaster) { | |
| 1119 | + | auto summon = | |
| 1120 | + | std::find(oldMaster->summons.begin(), oldMaster->summons.end(), this); | |
| 1121 | + | if (summon != oldMaster->summons.end()) { | |
| 1122 | + | oldMaster->summons.erase(summon); | |
| 1123 | + | decrementReferenceCounter(); | |
| 1124 | + | } | |
| 1125 | + | } | |
| 1126 | + | return true; | |
| 1127 | + | } | |
| 1128 | + | ||
| 1129 | + | bool Creature::addCondition(Condition *condition, bool force /* = false*/) { | |
| 1130 | + | if (condition == nullptr) { | |
| 1131 | + | return false; | |
| 1132 | + | } | |
| 1133 | + | ||
| 1134 | + | if (!force && condition->getType() == CONDITION_HASTE && | |
| 1135 | + | hasCondition(CONDITION_PARALYZE)) { | |
| 1136 | + | int64_t walkDelay = getWalkDelay(); | |
| 1137 | + | if (walkDelay > 0) { | |
| 1138 | + | g_scheduler.addEvent(createSchedulerTask( | |
| 1139 | + | walkDelay, | |
| 1140 | + | std::bind(&Game::forceAddCondition, &g_game, getID(), condition))); | |
| 1141 | + | return false; | |
| 1142 | + | } | |
| 1143 | + | } | |
| 1144 | + | ||
| 1145 | + | Condition *prevCond = getCondition(condition->getType(), condition->getId(), | |
| 1146 | + | condition->getSubId()); | |
| 1147 | + | if (prevCond) { | |
| 1148 | + | prevCond->addCondition(this, condition); | |
| 1149 | + | delete condition; | |
| 1150 | + | return true; | |
| 1151 | + | } | |
| 1152 | + | ||
| 1153 | + | if (condition->startCondition(this)) { | |
| 1154 | + | conditions.push_back(condition); | |
| 1155 | + | onAddCondition(condition->getType()); | |
| 1156 | + | return true; | |
| 1157 | + | } | |
| 1158 | + | ||
| 1159 | + | delete condition; | |
| 1160 | + | return false; | |
| 1161 | + | } | |
| 1162 | + | ||
| 1163 | + | bool Creature::addCombatCondition(Condition *condition) { | |
| 1164 | + | // Caution: condition variable could be deleted after the call to addCondition | |
| 1165 | + | ConditionType_t type = condition->getType(); | |
| 1166 | + | ||
| 1167 | + | if (!addCondition(condition)) { | |
| 1168 | + | return false; | |
| 1169 | + | } | |
| 1170 | + | ||
| 1171 | + | onAddCombatCondition(type); | |
| 1172 | + | return true; | |
| 1173 | + | } | |
| 1174 | + | ||
| 1175 | + | void Creature::removeCondition(ConditionType_t type, bool force /* = false*/) { | |
| 1176 | + | auto it = conditions.begin(), end = conditions.end(); | |
| 1177 | + | while (it != end) { | |
| 1178 | + | Condition *condition = *it; | |
| 1179 | + | if (condition->getType() != type) { | |
| 1180 | + | ++it; | |
| 1181 | + | continue; | |
| 1182 | + | } | |
| 1183 | + | ||
| 1184 | + | if (!force && type == CONDITION_PARALYZE) { | |
| 1185 | + | int64_t walkDelay = getWalkDelay(); | |
| 1186 | + | if (walkDelay > 0) { | |
| 1187 | + | g_scheduler.addEvent(createSchedulerTask( | |
| 1188 | + | walkDelay, | |
| 1189 | + | std::bind(&Game::forceRemoveCondition, &g_game, getID(), type))); | |
| 1190 | + | return; | |
| 1191 | + | } | |
| 1192 | + | } | |
| 1193 | + | ||
| 1194 | + | it = conditions.erase(it); | |
| 1195 | + | ||
| 1196 | + | condition->endCondition(this); | |
| 1197 | + | delete condition; | |
| 1198 | + | ||
| 1199 | + | onEndCondition(type); | |
| 1200 | + | } | |
| 1201 | + | } | |
| 1202 | + | ||
| 1203 | + | void Creature::removeCondition(ConditionType_t type, ConditionId_t conditionId, | |
| 1204 | + | bool force /* = false*/) { | |
| 1205 | + | auto it = conditions.begin(), end = conditions.end(); | |
| 1206 | + | while (it != end) { | |
| 1207 | + | Condition *condition = *it; | |
| 1208 | + | if (condition->getType() != type || condition->getId() != conditionId) { | |
| 1209 | + | ++it; | |
| 1210 | + | continue; | |
| 1211 | + | } | |
| 1212 | + | ||
| 1213 | + | if (!force && type == CONDITION_PARALYZE) { | |
| 1214 | + | int64_t walkDelay = getWalkDelay(); | |
| 1215 | + | if (walkDelay > 0) { | |
| 1216 | + | g_scheduler.addEvent(createSchedulerTask( | |
| 1217 | + | walkDelay, | |
| 1218 | + | std::bind(&Game::forceRemoveCondition, &g_game, getID(), type))); | |
| 1219 | + | return; | |
| 1220 | + | } | |
| 1221 | + | } | |
| 1222 | + | ||
| 1223 | + | it = conditions.erase(it); | |
| 1224 | + | ||
| 1225 | + | condition->endCondition(this); | |
| 1226 | + | delete condition; | |
| 1227 | + | ||
| 1228 | + | onEndCondition(type); | |
| 1229 | + | } | |
| 1230 | + | } | |
| 1231 | + | ||
| 1232 | + | void Creature::removeCombatCondition(ConditionType_t type) { | |
| 1233 | + | std::vector<Condition *> removeConditions; | |
| 1234 | + | for (Condition *condition : conditions) { | |
| 1235 | + | if (condition->getType() == type) { | |
| 1236 | + | removeConditions.push_back(condition); | |
| 1237 | + | } | |
| 1238 | + | } | |
| 1239 | + | ||
| 1240 | + | for (Condition *condition : removeConditions) { | |
| 1241 | + | onCombatRemoveCondition(condition); | |
| 1242 | + | } | |
| 1243 | + | } | |
| 1244 | + | ||
| 1245 | + | void Creature::removeCondition(Condition *condition, bool force /* = false*/) { | |
| 1246 | + | auto it = std::find(conditions.begin(), conditions.end(), condition); | |
| 1247 | + | if (it == conditions.end()) { | |
| 1248 | + | return; | |
| 1249 | + | } | |
| 1250 | + | ||
| 1251 | + | if (!force && condition->getType() == CONDITION_PARALYZE) { | |
| 1252 | + | int64_t walkDelay = getWalkDelay(); | |
| 1253 | + | if (walkDelay > 0) { | |
| 1254 | + | g_scheduler.addEvent(createSchedulerTask( | |
| 1255 | + | walkDelay, std::bind(&Game::forceRemoveCondition, &g_game, getID(), | |
| 1256 | + | condition->getType()))); | |
| 1257 | + | return; | |
| 1258 | + | } | |
| 1259 | + | } | |
| 1260 | + | ||
| 1261 | + | conditions.erase(it); | |
| 1262 | + | ||
| 1263 | + | condition->endCondition(this); | |
| 1264 | + | onEndCondition(condition->getType()); | |
| 1265 | + | delete condition; | |
| 1266 | + | } | |
| 1267 | + | ||
| 1268 | + | Condition *Creature::getCondition(ConditionType_t type) const { | |
| 1269 | + | for (Condition *condition : conditions) { | |
| 1270 | + | if (condition->getType() == type) { | |
| 1271 | + | return condition; | |
| 1272 | + | } | |
| 1273 | + | } | |
| 1274 | + | return nullptr; | |
| 1275 | + | } | |
| 1276 | + | ||
| 1277 | + | Condition *Creature::getCondition(ConditionType_t type, | |
| 1278 | + | ConditionId_t conditionId, | |
| 1279 | + | uint32_t subId /* = 0*/) const { | |
| 1280 | + | for (Condition *condition : conditions) { | |
| 1281 | + | if (condition->getType() == type && condition->getId() == conditionId && | |
| 1282 | + | condition->getSubId() == subId) { | |
| 1283 | + | return condition; | |
| 1284 | + | } | |
| 1285 | + | } | |
| 1286 | + | return nullptr; | |
| 1287 | + | } | |
| 1288 | + | ||
| 1289 | + | void Creature::executeConditions(uint32_t interval) { | |
| 1290 | + | ConditionList tempConditions{conditions}; | |
| 1291 | + | for (Condition *condition : tempConditions) { | |
| 1292 | + | auto it = std::find(conditions.begin(), conditions.end(), condition); | |
| 1293 | + | if (it == conditions.end()) { | |
| 1294 | + | continue; | |
| 1295 | + | } | |
| 1296 | + | ||
| 1297 | + | if (!condition->executeCondition(this, interval)) { | |
| 1298 | + | it = std::find(conditions.begin(), conditions.end(), condition); | |
| 1299 | + | if (it != conditions.end()) { | |
| 1300 | + | conditions.erase(it); | |
| 1301 | + | condition->endCondition(this); | |
| 1302 | + | onEndCondition(condition->getType()); | |
| 1303 | + | delete condition; | |
| 1304 | + | } | |
| 1305 | + | } | |
| 1306 | + | } | |
| 1307 | + | } | |
| 1308 | + | ||
| 1309 | + | bool Creature::hasCondition(ConditionType_t type, | |
| 1310 | + | uint32_t subId /* = 0*/) const { | |
| 1311 | + | if (isSuppress(type)) { | |
| 1312 | + | return false; | |
| 1313 | + | } | |
| 1314 | + | ||
| 1315 | + | int64_t timeNow = OTSYS_TIME(); | |
| 1316 | + | for (Condition *condition : conditions) { | |
| 1317 | + | if (condition->getType() != type || condition->getSubId() != subId) { | |
| 1318 | + | continue; | |
| 1319 | + | } | |
| 1320 | + | ||
| 1321 | + | if (condition->getEndTime() >= timeNow || condition->getTicks() == -1) { | |
| 1322 | + | return true; | |
| 1323 | + | } | |
| 1324 | + | } | |
| 1325 | + | return false; | |
| 1326 | + | } | |
| 1327 | + | ||
| 1328 | + | bool Creature::isImmune(CombatType_t type) const { | |
| 1329 | + | return hasBitSet(static_cast<uint32_t>(type), getDamageImmunities()); | |
| 1330 | + | } | |
| 1331 | + | ||
| 1332 | + | bool Creature::isImmune(ConditionType_t type) const { | |
| 1333 | + | return hasBitSet(static_cast<uint32_t>(type), getConditionImmunities()); | |
| 1334 | + | } | |
| 1335 | + | ||
| 1336 | + | bool Creature::isSuppress(ConditionType_t type) const { | |
| 1337 | + | return hasBitSet(static_cast<uint32_t>(type), getConditionSuppressions()); | |
| 1338 | + | } | |
| 1339 | + | ||
| 1340 | + | int64_t Creature::getStepDuration(Direction dir) const { | |
| 1341 | + | int64_t stepDuration = getStepDuration(); | |
| 1342 | + | if ((dir & DIRECTION_DIAGONAL_MASK) != 0) { | |
| 1343 | + | stepDuration *= 3; | |
| 1344 | + | } | |
| 1345 | + | return stepDuration; | |
| 1346 | + | } | |
| 1347 | + | ||
| 1348 | + | int64_t Creature::getStepDuration(bool forAnimation) const { | |
| 1349 | + | if (isRemoved()) { | |
| 1350 | + | return 0; | |
| 1351 | + | } | |
| 1352 | + | ||
| 1353 | + | uint32_t calculatedStepSpeed; | |
| 1354 | + | uint32_t groundSpeed; | |
| 1355 | + | ||
| 1356 | + | int32_t stepSpeed = getStepSpeed(); | |
| 1357 | + | if (stepSpeed > -Creature::speedB) { | |
| 1358 | + | calculatedStepSpeed = | |
| 1359 | + | floor((Creature::speedA * log((stepSpeed / 2) + Creature::speedB) + | |
| 1360 | + | Creature::speedC) + | |
| 1361 | + | 0.5); | |
| 1362 | + | if (calculatedStepSpeed == 0) { | |
| 1363 | + | calculatedStepSpeed = 1; | |
| 1364 | + | } | |
| 1365 | + | } else { | |
| 1366 | + | calculatedStepSpeed = 1; | |
| 1367 | + | } | |
| 1368 | + | ||
| 1369 | + | Item *ground = tile->getGround(); | |
| 1370 | + | if (ground) { | |
| 1371 | + | groundSpeed = Item::items[ground->getID()].speed; | |
| 1372 | + | if (groundSpeed == 0) { | |
| 1373 | + | groundSpeed = 150; | |
| 1374 | + | } | |
| 1375 | + | } else { | |
| 1376 | + | groundSpeed = 150; | |
| 1377 | + | } | |
| 1378 | + | ||
| 1379 | + | double duration = std::floor(1000 * groundSpeed / calculatedStepSpeed); | |
| 1380 | + | int64_t stepDuration = duration; | |
| 1381 | + | ||
| 1382 | + | if (!forAnimation) { | |
| 1383 | + | const Monster *monster = getMonster(); | |
| 1384 | + | if (monster && monster->isTargetNearby() && !monster->isFleeing() && | |
| 1385 | + | !monster->getMaster()) { | |
| 1386 | + | stepDuration *= 2; | |
| 1387 | + | } | |
| 1388 | + | } | |
| 1389 | + | ||
| 1390 | + | return stepDuration; | |
| 1391 | + | } | |
| 1392 | + | ||
| 1393 | + | int64_t Creature::getEventStepTicks() const { | |
| 1394 | + | int64_t ret = getWalkDelay(); | |
| 1395 | + | if (ret <= 0) { | |
| 1396 | + | ret = 1; | |
| 1397 | + | } | |
| 1398 | + | return ret; | |
| 1399 | + | } | |
| 1400 | + | ||
| 1401 | + | LightInfo Creature::getCreatureLight() const { return internalLight; } | |
| 1402 | + | ||
| 1403 | + | void Creature::setCreatureLight(LightInfo lightInfo) { | |
| 1404 | + | internalLight = std::move(lightInfo); | |
| 1405 | + | } | |
| 1406 | + | ||
| 1407 | + | void Creature::setNormalCreatureLight() { internalLight = {}; } | |
| 1408 | + | ||
| 1409 | + | bool Creature::registerCreatureEvent(const std::string &name) { | |
| 1410 | + | CreatureEvent *event = g_creatureEvents->getEventByName(name); | |
| 1411 | + | if (!event) { | |
| 1412 | + | return false; | |
| 1413 | + | } | |
| 1414 | + | ||
| 1415 | + | CreatureEventType_t type = event->getEventType(); | |
| 1416 | + | if (hasEventRegistered(type)) { | |
| 1417 | + | for (CreatureEvent *creatureEvent : eventsList) { | |
| 1418 | + | if (creatureEvent == event) { | |
| 1419 | + | return false; | |
| 1420 | + | } | |
| 1421 | + | } | |
| 1422 | + | } else { | |
| 1423 | + | scriptEventsBitField |= static_cast<uint32_t>(1) << type; | |
| 1424 | + | } | |
| 1425 | + | ||
| 1426 | + | eventsList.push_back(event); | |
| 1427 | + | return true; | |
| 1428 | + | } | |
| 1429 | + | ||
| 1430 | + | bool Creature::unregisterCreatureEvent(const std::string &name) { | |
| 1431 | + | CreatureEvent *event = g_creatureEvents->getEventByName(name); | |
| 1432 | + | if (!event) { | |
| 1433 | + | return false; | |
| 1434 | + | } | |
| 1435 | + | ||
| 1436 | + | CreatureEventType_t type = event->getEventType(); | |
| 1437 | + | if (!hasEventRegistered(type)) { | |
| 1438 | + | return false; | |
| 1439 | + | } | |
| 1440 | + | ||
| 1441 | + | bool resetTypeBit = true; | |
| 1442 | + | ||
| 1443 | + | auto it = eventsList.begin(), end = eventsList.end(); | |
| 1444 | + | while (it != end) { | |
| 1445 | + | CreatureEvent *curEvent = *it; | |
| 1446 | + | if (curEvent == event) { | |
| 1447 | + | it = eventsList.erase(it); | |
| 1448 | + | continue; | |
| 1449 | + | } | |
| 1450 | + | ||
| 1451 | + | if (curEvent->getEventType() == type) { | |
| 1452 | + | resetTypeBit = false; | |
| 1453 | + | } | |
| 1454 | + | ++it; | |
| 1455 | + | } | |
| 1456 | + | ||
| 1457 | + | if (resetTypeBit) { | |
| 1458 | + | scriptEventsBitField &= ~(static_cast<uint32_t>(1) << type); | |
| 1459 | + | } | |
| 1460 | + | return true; | |
| 1461 | + | } | |
| 1462 | + | ||
| 1463 | + | CreatureEventList Creature::getCreatureEvents(CreatureEventType_t type) { | |
| 1464 | + | CreatureEventList tmpEventList; | |
| 1465 | + | ||
| 1466 | + | if (!hasEventRegistered(type)) { | |
| 1467 | + | return tmpEventList; | |
| 1468 | + | } | |
| 1469 | + | ||
| 1470 | + | for (CreatureEvent *creatureEvent : eventsList) { | |
| 1471 | + | if (!creatureEvent->isLoaded()) { | |
| 1472 | + | continue; | |
| 1473 | + | } | |
| 1474 | + | ||
| 1475 | + | if (creatureEvent->getEventType() == type) { | |
| 1476 | + | tmpEventList.push_back(creatureEvent); | |
| 1477 | + | } | |
| 1478 | + | } | |
| 1479 | + | ||
| 1480 | + | return tmpEventList; | |
| 1481 | + | } | |
| 1482 | + | ||
| 1483 | + | bool FrozenPathingConditionCall::isInRange(const Position &startPos, | |
| 1484 | + | const Position &testPos, | |
| 1485 | + | const FindPathParams &fpp) const { | |
| 1486 | + | if (fpp.fullPathSearch) { | |
| 1487 | + | if (testPos.x > targetPos.x + fpp.maxTargetDist) { | |
| 1488 | + | return false; | |
| 1489 | + | } | |
| 1490 | + | ||
| 1491 | + | if (testPos.x < targetPos.x - fpp.maxTargetDist) { | |
| 1492 | + | return false; | |
| 1493 | + | } | |
| 1494 | + | ||
| 1495 | + | if (testPos.y > targetPos.y + fpp.maxTargetDist) { | |
| 1496 | + | return false; | |
| 1497 | + | } | |
| 1498 | + | ||
| 1499 | + | if (testPos.y < targetPos.y - fpp.maxTargetDist) { | |
| 1500 | + | return false; | |
| 1501 | + | } | |
| 1502 | + | } else { | |
| 1503 | + | int_fast32_t dx = Position::getOffsetX(startPos, targetPos); | |
| 1504 | + | ||
| 1505 | + | int32_t dxMax = (dx >= 0 ? fpp.maxTargetDist : 0); | |
| 1506 | + | if (testPos.x > targetPos.x + dxMax) { | |
| 1507 | + | return false; | |
| 1508 | + | } | |
| 1509 | + | ||
| 1510 | + | int32_t dxMin = (dx <= 0 ? fpp.maxTargetDist : 0); | |
| 1511 | + | if (testPos.x < targetPos.x - dxMin) { | |
| 1512 | + | return false; | |
| 1513 | + | } | |
| 1514 | + | ||
| 1515 | + | int_fast32_t dy = Position::getOffsetY(startPos, targetPos); | |
| 1516 | + | ||
| 1517 | + | int32_t dyMax = (dy >= 0 ? fpp.maxTargetDist : 0); | |
| 1518 | + | if (testPos.y > targetPos.y + dyMax) { | |
| 1519 | + | return false; | |
| 1520 | + | } | |
| 1521 | + | ||
| 1522 | + | int32_t dyMin = (dy <= 0 ? fpp.maxTargetDist : 0); | |
| 1523 | + | if (testPos.y < targetPos.y - dyMin) { | |
| 1524 | + | return false; | |
| 1525 | + | } | |
| 1526 | + | } | |
| 1527 | + | return true; | |
| 1528 | + | } | |
| 1529 | + | ||
| 1530 | + | bool FrozenPathingConditionCall::operator()(const Position &startPos, | |
| 1531 | + | const Position &testPos, | |
| 1532 | + | const FindPathParams &fpp, | |
| 1533 | + | int32_t &bestMatchDist) const { | |
| 1534 | + | if (!isInRange(startPos, testPos, fpp)) { | |
| 1535 | + | return false; | |
| 1536 | + | } | |
| 1537 | + | ||
| 1538 | + | if (fpp.clearSight && !g_game.isSightClear(testPos, targetPos, true)) { | |
| 1539 | + | return false; | |
| 1540 | + | } | |
| 1541 | + | ||
| 1542 | + | int32_t testDist = | |
| 1543 | + | std::max<int32_t>(Position::getDistanceX(targetPos, testPos), | |
| 1544 | + | Position::getDistanceY(targetPos, testPos)); | |
| 1545 | + | if (fpp.maxTargetDist == 1) { | |
| 1546 | + | if (testDist < fpp.minTargetDist || testDist > fpp.maxTargetDist) { | |
| 1547 | + | return false; | |
| 1548 | + | } | |
| 1549 | + | ||
| 1550 | + | return true; | |
| 1551 | + | } else if (testDist <= fpp.maxTargetDist) { | |
| 1552 | + | if (testDist < fpp.minTargetDist) { | |
| 1553 | + | return false; | |
| 1554 | + | } | |
| 1555 | + | ||
| 1556 | + | if (testDist == fpp.maxTargetDist) { | |
| 1557 | + | bestMatchDist = 0; | |
| 1558 | + | return true; | |
| 1559 | + | } else if (testDist > bestMatchDist) { | |
| 1560 | + | // not quite what we want, but the best so far | |
| 1561 | + | bestMatchDist = testDist; | |
| 1562 | + | return true; | |
| 1563 | + | } | |
| 1564 | + | } | |
| 1565 | + | return false; | |
| 1566 | + | } | |
| 1567 | + | ||
| 1568 | + | bool Creature::isInvisible() const { | |
| 1569 | + | return std::find_if(conditions.begin(), conditions.end(), | |
| 1570 | + | [](const Condition *condition) { | |
| 1571 | + | return condition->getType() == CONDITION_INVISIBLE; | |
| 1572 | + | }) != conditions.end(); | |
| 1573 | + | } | |
| 1574 | + | ||
| 1575 | + | bool Creature::getPathTo(const Position &targetPos, | |
| 1576 | + | std::list<Direction> &dirList, | |
| 1577 | + | const FindPathParams &fpp) const { | |
| 1578 | + | return g_game.map.getPathMatching(*this, dirList, | |
| 1579 | + | FrozenPathingConditionCall(targetPos), fpp); | |
| 1580 | + | } | |
| 1581 | + | ||
| 1582 | + | bool Creature::getPathTo(const Position &targetPos, | |
| 1583 | + | std::list<Direction> &dirList, int32_t minTargetDist, | |
| 1584 | + | int32_t maxTargetDist, bool fullPathSearch /*= true*/, | |
| 1585 | + | bool clearSight /*= true*/, | |
| 1586 | + | int32_t maxSearchDist /*= 0*/) const { | |
| 1587 | + | FindPathParams fpp; | |
| 1588 | + | fpp.fullPathSearch = fullPathSearch; | |
| 1589 | + | fpp.maxSearchDist = maxSearchDist; | |
| 1590 | + | fpp.clearSight = clearSight; | |
| 1591 | + | fpp.minTargetDist = minTargetDist; | |
| 1592 | + | fpp.maxTargetDist = maxTargetDist; | |
| 1593 | + | return getPathTo(targetPos, dirList, fpp); | |
| 1594 | + | } | |
| 1595 | + | ||
| 1596 | + | // WARNING: This function may use extra cpu but makes autowalk (map click) much | |
| 1597 | + | // better | |
| 1598 | + | bool Creature::tryToFixAutoWalk(Direction dir) { | |
| 1599 | + | Position pos = getNextPosition(dir, getPosition()); | |
| 1600 | + | for (int x = 0; x < 3; ++x) { | |
| 1601 | + | if (listWalkDir.empty()) { | |
| 1602 | + | break; | |
| 1603 | + | } | |
| 1604 | + | ||
| 1605 | + | for (int i = 0; i < 2; ++i) { | |
| 1606 | + | if (listWalkDir.empty()) | |
| 1607 | + | break; | |
| 1608 | + | pos = getNextPosition(listWalkDir.front(), pos); | |
| 1609 | + | listWalkDir.pop_front(); | |
| 1610 | + | } | |
| 1611 | + | ||
| 1612 | + | std::list<Direction> newPath; | |
| 1613 | + | if (getPathTo(pos, newPath, 0, 0, false, true)) { | |
| 1614 | + | newPath.reverse(); | |
| 1615 | + | for (auto &it : newPath) | |
| 1616 | + | listWalkDir.push_front(it); | |
| 1617 | + | return true; | |
| 1618 | + | } | |
| 1619 | + | } | |
| 1620 | + | ||
| 1621 | + | return false; | |
| 1622 | + | } | |
| 1623 | + | ||
| 1624 | + | void Creature::pulseAttackOutfit(const Outfit_t &outfit, | |
| 1625 | + | uint32_t durationMs /* = 1000 */) { | |
| 1626 | + | // Si no estamos pulsando, guardamos el original para poder revertir | |
| 1627 | + | // fielmente. | |
| 1628 | + | if (!isAttackPulsing) { | |
| 1629 | + | originalOutfit = currentOutfit; | |
| 1630 | + | isAttackPulsing = true; | |
| 1631 | + | } | |
| 1632 | + | ||
| 1633 | + | // Siempre enviamos el cambio de outfit para que el cliente pueda reiniciar | |
| 1634 | + | // el ciclo de animación (swing) en cada "tick" de ataque, incluso si es | |
| 1635 | + | // el mismo lookType. | |
| 1636 | + | // Usamos sendCreatureAttackPulse para enviar también la duración. | |
| 1637 | + | SpectatorVec spectators; | |
| 1638 | + | g_game.map.getSpectators(spectators, getPosition(), true, true); | |
| 1639 | + | for (Creature *spectator : spectators) { | |
| 1640 | + | spectator->getPlayer()->sendCreatureAttackPulse(this, outfit, durationMs); | |
| 1641 | + | } | |
| 1642 | + | ||
| 1643 | + | g_game.internalCreatureTurn(this, getDirection()); | |
| 1644 | + | ||
| 1645 | + | // Renovar el timer de reversión. | |
| 1646 | + | if (attackOutfitEventId != 0) { | |
| 1647 | + | g_scheduler.stopEvent(attackOutfitEventId); | |
| 1648 | + | attackOutfitEventId = 0; | |
| 1649 | + | } | |
| 1650 | + | ||
| 1651 | + | uint32_t creatureId = getID(); | |
| 1652 | + | attackOutfitEventId = g_scheduler.addEvent(createSchedulerTask( | |
| 1653 | + | std::max<uint32_t>(SCHEDULER_MINTICKS, durationMs), [creatureId]() { | |
| 1654 | + | Creature *c = g_game.getCreatureByID(creatureId); | |
| 1655 | + | if (c) { | |
| 1656 | + | c->revertAttackOutfit(); | |
| 1657 | + | } | |
| 1658 | + | })); | |
| 1659 | + | } | |
| 1660 | + | ||
| 1661 | + | void Creature::revertAttackOutfit() { | |
| 1662 | + | if (attackOutfitEventId != 0) { | |
| 1663 | + | g_scheduler.stopEvent(attackOutfitEventId); | |
| 1664 | + | attackOutfitEventId = 0; | |
| 1665 | + | } | |
| 1666 | + | ||
| 1667 | + | if (isAttackPulsing) { | |
| 1668 | + | // Restauramos el original guardado al inicio del combate | |
| 1669 | + | g_game.internalCreatureChangeOutfit(this, originalOutfit); | |
| 1670 | + | isAttackPulsing = false; | |
| 1671 | + | } | |
| 1672 | + | } |
| @@ -0,0 +1,476 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_CREATURE_H_5363C04015254E298F84E6D59A139508 | |
| 21 | + | #define FS_CREATURE_H_5363C04015254E298F84E6D59A139508 | |
| 22 | + | ||
| 23 | + | #include "condition.h" | |
| 24 | + | #include "const.h" | |
| 25 | + | #include "creatureevent.h" | |
| 26 | + | #include "enums.h" | |
| 27 | + | #include "map.h" | |
| 28 | + | #include "position.h" | |
| 29 | + | #include "tile.h" | |
| 30 | + | #include <list> | |
| 31 | + | #include <map> | |
| 32 | + | ||
| 33 | + | using ConditionList = std::list<Condition *>; | |
| 34 | + | using CreatureEventList = std::list<CreatureEvent *>; | |
| 35 | + | ||
| 36 | + | enum slots_t : uint8_t { | |
| 37 | + | CONST_SLOT_WHEREEVER = 0, | |
| 38 | + | CONST_SLOT_HEAD = 1, | |
| 39 | + | CONST_SLOT_NECKLACE = 2, | |
| 40 | + | CONST_SLOT_BACKPACK = 3, | |
| 41 | + | CONST_SLOT_ARMOR = 4, | |
| 42 | + | CONST_SLOT_RIGHT = 5, | |
| 43 | + | CONST_SLOT_LEFT = 6, | |
| 44 | + | CONST_SLOT_LEGS = 7, | |
| 45 | + | CONST_SLOT_FEET = 8, | |
| 46 | + | CONST_SLOT_RING = 9, | |
| 47 | + | CONST_SLOT_AMMO = 10, | |
| 48 | + | ||
| 49 | + | CONST_SLOT_FIRST = CONST_SLOT_HEAD, | |
| 50 | + | CONST_SLOT_LAST = CONST_SLOT_AMMO, | |
| 51 | + | }; | |
| 52 | + | ||
| 53 | + | struct FindPathParams { | |
| 54 | + | bool fullPathSearch = true; | |
| 55 | + | bool clearSight = true; | |
| 56 | + | bool allowDiagonal = true; | |
| 57 | + | bool keepDistance = false; | |
| 58 | + | int32_t maxSearchDist = 0; | |
| 59 | + | int32_t minTargetDist = -1; | |
| 60 | + | int32_t maxTargetDist = -1; | |
| 61 | + | }; | |
| 62 | + | ||
| 63 | + | class Map; | |
| 64 | + | class Thing; | |
| 65 | + | class Container; | |
| 66 | + | class Player; | |
| 67 | + | class Monster; | |
| 68 | + | class Npc; | |
| 69 | + | class Item; | |
| 70 | + | class Tile; | |
| 71 | + | ||
| 72 | + | static constexpr int32_t EVENT_CREATURECOUNT = 10; | |
| 73 | + | static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 1000; | |
| 74 | + | static constexpr int32_t EVENT_CHECK_CREATURE_INTERVAL = | |
| 75 | + | (EVENT_CREATURE_THINK_INTERVAL / EVENT_CREATURECOUNT); | |
| 76 | + | ||
| 77 | + | class FrozenPathingConditionCall { | |
| 78 | + | public: | |
| 79 | + | explicit FrozenPathingConditionCall(Position targetPos) | |
| 80 | + | : targetPos(std::move(targetPos)) {} | |
| 81 | + | ||
| 82 | + | bool operator()(const Position &startPos, const Position &testPos, | |
| 83 | + | const FindPathParams &fpp, int32_t &bestMatchDist) const; | |
| 84 | + | ||
| 85 | + | bool isInRange(const Position &startPos, const Position &testPos, | |
| 86 | + | const FindPathParams &fpp) const; | |
| 87 | + | ||
| 88 | + | private: | |
| 89 | + | Position targetPos; | |
| 90 | + | }; | |
| 91 | + | ||
| 92 | + | ////////////////////////////////////////////////////////////////////// | |
| 93 | + | // Defines the Base class for all creatures and base functions which | |
| 94 | + | // every creature has | |
| 95 | + | ||
| 96 | + | class Creature : virtual public Thing { | |
| 97 | + | protected: | |
| 98 | + | Creature(); | |
| 99 | + | ||
| 100 | + | public: | |
| 101 | + | static double speedA, speedB, speedC; | |
| 102 | + | ||
| 103 | + | virtual ~Creature(); | |
| 104 | + | ||
| 105 | + | // non-copyable | |
| 106 | + | Creature(const Creature &) = delete; | |
| 107 | + | Creature &operator=(const Creature &) = delete; | |
| 108 | + | ||
| 109 | + | Creature *getCreature() override final { return this; } | |
| 110 | + | const Creature *getCreature() const override final { return this; } | |
| 111 | + | virtual Player *getPlayer() { return nullptr; } | |
| 112 | + | virtual const Player *getPlayer() const { return nullptr; } | |
| 113 | + | virtual Npc *getNpc() { return nullptr; } | |
| 114 | + | virtual const Npc *getNpc() const { return nullptr; } | |
| 115 | + | virtual Monster *getMonster() { return nullptr; } | |
| 116 | + | virtual const Monster *getMonster() const { return nullptr; } | |
| 117 | + | ||
| 118 | + | virtual const std::string &getName() const = 0; | |
| 119 | + | virtual const std::string &getNameDescription() const = 0; | |
| 120 | + | ||
| 121 | + | virtual CreatureType_t getType() const = 0; | |
| 122 | + | ||
| 123 | + | virtual void setID() = 0; | |
| 124 | + | void setRemoved() { isInternalRemoved = true; } | |
| 125 | + | ||
| 126 | + | uint32_t getID() const { return id; } | |
| 127 | + | virtual void removeList() = 0; | |
| 128 | + | virtual void addList() = 0; | |
| 129 | + | ||
| 130 | + | virtual bool canSee(const Position &pos) const; | |
| 131 | + | virtual bool canSeeCreature(const Creature *creature) const; | |
| 132 | + | ||
| 133 | + | virtual RaceType_t getRace() const { return RACE_NONE; } | |
| 134 | + | virtual Skulls_t getSkull() const { return skull; } | |
| 135 | + | virtual Skulls_t getSkullClient(const Creature *creature) const { | |
| 136 | + | return creature->getSkull(); | |
| 137 | + | } | |
| 138 | + | void setSkull(Skulls_t newSkull); | |
| 139 | + | Direction getDirection() const { return direction; } | |
| 140 | + | void setDirection(Direction dir) { direction = dir; } | |
| 141 | + | ||
| 142 | + | bool isHealthHidden() const { return hiddenHealth; } | |
| 143 | + | void setHiddenHealth(bool b) { hiddenHealth = b; } | |
| 144 | + | ||
| 145 | + | int32_t getThrowRange() const override final { return 1; } | |
| 146 | + | bool isPushable() const override { return getWalkDelay() <= 0; } | |
| 147 | + | bool isRemoved() const override final { return isInternalRemoved; } | |
| 148 | + | virtual bool canSeeInvisibility() const { return false; } | |
| 149 | + | virtual bool isInGhostMode() const { return false; } | |
| 150 | + | ||
| 151 | + | int32_t getWalkDelay(Direction dir) const; | |
| 152 | + | int32_t getWalkDelay() const; | |
| 153 | + | int64_t getTimeSinceLastMove() const; | |
| 154 | + | ||
| 155 | + | int64_t getEventStepTicks() const; | |
| 156 | + | int64_t getStepDuration(Direction dir) const; | |
| 157 | + | int64_t getStepDuration(bool forAnimation = false) const; | |
| 158 | + | virtual int32_t getStepSpeed() const { return getSpeed(); } | |
| 159 | + | int32_t getSpeed() const { return baseSpeed + varSpeed; } | |
| 160 | + | void setSpeed(int32_t varSpeedDelta) { | |
| 161 | + | int32_t oldSpeed = getSpeed(); | |
| 162 | + | varSpeed = varSpeedDelta; | |
| 163 | + | ||
| 164 | + | if (getSpeed() <= 0) { | |
| 165 | + | stopEventWalk(); | |
| 166 | + | cancelNextWalk = true; | |
| 167 | + | } else if (oldSpeed <= 0 && !listWalkDir.empty()) { | |
| 168 | + | addEventWalk(); | |
| 169 | + | } | |
| 170 | + | } | |
| 171 | + | ||
| 172 | + | void setBaseSpeed(uint32_t newBaseSpeed) { baseSpeed = newBaseSpeed; } | |
| 173 | + | uint32_t getBaseSpeed() const { return baseSpeed; } | |
| 174 | + | ||
| 175 | + | int32_t getHealth() const { return health; } | |
| 176 | + | virtual int32_t getMaxHealth() const { return healthMax; } | |
| 177 | + | ||
| 178 | + | const Outfit_t getCurrentOutfit() const { return currentOutfit; } | |
| 179 | + | void setCurrentOutfit(Outfit_t outfit) { currentOutfit = outfit; } | |
| 180 | + | const Outfit_t getDefaultOutfit() const { return defaultOutfit; } | |
| 181 | + | bool isInvisible() const; | |
| 182 | + | ||
| 183 | + | ZoneType_t getZone() const { return getTile()->getZone(); } | |
| 184 | + | ||
| 185 | + | // walk functions | |
| 186 | + | void startAutoWalk(const std::list<Direction> &listDir); | |
| 187 | + | void addEventWalk(); | |
| 188 | + | void stopEventWalk(); | |
| 189 | + | virtual void goToFollowCreature(); | |
| 190 | + | ||
| 191 | + | // walk events | |
| 192 | + | virtual void onWalk(Direction &dir); | |
| 193 | + | virtual void onWalkAborted() {} | |
| 194 | + | virtual void onWalkComplete() {} | |
| 195 | + | ||
| 196 | + | // follow functions | |
| 197 | + | Creature *getFollowCreature() const { return followCreature; } | |
| 198 | + | virtual bool setFollowCreature(Creature *creature); | |
| 199 | + | ||
| 200 | + | // follow events | |
| 201 | + | virtual void onFollowCreature(const Creature *) {} | |
| 202 | + | virtual void onFollowCreatureComplete(const Creature *) {} | |
| 203 | + | ||
| 204 | + | // combat functions | |
| 205 | + | Creature *getAttackedCreature() { return attackedCreature; } | |
| 206 | + | virtual bool setAttackedCreature(Creature *creature); | |
| 207 | + | virtual BlockType_t blockHit(Creature *attacker, CombatType_t combatType, | |
| 208 | + | int32_t &damage, bool checkDefense = false, | |
| 209 | + | bool checkArmor = false, bool field = false); | |
| 210 | + | ||
| 211 | + | bool setMaster(Creature *newMaster); | |
| 212 | + | ||
| 213 | + | void removeMaster() { | |
| 214 | + | if (master) { | |
| 215 | + | master = nullptr; | |
| 216 | + | decrementReferenceCounter(); | |
| 217 | + | } | |
| 218 | + | } | |
| 219 | + | ||
| 220 | + | bool isSummon() const { return master != nullptr; } | |
| 221 | + | Creature *getMaster() const { return master; } | |
| 222 | + | ||
| 223 | + | virtual uint32_t getMana() const { return 0; } | |
| 224 | + | virtual uint32_t getMaxMana() const { return 0; } | |
| 225 | + | ||
| 226 | + | const std::list<Creature *> &getSummons() const { return summons; } | |
| 227 | + | ||
| 228 | + | virtual int32_t getArmor() const { return 0; } | |
| 229 | + | virtual int32_t getDefense() const { return 0; } | |
| 230 | + | virtual float getAttackFactor() const { return 1.0f; } | |
| 231 | + | virtual float getDefenseFactor() const { return 1.0f; } | |
| 232 | + | ||
| 233 | + | virtual uint8_t getSpeechBubble() const { return SPEECHBUBBLE_NONE; } | |
| 234 | + | ||
| 235 | + | bool addCondition(Condition *condition, bool force = false); | |
| 236 | + | bool addCombatCondition(Condition *condition); | |
| 237 | + | void removeCondition(ConditionType_t type, ConditionId_t conditionId, | |
| 238 | + | bool force = false); | |
| 239 | + | void removeCondition(ConditionType_t type, bool force = false); | |
| 240 | + | void removeCondition(Condition *condition, bool force = false); | |
| 241 | + | void removeCombatCondition(ConditionType_t type); | |
| 242 | + | Condition *getCondition(ConditionType_t type) const; | |
| 243 | + | Condition *getCondition(ConditionType_t type, ConditionId_t conditionId, | |
| 244 | + | uint32_t subId = 0) const; | |
| 245 | + | void executeConditions(uint32_t interval); | |
| 246 | + | bool hasCondition(ConditionType_t type, uint32_t subId = 0) const; | |
| 247 | + | virtual bool isImmune(ConditionType_t type) const; | |
| 248 | + | virtual bool isImmune(CombatType_t type) const; | |
| 249 | + | virtual bool isSuppress(ConditionType_t type) const; | |
| 250 | + | virtual uint32_t getDamageImmunities() const { return 0; } | |
| 251 | + | virtual uint32_t getConditionImmunities() const { return 0; } | |
| 252 | + | virtual uint32_t getConditionSuppressions() const { return 0; } | |
| 253 | + | virtual bool isAttackable() const { return true; } | |
| 254 | + | ||
| 255 | + | virtual void changeHealth(int32_t healthChange, bool sendHealthChange = true); | |
| 256 | + | ||
| 257 | + | void gainHealth(Creature *healer, int32_t healthGain); | |
| 258 | + | virtual void drainHealth(Creature *attacker, int32_t damage); | |
| 259 | + | ||
| 260 | + | virtual bool challengeCreature(Creature *) { return false; } | |
| 261 | + | ||
| 262 | + | void onDeath(); | |
| 263 | + | virtual uint64_t getGainedExperience(Creature *attacker) const; | |
| 264 | + | void addDamagePoints(Creature *attacker, int32_t damagePoints); | |
| 265 | + | bool hasBeenAttacked(uint32_t attackerId); | |
| 266 | + | ||
| 267 | + | // combat event functions | |
| 268 | + | virtual void onAddCondition(ConditionType_t type); | |
| 269 | + | virtual void onAddCombatCondition(ConditionType_t type); | |
| 270 | + | virtual void onEndCondition(ConditionType_t type); | |
| 271 | + | void onTickCondition(ConditionType_t type, bool &bRemove); | |
| 272 | + | virtual void onCombatRemoveCondition(Condition *condition); | |
| 273 | + | virtual void onAttackedCreature(Creature *, bool = true) {} | |
| 274 | + | virtual void onAttacked(); | |
| 275 | + | virtual void onAttackedCreatureDrainHealth(Creature *target, int32_t points); | |
| 276 | + | virtual void onTargetCreatureGainHealth(Creature *, int32_t) {} | |
| 277 | + | virtual bool onKilledCreature(Creature *target, bool lastHit = true); | |
| 278 | + | virtual void onGainExperience(uint64_t gainExp, Creature *target); | |
| 279 | + | virtual void onAttackedCreatureBlockHit(BlockType_t) {} | |
| 280 | + | virtual void onBlockHit() {} | |
| 281 | + | virtual void onChangeZone(ZoneType_t zone); | |
| 282 | + | virtual void onAttackedCreatureChangeZone(ZoneType_t zone); | |
| 283 | + | virtual void onIdleStatus(); | |
| 284 | + | ||
| 285 | + | virtual LightInfo getCreatureLight() const; | |
| 286 | + | virtual void setNormalCreatureLight(); | |
| 287 | + | void setCreatureLight(LightInfo lightInfo); | |
| 288 | + | ||
| 289 | + | virtual void onThink(uint32_t interval); | |
| 290 | + | void onAttacking(uint32_t interval); | |
| 291 | + | virtual void onWalk(); | |
| 292 | + | virtual bool getNextStep(Direction &dir, uint32_t &flags); | |
| 293 | + | ||
| 294 | + | virtual void onAddTileItem(const Tile *tile, const Position &pos); | |
| 295 | + | virtual void onUpdateTileItem(const Tile *tile, const Position &pos, | |
| 296 | + | const Item *oldItem, const ItemType &oldType, | |
| 297 | + | const Item *newItem, const ItemType &newType); | |
| 298 | + | virtual void onRemoveTileItem(const Tile *tile, const Position &pos, | |
| 299 | + | const ItemType &iType, const Item *item); | |
| 300 | + | ||
| 301 | + | virtual void onCreatureAppear(Creature *creature, bool isLogin); | |
| 302 | + | virtual void onRemoveCreature(Creature *creature, bool isLogout); | |
| 303 | + | virtual void onCreatureMove(Creature *creature, const Tile *newTile, | |
| 304 | + | const Position &newPos, const Tile *oldTile, | |
| 305 | + | const Position &oldPos, bool teleport); | |
| 306 | + | ||
| 307 | + | virtual void onAttackedCreatureDisappear(bool) {} | |
| 308 | + | virtual void onFollowCreatureDisappear(bool) {} | |
| 309 | + | ||
| 310 | + | virtual void onCreatureSay(Creature *, SpeakClasses, const std::string &) {} | |
| 311 | + | ||
| 312 | + | virtual void onPlacedCreature() {} | |
| 313 | + | ||
| 314 | + | virtual bool getCombatValues(int32_t &, int32_t &) { return false; } | |
| 315 | + | ||
| 316 | + | size_t getSummonCount() const { return summons.size(); } | |
| 317 | + | void setDropLoot(bool lootDrop) { this->lootDrop = lootDrop; } | |
| 318 | + | void setSkillLoss(bool skillLoss) { this->skillLoss = skillLoss; } | |
| 319 | + | void setUseDefense(bool useDefense) { canUseDefense = useDefense; } | |
| 320 | + | ||
| 321 | + | // creature script events | |
| 322 | + | bool registerCreatureEvent(const std::string &name); | |
| 323 | + | bool unregisterCreatureEvent(const std::string &name); | |
| 324 | + | ||
| 325 | + | Cylinder *getParent() const override final { return tile; } | |
| 326 | + | void setParent(Cylinder *cylinder) override final { | |
| 327 | + | tile = static_cast<Tile *>(cylinder); | |
| 328 | + | position = tile->getPosition(); | |
| 329 | + | } | |
| 330 | + | ||
| 331 | + | const Position &getPosition() const override final { return position; } | |
| 332 | + | ||
| 333 | + | Tile *getTile() override final { return tile; } | |
| 334 | + | const Tile *getTile() const override final { return tile; } | |
| 335 | + | ||
| 336 | + | int32_t getWalkCache(const Position &pos) const; | |
| 337 | + | ||
| 338 | + | const Position &getLastPosition() const { return lastPosition; } | |
| 339 | + | void setLastPosition(Position newLastPos) { lastPosition = newLastPos; } | |
| 340 | + | ||
| 341 | + | static bool canSee(const Position &myPos, const Position &pos, | |
| 342 | + | int32_t viewRangeX, int32_t viewRangeY); | |
| 343 | + | ||
| 344 | + | double getDamageRatio(Creature *attacker) const; | |
| 345 | + | ||
| 346 | + | bool getPathTo(const Position &targetPos, std::list<Direction> &dirList, | |
| 347 | + | const FindPathParams &fpp) const; | |
| 348 | + | bool getPathTo(const Position &targetPos, std::list<Direction> &dirList, | |
| 349 | + | int32_t minTargetDist, int32_t maxTargetDist, | |
| 350 | + | bool fullPathSearch = true, bool clearSight = true, | |
| 351 | + | int32_t maxSearchDist = 0) const; | |
| 352 | + | ||
| 353 | + | void incrementReferenceCounter() { ++referenceCounter; } | |
| 354 | + | void decrementReferenceCounter() { | |
| 355 | + | if (--referenceCounter == 0) { | |
| 356 | + | delete this; | |
| 357 | + | } | |
| 358 | + | } | |
| 359 | + | ||
| 360 | + | std::list<Direction> &getListWalkDir() { return listWalkDir; } | |
| 361 | + | bool tryToFixAutoWalk(Direction dir); | |
| 362 | + | ||
| 363 | + | protected: | |
| 364 | + | virtual bool useCacheMap() const { return false; } | |
| 365 | + | ||
| 366 | + | struct CountBlock_t { | |
| 367 | + | int32_t total; | |
| 368 | + | int64_t ticks; | |
| 369 | + | }; | |
| 370 | + | ||
| 371 | + | static constexpr int32_t mapWalkWidth = Map::maxViewportX * 2 + 1; | |
| 372 | + | static constexpr int32_t mapWalkHeight = Map::maxViewportY * 2 + 1; | |
| 373 | + | static constexpr int32_t maxWalkCacheWidth = (mapWalkWidth - 1) / 2; | |
| 374 | + | static constexpr int32_t maxWalkCacheHeight = (mapWalkHeight - 1) / 2; | |
| 375 | + | ||
| 376 | + | Position position; | |
| 377 | + | ||
| 378 | + | using CountMap = std::map<uint32_t, CountBlock_t>; | |
| 379 | + | CountMap damageMap; | |
| 380 | + | ||
| 381 | + | std::list<Creature *> summons; | |
| 382 | + | CreatureEventList eventsList; | |
| 383 | + | ConditionList conditions; | |
| 384 | + | ||
| 385 | + | std::list<Direction> listWalkDir; | |
| 386 | + | ||
| 387 | + | Tile *tile = nullptr; | |
| 388 | + | Creature *attackedCreature = nullptr; | |
| 389 | + | Creature *master = nullptr; | |
| 390 | + | Creature *followCreature = nullptr; | |
| 391 | + | ||
| 392 | + | uint64_t lastStep = 0; | |
| 393 | + | uint32_t referenceCounter = 0; | |
| 394 | + | uint32_t id = 0; | |
| 395 | + | uint32_t scriptEventsBitField = 0; | |
| 396 | + | uint32_t eventWalk = 0; | |
| 397 | + | uint32_t walkUpdateTicks = 0; | |
| 398 | + | uint32_t lastHitCreatureId = 0; | |
| 399 | + | uint32_t blockCount = 0; | |
| 400 | + | uint32_t blockTicks = 0; | |
| 401 | + | uint32_t lastStepCost = 1; | |
| 402 | + | uint32_t baseSpeed = 220; | |
| 403 | + | int32_t varSpeed = 0; | |
| 404 | + | int32_t health = 1000; | |
| 405 | + | int32_t healthMax = 1000; | |
| 406 | + | ||
| 407 | + | Outfit_t currentOutfit; | |
| 408 | + | Outfit_t defaultOutfit; | |
| 409 | + | Position lastPosition; | |
| 410 | + | LightInfo internalLight; | |
| 411 | + | ||
| 412 | + | Direction direction = DIRECTION_SOUTH; | |
| 413 | + | Skulls_t skull = SKULL_NONE; | |
| 414 | + | ||
| 415 | + | bool localMapCache[mapWalkHeight][mapWalkWidth] = {{false}}; | |
| 416 | + | bool isInternalRemoved = false; | |
| 417 | + | bool isMapLoaded = false; | |
| 418 | + | bool isUpdatingPath = false; | |
| 419 | + | bool creatureCheck = false; | |
| 420 | + | bool inCheckCreaturesVector = false; | |
| 421 | + | bool skillLoss = true; | |
| 422 | + | bool lootDrop = true; | |
| 423 | + | bool cancelNextWalk = false; | |
| 424 | + | bool hasFollowPath = false; | |
| 425 | + | bool forceUpdateFollowPath = false; | |
| 426 | + | bool hiddenHealth = false; | |
| 427 | + | bool canUseDefense = true; | |
| 428 | + | ||
| 429 | + | // creature script events | |
| 430 | + | bool hasEventRegistered(CreatureEventType_t event) const { | |
| 431 | + | return (0 != (scriptEventsBitField & (static_cast<uint32_t>(1) << event))); | |
| 432 | + | } | |
| 433 | + | CreatureEventList getCreatureEvents(CreatureEventType_t type); | |
| 434 | + | ||
| 435 | + | void updateMapCache(); | |
| 436 | + | void updateTileCache(const Tile *tile, int32_t dx, int32_t dy); | |
| 437 | + | void updateTileCache(const Tile *tile, const Position &pos); | |
| 438 | + | void onCreatureDisappear(const Creature *creature, bool isLogout); | |
| 439 | + | virtual void doAttacking(uint32_t) {} | |
| 440 | + | virtual bool hasExtraSwing() { return false; } | |
| 441 | + | ||
| 442 | + | virtual uint64_t getLostExperience() const { return 0; } | |
| 443 | + | virtual void dropLoot(Container *, Creature *) {} | |
| 444 | + | virtual uint16_t getLookCorpse() const { return 0; } | |
| 445 | + | virtual void getPathSearchParams(const Creature *creature, | |
| 446 | + | FindPathParams &fpp) const; | |
| 447 | + | virtual void death(Creature *) {} | |
| 448 | + | virtual bool dropCorpse(Creature *lastHitCreature, | |
| 449 | + | Creature *mostDamageCreature, bool lastHitUnjustified, | |
| 450 | + | bool mostDamageUnjustified); | |
| 451 | + | virtual Item *getCorpse(Creature *lastHitCreature, | |
| 452 | + | Creature *mostDamageCreature); | |
| 453 | + | ||
| 454 | + | friend class Game; | |
| 455 | + | friend class Map; | |
| 456 | + | friend class LuaScriptInterface; | |
| 457 | + | ||
| 458 | + | // Attack outfit animation members (added at the end for binary compatibility) | |
| 459 | + | Outfit_t attackOutfit; // outfit temporal durante el ataque | |
| 460 | + | uint32_t attackOutfitEventId = 0; // ID del evento de reversión programado | |
| 461 | + | Outfit_t originalOutfit; // outfit base antes del pulso | |
| 462 | + | bool isAttackPulsing = false; // si está en un pulso de ataque | |
| 463 | + | ||
| 464 | + | public: | |
| 465 | + | // Attack outfit animation methods (added at the end for binary compatibility) | |
| 466 | + | void pulseAttackOutfit(const Outfit_t &outfit, uint32_t durationMs = 1000); | |
| 467 | + | void revertAttackOutfit(); | |
| 468 | + | virtual uint16_t getAttackOutfitLookType() const { | |
| 469 | + | return attackOutfit.lookType; | |
| 470 | + | } | |
| 471 | + | void setAttackOutfitLookType(uint16_t lookType) { | |
| 472 | + | attackOutfit.lookType = lookType; | |
| 473 | + | } | |
| 474 | + | }; | |
| 475 | + | ||
| 476 | + | #endif |
| @@ -0,0 +1,607 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "creatureevent.h" | |
| 23 | + | #include "tools.h" | |
| 24 | + | #include "player.h" | |
| 25 | + | ||
| 26 | + | CreatureEvents::CreatureEvents() : | |
| 27 | + | scriptInterface("CreatureScript Interface") | |
| 28 | + | { | |
| 29 | + | scriptInterface.initState(); | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | void CreatureEvents::clear(bool fromLua) | |
| 33 | + | { | |
| 34 | + | for (auto it = creatureEvents.begin(); it != creatureEvents.end(); ++it) { | |
| 35 | + | if (fromLua == it->second.fromLua) { | |
| 36 | + | it->second.clearEvent(); | |
| 37 | + | } | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | reInitState(fromLua); | |
| 41 | + | } | |
| 42 | + | ||
| 43 | + | void CreatureEvents::removeInvalidEvents() | |
| 44 | + | { | |
| 45 | + | for (auto it = creatureEvents.begin(); it != creatureEvents.end(); ++it) { | |
| 46 | + | if (it->second.getScriptId() == 0) { | |
| 47 | + | creatureEvents.erase(it->second.getName()); | |
| 48 | + | } | |
| 49 | + | } | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | LuaScriptInterface& CreatureEvents::getScriptInterface() | |
| 53 | + | { | |
| 54 | + | return scriptInterface; | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | std::string CreatureEvents::getScriptBaseName() const | |
| 58 | + | { | |
| 59 | + | return "creaturescripts"; | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | Event_ptr CreatureEvents::getEvent(const std::string& nodeName) | |
| 63 | + | { | |
| 64 | + | if (strcasecmp(nodeName.c_str(), "event") != 0) { | |
| 65 | + | return nullptr; | |
| 66 | + | } | |
| 67 | + | return Event_ptr(new CreatureEvent(&scriptInterface)); | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | bool CreatureEvents::registerEvent(Event_ptr event, const pugi::xml_node&) | |
| 71 | + | { | |
| 72 | + | CreatureEvent_ptr creatureEvent{static_cast<CreatureEvent*>(event.release())}; //event is guaranteed to be a CreatureEvent | |
| 73 | + | if (creatureEvent->getEventType() == CREATURE_EVENT_NONE) { | |
| 74 | + | std::cout << "Error: [CreatureEvents::registerEvent] Trying to register event without type!" << std::endl; | |
| 75 | + | return false; | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | CreatureEvent* oldEvent = getEventByName(creatureEvent->getName(), false); | |
| 79 | + | if (oldEvent) { | |
| 80 | + | //if there was an event with the same that is not loaded | |
| 81 | + | //(happens when realoading), it is reused | |
| 82 | + | if (!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType()) { | |
| 83 | + | oldEvent->copyEvent(creatureEvent.get()); | |
| 84 | + | } | |
| 85 | + | ||
| 86 | + | return false; | |
| 87 | + | } else { | |
| 88 | + | //if not, register it normally | |
| 89 | + | creatureEvents.emplace(creatureEvent->getName(), std::move(*creatureEvent)); | |
| 90 | + | return true; | |
| 91 | + | } | |
| 92 | + | } | |
| 93 | + | ||
| 94 | + | bool CreatureEvents::registerLuaEvent(CreatureEvent* event) | |
| 95 | + | { | |
| 96 | + | CreatureEvent_ptr creatureEvent{ event }; | |
| 97 | + | if (creatureEvent->getEventType() == CREATURE_EVENT_NONE) { | |
| 98 | + | std::cout << "Error: [CreatureEvents::registerLuaEvent] Trying to register event without type!" << std::endl; | |
| 99 | + | return false; | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | CreatureEvent* oldEvent = getEventByName(creatureEvent->getName(), false); | |
| 103 | + | if (oldEvent) { | |
| 104 | + | //if there was an event with the same that is not loaded | |
| 105 | + | //(happens when realoading), it is reused | |
| 106 | + | if (!oldEvent->isLoaded() && oldEvent->getEventType() == creatureEvent->getEventType()) { | |
| 107 | + | oldEvent->copyEvent(creatureEvent.get()); | |
| 108 | + | } | |
| 109 | + | ||
| 110 | + | return false; | |
| 111 | + | } else { | |
| 112 | + | //if not, register it normally | |
| 113 | + | creatureEvents.emplace(creatureEvent->getName(), std::move(*creatureEvent)); | |
| 114 | + | return true; | |
| 115 | + | } | |
| 116 | + | } | |
| 117 | + | ||
| 118 | + | CreatureEvent* CreatureEvents::getEventByName(const std::string& name, bool forceLoaded /*= true*/) | |
| 119 | + | { | |
| 120 | + | auto it = creatureEvents.find(name); | |
| 121 | + | if (it != creatureEvents.end()) { | |
| 122 | + | if (!forceLoaded || it->second.isLoaded()) { | |
| 123 | + | return &it->second; | |
| 124 | + | } | |
| 125 | + | } | |
| 126 | + | return nullptr; | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | bool CreatureEvents::playerLogin(Player* player) const | |
| 130 | + | { | |
| 131 | + | //fire global event if is registered | |
| 132 | + | for (const auto& it : creatureEvents) { | |
| 133 | + | if (it.second.getEventType() == CREATURE_EVENT_LOGIN) { | |
| 134 | + | if (!it.second.executeOnLogin(player)) { | |
| 135 | + | return false; | |
| 136 | + | } | |
| 137 | + | } | |
| 138 | + | } | |
| 139 | + | return true; | |
| 140 | + | } | |
| 141 | + | ||
| 142 | + | bool CreatureEvents::playerLogout(Player* player) const | |
| 143 | + | { | |
| 144 | + | //fire global event if is registered | |
| 145 | + | for (const auto& it : creatureEvents) { | |
| 146 | + | if (it.second.getEventType() == CREATURE_EVENT_LOGOUT) { | |
| 147 | + | if (!it.second.executeOnLogout(player)) { | |
| 148 | + | return false; | |
| 149 | + | } | |
| 150 | + | } | |
| 151 | + | } | |
| 152 | + | return true; | |
| 153 | + | } | |
| 154 | + | ||
| 155 | + | bool CreatureEvents::playerAdvance(Player* player, skills_t skill, uint32_t oldLevel, | |
| 156 | + | uint32_t newLevel) | |
| 157 | + | { | |
| 158 | + | for (auto& it : creatureEvents) { | |
| 159 | + | if (it.second.getEventType() == CREATURE_EVENT_ADVANCE) { | |
| 160 | + | if (!it.second.executeAdvance(player, skill, oldLevel, newLevel)) { | |
| 161 | + | return false; | |
| 162 | + | } | |
| 163 | + | } | |
| 164 | + | } | |
| 165 | + | return true; | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | ///////////////////////////////////// | |
| 169 | + | ||
| 170 | + | CreatureEvent::CreatureEvent(LuaScriptInterface* interface) : | |
| 171 | + | Event(interface), type(CREATURE_EVENT_NONE), loaded(false) {} | |
| 172 | + | ||
| 173 | + | bool CreatureEvent::configureEvent(const pugi::xml_node& node) | |
| 174 | + | { | |
| 175 | + | // Name that will be used in monster xml files and | |
| 176 | + | // lua function to register events to reference this event | |
| 177 | + | pugi::xml_attribute nameAttribute = node.attribute("name"); | |
| 178 | + | if (!nameAttribute) { | |
| 179 | + | std::cout << "[Error - CreatureEvent::configureEvent] Missing name for creature event" << std::endl; | |
| 180 | + | return false; | |
| 181 | + | } | |
| 182 | + | ||
| 183 | + | eventName = nameAttribute.as_string(); | |
| 184 | + | ||
| 185 | + | pugi::xml_attribute typeAttribute = node.attribute("type"); | |
| 186 | + | if (!typeAttribute) { | |
| 187 | + | std::cout << "[Error - CreatureEvent::configureEvent] Missing type for creature event: " << eventName << std::endl; | |
| 188 | + | return false; | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | std::string tmpStr = asLowerCaseString(typeAttribute.as_string()); | |
| 192 | + | if (tmpStr == "login") { | |
| 193 | + | type = CREATURE_EVENT_LOGIN; | |
| 194 | + | } else if (tmpStr == "logout") { | |
| 195 | + | type = CREATURE_EVENT_LOGOUT; | |
| 196 | + | } else if (tmpStr == "think") { | |
| 197 | + | type = CREATURE_EVENT_THINK; | |
| 198 | + | } else if (tmpStr == "preparedeath") { | |
| 199 | + | type = CREATURE_EVENT_PREPAREDEATH; | |
| 200 | + | } else if (tmpStr == "death") { | |
| 201 | + | type = CREATURE_EVENT_DEATH; | |
| 202 | + | } else if (tmpStr == "kill") { | |
| 203 | + | type = CREATURE_EVENT_KILL; | |
| 204 | + | } else if (tmpStr == "advance") { | |
| 205 | + | type = CREATURE_EVENT_ADVANCE; | |
| 206 | + | } else if (tmpStr == "modalwindow") { | |
| 207 | + | type = CREATURE_EVENT_MODALWINDOW; | |
| 208 | + | } else if (tmpStr == "textedit") { | |
| 209 | + | type = CREATURE_EVENT_TEXTEDIT; | |
| 210 | + | } else if (tmpStr == "healthchange") { | |
| 211 | + | type = CREATURE_EVENT_HEALTHCHANGE; | |
| 212 | + | } else if (tmpStr == "manachange") { | |
| 213 | + | type = CREATURE_EVENT_MANACHANGE; | |
| 214 | + | } else if (tmpStr == "extendedopcode") { | |
| 215 | + | type = CREATURE_EVENT_EXTENDED_OPCODE; | |
| 216 | + | } else { | |
| 217 | + | std::cout << "[Error - CreatureEvent::configureEvent] Invalid type for creature event: " << eventName << std::endl; | |
| 218 | + | return false; | |
| 219 | + | } | |
| 220 | + | ||
| 221 | + | loaded = true; | |
| 222 | + | return true; | |
| 223 | + | } | |
| 224 | + | ||
| 225 | + | std::string CreatureEvent::getScriptEventName() const | |
| 226 | + | { | |
| 227 | + | //Depending on the type script event name is different | |
| 228 | + | switch (type) { | |
| 229 | + | case CREATURE_EVENT_LOGIN: | |
| 230 | + | return "onLogin"; | |
| 231 | + | ||
| 232 | + | case CREATURE_EVENT_LOGOUT: | |
| 233 | + | return "onLogout"; | |
| 234 | + | ||
| 235 | + | case CREATURE_EVENT_THINK: | |
| 236 | + | return "onThink"; | |
| 237 | + | ||
| 238 | + | case CREATURE_EVENT_PREPAREDEATH: | |
| 239 | + | return "onPrepareDeath"; | |
| 240 | + | ||
| 241 | + | case CREATURE_EVENT_DEATH: | |
| 242 | + | return "onDeath"; | |
| 243 | + | ||
| 244 | + | case CREATURE_EVENT_KILL: | |
| 245 | + | return "onKill"; | |
| 246 | + | ||
| 247 | + | case CREATURE_EVENT_ADVANCE: | |
| 248 | + | return "onAdvance"; | |
| 249 | + | ||
| 250 | + | case CREATURE_EVENT_MODALWINDOW: | |
| 251 | + | return "onModalWindow"; | |
| 252 | + | ||
| 253 | + | case CREATURE_EVENT_TEXTEDIT: | |
| 254 | + | return "onTextEdit"; | |
| 255 | + | ||
| 256 | + | case CREATURE_EVENT_HEALTHCHANGE: | |
| 257 | + | return "onHealthChange"; | |
| 258 | + | ||
| 259 | + | case CREATURE_EVENT_MANACHANGE: | |
| 260 | + | return "onManaChange"; | |
| 261 | + | ||
| 262 | + | case CREATURE_EVENT_EXTENDED_OPCODE: | |
| 263 | + | return "onExtendedOpcode"; | |
| 264 | + | ||
| 265 | + | case CREATURE_EVENT_NONE: | |
| 266 | + | default: | |
| 267 | + | return std::string(); | |
| 268 | + | } | |
| 269 | + | } | |
| 270 | + | ||
| 271 | + | void CreatureEvent::copyEvent(CreatureEvent* creatureEvent) | |
| 272 | + | { | |
| 273 | + | scriptId = creatureEvent->scriptId; | |
| 274 | + | scriptInterface = creatureEvent->scriptInterface; | |
| 275 | + | scripted = creatureEvent->scripted; | |
| 276 | + | loaded = creatureEvent->loaded; | |
| 277 | + | } | |
| 278 | + | ||
| 279 | + | void CreatureEvent::clearEvent() | |
| 280 | + | { | |
| 281 | + | scriptId = 0; | |
| 282 | + | scriptInterface = nullptr; | |
| 283 | + | scripted = false; | |
| 284 | + | loaded = false; | |
| 285 | + | } | |
| 286 | + | ||
| 287 | + | bool CreatureEvent::executeOnLogin(Player* player) const | |
| 288 | + | { | |
| 289 | + | //onLogin(player) | |
| 290 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 291 | + | std::cout << "[Error - CreatureEvent::executeOnLogin] Call stack overflow" << std::endl; | |
| 292 | + | return false; | |
| 293 | + | } | |
| 294 | + | ||
| 295 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 296 | + | env->setScriptId(scriptId, scriptInterface); | |
| 297 | + | ||
| 298 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 299 | + | ||
| 300 | + | scriptInterface->pushFunction(scriptId); | |
| 301 | + | LuaScriptInterface::pushUserdata(L, player); | |
| 302 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 303 | + | return scriptInterface->callFunction(1); | |
| 304 | + | } | |
| 305 | + | ||
| 306 | + | bool CreatureEvent::executeOnLogout(Player* player) const | |
| 307 | + | { | |
| 308 | + | //onLogout(player) | |
| 309 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 310 | + | std::cout << "[Error - CreatureEvent::executeOnLogout] Call stack overflow" << std::endl; | |
| 311 | + | return false; | |
| 312 | + | } | |
| 313 | + | ||
| 314 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 315 | + | env->setScriptId(scriptId, scriptInterface); | |
| 316 | + | ||
| 317 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 318 | + | ||
| 319 | + | scriptInterface->pushFunction(scriptId); | |
| 320 | + | LuaScriptInterface::pushUserdata(L, player); | |
| 321 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 322 | + | return scriptInterface->callFunction(1); | |
| 323 | + | } | |
| 324 | + | ||
| 325 | + | bool CreatureEvent::executeOnThink(Creature* creature, uint32_t interval) | |
| 326 | + | { | |
| 327 | + | //onThink(creature, interval) | |
| 328 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 329 | + | std::cout << "[Error - CreatureEvent::executeOnThink] Call stack overflow" << std::endl; | |
| 330 | + | return false; | |
| 331 | + | } | |
| 332 | + | ||
| 333 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 334 | + | env->setScriptId(scriptId, scriptInterface); | |
| 335 | + | ||
| 336 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 337 | + | ||
| 338 | + | scriptInterface->pushFunction(scriptId); | |
| 339 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 340 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 341 | + | lua_pushnumber(L, interval); | |
| 342 | + | ||
| 343 | + | return scriptInterface->callFunction(2); | |
| 344 | + | } | |
| 345 | + | ||
| 346 | + | bool CreatureEvent::executeOnPrepareDeath(Creature* creature, Creature* killer) | |
| 347 | + | { | |
| 348 | + | //onPrepareDeath(creature, killer) | |
| 349 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 350 | + | std::cout << "[Error - CreatureEvent::executeOnPrepareDeath] Call stack overflow" << std::endl; | |
| 351 | + | return false; | |
| 352 | + | } | |
| 353 | + | ||
| 354 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 355 | + | env->setScriptId(scriptId, scriptInterface); | |
| 356 | + | ||
| 357 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 358 | + | ||
| 359 | + | scriptInterface->pushFunction(scriptId); | |
| 360 | + | ||
| 361 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 362 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 363 | + | ||
| 364 | + | if (killer) { | |
| 365 | + | LuaScriptInterface::pushUserdata<Creature>(L, killer); | |
| 366 | + | LuaScriptInterface::setCreatureMetatable(L, -1, killer); | |
| 367 | + | } else { | |
| 368 | + | lua_pushnil(L); | |
| 369 | + | } | |
| 370 | + | ||
| 371 | + | return scriptInterface->callFunction(2); | |
| 372 | + | } | |
| 373 | + | ||
| 374 | + | bool CreatureEvent::executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified) | |
| 375 | + | { | |
| 376 | + | //onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified) | |
| 377 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 378 | + | std::cout << "[Error - CreatureEvent::executeOnDeath] Call stack overflow" << std::endl; | |
| 379 | + | return false; | |
| 380 | + | } | |
| 381 | + | ||
| 382 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 383 | + | env->setScriptId(scriptId, scriptInterface); | |
| 384 | + | ||
| 385 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 386 | + | ||
| 387 | + | scriptInterface->pushFunction(scriptId); | |
| 388 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 389 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 390 | + | ||
| 391 | + | LuaScriptInterface::pushThing(L, corpse); | |
| 392 | + | ||
| 393 | + | if (killer) { | |
| 394 | + | LuaScriptInterface::pushUserdata<Creature>(L, killer); | |
| 395 | + | LuaScriptInterface::setCreatureMetatable(L, -1, killer); | |
| 396 | + | } else { | |
| 397 | + | lua_pushnil(L); | |
| 398 | + | } | |
| 399 | + | ||
| 400 | + | if (mostDamageKiller) { | |
| 401 | + | LuaScriptInterface::pushUserdata<Creature>(L, mostDamageKiller); | |
| 402 | + | LuaScriptInterface::setCreatureMetatable(L, -1, mostDamageKiller); | |
| 403 | + | } else { | |
| 404 | + | lua_pushnil(L); | |
| 405 | + | } | |
| 406 | + | ||
| 407 | + | LuaScriptInterface::pushBoolean(L, lastHitUnjustified); | |
| 408 | + | LuaScriptInterface::pushBoolean(L, mostDamageUnjustified); | |
| 409 | + | ||
| 410 | + | return scriptInterface->callFunction(6); | |
| 411 | + | } | |
| 412 | + | ||
| 413 | + | bool CreatureEvent::executeAdvance(Player* player, skills_t skill, uint32_t oldLevel, | |
| 414 | + | uint32_t newLevel) | |
| 415 | + | { | |
| 416 | + | //onAdvance(player, skill, oldLevel, newLevel) | |
| 417 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 418 | + | std::cout << "[Error - CreatureEvent::executeAdvance] Call stack overflow" << std::endl; | |
| 419 | + | return false; | |
| 420 | + | } | |
| 421 | + | ||
| 422 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 423 | + | env->setScriptId(scriptId, scriptInterface); | |
| 424 | + | ||
| 425 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 426 | + | ||
| 427 | + | scriptInterface->pushFunction(scriptId); | |
| 428 | + | LuaScriptInterface::pushUserdata(L, player); | |
| 429 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 430 | + | lua_pushnumber(L, static_cast<uint32_t>(skill)); | |
| 431 | + | lua_pushnumber(L, oldLevel); | |
| 432 | + | lua_pushnumber(L, newLevel); | |
| 433 | + | ||
| 434 | + | return scriptInterface->callFunction(4); | |
| 435 | + | } | |
| 436 | + | ||
| 437 | + | void CreatureEvent::executeOnKill(Creature* creature, Creature* target) | |
| 438 | + | { | |
| 439 | + | //onKill(creature, target) | |
| 440 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 441 | + | std::cout << "[Error - CreatureEvent::executeOnKill] Call stack overflow" << std::endl; | |
| 442 | + | return; | |
| 443 | + | } | |
| 444 | + | ||
| 445 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 446 | + | env->setScriptId(scriptId, scriptInterface); | |
| 447 | + | ||
| 448 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 449 | + | ||
| 450 | + | scriptInterface->pushFunction(scriptId); | |
| 451 | + | LuaScriptInterface::pushUserdata<Creature>(L, creature); | |
| 452 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 453 | + | LuaScriptInterface::pushUserdata<Creature>(L, target); | |
| 454 | + | LuaScriptInterface::setCreatureMetatable(L, -1, target); | |
| 455 | + | scriptInterface->callVoidFunction(2); | |
| 456 | + | } | |
| 457 | + | ||
| 458 | + | void CreatureEvent::executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId) | |
| 459 | + | { | |
| 460 | + | //onModalWindow(player, modalWindowId, buttonId, choiceId) | |
| 461 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 462 | + | std::cout << "[Error - CreatureEvent::executeModalWindow] Call stack overflow" << std::endl; | |
| 463 | + | return; | |
| 464 | + | } | |
| 465 | + | ||
| 466 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 467 | + | env->setScriptId(scriptId, scriptInterface); | |
| 468 | + | ||
| 469 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 470 | + | scriptInterface->pushFunction(scriptId); | |
| 471 | + | ||
| 472 | + | LuaScriptInterface::pushUserdata(L, player); | |
| 473 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 474 | + | ||
| 475 | + | lua_pushnumber(L, modalWindowId); | |
| 476 | + | lua_pushnumber(L, buttonId); | |
| 477 | + | lua_pushnumber(L, choiceId); | |
| 478 | + | ||
| 479 | + | scriptInterface->callVoidFunction(4); | |
| 480 | + | } | |
| 481 | + | ||
| 482 | + | bool CreatureEvent::executeTextEdit(Player* player, Item* item, const std::string& text) | |
| 483 | + | { | |
| 484 | + | //onTextEdit(player, item, text) | |
| 485 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 486 | + | std::cout << "[Error - CreatureEvent::executeTextEdit] Call stack overflow" << std::endl; | |
| 487 | + | return false; | |
| 488 | + | } | |
| 489 | + | ||
| 490 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 491 | + | env->setScriptId(scriptId, scriptInterface); | |
| 492 | + | ||
| 493 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 494 | + | scriptInterface->pushFunction(scriptId); | |
| 495 | + | ||
| 496 | + | LuaScriptInterface::pushUserdata(L, player); | |
| 497 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 498 | + | ||
| 499 | + | LuaScriptInterface::pushThing(L, item); | |
| 500 | + | LuaScriptInterface::pushString(L, text); | |
| 501 | + | ||
| 502 | + | return scriptInterface->callFunction(3); | |
| 503 | + | } | |
| 504 | + | ||
| 505 | + | void CreatureEvent::executeHealthChange(Creature* creature, Creature* attacker, CombatDamage& damage) | |
| 506 | + | { | |
| 507 | + | //onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin) | |
| 508 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 509 | + | std::cout << "[Error - CreatureEvent::executeHealthChange] Call stack overflow" << std::endl; | |
| 510 | + | return; | |
| 511 | + | } | |
| 512 | + | ||
| 513 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 514 | + | env->setScriptId(scriptId, scriptInterface); | |
| 515 | + | ||
| 516 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 517 | + | scriptInterface->pushFunction(scriptId); | |
| 518 | + | ||
| 519 | + | LuaScriptInterface::pushUserdata(L, creature); | |
| 520 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 521 | + | if (attacker) { | |
| 522 | + | LuaScriptInterface::pushUserdata(L, attacker); | |
| 523 | + | LuaScriptInterface::setCreatureMetatable(L, -1, attacker); | |
| 524 | + | } else { | |
| 525 | + | lua_pushnil(L); | |
| 526 | + | } | |
| 527 | + | ||
| 528 | + | LuaScriptInterface::pushCombatDamage(L, damage); | |
| 529 | + | ||
| 530 | + | if (scriptInterface->protectedCall(L, 7, 4) != 0) { | |
| 531 | + | LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L)); | |
| 532 | + | } else { | |
| 533 | + | damage.primary.value = std::abs(LuaScriptInterface::getNumber<int32_t>(L, -4)); | |
| 534 | + | damage.primary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -3); | |
| 535 | + | damage.secondary.value = std::abs(LuaScriptInterface::getNumber<int32_t>(L, -2)); | |
| 536 | + | damage.secondary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -1); | |
| 537 | + | ||
| 538 | + | lua_pop(L, 4); | |
| 539 | + | if (damage.primary.type != COMBAT_HEALING) { | |
| 540 | + | damage.primary.value = -damage.primary.value; | |
| 541 | + | damage.secondary.value = -damage.secondary.value; | |
| 542 | + | } | |
| 543 | + | } | |
| 544 | + | ||
| 545 | + | scriptInterface->resetScriptEnv(); | |
| 546 | + | } | |
| 547 | + | ||
| 548 | + | void CreatureEvent::executeManaChange(Creature* creature, Creature* attacker, CombatDamage& damage) { | |
| 549 | + | //onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin) | |
| 550 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 551 | + | std::cout << "[Error - CreatureEvent::executeManaChange] Call stack overflow" << std::endl; | |
| 552 | + | return; | |
| 553 | + | } | |
| 554 | + | ||
| 555 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 556 | + | env->setScriptId(scriptId, scriptInterface); | |
| 557 | + | ||
| 558 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 559 | + | scriptInterface->pushFunction(scriptId); | |
| 560 | + | ||
| 561 | + | LuaScriptInterface::pushUserdata(L, creature); | |
| 562 | + | LuaScriptInterface::setCreatureMetatable(L, -1, creature); | |
| 563 | + | if (attacker) { | |
| 564 | + | LuaScriptInterface::pushUserdata(L, attacker); | |
| 565 | + | LuaScriptInterface::setCreatureMetatable(L, -1, attacker); | |
| 566 | + | } else { | |
| 567 | + | lua_pushnil(L); | |
| 568 | + | } | |
| 569 | + | ||
| 570 | + | LuaScriptInterface::pushCombatDamage(L, damage); | |
| 571 | + | ||
| 572 | + | if (scriptInterface->protectedCall(L, 7, 4) != 0) { | |
| 573 | + | LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L)); | |
| 574 | + | } else { | |
| 575 | + | damage.primary.value = LuaScriptInterface::getNumber<int32_t>(L, -4); | |
| 576 | + | damage.primary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -3); | |
| 577 | + | damage.secondary.value = LuaScriptInterface::getNumber<int32_t>(L, -2); | |
| 578 | + | damage.secondary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -1); | |
| 579 | + | lua_pop(L, 4); | |
| 580 | + | } | |
| 581 | + | ||
| 582 | + | scriptInterface->resetScriptEnv(); | |
| 583 | + | } | |
| 584 | + | ||
| 585 | + | void CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer) | |
| 586 | + | { | |
| 587 | + | //onExtendedOpcode(player, opcode, buffer) | |
| 588 | + | if (!scriptInterface->reserveScriptEnv()) { | |
| 589 | + | std::cout << "[Error - CreatureEvent::executeExtendedOpcode] Call stack overflow" << std::endl; | |
| 590 | + | return; | |
| 591 | + | } | |
| 592 | + | ||
| 593 | + | ScriptEnvironment* env = scriptInterface->getScriptEnv(); | |
| 594 | + | env->setScriptId(scriptId, scriptInterface); | |
| 595 | + | ||
| 596 | + | lua_State* L = scriptInterface->getLuaState(); | |
| 597 | + | ||
| 598 | + | scriptInterface->pushFunction(scriptId); | |
| 599 | + | ||
| 600 | + | LuaScriptInterface::pushUserdata<Player>(L, player); | |
| 601 | + | LuaScriptInterface::setMetatable(L, -1, "Player"); | |
| 602 | + | ||
| 603 | + | lua_pushnumber(L, opcode); | |
| 604 | + | LuaScriptInterface::pushString(L, buffer); | |
| 605 | + | ||
| 606 | + | scriptInterface->callVoidFunction(3); | |
| 607 | + | } |
| @@ -0,0 +1,132 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #ifndef FS_CREATUREEVENT_H_73FCAF4608CB41399D53C919316646A9 | |
| 21 | + | #define FS_CREATUREEVENT_H_73FCAF4608CB41399D53C919316646A9 | |
| 22 | + | ||
| 23 | + | #include "luascript.h" | |
| 24 | + | #include "baseevents.h" | |
| 25 | + | #include "enums.h" | |
| 26 | + | ||
| 27 | + | class CreatureEvent; | |
| 28 | + | using CreatureEvent_ptr = std::unique_ptr<CreatureEvent>; | |
| 29 | + | ||
| 30 | + | enum CreatureEventType_t { | |
| 31 | + | CREATURE_EVENT_NONE, | |
| 32 | + | CREATURE_EVENT_LOGIN, | |
| 33 | + | CREATURE_EVENT_LOGOUT, | |
| 34 | + | CREATURE_EVENT_THINK, | |
| 35 | + | CREATURE_EVENT_PREPAREDEATH, | |
| 36 | + | CREATURE_EVENT_DEATH, | |
| 37 | + | CREATURE_EVENT_KILL, | |
| 38 | + | CREATURE_EVENT_ADVANCE, | |
| 39 | + | CREATURE_EVENT_MODALWINDOW, | |
| 40 | + | CREATURE_EVENT_TEXTEDIT, | |
| 41 | + | CREATURE_EVENT_HEALTHCHANGE, | |
| 42 | + | CREATURE_EVENT_MANACHANGE, | |
| 43 | + | CREATURE_EVENT_EXTENDED_OPCODE, // otclient additional network opcodes | |
| 44 | + | }; | |
| 45 | + | ||
| 46 | + | class CreatureEvent final : public Event | |
| 47 | + | { | |
| 48 | + | public: | |
| 49 | + | explicit CreatureEvent(LuaScriptInterface* interface); | |
| 50 | + | ||
| 51 | + | bool configureEvent(const pugi::xml_node& node) override; | |
| 52 | + | ||
| 53 | + | CreatureEventType_t getEventType() const { | |
| 54 | + | return type; | |
| 55 | + | } | |
| 56 | + | void setEventType(CreatureEventType_t eventType) { | |
| 57 | + | type = eventType; | |
| 58 | + | } | |
| 59 | + | const std::string& getName() const { | |
| 60 | + | return eventName; | |
| 61 | + | } | |
| 62 | + | void setName(const std::string& name) { | |
| 63 | + | eventName = name; | |
| 64 | + | } | |
| 65 | + | bool isLoaded() const { | |
| 66 | + | return loaded; | |
| 67 | + | } | |
| 68 | + | void setLoaded(bool b) { | |
| 69 | + | loaded = b; | |
| 70 | + | } | |
| 71 | + | ||
| 72 | + | void clearEvent(); | |
| 73 | + | void copyEvent(CreatureEvent* creatureEvent); | |
| 74 | + | ||
| 75 | + | //scripting | |
| 76 | + | bool executeOnLogin(Player* player) const; | |
| 77 | + | bool executeOnLogout(Player* player) const; | |
| 78 | + | bool executeOnThink(Creature* creature, uint32_t interval); | |
| 79 | + | bool executeOnPrepareDeath(Creature* creature, Creature* killer); | |
| 80 | + | bool executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified); | |
| 81 | + | void executeOnKill(Creature* creature, Creature* target); | |
| 82 | + | bool executeAdvance(Player* player, skills_t, uint32_t, uint32_t); | |
| 83 | + | void executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId); | |
| 84 | + | bool executeTextEdit(Player* player, Item* item, const std::string& text); | |
| 85 | + | void executeHealthChange(Creature* creature, Creature* attacker, CombatDamage& damage); | |
| 86 | + | void executeManaChange(Creature* creature, Creature* attacker, CombatDamage& damage); | |
| 87 | + | void executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer); | |
| 88 | + | // | |
| 89 | + | ||
| 90 | + | private: | |
| 91 | + | std::string getScriptEventName() const override; | |
| 92 | + | ||
| 93 | + | std::string eventName; | |
| 94 | + | CreatureEventType_t type; | |
| 95 | + | bool loaded; | |
| 96 | + | }; | |
| 97 | + | ||
| 98 | + | class CreatureEvents final : public BaseEvents | |
| 99 | + | { | |
| 100 | + | public: | |
| 101 | + | CreatureEvents(); | |
| 102 | + | ||
| 103 | + | // non-copyable | |
| 104 | + | CreatureEvents(const CreatureEvents&) = delete; | |
| 105 | + | CreatureEvents& operator=(const CreatureEvents&) = delete; | |
| 106 | + | ||
| 107 | + | // global events | |
| 108 | + | bool playerLogin(Player* player) const; | |
| 109 | + | bool playerLogout(Player* player) const; | |
| 110 | + | bool playerAdvance(Player* player, skills_t, uint32_t, uint32_t); | |
| 111 | + | ||
| 112 | + | CreatureEvent* getEventByName(const std::string& name, bool forceLoaded = true); | |
| 113 | + | ||
| 114 | + | bool registerLuaEvent(CreatureEvent* event); | |
| 115 | + | void clear(bool fromLua) override final; | |
| 116 | + | ||
| 117 | + | void removeInvalidEvents(); | |
| 118 | + | ||
| 119 | + | private: | |
| 120 | + | LuaScriptInterface& getScriptInterface() override; | |
| 121 | + | std::string getScriptBaseName() const override; | |
| 122 | + | Event_ptr getEvent(const std::string& nodeName) override; | |
| 123 | + | bool registerEvent(Event_ptr event, const pugi::xml_node& node) override; | |
| 124 | + | ||
| 125 | + | //creature events | |
| 126 | + | using CreatureEventMap = std::map<std::string, CreatureEvent>; | |
| 127 | + | CreatureEventMap creatureEvents; | |
| 128 | + | ||
| 129 | + | LuaScriptInterface scriptInterface; | |
| 130 | + | }; | |
| 131 | + | ||
| 132 | + | #endif |
| @@ -0,0 +1,69 @@ | |||
| 1 | + | /** | |
| 2 | + | * The Forgotten Server - a free and open-source MMORPG server emulator | |
| 3 | + | * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com> | |
| 4 | + | * | |
| 5 | + | * This program is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published by | |
| 7 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 8 | + | * (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * This program is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License along | |
| 16 | + | * with this program; if not, write to the Free Software Foundation, Inc., | |
| 17 | + | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 18 | + | */ | |
| 19 | + | ||
| 20 | + | #include "otpch.h" | |
| 21 | + | ||
| 22 | + | #include "cylinder.h" | |
| 23 | + | ||
| 24 | + | VirtualCylinder* VirtualCylinder::virtualCylinder = new VirtualCylinder; | |
| 25 | + | ||
| 26 | + | int32_t Cylinder::getThingIndex(const Thing*) const | |
| 27 | + | { | |
| 28 | + | return -1; | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | size_t Cylinder::getFirstIndex() const | |
| 32 | + | { | |
| 33 | + | return 0; | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | size_t Cylinder::getLastIndex() const | |
| 37 | + | { | |
| 38 | + | return 0; | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | uint32_t Cylinder::getItemTypeCount(uint16_t, int32_t) const | |
| 42 | + | { | |
| 43 | + | return 0; | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | std::map<uint32_t, uint32_t>& Cylinder::getAllItemTypeCount(std::map<uint32_t, uint32_t>& countMap) const | |
| 47 | + | { | |
| 48 | + | return countMap; | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | Thing* Cylinder::getThing(size_t) const | |
| 52 | + | { | |
| 53 | + | return nullptr; | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | void Cylinder::internalAddThing(Thing*) | |
| 57 | + | { | |
| 58 | + | // | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | void Cylinder::internalAddThing(uint32_t, Thing*) | |
| 62 | + | { | |
| 63 | + | // | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | void Cylinder::startDecaying() | |
| 67 | + | { | |
| 68 | + | // | |
| 69 | + | } |