BattleStacksController.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * BattleStacksController.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleStacksController.h"
  12. #include "BattleSiegeController.h"
  13. #include "BattleInterfaceClasses.h"
  14. #include "BattleInterface.h"
  15. #include "BattleAnimationClasses.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleEffectsController.h"
  18. #include "BattleProjectileController.h"
  19. #include "BattleControlPanel.h"
  20. #include "BattleRenderer.h"
  21. #include "CreatureAnimation.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../CMusicHandler.h"
  24. #include "../CGameInfo.h"
  25. #include "../gui/CAnimation.h"
  26. #include "../gui/CGuiHandler.h"
  27. #include "../gui/Canvas.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/battle/BattleHex.h"
  30. #include "../../lib/CGameState.h"
  31. #include "../../lib/CStack.h"
  32. #include "../../lib/CondSh.h"
  33. static void onAnimationFinished(const CStack *stack, std::weak_ptr<CreatureAnimation> anim)
  34. {
  35. std::shared_ptr<CreatureAnimation> animation = anim.lock();
  36. if(!animation)
  37. return;
  38. if (animation->isIdle())
  39. {
  40. const CCreature *creature = stack->getCreature();
  41. if (animation->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
  42. {
  43. if (CRandomGenerator::getDefault().nextDouble(99.0) < creature->animation.timeBetweenFidgets *10)
  44. animation->playOnce(ECreatureAnimType::MOUSEON);
  45. else
  46. animation->setType(ECreatureAnimType::HOLDING);
  47. }
  48. else
  49. {
  50. animation->setType(ECreatureAnimType::HOLDING);
  51. }
  52. }
  53. // always reset callback
  54. animation->onAnimationReset += std::bind(&onAnimationFinished, stack, anim);
  55. }
  56. BattleStacksController::BattleStacksController(BattleInterface & owner):
  57. owner(owner),
  58. activeStack(nullptr),
  59. mouseHoveredStack(nullptr),
  60. stackToActivate(nullptr),
  61. selectedStack(nullptr),
  62. stackCanCastSpell(false),
  63. creatureSpellToCast(uint32_t(-1)),
  64. animIDhelper(0)
  65. {
  66. //preparing graphics for displaying amounts of creatures
  67. amountNormal = IImage::createFromFile("CMNUMWIN.BMP");
  68. amountPositive = IImage::createFromFile("CMNUMWIN.BMP");
  69. amountNegative = IImage::createFromFile("CMNUMWIN.BMP");
  70. amountEffNeutral = IImage::createFromFile("CMNUMWIN.BMP");
  71. static const ColorShifterMultiplyAndAddExcept shifterNormal ({150, 50, 255, 255}, {0,0,0,0}, {255, 231, 132, 255});
  72. static const ColorShifterMultiplyAndAddExcept shifterPositive({ 50, 255, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  73. static const ColorShifterMultiplyAndAddExcept shifterNegative({255, 50, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  74. static const ColorShifterMultiplyAndAddExcept shifterNeutral ({255, 255, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  75. amountNormal->adjustPalette(&shifterNormal);
  76. amountPositive->adjustPalette(&shifterPositive);
  77. amountNegative->adjustPalette(&shifterNegative);
  78. amountEffNeutral->adjustPalette(&shifterNeutral);
  79. std::vector<const CStack*> stacks = owner.curInt->cb->battleGetAllStacks(true);
  80. for(const CStack * s : stacks)
  81. {
  82. stackAdded(s);
  83. }
  84. }
  85. BattleHex BattleStacksController::getStackCurrentPosition(const CStack * stack) const
  86. {
  87. if ( !stackAnimation.at(stack->ID)->isMoving())
  88. return stack->getPosition();
  89. if (stack->hasBonusOfType(Bonus::FLYING))
  90. return BattleHex::HEX_AFTER_ALL;
  91. for (auto & anim : currentAnimations)
  92. {
  93. // certainly ugly workaround but fixes quite annoying bug
  94. // stack position will be updated only *after* movement is finished
  95. // before this - stack is always at its initial position. Thus we need to find
  96. // its current position. Which can be found only in this class
  97. if (CStackMoveAnimation *move = dynamic_cast<CStackMoveAnimation*>(anim))
  98. {
  99. if (move->stack == stack)
  100. return move->currentHex;
  101. }
  102. }
  103. return stack->getPosition();
  104. }
  105. void BattleStacksController::collectRenderableObjects(BattleRenderer & renderer)
  106. {
  107. auto stacks = owner.curInt->cb->battleGetAllStacks(false);
  108. for (auto stack : stacks)
  109. {
  110. if (stackAnimation.find(stack->ID) == stackAnimation.end()) //e.g. for summoned but not yet handled stacks
  111. continue;
  112. //FIXME: hack to ignore ghost stacks
  113. if ((stackAnimation[stack->ID]->getType() == ECreatureAnimType::DEAD || stackAnimation[stack->ID]->getType() == ECreatureAnimType::HOLDING) && stack->isGhost())
  114. continue;
  115. auto layer = stackAnimation[stack->ID]->isDead() ? EBattleFieldLayer::CORPSES : EBattleFieldLayer::STACKS;
  116. auto location = getStackCurrentPosition(stack);
  117. renderer.insert(layer, location, [this, stack]( BattleRenderer::RendererRef renderer ){
  118. showStack(renderer, stack);
  119. });
  120. if (stackNeedsAmountBox(stack))
  121. {
  122. renderer.insert(EBattleFieldLayer::STACK_AMOUNTS, location, [this, stack]( BattleRenderer::RendererRef renderer ){
  123. showStackAmountBox(renderer, stack);
  124. });
  125. }
  126. }
  127. }
  128. void BattleStacksController::stackReset(const CStack * stack)
  129. {
  130. auto iter = stackAnimation.find(stack->ID);
  131. if(iter == stackAnimation.end())
  132. {
  133. logGlobal->error("Unit %d have no animation", stack->ID);
  134. return;
  135. }
  136. auto animation = iter->second;
  137. if(stack->alive() && animation->isDeadOrDying())
  138. animation->setType(ECreatureAnimType::HOLDING);
  139. static const ColorShifterMultiplyAndAdd shifterClone ({255, 255, 0, 255}, {0, 0, 255, 0});
  140. if (stack->isClone())
  141. {
  142. animation->shiftColor(&shifterClone);
  143. }
  144. //TODO: handle more cases
  145. }
  146. void BattleStacksController::stackAdded(const CStack * stack)
  147. {
  148. // Tower shooters have only their upper half visible
  149. static const int turretCreatureAnimationHeight = 235;
  150. stackFacingRight[stack->ID] = stack->side == BattleSide::ATTACKER; // must be set before getting stack position
  151. Point coords = getStackPositionAtHex(stack->getPosition(), stack);
  152. if(stack->initialPosition < 0) //turret
  153. {
  154. assert(owner.siegeController);
  155. const CCreature *turretCreature = owner.siegeController->getTurretCreature();
  156. stackAnimation[stack->ID] = AnimationControls::getAnimation(turretCreature);
  157. stackAnimation[stack->ID]->pos.h = turretCreatureAnimationHeight;
  158. coords = owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  159. }
  160. else
  161. {
  162. stackAnimation[stack->ID] = AnimationControls::getAnimation(stack->getCreature());
  163. stackAnimation[stack->ID]->onAnimationReset += std::bind(&onAnimationFinished, stack, stackAnimation[stack->ID]);
  164. stackAnimation[stack->ID]->pos.h = stackAnimation[stack->ID]->getHeight();
  165. }
  166. stackAnimation[stack->ID]->pos.x = coords.x;
  167. stackAnimation[stack->ID]->pos.y = coords.y;
  168. stackAnimation[stack->ID]->pos.w = stackAnimation[stack->ID]->getWidth();
  169. stackAnimation[stack->ID]->setType(ECreatureAnimType::HOLDING);
  170. }
  171. void BattleStacksController::setActiveStack(const CStack *stack)
  172. {
  173. if (activeStack) // update UI
  174. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  175. activeStack = stack;
  176. if (activeStack) // update UI
  177. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getGoldBorder());
  178. owner.controlPanel->blockUI(activeStack == nullptr);
  179. }
  180. void BattleStacksController::setHoveredStack(const CStack *stack)
  181. {
  182. if ( stack == mouseHoveredStack )
  183. return;
  184. if (mouseHoveredStack)
  185. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  186. // stack must be alive and not active (which uses gold border instead)
  187. if (stack && stack->alive() && stack != activeStack)
  188. {
  189. mouseHoveredStack = stack;
  190. if (mouseHoveredStack)
  191. {
  192. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getBlueBorder());
  193. if (stackAnimation[mouseHoveredStack->ID]->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
  194. stackAnimation[mouseHoveredStack->ID]->playOnce(ECreatureAnimType::MOUSEON);
  195. }
  196. }
  197. else
  198. mouseHoveredStack = nullptr;
  199. }
  200. bool BattleStacksController::stackNeedsAmountBox(const CStack * stack) const
  201. {
  202. BattleHex currentActionTarget;
  203. if(owner.curInt->curAction)
  204. {
  205. auto target = owner.curInt->curAction->getTarget(owner.curInt->cb.get());
  206. if(!target.empty())
  207. currentActionTarget = target.at(0).hexValue;
  208. }
  209. if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON) && stack->getCount() == 1) //do not show box for singular war machines, stacked war machines with box shown are supported as extension feature
  210. return false;
  211. if (!owner.battleActionsStarted) // do not perform any further checks since they are related to actions that will only occur after intro music
  212. return true;
  213. if(!stack->alive())
  214. return false;
  215. if(stack->getCount() == 0) //hide box when target is going to die anyway - do not display "0 creatures"
  216. return false;
  217. for(auto anim : currentAnimations) //no matter what other conditions below are, hide box when creature is playing hit animation
  218. {
  219. auto hitAnimation = dynamic_cast<CDefenceAnimation*>(anim);
  220. if(hitAnimation && (hitAnimation->stack->ID == stack->ID))
  221. return false;
  222. }
  223. if(owner.curInt->curAction)
  224. {
  225. if(owner.curInt->curAction->stackNumber == stack->ID) //stack is currently taking action (is not a target of another creature's action etc)
  226. {
  227. if(owner.curInt->curAction->actionType == EActionType::WALK || owner.curInt->curAction->actionType == EActionType::SHOOT) //hide when stack walks or shoots
  228. return false;
  229. else if(owner.curInt->curAction->actionType == EActionType::WALK_AND_ATTACK && currentActionTarget != stack->getPosition()) //when attacking, hide until walk phase finished
  230. return false;
  231. }
  232. if(owner.curInt->curAction->actionType == EActionType::SHOOT && currentActionTarget == stack->getPosition()) //hide if we are ranged attack target
  233. return false;
  234. }
  235. return true;
  236. }
  237. std::shared_ptr<IImage> BattleStacksController::getStackAmountBox(const CStack * stack)
  238. {
  239. std::vector<si32> activeSpells = stack->activeSpells();
  240. if ( activeSpells.empty())
  241. return amountNormal;
  242. int effectsPositivness = 0;
  243. for ( auto const & spellID : activeSpells)
  244. effectsPositivness += CGI->spellh->objects.at(spellID)->positiveness;
  245. if (effectsPositivness > 0)
  246. return amountPositive;
  247. if (effectsPositivness < 0)
  248. return amountNegative;
  249. return amountEffNeutral;
  250. }
  251. void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack * stack)
  252. {
  253. //blitting amount background box
  254. auto amountBG = getStackAmountBox(stack);
  255. const int sideShift = stack->side == BattleSide::ATTACKER ? 1 : -1;
  256. const int reverseSideShift = stack->side == BattleSide::ATTACKER ? -1 : 1;
  257. const BattleHex nextPos = stack->getPosition() + sideShift;
  258. const bool edge = stack->getPosition() % GameConstants::BFIELD_WIDTH == (stack->side == BattleSide::ATTACKER ? GameConstants::BFIELD_WIDTH - 2 : 1);
  259. const bool moveInside = !edge && !owner.fieldController->stackCountOutsideHex(nextPos);
  260. int xAdd = (stack->side == BattleSide::ATTACKER ? 220 : 202) +
  261. (stack->doubleWide() ? 44 : 0) * sideShift +
  262. (moveInside ? amountBG->width() + 10 : 0) * reverseSideShift;
  263. int yAdd = 260 + ((stack->side == BattleSide::ATTACKER || moveInside) ? 0 : -15);
  264. canvas.draw(amountBG, stackAnimation[stack->ID]->pos.topLeft() + Point(xAdd, yAdd));
  265. //blitting amount
  266. Point textPos = stackAnimation[stack->ID]->pos.topLeft() + amountBG->dimensions()/2 + Point(xAdd, yAdd);
  267. canvas.drawText(textPos, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, makeNumberShort(stack->getCount()));
  268. }
  269. void BattleStacksController::showStack(Canvas & canvas, const CStack * stack)
  270. {
  271. stackAnimation[stack->ID]->nextFrame(canvas, facingRight(stack)); // do actual blit
  272. stackAnimation[stack->ID]->incrementFrame(float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000);
  273. }
  274. void BattleStacksController::updateBattleAnimations()
  275. {
  276. for (auto & elem : currentAnimations)
  277. {
  278. if (!elem)
  279. continue;
  280. if (elem->isInitialized())
  281. elem->nextFrame();
  282. else
  283. elem->tryInitialize();
  284. }
  285. bool hadAnimations = !currentAnimations.empty();
  286. vstd::erase(currentAnimations, nullptr);
  287. if (hadAnimations && currentAnimations.empty())
  288. {
  289. //anims ended
  290. owner.controlPanel->blockUI(activeStack == nullptr);
  291. owner.animsAreDisplayed.setn(false);
  292. }
  293. }
  294. void BattleStacksController::addNewAnim(CBattleAnimation *anim)
  295. {
  296. currentAnimations.push_back(anim);
  297. owner.animsAreDisplayed.setn(true);
  298. }
  299. void BattleStacksController::stackActivated(const CStack *stack) //TODO: check it all before game state is changed due to abilities
  300. {
  301. stackToActivate = stack;
  302. owner.waitForAnims();
  303. if (stackToActivate) //during waiting stack may have gotten activated through show
  304. owner.activateStack();
  305. }
  306. void BattleStacksController::stackRemoved(uint32_t stackID)
  307. {
  308. if (getActiveStack() && getActiveStack()->ID == stackID)
  309. {
  310. BattleAction *action = new BattleAction();
  311. action->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  312. action->actionType = EActionType::CANCEL;
  313. action->stackNumber = getActiveStack()->ID;
  314. owner.givenCommand.setn(action);
  315. setActiveStack(nullptr);
  316. }
  317. }
  318. void BattleStacksController::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  319. {
  320. for(auto & attackedInfo : attackedInfos)
  321. {
  322. //if (!attackedInfo.cloneKilled) //FIXME: play dead animation for cloned creature before it vanishes
  323. addNewAnim(new CDefenceAnimation(attackedInfo, owner));
  324. if(attackedInfo.rebirth)
  325. {
  326. owner.effectsController->displayEffect(EBattleEffect::RESURRECT, soundBase::RESURECT, attackedInfo.defender->getPosition()); //TODO: play reverse death animation
  327. }
  328. }
  329. owner.waitForAnims();
  330. for (auto & attackedInfo : attackedInfos)
  331. {
  332. if (attackedInfo.rebirth)
  333. stackAnimation[attackedInfo.defender->ID]->setType(ECreatureAnimType::HOLDING);
  334. if (attackedInfo.cloneKilled)
  335. stackRemoved(attackedInfo.defender->ID);
  336. }
  337. }
  338. void BattleStacksController::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance)
  339. {
  340. addNewAnim(new CMovementAnimation(owner, stack, destHex, distance));
  341. owner.waitForAnims();
  342. }
  343. void BattleStacksController::stackAttacking( const CStack *attacker, BattleHex dest, const CStack *attacked, bool shooting )
  344. {
  345. if (shooting)
  346. {
  347. addNewAnim(new CShootingAnimation(owner, attacker, dest, attacked));
  348. }
  349. else
  350. {
  351. addNewAnim(new CMeleeAttackAnimation(owner, attacker, dest, attacked));
  352. }
  353. //waitForAnims();
  354. }
  355. bool BattleStacksController::shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const
  356. {
  357. Point begPosition = getStackPositionAtHex(oldPos,stack);
  358. Point endPosition = getStackPositionAtHex(nextHex, stack);
  359. if((begPosition.x > endPosition.x) && facingRight(stack))
  360. return true;
  361. else if((begPosition.x < endPosition.x) && !facingRight(stack))
  362. return true;
  363. return false;
  364. }
  365. void BattleStacksController::endAction(const BattleAction* action)
  366. {
  367. //check if we should reverse stacks
  368. //for some strange reason, it's not enough
  369. TStacks stacks = owner.curInt->cb->battleGetStacks(CBattleCallback::MINE_AND_ENEMY);
  370. for (const CStack *s : stacks)
  371. {
  372. bool shouldFaceRight = s && s->side == BattleSide::ATTACKER;
  373. if (s && facingRight(s) != shouldFaceRight && s->alive() && stackAnimation[s->ID]->isIdle())
  374. {
  375. addNewAnim(new CReverseAnimation(owner, s, s->getPosition(), false));
  376. }
  377. }
  378. }
  379. void BattleStacksController::startAction(const BattleAction* action)
  380. {
  381. const CStack *stack = owner.curInt->cb->battleGetStackByID(action->stackNumber);
  382. setHoveredStack(nullptr);
  383. auto actionTarget = action->getTarget(owner.curInt->cb.get());
  384. if(action->actionType == EActionType::WALK
  385. || (action->actionType == EActionType::WALK_AND_ATTACK && actionTarget.at(0).hexValue != stack->getPosition()))
  386. {
  387. assert(stack);
  388. owner.moveStarted = true;
  389. if (stackAnimation[action->stackNumber]->framesInGroup(ECreatureAnimType::MOVE_START))
  390. addNewAnim(new CMovementStartAnimation(owner, stack));
  391. //if(shouldRotate(stack, stack->getPosition(), actionTarget.at(0).hexValue))
  392. // addNewAnim(new CReverseAnimation(owner, stack, stack->getPosition(), true));
  393. }
  394. }
  395. void BattleStacksController::activateStack()
  396. {
  397. if ( !currentAnimations.empty())
  398. return;
  399. if ( !stackToActivate)
  400. return;
  401. owner.trySetActivePlayer(stackToActivate->owner);
  402. setActiveStack(stackToActivate);
  403. stackToActivate = nullptr;
  404. const CStack * s = getActiveStack();
  405. if(!s)
  406. return;
  407. //set casting flag to true if creature can use it to not check it every time
  408. const auto spellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  409. const auto randomSpellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::RANDOM_SPELLCASTER));
  410. if(s->canCast() && (spellcaster || randomSpellcaster))
  411. {
  412. stackCanCastSpell = true;
  413. if(randomSpellcaster)
  414. creatureSpellToCast = -1; //spell will be set later on cast
  415. else
  416. creatureSpellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), s, CBattleInfoCallback::RANDOM_AIMED); //faerie dragon can cast only one spell until their next move
  417. //TODO: what if creature can cast BOTH random genie spell and aimed spell?
  418. //TODO: faerie dragon type spell should be selected by server
  419. }
  420. else
  421. {
  422. stackCanCastSpell = false;
  423. creatureSpellToCast = -1;
  424. }
  425. }
  426. void BattleStacksController::setSelectedStack(const CStack *stack)
  427. {
  428. selectedStack = stack;
  429. }
  430. const CStack* BattleStacksController::getSelectedStack() const
  431. {
  432. return selectedStack;
  433. }
  434. const CStack* BattleStacksController::getActiveStack() const
  435. {
  436. return activeStack;
  437. }
  438. bool BattleStacksController::facingRight(const CStack * stack) const
  439. {
  440. return stackFacingRight.at(stack->ID);
  441. }
  442. bool BattleStacksController::activeStackSpellcaster()
  443. {
  444. return stackCanCastSpell;
  445. }
  446. SpellID BattleStacksController::activeStackSpellToCast()
  447. {
  448. if (!stackCanCastSpell)
  449. return SpellID::NONE;
  450. return SpellID(creatureSpellToCast);
  451. }
  452. Point BattleStacksController::getStackPositionAtHex(BattleHex hexNum, const CStack * stack) const
  453. {
  454. Point ret(-500, -500); //returned value
  455. if(stack && stack->initialPosition < 0) //creatures in turrets
  456. return owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  457. static const Point basePos(-190, -139); // position of creature in topleft corner
  458. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  459. ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
  460. ret.y = basePos.y + 42 * hexNum.getY();
  461. if (stack)
  462. {
  463. if(facingRight(stack))
  464. ret.x += imageShiftX;
  465. else
  466. ret.x -= imageShiftX;
  467. //shifting position for double - hex creatures
  468. if(stack->doubleWide())
  469. {
  470. if(stack->side == BattleSide::ATTACKER)
  471. {
  472. if(facingRight(stack))
  473. ret.x -= 44;
  474. }
  475. else
  476. {
  477. if(!facingRight(stack))
  478. ret.x += 44;
  479. }
  480. }
  481. }
  482. //returning
  483. return ret + owner.pos.topLeft();
  484. }