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

392 lines
14 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* 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"
2011-12-28 12:26:52 +01:00
#include "game.h"
2011-08-31 22:22:57 +02:00
2011-08-15 16:11:24 +02:00
#include <framework/graphics/graphics.h>
#include <framework/core/eventdispatcher.h>
#include <framework/core/clock.h>
2011-08-15 16:11:24 +02:00
2011-12-08 18:28:29 +01:00
#include <framework/graphics/paintershaderprogram.h>
#include <framework/graphics/paintershadersources.h>
#include <framework/graphics/texturemanager.h>
2011-12-08 18:28:29 +01:00
#include "spritemanager.h"
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;
2012-01-05 15:24:38 +01:00
m_showVolatileSquare = false;
m_showStaticSquare = false;
m_direction = Otc::South;
m_walkTimePerPixel = 1000.0/32.0;
m_walking = false;
2011-12-26 07:14:57 +01:00
m_inverseWalking = true;
2011-08-29 02:38:26 +02:00
2011-11-01 03:35:50 +01:00
m_informationFont = g_fonts.getFont("verdana-11px-rounded");
2011-08-15 16:11:24 +02:00
}
2011-12-08 18:28:29 +01:00
PainterShaderProgramPtr outfitProgram;
int HEAD_COLOR_UNIFORM = 10;
int BODY_COLOR_UNIFORM = 11;
int LEGS_COLOR_UNIFORM = 12;
int FEET_COLOR_UNIFORM = 13;
int MASK_TEXTURE_UNIFORM = 14;
2012-01-05 19:34:37 +01:00
void Creature::draw(const Point& p, const Rect&)
2011-08-15 16:11:24 +02:00
{
2012-01-05 15:24:38 +01:00
if(m_showVolatileSquare) {
g_painter.setColor(m_volatileSquareColor);
g_painter.drawBoundingRect(Rect(p + m_walkOffset - Point(m_type->parameters[ThingType::DisplacementX], m_type->parameters[ThingType::DisplacementY]) + 3, Size(28, 28)), 2);
}
2012-01-05 15:24:38 +01:00
if(m_showStaticSquare) {
g_painter.setColor(m_staticSquareColor);
g_painter.drawBoundingRect(Rect(p + m_walkOffset - Point(m_type->parameters[ThingType::DisplacementX], m_type->parameters[ThingType::DisplacementY]) + 1, Size(32, 32)), 2);
}
g_painter.setColor(Fw::white);
2011-12-08 18:28:29 +01:00
if(!outfitProgram) {
outfitProgram = PainterShaderProgramPtr(new PainterShaderProgram);
outfitProgram->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
outfitProgram->addShaderFromSourceFile(Shader::Fragment, "/game_shaders/outfit.frag");
2011-12-08 18:28:29 +01:00
assert(outfitProgram->link());
outfitProgram->bindUniformLocation(HEAD_COLOR_UNIFORM, "headColor");
outfitProgram->bindUniformLocation(BODY_COLOR_UNIFORM, "bodyColor");
outfitProgram->bindUniformLocation(LEGS_COLOR_UNIFORM, "legsColor");
outfitProgram->bindUniformLocation(FEET_COLOR_UNIFORM, "feetColor");
outfitProgram->bindUniformLocation(MASK_TEXTURE_UNIFORM, "maskTexture");
}
2011-08-29 02:38:26 +02:00
// Render creature
2011-12-02 03:29:05 +01:00
for(m_yPattern = 0; m_yPattern < m_type->dimensions[ThingType::PatternY]; m_yPattern++) {
2011-08-24 05:58:23 +02:00
// continue if we dont have this addon.
2011-11-13 09:46:19 +01:00
if(m_yPattern > 0 && !(m_outfit.getAddons() & (1 << (m_yPattern-1))))
2011-08-24 05:58:23 +02:00
continue;
2011-12-08 18:28:29 +01:00
g_painter.setCustomProgram(outfitProgram);
outfitProgram->bind();
outfitProgram->setUniformValue(HEAD_COLOR_UNIFORM, m_outfit.getHeadColor());
outfitProgram->setUniformValue(BODY_COLOR_UNIFORM, m_outfit.getBodyColor());
outfitProgram->setUniformValue(LEGS_COLOR_UNIFORM, m_outfit.getLegsColor());
outfitProgram->setUniformValue(FEET_COLOR_UNIFORM, m_outfit.getFeetColor());
2011-12-28 12:00:09 +01:00
for(int h = 0; h < m_type->dimensions[ThingType::Height]; h++) {
for(int w = 0; w < m_type->dimensions[ThingType::Width]; w++) {
int spriteId = m_type->getSpriteId(w, h, 0, m_xPattern, m_yPattern, m_zPattern, m_animation);
if(!spriteId)
continue;
TexturePtr spriteTex = g_sprites.getSpriteTexture(spriteId);
if(!spriteTex)
continue;
2011-12-08 18:28:29 +01:00
if(m_type->dimensions[ThingType::Layers] > 1) {
2011-12-28 12:00:09 +01:00
int maskId = m_type->getSpriteId(w, h, 1, m_xPattern, m_yPattern, m_zPattern, m_animation);
if(!maskId)
2011-12-08 18:28:29 +01:00
continue;
2011-12-28 12:00:09 +01:00
TexturePtr maskTex = g_sprites.getSpriteTexture(maskId);
2011-12-08 18:28:29 +01:00
outfitProgram->setUniformTexture(MASK_TEXTURE_UNIFORM, maskTex, 1);
}
2011-12-28 12:00:09 +01:00
Rect drawRect(((p + m_walkOffset).x - w*32) - m_type->parameters[ThingType::DisplacementX],
((p + m_walkOffset).y - h*32) - m_type->parameters[ThingType::DisplacementY],
2011-12-08 18:28:29 +01:00
32, 32);
g_painter.drawTexturedRect(drawRect, spriteTex);
}
2011-08-24 05:58:23 +02:00
}
2011-12-08 18:28:29 +01:00
g_painter.releaseCustomProgram();
2011-08-19 14:26:26 +02:00
}
}
2011-08-19 15:23:35 +02:00
2012-01-05 19:34:37 +01:00
void Creature::drawInformation(int x, int y, bool useGray, const Rect& visibleRect)
{
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-11-04 05:53:00 +01:00
// calculate main rects
Rect backgroundRect = Rect(x-(13.5), y, 27, 4);
2012-01-05 19:34:37 +01:00
backgroundRect.bound(visibleRect);
2011-11-04 05:53:00 +01:00
2011-12-29 08:08:02 +01:00
Rect textRect = Rect(x - m_nameSize.width() / 2.0, y-12, m_nameSize);
2012-01-05 19:34:37 +01:00
textRect.bound(visibleRect);
2011-11-04 05:53:00 +01:00
// distance them
2012-01-05 19:34:37 +01:00
if(textRect.top() == visibleRect.top())
2011-12-29 08:08:02 +01:00
backgroundRect.moveTop(textRect.top() + 12);
2012-01-05 19:34:37 +01:00
if(backgroundRect.bottom() == visibleRect.bottom())
2011-12-29 08:08:02 +01:00
textRect.moveTop(backgroundRect.top() - 12);
2011-11-04 05:53:00 +01:00
// health rect is based on background rect, so no worries
Rect healthRect = backgroundRect.expanded(-1);
2011-11-04 05:53:00 +01:00
healthRect.setWidth((m_healthPercent / 100.0) * 25);
2011-08-20 22:30:41 +02:00
2011-11-04 05:53:00 +01:00
// draw
2011-12-07 01:31:55 +01:00
g_painter.setColor(Fw::black);
g_painter.drawFilledRect(backgroundRect);
2011-08-20 22:30:41 +02:00
2011-12-07 01:31:55 +01:00
g_painter.setColor(fillColor);
g_painter.drawFilledRect(healthRect);
2011-08-20 22:30:41 +02:00
2011-12-28 12:00:09 +01:00
if(m_informationFont)
m_informationFont->renderText(m_name, textRect, Fw::AlignTopCenter, fillColor);
if(m_skull != Otc::SkullNone && m_skullTexture) {
g_painter.setColor(Fw::white);
g_painter.drawTexturedRect(Rect(x + 12, y + 5, m_skullTexture->getSize()), m_skullTexture);
}
if(m_shield != Otc::ShieldNone && m_shieldTexture) {
g_painter.setColor(Fw::white);
g_painter.drawTexturedRect(Rect(x, y + 5, m_shieldTexture->getSize()), m_shieldTexture);
}
if(m_emblem != Otc::EmblemNone && m_emblemTexture) {
g_painter.setColor(Fw::white);
g_painter.drawTexturedRect(Rect(x + 12, y + 16, m_emblemTexture->getSize()), m_emblemTexture);
}
2011-08-15 16:11:24 +02:00
}
2011-08-15 23:02:52 +02:00
2011-11-05 21:34:49 +01:00
void Creature::walk(const Position& position, bool inverse)
{
2011-12-28 12:00:09 +01:00
// We're walking
2011-12-26 07:14:57 +01:00
if(m_position.isInRange(position, 1, 1, 0)) {
Otc::Direction direction = m_position.getDirectionFromPosition(position);
setDirection(direction);
2011-12-28 12:00:09 +01:00
if(inverse) {
Position positionDelta = m_position - position;
2011-12-28 12:26:52 +01:00
m_walkOffset = Point(positionDelta.x * Map::NUM_TILE_PIXELS, positionDelta.y * Map::NUM_TILE_PIXELS);
2011-12-26 07:14:57 +01:00
}
2011-12-28 12:00:09 +01:00
else
m_walkOffset = Point(0, 0);
2011-12-28 12:00:09 +01:00
// Diagonal walking lasts 3 times more.
int walkTimeFactor = 1;
if(direction == Otc::NorthWest || direction == Otc::NorthEast || direction == Otc::SouthWest || direction == Otc::SouthEast)
walkTimeFactor = 3;
2011-08-29 07:54:28 +02:00
2011-12-28 12:00:09 +01:00
// Get walking speed
2011-11-05 22:01:41 +01:00
int groundSpeed = 100;
2011-12-28 12:00:09 +01:00
if(ItemPtr ground = g_map.getTile(position)->getGround())
2011-12-02 03:29:05 +01:00
groundSpeed = ground->getType()->parameters[ThingType::GroundSpeed];
2011-12-28 12:00:09 +01:00
float walkTime = 1000.0 * (float)groundSpeed / m_speed;
2011-11-05 22:01:41 +01:00
walkTime = (walkTime == 0) ? 1000 : walkTime;
2011-12-28 12:26:52 +01:00
walkTime = std::ceil(walkTime / g_game.getServerBeat()) * g_game.getServerBeat();
2011-08-29 02:38:26 +02:00
2011-12-28 12:00:09 +01:00
bool sameWalk = m_walking && !m_inverseWalking && inverse;
m_inverseWalking = inverse;
m_walking = true;
2011-08-30 16:37:48 +02:00
m_walkTimePerPixel = walkTime / 32.0;
2011-12-28 12:00:09 +01:00
m_walkStart = sameWalk ? m_walkStart : g_clock.ticks();
m_walkEnd = m_walkStart + walkTime * walkTimeFactor;
m_turnDirection = m_direction;
2011-11-05 22:43:13 +01:00
updateWalk();
}
2011-12-28 12:00:09 +01:00
// Teleport
else {
m_walking = false;
m_walkOffset = Point(0, 0);
m_animation = 0;
}
}
2011-12-28 12:00:09 +01:00
void Creature::turn(Otc::Direction direction)
{
2011-12-28 12:00:09 +01:00
if(!m_walking)
setDirection(direction);
else
m_turnDirection = direction;
}
2011-12-28 12:00:09 +01:00
void Creature::updateWalk()
{
if(!m_walking)
return;
int elapsedTicks = g_clock.ticksElapsed(m_walkStart);
int totalPixelsWalked = std::min((int)round(elapsedTicks / m_walkTimePerPixel), 32);
if(m_inverseWalking) {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffset.y = 32 - totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffset.y = totalPixelsWalked - 32;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffset.x = totalPixelsWalked - 32;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffset.x = 32 - totalPixelsWalked;
}
else {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffset.y = -totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffset.y = totalPixelsWalked;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffset.x = totalPixelsWalked;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffset.x = -totalPixelsWalked;
}
2011-12-26 07:14:57 +01:00
2011-12-28 12:26:52 +01:00
if(totalPixelsWalked == 32 || m_type->dimensions[ThingType::AnimationPhases] <= 1)
2011-12-28 12:00:09 +01:00
m_animation = 0;
2011-12-28 12:26:52 +01:00
else if(m_type->dimensions[ThingType::AnimationPhases] > 1)
2011-12-28 12:00:09 +01:00
m_animation = 1 + totalPixelsWalked * 4 / Map::NUM_TILE_PIXELS % (m_type->dimensions[ThingType::AnimationPhases] - 1);
2011-12-28 12:00:09 +01:00
if(g_clock.ticks() > m_walkEnd)
cancelWalk(m_turnDirection);
else
g_dispatcher.scheduleEvent(std::bind(&Creature::updateWalk, asCreature()), m_walkTimePerPixel);
2011-08-30 16:37:48 +02:00
}
2011-08-29 02:38:26 +02:00
2011-12-29 08:08:02 +01:00
void Creature::cancelWalk(Otc::Direction direction, bool)
2011-08-30 16:37:48 +02:00
{
m_walking = false;
2011-12-28 12:00:09 +01:00
m_walkStart = 0;
2011-12-28 12:26:52 +01:00
m_animation = 0;
2011-12-28 12:00:09 +01:00
m_walkOffset = Point(0, 0);
setDirection(direction);
}
2011-11-13 09:46:19 +01:00
void Creature::setName(const std::string& name)
2011-08-15 23:02:52 +02:00
{
2011-12-28 12:00:09 +01:00
if(m_informationFont)
m_nameSize = m_informationFont->calculateTextRectSize(name);
2011-11-13 09:46:19 +01:00
m_name = name;
2011-08-15 23:02:52 +02:00
}
2011-08-29 05:05:57 +02:00
2011-11-13 09:46:19 +01:00
void Creature::setHealthPercent(uint8 healthPercent)
2011-08-29 05:05:57 +02:00
{
m_informationColor = Fw::black;
2011-11-13 09:46:19 +01:00
if(healthPercent > 92) {
2011-08-29 05:05:57 +02:00
m_informationColor.setGreen(188);
}
2011-11-13 09:46:19 +01:00
else if(healthPercent > 60) {
2011-08-29 05:05:57 +02:00
m_informationColor.setRed(80);
m_informationColor.setGreen(161);
m_informationColor.setBlue(80);
}
2011-11-13 09:46:19 +01:00
else if(healthPercent > 30) {
2011-08-29 05:05:57 +02:00
m_informationColor.setRed(161);
m_informationColor.setGreen(161);
}
2011-11-13 09:46:19 +01:00
else if(healthPercent > 8) {
2011-08-29 05:05:57 +02:00
m_informationColor.setRed(160);
m_informationColor.setGreen(39);
m_informationColor.setBlue(39);
}
2011-11-13 09:46:19 +01:00
else if(healthPercent > 3) {
2011-08-29 05:05:57 +02:00
m_informationColor.setRed(160);
}
else {
m_informationColor.setRed(79);
}
2011-11-13 09:46:19 +01:00
m_healthPercent = healthPercent;
2011-08-29 05:05:57 +02:00
}
2011-11-06 22:45:42 +01:00
2011-11-13 09:46:19 +01:00
void Creature::setDirection(Otc::Direction direction)
2011-11-06 22:45:42 +01:00
{
2011-12-13 21:06:22 +01:00
if(direction == Otc::NorthEast || direction == Otc::SouthEast)
m_xPattern = Otc::East;
else if(direction == Otc::NorthWest || direction == Otc::SouthWest)
m_xPattern = Otc::West;
else
2011-11-13 09:46:19 +01:00
m_xPattern = direction;
m_direction = direction;
}
2011-12-02 03:29:05 +01:00
void Creature::setOutfit(const Outfit& outfit)
{
m_outfit = outfit;
m_type = getType();
2011-12-28 12:00:09 +01:00
2011-12-29 20:36:43 +01:00
if(m_type->dimensions[ThingType::Layers] == 1)
m_outfit.resetClothes();
2011-12-02 03:29:05 +01:00
}
void Creature::setSkull(uint8 skull)
{
m_skull = skull;
g_lua.callGlobalField("Creature","onSkullChange", asCreature(), m_skull);
}
void Creature::setShield(uint8 shield)
{
m_shield = shield;
g_lua.callGlobalField("Creature","onShieldChange", asCreature(), m_shield);
}
void Creature::setEmblem(uint8 emblem)
{
m_emblem = emblem;
g_lua.callGlobalField("Creature","onEmblemChange", asCreature(), m_emblem);
}
void Creature::setSkullTexture(const std::string& filename)
{
m_skullTexture = g_textures.getTexture(filename);
}
void Creature::setShieldTexture(const std::string& filename)
{
m_shieldTexture = g_textures.getTexture(filename);
}
void Creature::setEmblemTexture(const std::string& filename)
{
m_emblemTexture = g_textures.getTexture(filename);
}
2012-01-05 15:24:38 +01:00
void Creature::addVolatileSquare(uint8 color)
{
m_showVolatileSquare = true;
m_volatileSquareColor = Color::from8bit(color);
// schedule removal
auto self = asCreature();
g_dispatcher.scheduleEvent([self]() {
self->removeVolatileSquare();
}, VOLATILE_SQUARE_DURATION);
}
2011-12-02 03:29:05 +01:00
ThingType *Creature::getType()
2011-11-13 09:46:19 +01:00
{
return g_thingsType.getThingType(m_outfit.getType(), ThingsType::Creature);
2011-11-06 22:45:42 +01:00
}