From 20a8011a4618fc3782cce698b8a719e3ffa3dfeb Mon Sep 17 00:00:00 2001 From: titison Date: Sun, 26 Oct 2014 21:34:14 +0100 Subject: [PATCH 1/4] first hp-bars --- source/glest_game/graphics/renderer.cpp | 75 +++++++++++++++++++++++++ source/glest_game/graphics/renderer.h | 1 + 2 files changed, 76 insertions(+) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 8638650e..5ce3d710 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -5445,6 +5445,11 @@ void Renderer::renderSelectionEffects() { glColor4f(unit->getHpRatio(), 0, 0, 0.3f); } renderSelectionCircle(currVec, unit->getType()->getSize(), selectionCircleRadius); + if(unit->getType()->getMaxEp() > 0) { + renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.08,unit->getEpRatio()); + } else { + renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.05); + } if( showDebugUI == true && (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { @@ -8215,6 +8220,76 @@ void Renderer::enableProjectiveTexturing() { } // ==================== private aux drawing ==================== +void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, float ep) { + Vec3f rightVector; + Vec3f upVector; + Vec3f posUpVector=Vec3f(0,1,0); + v+=posUpVector; + float modelview[16]; + float width=size/2+0.5f; + float red; + float green; + float brightness=0.6f; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + // get the current modelview state + glGetFloatv(GL_MODELVIEW_MATRIX , modelview); + rightVector= Vec3f(modelview[0], modelview[4], modelview[8]); + upVector= Vec3f(modelview[1], modelview[5], modelview[9]); + +// vertexBuffer[bufferIndex] = pos - (rightVector - upVector) * size; +// vertexBuffer[bufferIndex+1] = pos - (rightVector + upVector) * size; +// vertexBuffer[bufferIndex+2] = pos + (rightVector - upVector) * size; +// vertexBuffer[bufferIndex+3] = pos + (rightVector + upVector) * size; + + //printf("size:%f\n",size); + hp=hp*2-1; + ep=ep*2-1; + + if(hp >= 0.0f) { + green=brightness; + red=brightness-hp*brightness; + } else { + red=brightness; + green=brightness+hp*brightness; + } + + glColor4f(red,green,0.0f,1.0f); + glBegin(GL_QUADS); + if(ep < -2.0f) { + glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); + glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); + glVertex3fv((v + (rightVector*hp*width - upVector*height)).ptr()); + glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); + + } else { + glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); + glVertex3fv((v - (rightVector*width + upVector*height*0.1f)).ptr()); + glVertex3fv((v + (rightVector*hp*width - upVector*height*0.1f)).ptr()); + glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); + + glColor4f(brightness/2,brightness/2,brightness,1.0f); + glVertex3fv((v - (rightVector*width + upVector*height*0.1f)).ptr()); + glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); + glVertex3fv((v + (rightVector*ep*width - upVector*height)).ptr()); + glVertex3fv((v + (rightVector*ep*width - upVector*height*0.1f)).ptr()); + printf("Found Ep Unit\n"); + } + glEnd(); + + glColor4f(red+0.1f,green+0.1f,0.1f,1.0f); + glBegin(GL_LINE_LOOP); + glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); + glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); + glVertex3fv((v + (rightVector*width - upVector*height)).ptr()); + glVertex3fv((v + (rightVector*width + upVector*height)).ptr()); + glEnd(); + + + glPopMatrix(); +} void Renderer::renderSelectionCircle(Vec3f v, int size, float radius, float thickness) { if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index ad7223b7..a48777a3 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -680,6 +680,7 @@ private: //private aux drawing void renderSelectionCircle(Vec3f v, int size, float radius, float thickness=0.2f); + void renderSelectionHpBar(Vec3f v, int size, float hp, float height, float ep=-1.0f); void renderTeamColorEffect(Vec3f &v, int heigth, int size, Vec3f color, const Texture2D *texture); void renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &color, float width); void renderTile(const Vec2i &pos); From a2790bd9cbc487fe0931c9a7cd9a35a5d72d946e Mon Sep 17 00:00:00 2001 From: titison Date: Mon, 27 Oct 2014 16:16:50 +0100 Subject: [PATCH 2/4] Make them actually usable. added ep bars. Of course its not finished yet --- source/glest_game/game/game.cpp | 3 ++ source/glest_game/graphics/renderer.cpp | 40 +++++++++++++++++++++---- source/glest_game/graphics/renderer.h | 1 + 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index a0fc693c..dc8a4c88 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -5223,6 +5223,9 @@ void Game::render3d(){ if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + //renderOnTopBars (aka Healthbars) + renderer.renderOnTopBars(); + //objects renderer.renderObjects(avgRenderFps); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 5ce3d710..7cc723ad 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -5445,11 +5445,6 @@ void Renderer::renderSelectionEffects() { glColor4f(unit->getHpRatio(), 0, 0, 0.3f); } renderSelectionCircle(currVec, unit->getType()->getSize(), selectionCircleRadius); - if(unit->getType()->getMaxEp() > 0) { - renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.08,unit->getEpRatio()); - } else { - renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.05); - } if( showDebugUI == true && (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { @@ -5615,6 +5610,41 @@ void Renderer::renderSelectionEffects() { glPopAttrib(); } +void Renderer::renderOnTopBars(){ + if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config= Config::getInstance(); + if(config.getBool("RecordMode","false") == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleQuadUnitList.empty() == false) { + for(int visibleUnitIndex = 0; + visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + Vec3f currVec= unit->getCurrVectorFlat(); + if(unit->getType()->getMaxEp() > 0) { + renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.08,unit->getEpRatio()); + } else { + renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.05); + } + } + } + glPopAttrib(); +} + void Renderer::renderWaterEffects(){ if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { return; diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index a48777a3..fa15818b 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -549,6 +549,7 @@ public: void renderUnitsToBuild(const int renderFps); void renderSelectionEffects(); + void renderOnTopBars(); void renderWaterEffects(); void renderHud(); void renderMinimap(); From de5135bfaf55c6cd3e02e5d0885151f017f70213 Mon Sep 17 00:00:00 2001 From: titison Date: Wed, 29 Oct 2014 00:50:45 +0100 Subject: [PATCH 3/4] *added split(s,d) function for strings *reading unit xml healthbar node *adjusted hpbars --- source/glest_game/game/game.cpp | 6 +-- source/glest_game/graphics/renderer.cpp | 59 ++++++++++++++++--------- source/glest_game/types/unit_type.cpp | 39 ++++++++++++++++ source/glest_game/types/unit_type.h | 17 +++++++ source/shared_lib/include/util/util.h | 2 + source/shared_lib/sources/util/util.cpp | 16 +++++++ 6 files changed, 114 insertions(+), 25 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index dc8a4c88..39acbd64 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -5223,9 +5223,6 @@ void Game::render3d(){ if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - //renderOnTopBars (aka Healthbars) - renderer.renderOnTopBars(); - //objects renderer.renderObjects(avgRenderFps); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); @@ -5254,6 +5251,9 @@ void Game::render3d(){ if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + //renderOnTopBars (aka Healthbars) + renderer.renderOnTopBars(); + //particles renderer.renderParticleManager(rsGame); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderParticleManager]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 7cc723ad..c8d8cbd1 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -5634,11 +5634,33 @@ void Renderer::renderOnTopBars(){ for(int visibleUnitIndex = 0; visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - Vec3f currVec= unit->getCurrVectorFlat(); - if(unit->getType()->getMaxEp() > 0) { - renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.08,unit->getEpRatio()); - } else { - renderSelectionHpBar((currVec+Vec3f(0,unit->getType()->getHeight(),0)),unit->getType()->getSize(),unit->getHpRatio(),0.05); + if(unit->isAlive() && unit->getType()->isHealthbarEnabled() + && ((unit->getType()->getHealthbarVisible()&hbvAlways) + || ((unit->getType()->getHealthbarVisible()&hbvDamaged) && unit->getHp()!=unit->getType()->getMaxHp()) + || ((unit->getType()->getHealthbarVisible()&hbvSelected) && game->getGui()->isSelected(unit)))) { + Vec3f currVec= unit->getCurrVectorFlat(); + float thickness; + if(unit->getType()->getHealthbarHeight()==-100.0f) { + currVec.y+=unit->getType()->getHeight()+1; + } else { + currVec.y+=unit->getType()->getHealthbarHeight(); + } + + if(unit->getType()->getHealthbarThickness()==-1.0f) { + if(unit->getType()->getMaxEp() > 0 && unit->getType()->isHealthbarShowEp()) { + thickness=0.08f; + } else { + thickness=0.05f; + } + } else { + thickness=unit->getType()->getHealthbarThickness(); + } + + if(unit->getType()->getMaxEp() > 0 && unit->getType()->isHealthbarShowEp()) { + renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),thickness,unit->getEpRatio()); + } else { + renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),thickness); + } } } } @@ -8253,13 +8275,12 @@ void Renderer::enableProjectiveTexturing() { void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, float ep) { Vec3f rightVector; Vec3f upVector; - Vec3f posUpVector=Vec3f(0,1,0); - v+=posUpVector; + v.y+=1; float modelview[16]; - float width=size/2+0.5f; + float width=(float)size/6+0.25f; float red; float green; - float brightness=0.6f; + float brightness=0.7f; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -8269,12 +8290,6 @@ void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, f rightVector= Vec3f(modelview[0], modelview[4], modelview[8]); upVector= Vec3f(modelview[1], modelview[5], modelview[9]); -// vertexBuffer[bufferIndex] = pos - (rightVector - upVector) * size; -// vertexBuffer[bufferIndex+1] = pos - (rightVector + upVector) * size; -// vertexBuffer[bufferIndex+2] = pos + (rightVector - upVector) * size; -// vertexBuffer[bufferIndex+3] = pos + (rightVector + upVector) * size; - - //printf("size:%f\n",size); hp=hp*2-1; ep=ep*2-1; @@ -8286,7 +8301,7 @@ void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, f green=brightness+hp*brightness; } - glColor4f(red,green,0.0f,1.0f); + glColor4f(red,green,0.0f,0.4f); glBegin(GL_QUADS); if(ep < -2.0f) { glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); @@ -8296,20 +8311,20 @@ void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, f } else { glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); - glVertex3fv((v - (rightVector*width + upVector*height*0.1f)).ptr()); - glVertex3fv((v + (rightVector*hp*width - upVector*height*0.1f)).ptr()); + glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); + glVertex3fv((v + (rightVector*hp*width - upVector*height*0.0f)).ptr()); glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); - glColor4f(brightness/2,brightness/2,brightness,1.0f); - glVertex3fv((v - (rightVector*width + upVector*height*0.1f)).ptr()); + glColor4f(brightness,0,brightness,0.4f); + glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); glVertex3fv((v + (rightVector*ep*width - upVector*height)).ptr()); - glVertex3fv((v + (rightVector*ep*width - upVector*height*0.1f)).ptr()); + glVertex3fv((v + (rightVector*ep*width - upVector*height*0.0f)).ptr()); printf("Found Ep Unit\n"); } glEnd(); - glColor4f(red+0.1f,green+0.1f,0.1f,1.0f); + glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); glBegin(GL_LINE_LOOP); glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index 82129a6a..cc3b5b18 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -88,6 +88,11 @@ UnitType::UnitType() : ProducibleType() { meetingPointImage = NULL; lightColor= Vec3f(0.f); light= false; + healthbar= false; + healthbarShowEp= true; + healthbarheight= -100.0f; + healthbarthickness=-1.0f; + healthbarVisible=hbvUndefined; //TODO Implement default multiSelect= false; commandable= true; armorType= NULL; @@ -446,6 +451,40 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, } } + //healthbar + if(parametersNode->hasChild("healthbar")) { + const XmlNode *HealthbarNode= parametersNode->getChild("healthbar"); + healthbar= HealthbarNode->getAttribute("enabled")->getBoolValue(); + if(healthbar){ + if(HealthbarNode->hasAttribute("show-ep")) { + healthbarShowEp= HealthbarNode->getAttribute("show-ep")->getBoolValue(); + } + if(HealthbarNode->hasAttribute("height")) { + healthbarheight= HealthbarNode->getAttribute("height")->getFloatValue(); + } + if(HealthbarNode->hasAttribute("thickness")) { + healthbarthickness= HealthbarNode->getAttribute("thickness")->getFloatValue(0.f, 1.f); + } + if(HealthbarNode->hasAttribute("visible")) { + string healthbarVisibleString=HealthbarNode->getAttribute("visible")->getValue(); + healthbarVisible=hbvUndefined; + vector v=split(healthbarVisibleString,"|"); + for (int i = 0; i < (int)v.size(); ++i) { + string current=trim(v[i]); + if(current=="always") { + healthbarVisible=healthbarVisible|hbvAlways; + } else if(current=="selected") { + healthbarVisible=healthbarVisible|hbvSelected; + } else if(current=="damaged") { + healthbarVisible=healthbarVisible|hbvDamaged; + } else { + throw megaglest_runtime_error("Unknown Healthbar Visible Option: " + current, validationMode); + } + } + } + } + } + //light const XmlNode *lightNode= parametersNode->getChild("light"); light= lightNode->getAttribute("enabled")->getBoolValue(); diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index 2bc37fb0..df61bdd8 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -116,6 +116,13 @@ enum UnitClass { typedef vector DamageParticleSystemTypes; +enum HealthbarVisible { + hbvUndefined=0, + hbvAlways=1, + hbvDamaged=2, + hbvSelected=4 +}; + enum UnitCountsInVictoryConditions { ucvcNotSet, ucvcTrue, @@ -170,6 +177,11 @@ private: const ArmorType *armorType; bool light; Vec3f lightColor; + bool healthbar; + bool healthbarShowEp; + float healthbarheight; + float healthbarthickness; + int healthbarVisible; bool multiSelect; bool commandable; int sight; @@ -256,6 +268,11 @@ public: inline bool getLight() const {return light;} inline bool getRotationAllowed() const {return rotationAllowed;} inline Vec3f getLightColor() const {return lightColor;} + inline bool isHealthbarEnabled() const {return healthbar;} + inline bool isHealthbarShowEp() const {return healthbarShowEp;} + inline float getHealthbarHeight() const {return healthbarheight;} + inline float getHealthbarThickness() const {return healthbarthickness;} + inline int getHealthbarVisible() const {return healthbarVisible;} inline bool getMultiSelect() const {return multiSelect;} inline bool isCommandable() const {return commandable;} inline int getSight() const {return sight;} diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index 1ba171b4..b6306bc2 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -13,6 +13,7 @@ #define _SHARED_UTIL_UTIL_H_ #include +#include #include #include #include "thread.h" @@ -226,6 +227,7 @@ string cutLastFile(const string &s); string cutLastExt(const string &s); string ext(const string &s); string replaceBy(const string &s, char c1, char c2); +vector split(string s,string d); string toLower(const string &s); void copyStringToBuffer(char *buffer, int bufferSize, const string& s); diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index e749937f..5142acea 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -709,6 +709,22 @@ string replaceBy(const string &s, char c1, char c2){ return rs; } +vector split(string s,string d) { + vector results; + size_t lastOffset = 0; + + while(true) + { + size_t offset = s.find_first_of(d, lastOffset); + results.push_back(s.substr(lastOffset, offset - lastOffset)); + if (offset == string::npos) + break; + else + lastOffset = offset + d.size(); //skip the delimiter + } + return results; +} + string toLower(const string &s){ string rs= s; From 08806c5416c82322e6ea39a9c6c1c5a17cfe8499 Mon Sep 17 00:00:00 2001 From: titison Date: Wed, 29 Oct 2014 22:23:46 +0100 Subject: [PATCH 4/4] *Finised Hp Bars *Added general settings in faction.xml --- source/glest_game/graphics/renderer.cpp | 57 +++++++++++++++--------- source/glest_game/types/faction_type.cpp | 32 +++++++++++++ source/glest_game/types/faction_type.h | 8 ++++ source/glest_game/types/unit_type.cpp | 51 +++++++++------------ source/glest_game/types/unit_type.h | 11 ++--- 5 files changed, 103 insertions(+), 56 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index c8d8cbd1..fea57fc7 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -5634,32 +5634,46 @@ void Renderer::renderOnTopBars(){ for(int visibleUnitIndex = 0; visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(unit->isAlive() && unit->getType()->isHealthbarEnabled() - && ((unit->getType()->getHealthbarVisible()&hbvAlways) - || ((unit->getType()->getHealthbarVisible()&hbvDamaged) && unit->getHp()!=unit->getType()->getMaxHp()) - || ((unit->getType()->getHealthbarVisible()&hbvSelected) && game->getGui()->isSelected(unit)))) { + + float healthbarheight; + float healthbarthickness; + int healthbarVisible; + + //get settings of the faction + healthbarheight=unit->getFaction()->getType()->getHealthbarHeight(); + healthbarthickness=unit->getFaction()->getType()->getHealthbarThickness(); + healthbarVisible=unit->getFaction()->getType()->getHealthbarVisible(); + + //replace them by the ones from the unit if existent + if(unit->getType()->getHealthbarVisible()!=hbvOff && unit->getType()->getHealthbarVisible()!=hbvUndefined) { + if(unit->getType()->getHealthbarHeight()!=-100.0f) { + healthbarheight=unit->getType()->getHealthbarHeight(); + } + if(unit->getType()->getHealthbarThickness()!=-1.0f) { + healthbarthickness=unit->getType()->getHealthbarThickness(); + } + healthbarVisible=unit->getType()->getHealthbarVisible(); + } + + if(unit->isAlive() && !(healthbarVisible==hbvUndefined || (healthbarVisible&hbvOff)) + && ((healthbarVisible&hbvAlways) + || ((healthbarVisible&hbvDamaged) && unit->getHp()!=unit->getType()->getMaxHp()) + || ((healthbarVisible&hbvSelected) && game->getGui()->isSelected(unit)))) { Vec3f currVec= unit->getCurrVectorFlat(); - float thickness; - if(unit->getType()->getHealthbarHeight()==-100.0f) { + if(healthbarheight==-100.0f) { currVec.y+=unit->getType()->getHeight()+1; } else { - currVec.y+=unit->getType()->getHealthbarHeight(); + currVec.y+=healthbarheight; } - if(unit->getType()->getHealthbarThickness()==-1.0f) { - if(unit->getType()->getMaxEp() > 0 && unit->getType()->isHealthbarShowEp()) { - thickness=0.08f; - } else { - thickness=0.05f; - } - } else { - thickness=unit->getType()->getHealthbarThickness(); + if(unit->getType()->getMaxEp() > 0) { + healthbarthickness=healthbarthickness*2; } - if(unit->getType()->getMaxEp() > 0 && unit->getType()->isHealthbarShowEp()) { - renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),thickness,unit->getEpRatio()); + if(unit->getType()->getMaxEp() > 0) { + renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),healthbarthickness,unit->getEpRatio()); } else { - renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),thickness); + renderSelectionHpBar(currVec,unit->getType()->getSize(),unit->getHpRatio(),healthbarthickness); } } } @@ -8293,6 +8307,7 @@ void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, f hp=hp*2-1; ep=ep*2-1; + //from green to yellow to red if(hp >= 0.0f) { green=brightness; red=brightness-hp*brightness; @@ -8304,26 +8319,28 @@ void Renderer::renderSelectionHpBar(Vec3f v, int size, float hp, float height, f glColor4f(red,green,0.0f,0.4f); glBegin(GL_QUADS); if(ep < -2.0f) { + //hpbar glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); glVertex3fv((v + (rightVector*hp*width - upVector*height)).ptr()); glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); } else { + //hpbar glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); glVertex3fv((v + (rightVector*hp*width - upVector*height*0.0f)).ptr()); glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); - + //epbar glColor4f(brightness,0,brightness,0.4f); glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); glVertex3fv((v + (rightVector*ep*width - upVector*height)).ptr()); glVertex3fv((v + (rightVector*ep*width - upVector*height*0.0f)).ptr()); - printf("Found Ep Unit\n"); } glEnd(); + //border glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); glBegin(GL_LINE_LOOP); glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); diff --git a/source/glest_game/types/faction_type.cpp b/source/glest_game/types/faction_type.cpp index bd8ae23f..1c335ef7 100644 --- a/source/glest_game/types/faction_type.cpp +++ b/source/glest_game/types/faction_type.cpp @@ -34,6 +34,9 @@ FactionType::FactionType() { music = NULL; personalityType = fpt_Normal; isLinked = false; + healthbarheight= -100.0f; + healthbarthickness= 0.05f; + healthbarVisible=hbvUndefined; } //load a faction, given a directory @@ -272,6 +275,35 @@ void FactionType::load(const string &factionName, const TechTree *techTree, Chec loadedFileList[musicNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(path,musicNode->getAttribute("path")->getRestrictedValue())); } + //healthbar + if(factionNode->hasChild("healthbar")) { + const XmlNode *HealthbarNode= factionNode->getChild("healthbar"); + if(HealthbarNode->hasChild("height")) { + healthbarheight= HealthbarNode->getChild("height")->getAttribute("value")->getFloatValue(); + } + if(HealthbarNode->hasChild("thickness")) { + healthbarthickness= HealthbarNode->getChild("thickness")->getAttribute("value")->getFloatValue(0.f, 1.f); + } + if(HealthbarNode->hasChild("visible")) { + string healthbarVisibleString=HealthbarNode->getChild("visible")->getAttribute("value")->getValue(); + vector v=split(healthbarVisibleString,"|"); + for (int i = 0; i < (int)v.size(); ++i) { + string current=trim(v[i]); + if(current=="always") { + healthbarVisible=healthbarVisible|hbvAlways; + } else if(current=="selected") { + healthbarVisible=healthbarVisible|hbvSelected; + } else if(current=="damaged") { + healthbarVisible=healthbarVisible|hbvDamaged; + } else if(current=="off") { + healthbarVisible=healthbarVisible|hbvOff; + } else { + throw megaglest_runtime_error("Unknown Healthbar Visible Option: " + current, validationMode); + } + } + } + } + //read ai behavior if(factionNode->hasChild("ai-behavior") == true) { const XmlNode *aiNode= factionNode->getChild("ai-behavior"); diff --git a/source/glest_game/types/faction_type.h b/source/glest_game/types/faction_type.h index 47641598..909e9571 100644 --- a/source/glest_game/types/faction_type.h +++ b/source/glest_game/types/faction_type.h @@ -95,6 +95,10 @@ private: std::map mapAIBehaviorStaticOverrideValues; bool isLinked; + float healthbarheight; + float healthbarthickness; + int healthbarVisible; + public: //init @@ -120,6 +124,10 @@ public: int getStartingUnitCount() const {return (int)startingUnits.size();} const UnitType *getStartingUnit(int i) const {return startingUnits[i].first;} int getStartingUnitAmount(int i) const {return startingUnits[i].second;} + inline float getHealthbarHeight() const {return healthbarheight;} + inline float getHealthbarThickness() const {return healthbarthickness;} + inline int getHealthbarVisible() const {return healthbarVisible;} + const UnitType *getUnitType(const string &name) const; const UnitType *getUnitTypeById(int id) const; diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index cc3b5b18..74c3cab6 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -88,11 +88,9 @@ UnitType::UnitType() : ProducibleType() { meetingPointImage = NULL; lightColor= Vec3f(0.f); light= false; - healthbar= false; - healthbarShowEp= true; healthbarheight= -100.0f; healthbarthickness=-1.0f; - healthbarVisible=hbvUndefined; //TODO Implement default + healthbarVisible=hbvUndefined; multiSelect= false; commandable= true; armorType= NULL; @@ -454,32 +452,27 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, //healthbar if(parametersNode->hasChild("healthbar")) { const XmlNode *HealthbarNode= parametersNode->getChild("healthbar"); - healthbar= HealthbarNode->getAttribute("enabled")->getBoolValue(); - if(healthbar){ - if(HealthbarNode->hasAttribute("show-ep")) { - healthbarShowEp= HealthbarNode->getAttribute("show-ep")->getBoolValue(); - } - if(HealthbarNode->hasAttribute("height")) { - healthbarheight= HealthbarNode->getAttribute("height")->getFloatValue(); - } - if(HealthbarNode->hasAttribute("thickness")) { - healthbarthickness= HealthbarNode->getAttribute("thickness")->getFloatValue(0.f, 1.f); - } - if(HealthbarNode->hasAttribute("visible")) { - string healthbarVisibleString=HealthbarNode->getAttribute("visible")->getValue(); - healthbarVisible=hbvUndefined; - vector v=split(healthbarVisibleString,"|"); - for (int i = 0; i < (int)v.size(); ++i) { - string current=trim(v[i]); - if(current=="always") { - healthbarVisible=healthbarVisible|hbvAlways; - } else if(current=="selected") { - healthbarVisible=healthbarVisible|hbvSelected; - } else if(current=="damaged") { - healthbarVisible=healthbarVisible|hbvDamaged; - } else { - throw megaglest_runtime_error("Unknown Healthbar Visible Option: " + current, validationMode); - } + if(HealthbarNode->hasChild("height")) { + healthbarheight= HealthbarNode->getChild("height")->getAttribute("value")->getFloatValue(); + } + if(HealthbarNode->hasChild("thickness")) { + healthbarthickness= HealthbarNode->getChild("thickness")->getAttribute("value")->getFloatValue(0.f, 1.f); + } + if(HealthbarNode->hasChild("visible")) { + string healthbarVisibleString=HealthbarNode->getChild("visible")->getAttribute("value")->getValue(); + vector v=split(healthbarVisibleString,"|"); + for (int i = 0; i < (int)v.size(); ++i) { + string current=trim(v[i]); + if(current=="always") { + healthbarVisible=healthbarVisible|hbvAlways; + } else if(current=="selected") { + healthbarVisible=healthbarVisible|hbvSelected; + } else if(current=="damaged") { + healthbarVisible=healthbarVisible|hbvDamaged; + } else if(current=="off") { + healthbarVisible=healthbarVisible|hbvOff; + } else { + throw megaglest_runtime_error("Unknown Healthbar Visible Option: " + current, validationMode); } } } diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index df61bdd8..27e5aa33 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -118,9 +118,10 @@ typedef vector DamageParticleSystemTypes; enum HealthbarVisible { hbvUndefined=0, - hbvAlways=1, - hbvDamaged=2, - hbvSelected=4 + hbvOff=1, + hbvAlways=2, + hbvDamaged=4, + hbvSelected=8 }; enum UnitCountsInVictoryConditions { @@ -177,8 +178,6 @@ private: const ArmorType *armorType; bool light; Vec3f lightColor; - bool healthbar; - bool healthbarShowEp; float healthbarheight; float healthbarthickness; int healthbarVisible; @@ -268,8 +267,6 @@ public: inline bool getLight() const {return light;} inline bool getRotationAllowed() const {return rotationAllowed;} inline Vec3f getLightColor() const {return lightColor;} - inline bool isHealthbarEnabled() const {return healthbar;} - inline bool isHealthbarShowEp() const {return healthbarShowEp;} inline float getHealthbarHeight() const {return healthbarheight;} inline float getHealthbarThickness() const {return healthbarthickness;} inline int getHealthbarVisible() const {return healthbarVisible;}