tibia-client/src/otclient/core/creature.cpp

305 lines
9.4 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-08-24 05:58:23 +02:00
#include "creature.h"
2011-08-31 17:03:33 +02:00
#include "thingstype.h"
#include "localplayer.h"
#include "map.h"
2011-08-31 22:22:57 +02:00
#include "tile.h"
#include "item.h"
#include <framework/platform/platform.h>
2011-08-15 16:11:24 +02:00
#include <framework/graphics/graphics.h>
2011-08-29 05:05:57 +02:00
2011-08-15 16:11:24 +02:00
2011-08-31 22:22:57 +02:00
Creature::Creature() : Thing()
2011-08-15 16:11:24 +02:00
{
2011-08-15 23:02:52 +02:00
m_healthPercent = 0;
m_direction = Otc::South;
m_walking = false;
m_walkOffsetX = 0;
m_walkOffsetY = 0;
2011-09-01 00:08:55 +02:00
m_lastWalkAnim = 1;
2011-08-29 02:38:26 +02:00
2011-08-29 05:05:57 +02:00
m_informationFont = g_fonts.getFont("tibia-12px-rounded");
2011-08-15 16:11:24 +02:00
}
void Creature::draw(int x, int y)
{
//dump << (int)m_speed;
// gspeed = 100
// pspeed = 234
// (recorded) 400 - 866 = 466
// (calc by ot eq) 1000 * 100 / 234 = 427
// gspeed = 150
// pspeed = 234
// (recorded) 934 - 1597 = 663
// (calc by ot eq) 1000 * 150 / 234 = 641
// gspeed = 100
// pspeed = 110
// (recorded) 900 - 1833 = 933
// (calc by ot eq) 1000 * 100 / 110 = 909
// 1000 * groundSpeed / playerSpeed
// TODO 1: FIX RENDER STEP 2
2011-08-29 07:54:28 +02:00
// TODO 2: FIX DIAGONAL WALKING, BUGS MAP
// TODO 3: ADD ANIMATION
2011-08-15 16:11:24 +02:00
x += m_walkOffsetX;
y += m_walkOffsetY;
2011-08-31 17:03:33 +02:00
const ThingType& type = getType();
2011-08-29 02:38:26 +02:00
// Render creature
2011-08-31 17:03:33 +02:00
for(m_yPattern = 0; m_yPattern < type.yPattern; m_yPattern++) {
2011-08-24 05:58:23 +02:00
// continue if we dont have this addon.
2011-08-31 15:58:01 +02:00
if(m_yPattern > 0 && !(m_outfit.addons & (1 << (m_yPattern-1))))
2011-08-24 05:58:23 +02:00
continue;
// draw white item
2011-08-28 21:47:23 +02:00
internalDraw(x, y, 0);
2011-08-24 05:58:23 +02:00
// draw mask if exists
2011-08-31 17:03:33 +02:00
if(type.layers > 1) {
2011-08-28 20:06:47 +02:00
// switch to blend color mode
g_graphics.bindBlendFunc(Fw::BlendColorzing);
2011-08-19 14:26:26 +02:00
2011-08-28 20:06:47 +02:00
// head
g_graphics.bindColor(Otc::OutfitColors[m_outfit.head]);
2011-08-28 21:47:23 +02:00
internalDraw(x, y, 1, Otc::SpriteYellowMask);
2011-08-24 05:58:23 +02:00
2011-08-28 20:06:47 +02:00
// body
g_graphics.bindColor(Otc::OutfitColors[m_outfit.body]);
2011-08-28 21:47:23 +02:00
internalDraw(x, y, 1, Otc::SpriteRedMask);
2011-08-24 05:58:23 +02:00
2011-08-28 20:06:47 +02:00
// legs
g_graphics.bindColor(Otc::OutfitColors[m_outfit.legs]);
2011-08-28 21:47:23 +02:00
internalDraw(x, y, 1, Otc::SpriteGreenMask);
2011-08-24 05:58:23 +02:00
2011-08-28 20:06:47 +02:00
// feet
g_graphics.bindColor(Otc::OutfitColors[m_outfit.feet]);
2011-08-28 21:47:23 +02:00
internalDraw(x, y, 1, Otc::SpriteBlueMask);
2011-08-28 20:06:47 +02:00
// restore default blend func
g_graphics.bindBlendFunc(Fw::BlendDefault);
g_graphics.bindColor(Fw::white);
2011-08-24 05:58:23 +02:00
}
2011-08-19 14:26:26 +02:00
}
2011-08-29 02:38:26 +02:00
// Update animation and position
2011-08-31 17:03:33 +02:00
if(m_walking && type.animationPhases > 1) {
2011-08-29 02:38:26 +02:00
if(g_platform.getTicks() - m_lastTicks >= m_walkTimePerPixel) {
2011-08-29 04:45:52 +02:00
int pixelsWalked = std::floor((g_platform.getTicks() - m_lastTicks) / m_walkTimePerPixel);
2011-08-29 02:38:26 +02:00
int remainingTime = (g_platform.getTicks() - m_lastTicks) % (int)m_walkTimePerPixel;
2011-08-29 07:54:28 +02:00
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
2011-08-29 02:38:26 +02:00
m_walkOffsetY = std::max(m_walkOffsetY - pixelsWalked, 0);
2011-08-29 07:54:28 +02:00
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
2011-08-29 02:38:26 +02:00
m_walkOffsetY = std::min(m_walkOffsetY + pixelsWalked, 0);
2011-08-29 07:54:28 +02:00
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = std::min(m_walkOffsetX + pixelsWalked, 0);
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
2011-08-29 02:38:26 +02:00
m_walkOffsetX = std::max(m_walkOffsetX - pixelsWalked, 0);
2011-08-30 02:40:52 +02:00
int walkOffset = std::max(std::abs(m_walkOffsetX), std::abs(m_walkOffsetY));
2011-08-31 17:03:33 +02:00
if(walkOffset % (int)std::ceil(32 / (float)type.animationPhases) == 0) {
2011-09-01 00:08:55 +02:00
if((m_lastWalkAnim+1) % type.animationPhases == 0)
m_lastWalkAnim = 1;
2011-08-29 02:38:26 +02:00
else
2011-09-01 00:08:55 +02:00
m_lastWalkAnim++;
2011-08-30 01:57:00 +02:00
}
2011-09-01 00:08:55 +02:00
m_animation = m_lastWalkAnim;
2011-08-29 02:38:26 +02:00
if(((m_walkOffsetX == 0 && m_walkOffsetY == 0) && m_walkOffsetX != m_walkOffsetY) ||
((m_walkOffsetX == 0 || m_walkOffsetY == 0) && m_walkOffsetX == m_walkOffsetY)) {
2011-08-30 16:37:48 +02:00
cancelWalk(m_direction);
2011-08-29 02:38:26 +02:00
}
m_lastTicks = g_platform.getTicks() - remainingTime;
}
}
}
2011-08-19 15:23:35 +02:00
void Creature::drawInformation(int x, int y, bool useGray)
{
Color fillColor = Color(96, 96, 96);
2011-08-29 05:05:57 +02:00
if(!useGray)
fillColor = m_informationColor;
2011-08-19 15:23:35 +02:00
2011-08-22 03:01:59 +02:00
Rect backgroundRect = Rect(x-(14.5), y, 27, 4);
Rect healthRect = backgroundRect.expanded(-1);
healthRect.setWidth((m_healthPercent/100.0)*25);
2011-08-20 22:30:41 +02:00
g_graphics.bindColor(Fw::black);
g_graphics.drawFilledRect(backgroundRect);
2011-08-20 22:30:41 +02:00
g_graphics.bindColor(fillColor);
g_graphics.drawFilledRect(healthRect);
2011-08-20 22:30:41 +02:00
// name
2011-08-29 05:05:57 +02:00
m_informationFont->renderText(m_name, Rect(x-100, y-15, 200, 15), Fw::AlignTopCenter, fillColor);
2011-08-15 16:11:24 +02:00
}
2011-08-15 23:02:52 +02:00
void Creature::walk(const Position& position)
{
// set walking state
m_walking = true;
m_walkOffsetX = 0;
m_walkOffsetY = 0;
2011-08-29 07:54:28 +02:00
int walkTimeFactor = 1;
// set new direction
2011-08-30 16:37:48 +02:00
if(m_position + Position(0, -1, 0) == position) {
m_direction = Otc::North;
m_walkOffsetY = 32;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(1, 0, 0) == position) {
m_direction = Otc::East;
m_walkOffsetX = -32;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(0, 1, 0) == position) {
m_direction = Otc::South;
m_walkOffsetY = -32;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(-1, 0, 0) == position) {
m_direction = Otc::West;
m_walkOffsetX = 32;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(1, -1, 0) == position) {
2011-08-29 07:54:28 +02:00
m_direction = Otc::NorthEast;
m_walkOffsetX = -32;
m_walkOffsetY = 32;
walkTimeFactor = 2;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(1, 1, 0) == position) {
2011-08-29 07:54:28 +02:00
m_direction = Otc::SouthEast;
m_walkOffsetX = -32;
m_walkOffsetY = -32;
walkTimeFactor = 2;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(-1, 1, 0) == position) {
2011-08-29 07:54:28 +02:00
m_direction = Otc::SouthWest;
m_walkOffsetX = 32;
m_walkOffsetY = -32;
walkTimeFactor = 2;
}
2011-08-30 16:37:48 +02:00
else if(m_position + Position(-1, -1, 0) == position) {
2011-08-29 07:54:28 +02:00
m_direction = Otc::NorthWest;
m_walkOffsetX = 32;
m_walkOffsetY = 32;
walkTimeFactor = 2;
}
else { // Teleport
// we teleported, dont walk or change direction
m_walking = false;
}
2011-08-30 16:37:48 +02:00
// update map tiles
2011-09-01 00:08:55 +02:00
g_map.removeThing(asThing());
g_map.addThing(asThing(), position);
2011-08-29 07:54:28 +02:00
2011-08-30 16:37:48 +02:00
if(m_walking) {
2011-08-31 15:58:01 +02:00
// Calculate xPattern
2011-08-30 16:37:48 +02:00
if(m_direction >= 4) {
if(m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
2011-08-31 15:58:01 +02:00
m_xPattern = Otc::East;
2011-08-30 16:37:48 +02:00
else if(m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
2011-08-31 15:58:01 +02:00
m_xPattern = Otc::West;
2011-08-30 16:37:48 +02:00
}
else {
2011-08-31 15:58:01 +02:00
m_xPattern = m_direction;
2011-08-30 16:37:48 +02:00
}
// get walk speed
int groundSpeed = 0;
2011-09-01 00:08:55 +02:00
ItemPtr ground = g_map.getTile(position)->getGround();
2011-08-30 16:37:48 +02:00
if(ground)
2011-08-31 17:03:33 +02:00
groundSpeed = ground->getType().groundSpeed;
2011-08-30 16:37:48 +02:00
float walkTime = walkTimeFactor * 1000.0 * (float)groundSpeed / m_speed;
walkTime = walkTime == 0 ? 1000 : walkTime;
2011-08-29 02:38:26 +02:00
2011-08-30 16:37:48 +02:00
m_walkTimePerPixel = walkTime / 32.0;
m_lastTicks = g_platform.getTicks();
}
}
2011-08-29 02:38:26 +02:00
2011-08-30 16:37:48 +02:00
void Creature::cancelWalk(Otc::Direction direction)
{
m_walking = false;
m_walkOffsetX = 0;
m_walkOffsetY = 0;
m_animation = 0;
m_direction = direction;
}
2011-08-29 05:05:57 +02:00
void Creature::setHealthPercent(uint8 healthPercent)
{
int oldHealthPercent = m_healthPercent;
m_healthPercent = healthPercent;
onHealthPercentChange(oldHealthPercent);
}
2011-08-31 17:03:33 +02:00
const ThingType& Creature::getType()
2011-08-15 23:02:52 +02:00
{
2011-08-31 17:03:33 +02:00
return g_thingsType.getCreatureType(m_outfit.type);
2011-08-15 23:02:52 +02:00
}
2011-08-29 05:05:57 +02:00
void Creature::onHealthPercentChange(int)
{
m_informationColor = Fw::black;
if(m_healthPercent > 92) {
m_informationColor.setGreen(188);
}
else if(m_healthPercent > 60) {
m_informationColor.setRed(80);
m_informationColor.setGreen(161);
m_informationColor.setBlue(80);
}
else if(m_healthPercent > 30) {
m_informationColor.setRed(161);
m_informationColor.setGreen(161);
}
else if(m_healthPercent > 8) {
m_informationColor.setRed(160);
m_informationColor.setGreen(39);
m_informationColor.setBlue(39);
}
else if(m_healthPercent > 3) {
m_informationColor.setRed(160);
}
else {
m_informationColor.setRed(79);
}
}