From f5b3bfda11c75c71a1dee002066b9774187ebc30 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 3 Dec 2012 21:18:39 -0200 Subject: [PATCH] Lights minor fixes --- src/otclient/creature.cpp | 6 ++-- src/otclient/lightview.cpp | 57 +++++++++++++++--------------- src/otclient/lightview.h | 7 ++-- src/otclient/map.cpp | 10 ++++-- src/otclient/mapview.cpp | 2 +- src/otclient/protocolgameparse.cpp | 12 +------ src/otclient/tile.cpp | 11 ++++++ src/otclient/tile.h | 1 + 8 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/otclient/creature.cpp b/src/otclient/creature.cpp index 1b533c32..9553557b 100644 --- a/src/otclient/creature.cpp +++ b/src/otclient/creature.cpp @@ -82,10 +82,12 @@ void Creature::draw(const Point& dest, float scaleFactor, bool animate, LightVie m_footStepDrawn = true; if(lightView) { - Light light = m_light; + Light light = rawGetThingType()->getLight(); + if(m_light.intensity > light.intensity) + light = m_light; // local player always have a minimum light in complete darkness - if(isLocalPlayer() && (g_map.getLight().intensity < 40 || m_position.z > Otc::SEA_FLOOR)) { + if(isLocalPlayer() && (g_map.getLight().intensity < 64 || m_position.z > Otc::SEA_FLOOR)) { light.intensity = std::max(light.intensity, 2); if(light.color == 0 || light.color > 215) light.color = 215; diff --git a/src/otclient/lightview.cpp b/src/otclient/lightview.cpp index 4337213b..10fbed32 100644 --- a/src/otclient/lightview.cpp +++ b/src/otclient/lightview.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012 OTClient + * Copyright (c) 2010-2012 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 @@ -27,35 +27,42 @@ #include #include +enum { + MAX_LIGHT_INTENSITY = 8, + MAX_AMBIENT_LIGHT_INTENSITY = 255 +}; + LightView::LightView() { m_lightbuffer = g_framebuffers.createFrameBuffer(); - generateLightBubble(); + m_lightTexture = generateLightBubble(0.1f); reset(); } -void LightView::generateLightBubble() +TexturePtr LightView::generateLightBubble(float centerFactor) { - m_lightRadius = 128; - int circleDiameter = m_lightRadius * 2; - ImagePtr lightImage = ImagePtr(new Image(Size(circleDiameter, circleDiameter))); + int bubbleRadius = 256; + int centerRadius = bubbleRadius * centerFactor; + int bubbleDiameter = bubbleRadius * 2; + ImagePtr lightImage = ImagePtr(new Image(Size(bubbleDiameter, bubbleDiameter))); - for(int x = 0; x < circleDiameter; x++) { - for(int y = 0; y < circleDiameter; y++) { - float radius = std::sqrt((m_lightRadius - x)*(m_lightRadius - x) + (m_lightRadius - y)*(m_lightRadius - y)); - float intensity = std::max((m_lightRadius-radius)/(float)m_lightRadius, 0.0f); + for(int x = 0; x < bubbleDiameter; x++) { + for(int y = 0; y < bubbleDiameter; y++) { + float radius = std::sqrt((bubbleRadius - x)*(bubbleRadius - x) + (bubbleRadius - y)*(bubbleRadius - y)); + float intensity = std::max(std::min((bubbleRadius-radius)/(float)(bubbleRadius-centerRadius), 1.0f), 0.0f); - // light intensity varies inversely with the square of the distance + // light intensity varies inversely with the square of the distance intensity = intensity * intensity; - uint8_t colorByte = intensity * 255; + uint8_t colorByte = intensity * 0xff; - uint8_t pixel[4] = {colorByte,colorByte,colorByte,255}; + uint8_t pixel[4] = {colorByte,colorByte,colorByte,0xff}; lightImage->setPixel(x, y, pixel); } } - m_lightTexture = TexturePtr(new Texture(lightImage, true)); - m_lightTexture->setSmooth(true); + TexturePtr tex = TexturePtr(new Texture(lightImage, true)); + tex->setSmooth(true); + return tex; } void LightView::reset() @@ -70,10 +77,11 @@ void LightView::setGlobalLight(const Light& light) void LightView::addLightSource(const Point& center, float scaleFactor, const Light& light) { - int radius = light.intensity * m_lightRadius * scaleFactor * 0.25f; + int intensity = std::min(light.intensity, MAX_LIGHT_INTENSITY); + int radius = intensity * Otc::TILE_PIXELS * scaleFactor; Color color = Color::from8bit(light.color); - float brightness = 0.8f + (light.intensity/8.0f)*0.2f; + float brightness = 0.5f + (intensity/(float)MAX_LIGHT_INTENSITY)*0.5f; color.setRed(color.rF() * brightness); color.setGreen(color.gF() * brightness); @@ -89,7 +97,7 @@ void LightView::addLightSource(const Point& center, float scaleFactor, const Lig void LightView::drawGlobalLight(const Light& light) { Color color = Color::from8bit(light.color); - float brightness = light.intensity / 256.0f; + float brightness = light.intensity / (float)MAX_AMBIENT_LIGHT_INTENSITY; color.setRed(color.rF() * brightness); color.setGreen(color.gF() * brightness); color.setBlue(color.bF() * brightness); @@ -99,18 +107,12 @@ void LightView::drawGlobalLight(const Light& light) void LightView::drawLightSource(const Point& center, const Color& color, int radius) { + // debug draw + //radius /= 16; + Rect dest = Rect(center - Point(radius, radius), Size(radius*2,radius*2)); g_painter->setColor(color); - g_painter->setCompositionMode(Painter::CompositionMode_Add); g_painter->drawTexturedRect(dest, m_lightTexture); - - // debug draw - /* - radius = 8; - g_painter->setColor(Color::black); - g_painter->setCompositionMode(Painter::CompositionMode_Replace); - g_painter->drawBoundingRect(Rect(center - Point(radius, radius), Size(radius*2,radius*2)), 4); - */ } void LightView::resize(const Size& size) @@ -128,7 +130,6 @@ void LightView::draw(const Rect& dest, const Rect& src) for(const LightSource& source : m_lightMap) drawLightSource(source.center, source.color, source.radius); m_lightbuffer->release(); - g_painter->setCompositionMode(Painter::CompositionMode_Light); m_lightbuffer->draw(dest, src); g_painter->restoreSavedState(); diff --git a/src/otclient/lightview.h b/src/otclient/lightview.h index c44dec42..3e3aff18 100644 --- a/src/otclient/lightview.h +++ b/src/otclient/lightview.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012 OTClient + * Copyright (c) 2010-2012 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 @@ -47,14 +47,13 @@ public: private: void drawGlobalLight(const Light& light); void drawLightSource(const Point& center, const Color& color, int radius); - void generateLightBubble(); + TexturePtr generateLightBubble(float centerFactor); TexturePtr m_lightTexture; - int m_lightRadius; FrameBufferPtr m_lightbuffer; MapView* m_mapView; Light m_globalLight; std::vector m_lightMap; }; -#endif // LIGHTVIEW_H +#endif diff --git a/src/otclient/map.cpp b/src/otclient/map.cpp index 8572c83f..f01e5094 100644 --- a/src/otclient/map.cpp +++ b/src/otclient/map.cpp @@ -375,16 +375,20 @@ bool Map::isCovered(const Position& pos, int firstFloor) bool Map::isCompletelyCovered(const Position& pos, int firstFloor) { + const TilePtr& checkTile = getTile(pos); Position tilePos = pos; while(tilePos.coveredUp() && tilePos.z >= firstFloor) { bool covered = true; + bool done = false; // check in 2x2 range tiles that has no transparent pixels - for(int x=0;x<2;++x) { - for(int y=0;y<2;++y) { + for(int x=0;x<2 && !done;++x) { + for(int y=0;y<2 && !done;++y) { const TilePtr& tile = getTile(tilePos.translated(-x, -y)); if(!tile || !tile->isFullyOpaque()) { covered = false; - break; + done = true; + } else if(x==0 && y==0 && (!checkTile || checkTile->isSingleDimension())) { + done = true; } } } diff --git a/src/otclient/mapview.cpp b/src/otclient/mapview.cpp index a49ea14c..c9e35c70 100644 --- a/src/otclient/mapview.cpp +++ b/src/otclient/mapview.cpp @@ -104,7 +104,7 @@ void MapView::draw(const Rect& rect) m_lightView->resize(m_framebuffer->getSize()); Light ambientLight; - if(cameraPosition.z <= 7) { + if(cameraPosition.z <= Otc::SEA_FLOOR) { ambientLight = g_map.getLight(); } else { ambientLight.color = 215; diff --git a/src/otclient/protocolgameparse.cpp b/src/otclient/protocolgameparse.cpp index cc97893d..25bfc9c3 100644 --- a/src/otclient/protocolgameparse.cpp +++ b/src/otclient/protocolgameparse.cpp @@ -1642,20 +1642,10 @@ CreaturePtr ProtocolGame::getCreature(const InputMessagePtr& msg, int type) creature->setSkull(skull); creature->setShield(shield); creature->setPassable(!unpass); + creature->setLight(light); if(emblem != -1) creature->setEmblem(emblem); - ThingTypePtr thing = creature->getThingType(); - if(thing) { - Light newLight = thing->getLight(); - if(light.color > 0 && light.color != 215) - newLight.color = light.color; - if(light.intensity > 0) - newLight.intensity = light.intensity; - - creature->setLight(newLight); - } - if(creature == m_localPlayer && !m_localPlayer->isKnown()) m_localPlayer->setKnown(true); } diff --git a/src/otclient/tile.cpp b/src/otclient/tile.cpp index dd6e538f..642d5dfe 100644 --- a/src/otclient/tile.cpp +++ b/src/otclient/tile.cpp @@ -508,6 +508,17 @@ bool Tile::isFullyOpaque() return firstObject && firstObject->isFullGround(); } +bool Tile::isSingleDimension() +{ + if(!m_walkingCreatures.empty()) + return false; + for(const ThingPtr& thing : m_things) { + if(thing->getHeight() != 1 || thing->getWidth() != 1) + return false; + } + return true; +} + bool Tile::isLookPossible() { for(const ThingPtr& thing : m_things) { diff --git a/src/otclient/tile.h b/src/otclient/tile.h index fd6b2c39..3d21f89a 100644 --- a/src/otclient/tile.h +++ b/src/otclient/tile.h @@ -98,6 +98,7 @@ public: bool changesFloor(); bool isFullGround(); bool isFullyOpaque(); + bool isSingleDimension(); bool isLookPossible(); bool isClickable(); bool isEmpty();