No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

creature.cpp 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "creature.h"
  23. #include "thingtypemanager.h"
  24. #include "localplayer.h"
  25. #include "map.h"
  26. #include "tile.h"
  27. #include "item.h"
  28. #include "game.h"
  29. #include "effect.h"
  30. #include "luavaluecasts.h"
  31. #include "lightview.h"
  32. #include <framework/graphics/graphics.h>
  33. #include <framework/core/eventdispatcher.h>
  34. #include <framework/core/clock.h>
  35. #include <framework/graphics/paintershaderprogram.h>
  36. #include <framework/graphics/ogl/painterogl2_shadersources.h>
  37. #include <framework/graphics/texturemanager.h>
  38. #include <framework/graphics/framebuffermanager.h>
  39. #include "spritemanager.h"
  40. Creature::Creature() : Thing()
  41. {
  42. m_id = 0;
  43. m_healthPercent = 100;
  44. m_speed = 200;
  45. m_direction = Otc::South;
  46. m_walkAnimationPhase = 0;
  47. m_walkedPixels = 0;
  48. m_walkTurnDirection = Otc::InvalidDirection;
  49. m_skull = Otc::SkullNone;
  50. m_shield = Otc::ShieldNone;
  51. m_emblem = Otc::EmblemNone;
  52. m_type = Proto::CreatureTypeUnknown;
  53. m_icon = Otc::NpcIconNone;
  54. m_lastStepDirection = Otc::InvalidDirection;
  55. m_nameCache.setFont(g_fonts.getFont("verdana-11px-rounded"));
  56. m_nameCache.setAlign(Fw::AlignTopCenter);
  57. m_footStep = 0;
  58. m_speedFormula.fill(-1);
  59. m_outfitColor = Color::white;
  60. }
  61. void Creature::draw(const Point& dest, float scaleFactor, bool animate, LightView *lightView)
  62. {
  63. if(!canBeSeen())
  64. return;
  65. Point animationOffset = animate ? m_walkOffset : Point(0,0);
  66. if(m_showTimedSquare && animate) {
  67. g_painter->setColor(m_timedSquareColor);
  68. g_painter->drawBoundingRect(Rect(dest + (animationOffset - getDisplacement() + 2)*scaleFactor, Size(28, 28)*scaleFactor), std::max<int>((int)(2*scaleFactor), 1));
  69. g_painter->setColor(Color::white);
  70. }
  71. if(m_showStaticSquare && animate) {
  72. g_painter->setColor(m_staticSquareColor);
  73. g_painter->drawBoundingRect(Rect(dest + (animationOffset - getDisplacement())*scaleFactor, Size(Otc::TILE_PIXELS, Otc::TILE_PIXELS)*scaleFactor), std::max<int>((int)(2*scaleFactor), 1));
  74. g_painter->setColor(Color::white);
  75. }
  76. internalDrawOutfit(dest + animationOffset * scaleFactor, scaleFactor, animate, animate, m_direction);
  77. m_footStepDrawn = true;
  78. if(lightView) {
  79. Light light = rawGetThingType()->getLight();
  80. if(m_light.intensity != light.intensity || m_light.color != light.color)
  81. light = m_light;
  82. // local player always have a minimum light in complete darkness
  83. if(isLocalPlayer() && (g_map.getLight().intensity < 64 || m_position.z > Otc::SEA_FLOOR)) {
  84. light.intensity = std::max<uint8>(light.intensity, 3);
  85. if(light.color == 0 || light.color > 215)
  86. light.color = 215;
  87. }
  88. if(light.intensity > 0)
  89. lightView->addLightSource(dest + (animationOffset + Point(16,16)) * scaleFactor, scaleFactor, light);
  90. }
  91. }
  92. void Creature::internalDrawOutfit(Point dest, float scaleFactor, bool animateWalk, bool animateIdle, Otc::Direction direction, LightView *lightView)
  93. {
  94. g_painter->setColor(m_outfitColor);
  95. // outfit is a real creature
  96. if(m_outfit.getCategory() == ThingCategoryCreature) {
  97. int animationPhase = animateWalk ? m_walkAnimationPhase : 0;
  98. if(isAnimateAlways() && animateIdle) {
  99. int ticksPerFrame = 1000 / getAnimationPhases();
  100. animationPhase = (g_clock.millis() % (ticksPerFrame * getAnimationPhases())) / ticksPerFrame;
  101. }
  102. // xPattern => creature direction
  103. int xPattern;
  104. if(direction == Otc::NorthEast || direction == Otc::SouthEast)
  105. xPattern = Otc::East;
  106. else if(direction == Otc::NorthWest || direction == Otc::SouthWest)
  107. xPattern = Otc::West;
  108. else
  109. xPattern = direction;
  110. int zPattern = 0;
  111. if(m_outfit.getMount() != 0) {
  112. auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
  113. dest -= datType->getDisplacement() * scaleFactor;
  114. datType->draw(dest, scaleFactor, 0, xPattern, 0, 0, animationPhase, lightView);
  115. dest += getDisplacement() * scaleFactor;
  116. zPattern = std::min<int>(1, getNumPatternZ() - 1);
  117. }
  118. PointF jumpOffset = m_jumpOffset * scaleFactor;
  119. dest -= Point(stdext::round(jumpOffset.x), stdext::round(jumpOffset.y));
  120. // yPattern => creature addon
  121. for(int yPattern = 0; yPattern < getNumPatternY(); yPattern++) {
  122. // continue if we dont have this addon
  123. if(yPattern > 0 && !(m_outfit.getAddons() & (1 << (yPattern-1))))
  124. continue;
  125. auto datType = rawGetThingType();
  126. datType->draw(dest, scaleFactor, 0, xPattern, yPattern, zPattern, animationPhase, yPattern == 0 ? lightView : nullptr);
  127. if(getLayers() > 1) {
  128. Color oldColor = g_painter->getColor();
  129. Painter::CompositionMode oldComposition = g_painter->getCompositionMode();
  130. g_painter->setCompositionMode(Painter::CompositionMode_Multiply);
  131. g_painter->setColor(m_outfit.getHeadColor());
  132. datType->draw(dest, scaleFactor, SpriteMaskYellow, xPattern, yPattern, zPattern, animationPhase);
  133. g_painter->setColor(m_outfit.getBodyColor());
  134. datType->draw(dest, scaleFactor, SpriteMaskRed, xPattern, yPattern, zPattern, animationPhase);
  135. g_painter->setColor(m_outfit.getLegsColor());
  136. datType->draw(dest, scaleFactor, SpriteMaskGreen, xPattern, yPattern, zPattern, animationPhase);
  137. g_painter->setColor(m_outfit.getFeetColor());
  138. datType->draw(dest, scaleFactor, SpriteMaskBlue, xPattern, yPattern, zPattern, animationPhase);
  139. g_painter->setColor(oldColor);
  140. g_painter->setCompositionMode(oldComposition);
  141. }
  142. }
  143. // outfit is a creature imitating an item or the invisible effect
  144. } else {
  145. ThingType *type = g_things.rawGetThingType(m_outfit.getAuxId(), m_outfit.getCategory());
  146. int animationPhase = 0;
  147. int animationPhases = type->getAnimationPhases();
  148. int animateTicks = Otc::ITEM_TICKS_PER_FRAME;
  149. // when creature is an effect we cant render the first and last animation phase,
  150. // instead we should loop in the phases between
  151. if(m_outfit.getCategory() == ThingCategoryEffect) {
  152. animationPhases = std::max<int>(1, animationPhases-2);
  153. animateTicks = Otc::INVISIBLE_TICKS_PER_FRAME;
  154. }
  155. if(animationPhases > 1) {
  156. if(animateIdle)
  157. animationPhase = (g_clock.millis() % (animateTicks * animationPhases)) / animateTicks;
  158. else
  159. animationPhase = animationPhases-1;
  160. }
  161. if(m_outfit.getCategory() == ThingCategoryEffect)
  162. animationPhase = std::min<int>(animationPhase+1, animationPhases);
  163. type->draw(dest - (getDisplacement() * scaleFactor), scaleFactor, 0, 0, 0, 0, animationPhase, lightView);
  164. }
  165. g_painter->resetColor();
  166. }
  167. void Creature::drawOutfit(const Rect& destRect, bool resize)
  168. {
  169. int exactSize;
  170. if(m_outfit.getCategory() == ThingCategoryCreature)
  171. exactSize = getExactSize();
  172. else
  173. exactSize = g_things.rawGetThingType(m_outfit.getAuxId(), m_outfit.getCategory())->getExactSize();
  174. int frameSize;
  175. if(!resize)
  176. frameSize = std::max<int>(exactSize * 0.75f, 2 * Otc::TILE_PIXELS * 0.75f);
  177. else if(!(frameSize = exactSize))
  178. return;
  179. if(g_graphics.canUseFBO()) {
  180. const FrameBufferPtr& outfitBuffer = g_framebuffers.getTemporaryFrameBuffer();
  181. outfitBuffer->resize(Size(frameSize, frameSize));
  182. outfitBuffer->bind();
  183. g_painter->setAlphaWriting(true);
  184. g_painter->clear(Color::alpha);
  185. internalDrawOutfit(Point(frameSize - Otc::TILE_PIXELS, frameSize - Otc::TILE_PIXELS) + getDisplacement(), 1, false, true, Otc::South);
  186. outfitBuffer->release();
  187. outfitBuffer->draw(destRect, Rect(0,0,frameSize,frameSize));
  188. } else {
  189. float scaleFactor = destRect.width() / (float)frameSize;
  190. Point dest = destRect.bottomRight() - (Point(Otc::TILE_PIXELS,Otc::TILE_PIXELS) - getDisplacement()) * scaleFactor;
  191. internalDrawOutfit(dest, scaleFactor, false, true, Otc::South);
  192. }
  193. }
  194. void Creature::drawInformation(const Point& point, bool useGray, const Rect& parentRect, int drawFlags)
  195. {
  196. if(m_healthPercent < 1) // creature is dead
  197. return;
  198. Color fillColor = Color(96, 96, 96);
  199. if(!useGray)
  200. fillColor = m_informationColor;
  201. // calculate main rects
  202. Rect backgroundRect = Rect(point.x-(13.5), point.y, 27, 4);
  203. backgroundRect.bind(parentRect);
  204. Size nameSize = m_nameCache.getTextSize();
  205. Rect textRect = Rect(point.x - nameSize.width() / 2.0, point.y-12, nameSize);
  206. textRect.bind(parentRect);
  207. // distance them
  208. uint32 offset = 12;
  209. if(isLocalPlayer()) {
  210. offset *= 2;
  211. }
  212. if(textRect.top() == parentRect.top())
  213. backgroundRect.moveTop(textRect.top() + offset);
  214. if(backgroundRect.bottom() == parentRect.bottom())
  215. textRect.moveTop(backgroundRect.top() - offset);
  216. // health rect is based on background rect, so no worries
  217. Rect healthRect = backgroundRect.expanded(-1);
  218. healthRect.setWidth((m_healthPercent / 100.0) * 25);
  219. // draw
  220. if(g_game.getFeature(Otc::GameBlueNpcNameColor) && isNpc() && m_healthPercent == 100 && !useGray)
  221. fillColor = Color(0x66, 0xcc, 0xff);
  222. if(drawFlags & Otc::DrawBars && (!isNpc() || !g_game.getFeature(Otc::GameHideNpcNames))) {
  223. g_painter->setColor(Color::black);
  224. g_painter->drawFilledRect(backgroundRect);
  225. g_painter->setColor(fillColor);
  226. g_painter->drawFilledRect(healthRect);
  227. if(drawFlags & Otc::DrawManaBar && isLocalPlayer()) {
  228. LocalPlayerPtr player = g_game.getLocalPlayer();
  229. if(player) {
  230. backgroundRect.moveTop(backgroundRect.bottom());
  231. g_painter->setColor(Color::black);
  232. g_painter->drawFilledRect(backgroundRect);
  233. Rect manaRect = backgroundRect.expanded(-1);
  234. double maxMana = player->getMaxMana();
  235. if(maxMana == 0) {
  236. manaRect.setWidth(25);
  237. } else {
  238. manaRect.setWidth(player->getMana() / (maxMana * 1.0) * 25);
  239. }
  240. g_painter->setColor(Color::blue);
  241. g_painter->drawFilledRect(manaRect);
  242. }
  243. }
  244. }
  245. if(drawFlags & Otc::DrawNames) {
  246. if(g_painter->getColor() != fillColor)
  247. g_painter->setColor(fillColor);
  248. m_nameCache.draw(textRect);
  249. }
  250. if(m_skull != Otc::SkullNone && m_skullTexture) {
  251. g_painter->setColor(Color::white);
  252. Rect skullRect = Rect(backgroundRect.x() + 13.5 + 12, backgroundRect.y() + 5, m_skullTexture->getSize());
  253. g_painter->drawTexturedRect(skullRect, m_skullTexture);
  254. }
  255. if(m_shield != Otc::ShieldNone && m_shieldTexture && m_showShieldTexture) {
  256. g_painter->setColor(Color::white);
  257. Rect shieldRect = Rect(backgroundRect.x() + 13.5, backgroundRect.y() + 5, m_shieldTexture->getSize());
  258. g_painter->drawTexturedRect(shieldRect, m_shieldTexture);
  259. }
  260. if(m_emblem != Otc::EmblemNone && m_emblemTexture) {
  261. g_painter->setColor(Color::white);
  262. Rect emblemRect = Rect(backgroundRect.x() + 13.5 + 12, backgroundRect.y() + 16, m_emblemTexture->getSize());
  263. g_painter->drawTexturedRect(emblemRect, m_emblemTexture);
  264. }
  265. if(m_type != Proto::CreatureTypeUnknown && m_typeTexture) {
  266. g_painter->setColor(Color::white);
  267. Rect typeRect = Rect(backgroundRect.x() + 13.5 + 12 + 12, backgroundRect.y() + 16, m_typeTexture->getSize());
  268. g_painter->drawTexturedRect(typeRect, m_typeTexture);
  269. }
  270. if(m_icon != Otc::NpcIconNone && m_iconTexture) {
  271. g_painter->setColor(Color::white);
  272. Rect iconRect = Rect(backgroundRect.x() + 13.5 + 12, backgroundRect.y() + 5, m_iconTexture->getSize());
  273. g_painter->drawTexturedRect(iconRect, m_iconTexture);
  274. }
  275. }
  276. void Creature::turn(Otc::Direction direction)
  277. {
  278. // if is not walking change the direction right away
  279. if(!m_walking)
  280. setDirection(direction);
  281. // schedules to set the new direction when walk ends
  282. else
  283. m_walkTurnDirection = direction;
  284. }
  285. void Creature::walk(const Position& oldPos, const Position& newPos)
  286. {
  287. if(oldPos == newPos)
  288. return;
  289. // get walk direction
  290. m_lastStepDirection = oldPos.getDirectionFromPosition(newPos);
  291. m_lastStepFromPosition = oldPos;
  292. m_lastStepToPosition = newPos;
  293. // set current walking direction
  294. setDirection(m_lastStepDirection);
  295. // starts counting walk
  296. m_walking = true;
  297. m_walkTimer.restart();
  298. m_walkedPixels = 0;
  299. if(m_walkFinishAnimEvent) {
  300. m_walkFinishAnimEvent->cancel();
  301. m_walkFinishAnimEvent = nullptr;
  302. }
  303. // no direction need to be changed when the walk ends
  304. m_walkTurnDirection = Otc::InvalidDirection;
  305. // starts updating walk
  306. nextWalkUpdate();
  307. }
  308. void Creature::stopWalk()
  309. {
  310. if(!m_walking)
  311. return;
  312. // stops the walk right away
  313. terminateWalk();
  314. }
  315. void Creature::jump(int height, int duration)
  316. {
  317. if(!m_jumpOffset.isNull())
  318. return;
  319. m_jumpTimer.restart();
  320. m_jumpHeight = height;
  321. m_jumpDuration = duration;
  322. updateJump();
  323. }
  324. void Creature::updateJump()
  325. {
  326. int t = m_jumpTimer.ticksElapsed();
  327. double a = -4 * m_jumpHeight / (m_jumpDuration * m_jumpDuration);
  328. double b = +4 * m_jumpHeight / (m_jumpDuration);
  329. double height = a*t*t + b*t;
  330. int roundHeight = stdext::round(height);
  331. int halfJumpDuration = m_jumpDuration / 2;
  332. // schedules next update
  333. if(m_jumpTimer.ticksElapsed() < m_jumpDuration) {
  334. m_jumpOffset = PointF(height, height);
  335. int diff = 0;
  336. if(m_jumpTimer.ticksElapsed() < halfJumpDuration)
  337. diff = 1;
  338. else if(m_jumpTimer.ticksElapsed() > halfJumpDuration)
  339. diff = -1;
  340. int nextT, i = 1;
  341. do {
  342. nextT = stdext::round((-b + std::sqrt(std::max<int>(b*b + 4*a*(roundHeight+diff*i), 0.0)) * diff) / (2*a));
  343. ++i;
  344. if(nextT < halfJumpDuration)
  345. diff = 1;
  346. else if(nextT > halfJumpDuration)
  347. diff = -1;
  348. } while(nextT - m_jumpTimer.ticksElapsed() == 0 && i < 3);
  349. auto self = static_self_cast<Creature>();
  350. g_dispatcher.scheduleEvent([self] {
  351. self->updateJump();
  352. }, nextT - m_jumpTimer.ticksElapsed());
  353. }
  354. else
  355. m_jumpOffset = PointF(0, 0);
  356. }
  357. void Creature::onPositionChange(const Position& newPos, const Position& oldPos)
  358. {
  359. callLuaField("onPositionChange", newPos, oldPos);
  360. }
  361. void Creature::onAppear()
  362. {
  363. // cancel any disappear event
  364. if(m_disappearEvent) {
  365. m_disappearEvent->cancel();
  366. m_disappearEvent = nullptr;
  367. }
  368. // creature appeared the first time or wasn't seen for a long time
  369. if(m_removed) {
  370. stopWalk();
  371. m_removed = false;
  372. callLuaField("onAppear");
  373. // walk
  374. } else if(m_oldPosition != m_position && m_oldPosition.isInRange(m_position,1,1) && m_allowAppearWalk) {
  375. m_allowAppearWalk = false;
  376. walk(m_oldPosition, m_position);
  377. callLuaField("onWalk", m_oldPosition, m_position);
  378. // teleport
  379. } else if(m_oldPosition != m_position) {
  380. stopWalk();
  381. callLuaField("onDisappear");
  382. callLuaField("onAppear");
  383. } // else turn
  384. }
  385. void Creature::onDisappear()
  386. {
  387. if(m_disappearEvent)
  388. m_disappearEvent->cancel();
  389. m_oldPosition = m_position;
  390. // a pair onDisappear and onAppear events are fired even when creatures walks or turns,
  391. // so we must filter
  392. auto self = static_self_cast<Creature>();
  393. m_disappearEvent = g_dispatcher.addEvent([self] {
  394. self->m_removed = true;
  395. self->stopWalk();
  396. self->callLuaField("onDisappear");
  397. // invalidate this creature position
  398. if(!self->isLocalPlayer())
  399. self->setPosition(Position());
  400. self->m_oldPosition = Position();
  401. self->m_disappearEvent = nullptr;
  402. });
  403. }
  404. void Creature::onDeath()
  405. {
  406. callLuaField("onDeath");
  407. }
  408. void Creature::updateWalkAnimation(int totalPixelsWalked)
  409. {
  410. // update outfit animation
  411. if(m_outfit.getCategory() != ThingCategoryCreature)
  412. return;
  413. int footAnimPhases = getAnimationPhases() - 1;
  414. int footDelay = getStepDuration(true) / 3;
  415. // Since mount is a different outfit we need to get the mount animation phases
  416. if(m_outfit.getMount() != 0) {
  417. ThingType *type = g_things.rawGetThingType(m_outfit.getMount(), m_outfit.getCategory());
  418. footAnimPhases = type->getAnimationPhases() - 1;
  419. }
  420. if(footAnimPhases == 0)
  421. m_walkAnimationPhase = 0;
  422. else if(m_footStepDrawn && m_footTimer.ticksElapsed() >= footDelay && totalPixelsWalked < 32) {
  423. m_footStep++;
  424. m_walkAnimationPhase = 1 + (m_footStep % footAnimPhases);
  425. m_footStepDrawn = false;
  426. m_footTimer.restart();
  427. } else if(m_walkAnimationPhase == 0 && totalPixelsWalked < 32) {
  428. m_walkAnimationPhase = 1 + (m_footStep % footAnimPhases);
  429. }
  430. if(totalPixelsWalked == 32 && !m_walkFinishAnimEvent) {
  431. auto self = static_self_cast<Creature>();
  432. m_walkFinishAnimEvent = g_dispatcher.scheduleEvent([self] {
  433. if(!self->m_walking || self->m_walkTimer.ticksElapsed() >= self->getStepDuration(true))
  434. self->m_walkAnimationPhase = 0;
  435. self->m_walkFinishAnimEvent = nullptr;
  436. }, std::min<int>(footDelay, 200));
  437. }
  438. }
  439. void Creature::updateWalkOffset(int totalPixelsWalked)
  440. {
  441. m_walkOffset = Point(0,0);
  442. if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
  443. m_walkOffset.y = 32 - totalPixelsWalked;
  444. else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
  445. m_walkOffset.y = totalPixelsWalked - 32;
  446. if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
  447. m_walkOffset.x = totalPixelsWalked - 32;
  448. else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
  449. m_walkOffset.x = 32 - totalPixelsWalked;
  450. }
  451. void Creature::updateWalkingTile()
  452. {
  453. // determine new walking tile
  454. TilePtr newWalkingTile;
  455. Rect virtualCreatureRect(Otc::TILE_PIXELS + (m_walkOffset.x - getDisplacementX()),
  456. Otc::TILE_PIXELS + (m_walkOffset.y - getDisplacementY()),
  457. Otc::TILE_PIXELS, Otc::TILE_PIXELS);
  458. for(int xi = -1; xi <= 1 && !newWalkingTile; ++xi) {
  459. for(int yi = -1; yi <= 1 && !newWalkingTile; ++yi) {
  460. Rect virtualTileRect((xi+1)*Otc::TILE_PIXELS, (yi+1)*Otc::TILE_PIXELS, Otc::TILE_PIXELS, Otc::TILE_PIXELS);
  461. // only render creatures where bottom right is inside tile rect
  462. if(virtualTileRect.contains(virtualCreatureRect.bottomRight())) {
  463. newWalkingTile = g_map.getOrCreateTile(m_position.translated(xi, yi, 0));
  464. }
  465. }
  466. }
  467. if(newWalkingTile != m_walkingTile) {
  468. if(m_walkingTile)
  469. m_walkingTile->removeWalkingCreature(static_self_cast<Creature>());
  470. if(newWalkingTile) {
  471. newWalkingTile->addWalkingCreature(static_self_cast<Creature>());
  472. // recache visible tiles in map views
  473. if(newWalkingTile->isEmpty())
  474. g_map.notificateTileUpdate(newWalkingTile->getPosition());
  475. }
  476. m_walkingTile = newWalkingTile;
  477. }
  478. }
  479. void Creature::nextWalkUpdate()
  480. {
  481. // remove any previous scheduled walk updates
  482. if(m_walkUpdateEvent)
  483. m_walkUpdateEvent->cancel();
  484. // do the update
  485. updateWalk();
  486. // schedules next update
  487. if(m_walking) {
  488. auto self = static_self_cast<Creature>();
  489. m_walkUpdateEvent = g_dispatcher.scheduleEvent([self] {
  490. self->m_walkUpdateEvent = nullptr;
  491. self->nextWalkUpdate();
  492. }, getStepDuration() / 32);
  493. }
  494. }
  495. void Creature::updateWalk()
  496. {
  497. float walkTicksPerPixel = getStepDuration(true) / 32;
  498. int totalPixelsWalked = std::min<int>(m_walkTimer.ticksElapsed() / walkTicksPerPixel, 32.0f);
  499. // needed for paralyze effect
  500. m_walkedPixels = std::max<int>(m_walkedPixels, totalPixelsWalked);
  501. // update walk animation and offsets
  502. updateWalkAnimation(totalPixelsWalked);
  503. updateWalkOffset(m_walkedPixels);
  504. updateWalkingTile();
  505. // terminate walk
  506. if(m_walking && m_walkTimer.ticksElapsed() >= getStepDuration())
  507. terminateWalk();
  508. }
  509. void Creature::terminateWalk()
  510. {
  511. // remove any scheduled walk update
  512. if(m_walkUpdateEvent) {
  513. m_walkUpdateEvent->cancel();
  514. m_walkUpdateEvent = nullptr;
  515. }
  516. // now the walk has ended, do any scheduled turn
  517. if(m_walkTurnDirection != Otc::InvalidDirection) {
  518. setDirection(m_walkTurnDirection);
  519. m_walkTurnDirection = Otc::InvalidDirection;
  520. }
  521. if(m_walkingTile) {
  522. m_walkingTile->removeWalkingCreature(static_self_cast<Creature>());
  523. m_walkingTile = nullptr;
  524. }
  525. m_walking = false;
  526. m_walkedPixels = 0;
  527. // reset walk animation states
  528. m_walkOffset = Point(0,0);
  529. m_walkAnimationPhase = 0;
  530. }
  531. void Creature::setName(const std::string& name)
  532. {
  533. m_nameCache.setText(name);
  534. m_name = name;
  535. }
  536. void Creature::setHealthPercent(uint8 healthPercent)
  537. {
  538. if(healthPercent > 92)
  539. m_informationColor = Color(0x00, 0xBC, 0x00);
  540. else if(healthPercent > 60)
  541. m_informationColor = Color(0x50, 0xA1, 0x50);
  542. else if(healthPercent > 30)
  543. m_informationColor = Color(0xA1, 0xA1, 0x00);
  544. else if(healthPercent > 8)
  545. m_informationColor = Color(0xBF, 0x0A, 0x0A);
  546. else if(healthPercent > 3)
  547. m_informationColor = Color(0x91, 0x0F, 0x0F);
  548. else
  549. m_informationColor = Color(0x85, 0x0C, 0x0C);
  550. m_healthPercent = healthPercent;
  551. callLuaField("onHealthPercentChange", healthPercent);
  552. if(healthPercent <= 0)
  553. onDeath();
  554. }
  555. void Creature::setDirection(Otc::Direction direction)
  556. {
  557. assert(direction != Otc::InvalidDirection);
  558. m_direction = direction;
  559. }
  560. void Creature::setOutfit(const Outfit& outfit)
  561. {
  562. Outfit oldOutfit = m_outfit;
  563. if(outfit.getCategory() != ThingCategoryCreature) {
  564. if(!g_things.isValidDatId(outfit.getAuxId(), outfit.getCategory()))
  565. return;
  566. m_outfit.setAuxId(outfit.getAuxId());
  567. m_outfit.setCategory(outfit.getCategory());
  568. } else {
  569. if(outfit.getId() > 0 && !g_things.isValidDatId(outfit.getId(), ThingCategoryCreature))
  570. return;
  571. m_outfit = outfit;
  572. }
  573. m_walkAnimationPhase = 0; // might happen when player is walking and outfit is changed.
  574. callLuaField("onOutfitChange", m_outfit, oldOutfit);
  575. }
  576. void Creature::setOutfitColor(const Color& color, int duration)
  577. {
  578. if(m_outfitColorUpdateEvent) {
  579. m_outfitColorUpdateEvent->cancel();
  580. m_outfitColorUpdateEvent = nullptr;
  581. }
  582. if(duration > 0) {
  583. Color delta = (color - m_outfitColor) / (float)duration;
  584. m_outfitColorTimer.restart();
  585. updateOutfitColor(m_outfitColor, color, delta, duration);
  586. }
  587. else
  588. m_outfitColor = color;
  589. }
  590. void Creature::updateOutfitColor(Color color, Color finalColor, Color delta, int duration)
  591. {
  592. if(m_outfitColorTimer.ticksElapsed() < duration) {
  593. m_outfitColor = color + delta * m_outfitColorTimer.ticksElapsed();
  594. auto self = static_self_cast<Creature>();
  595. m_outfitColorUpdateEvent = g_dispatcher.scheduleEvent([=] {
  596. self->updateOutfitColor(color, finalColor, delta, duration);
  597. }, 100);
  598. }
  599. else {
  600. m_outfitColor = finalColor;
  601. }
  602. }
  603. void Creature::setSpeed(uint16 speed)
  604. {
  605. uint16 oldSpeed = m_speed;
  606. m_speed = speed;
  607. // speed can change while walking (utani hur, paralyze, etc..)
  608. if(m_walking)
  609. nextWalkUpdate();
  610. callLuaField("onSpeedChange", m_speed, oldSpeed);
  611. }
  612. void Creature::setBaseSpeed(double baseSpeed)
  613. {
  614. if(m_baseSpeed != baseSpeed) {
  615. double oldBaseSpeed = m_baseSpeed;
  616. m_baseSpeed = baseSpeed;
  617. callLuaField("onBaseSpeedChange", baseSpeed, oldBaseSpeed);
  618. }
  619. }
  620. void Creature::setSkull(uint8 skull)
  621. {
  622. m_skull = skull;
  623. callLuaField("onSkullChange", m_skull);
  624. }
  625. void Creature::setShield(uint8 shield)
  626. {
  627. m_shield = shield;
  628. callLuaField("onShieldChange", m_shield);
  629. }
  630. void Creature::setEmblem(uint8 emblem)
  631. {
  632. m_emblem = emblem;
  633. callLuaField("onEmblemChange", m_emblem);
  634. }
  635. void Creature::setType(uint8 type)
  636. {
  637. m_type = type;
  638. callLuaField("onTypeChange", m_type);
  639. }
  640. void Creature::setIcon(uint8 icon)
  641. {
  642. m_icon = icon;
  643. callLuaField("onIconChange", m_icon);
  644. }
  645. void Creature::setSkullTexture(const std::string& filename)
  646. {
  647. m_skullTexture = g_textures.getTexture(filename);
  648. }
  649. void Creature::setShieldTexture(const std::string& filename, bool blink)
  650. {
  651. m_shieldTexture = g_textures.getTexture(filename);
  652. m_showShieldTexture = true;
  653. if(blink && !m_shieldBlink) {
  654. auto self = static_self_cast<Creature>();
  655. g_dispatcher.scheduleEvent([self]() {
  656. self->updateShield();
  657. }, SHIELD_BLINK_TICKS);
  658. }
  659. m_shieldBlink = blink;
  660. }
  661. void Creature::setEmblemTexture(const std::string& filename)
  662. {
  663. m_emblemTexture = g_textures.getTexture(filename);
  664. }
  665. void Creature::setTypeTexture(const std::string& filename)
  666. {
  667. m_typeTexture = g_textures.getTexture(filename);
  668. }
  669. void Creature::setIconTexture(const std::string& filename)
  670. {
  671. m_iconTexture = g_textures.getTexture(filename);
  672. }
  673. void Creature::setSpeedFormula(double speedA, double speedB, double speedC)
  674. {
  675. m_speedFormula[Otc::SpeedFormulaA] = speedA;
  676. m_speedFormula[Otc::SpeedFormulaB] = speedB;
  677. m_speedFormula[Otc::SpeedFormulaC] = speedC;
  678. }
  679. bool Creature::hasSpeedFormula()
  680. {
  681. return m_speedFormula[Otc::SpeedFormulaA] != -1 && m_speedFormula[Otc::SpeedFormulaB] != -1
  682. && m_speedFormula[Otc::SpeedFormulaC] != -1;
  683. }
  684. void Creature::addTimedSquare(uint8 color)
  685. {
  686. m_showTimedSquare = true;
  687. m_timedSquareColor = Color::from8bit(color);
  688. // schedule removal
  689. auto self = static_self_cast<Creature>();
  690. g_dispatcher.scheduleEvent([self]() {
  691. self->removeTimedSquare();
  692. }, VOLATILE_SQUARE_DURATION);
  693. }
  694. void Creature::updateShield()
  695. {
  696. m_showShieldTexture = !m_showShieldTexture;
  697. if(m_shield != Otc::ShieldNone && m_shieldBlink) {
  698. auto self = static_self_cast<Creature>();
  699. g_dispatcher.scheduleEvent([self]() {
  700. self->updateShield();
  701. }, SHIELD_BLINK_TICKS);
  702. }
  703. else if(!m_shieldBlink)
  704. m_showShieldTexture = true;
  705. }
  706. Point Creature::getDrawOffset()
  707. {
  708. Point drawOffset;
  709. if(m_walking) {
  710. if(m_walkingTile)
  711. drawOffset -= Point(1,1) * m_walkingTile->getDrawElevation();
  712. drawOffset += m_walkOffset;
  713. } else {
  714. const TilePtr& tile = getTile();
  715. if(tile)
  716. drawOffset -= Point(1,1) * tile->getDrawElevation();
  717. }
  718. return drawOffset;
  719. }
  720. int Creature::getStepDuration(bool ignoreDiagonal, Otc::Direction dir)
  721. {
  722. int speed = m_speed;
  723. if(speed < 1)
  724. return 0;
  725. if(g_game.getFeature(Otc::GameNewSpeedLaw))
  726. speed *= 2;
  727. int groundSpeed = 0;
  728. Position tilePos;
  729. if(dir == Otc::InvalidDirection)
  730. tilePos = m_lastStepToPosition;
  731. else
  732. tilePos = m_position.translatedToDirection(dir);
  733. if(!tilePos.isValid())
  734. tilePos = m_position;
  735. const TilePtr& tile = g_map.getTile(tilePos);
  736. if(tile) {
  737. groundSpeed = tile->getGroundSpeed();
  738. if(groundSpeed == 0)
  739. groundSpeed = 150;
  740. }
  741. int interval = 1000;
  742. if(groundSpeed > 0 && speed > 0)
  743. interval = 1000 * groundSpeed;
  744. if(g_game.getFeature(Otc::GameNewSpeedLaw) && hasSpeedFormula()) {
  745. int formulatedSpeed = 1;
  746. if(speed > -m_speedFormula[Otc::SpeedFormulaB]) {
  747. formulatedSpeed = std::max<int>(1, (int)floor((m_speedFormula[Otc::SpeedFormulaA] * log((speed / 2)
  748. + m_speedFormula[Otc::SpeedFormulaB]) + m_speedFormula[Otc::SpeedFormulaC]) + 0.5));
  749. }
  750. interval = std::floor(interval / (double)formulatedSpeed);
  751. }
  752. else
  753. interval /= speed;
  754. if(g_game.getClientVersion() >= 900)
  755. interval = (interval / g_game.getServerBeat()) * g_game.getServerBeat();
  756. float factor = 3;
  757. if(g_game.getClientVersion() <= 810)
  758. factor = 2;
  759. interval = std::max<int>(interval, g_game.getServerBeat());
  760. if(!ignoreDiagonal && (m_lastStepDirection == Otc::NorthWest || m_lastStepDirection == Otc::NorthEast ||
  761. m_lastStepDirection == Otc::SouthWest || m_lastStepDirection == Otc::SouthEast))
  762. interval *= factor;
  763. return interval;
  764. }
  765. Point Creature::getDisplacement()
  766. {
  767. if(m_outfit.getCategory() == ThingCategoryEffect)
  768. return Point(8, 8);
  769. else if(m_outfit.getCategory() == ThingCategoryItem)
  770. return Point(0, 0);
  771. return Thing::getDisplacement();
  772. }
  773. int Creature::getDisplacementX()
  774. {
  775. if(m_outfit.getCategory() == ThingCategoryEffect)
  776. return 8;
  777. else if(m_outfit.getCategory() == ThingCategoryItem)
  778. return 0;
  779. if(m_outfit.getMount() != 0) {
  780. auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
  781. return datType->getDisplacementX();
  782. }
  783. return Thing::getDisplacementX();
  784. }
  785. int Creature::getDisplacementY()
  786. {
  787. if(m_outfit.getCategory() == ThingCategoryEffect)
  788. return 8;
  789. else if(m_outfit.getCategory() == ThingCategoryItem)
  790. return 0;
  791. if(m_outfit.getMount() != 0) {
  792. auto datType = g_things.rawGetThingType(m_outfit.getMount(), ThingCategoryCreature);
  793. return datType->getDisplacementY();
  794. }
  795. return Thing::getDisplacementY();
  796. }
  797. int Creature::getExactSize(int layer, int xPattern, int yPattern, int zPattern, int animationPhase)
  798. {
  799. int exactSize = 0;
  800. animationPhase = 0;
  801. xPattern = Otc::South;
  802. zPattern = 0;
  803. if(m_outfit.getMount() != 0)
  804. zPattern = 1;
  805. for(yPattern = 0; yPattern < getNumPatternY(); yPattern++) {
  806. if(yPattern > 0 && !(m_outfit.getAddons() & (1 << (yPattern-1))))
  807. continue;
  808. for(layer = 0; layer < getLayers(); ++layer)
  809. exactSize = std::max<int>(exactSize, Thing::getExactSize(layer, xPattern, yPattern, zPattern, animationPhase));
  810. }
  811. return exactSize;
  812. }
  813. const ThingTypePtr& Creature::getThingType()
  814. {
  815. return g_things.getThingType(m_outfit.getId(), ThingCategoryCreature);
  816. }
  817. ThingType* Creature::rawGetThingType()
  818. {
  819. return g_things.rawGetThingType(m_outfit.getId(), ThingCategoryCreature);
  820. }