Fixed unload/disable crash. Added glowing plants.

develop
Warmist 2013-06-23 23:56:01 +03:00
parent 471eb9c02d
commit ee7902c529
3 changed files with 97 additions and 31 deletions

@ -1,6 +1,7 @@
#include "renderer_light.hpp"
#include <functional>
#include <string>
#include "Types.h"
@ -13,7 +14,9 @@
#include "df/flow_info.h"
#include "df/world.h"
#include "df/building.h"
#include "df/building_doorst.h"
#include "df/plant.h"
#include "df/plant_raw.h"
using df::global::gps;
using namespace DFHack;
@ -54,6 +57,7 @@ rect2d getMapViewport()
lightingEngineViewscreen::lightingEngineViewscreen(renderer_light* target):lightingEngine(target)
{
reinit();
initRawSpecific();
}
void lightingEngineViewscreen::reinit()
@ -216,15 +220,47 @@ bool lightingEngineViewscreen::addLight(int tileId,const lightSource& light)
lights[tileId].flicker=true;
return wasLight;
}
lightCell getStandartColor(int colorId)
{
return lightCell(df::global::enabler->ccolor[colorId][0]/255.0f,
df::global::enabler->ccolor[colorId][1]/255.0f,
df::global::enabler->ccolor[colorId][2]/255.0f);
}
int getPlantNumber(const std::string& id)
{
std::vector<df::plant_raw*>& vec=df::plant_raw::get_vector();
for(int i=0;i<vec.size();i++)
{
if(vec[i]->id==id)
return i;
}
return -1;
}
void addPlant(const std::string& id,std::map<int,lightSource>& map,const lightSource& v)
{
int nId=getPlantNumber(id);
if(nId>0)
{
map[nId]=v;
}
}
void lightingEngineViewscreen::initRawSpecific()
{
addPlant("TOWER_CAP",glowPlants,lightSource(lightCell(0.65,0.65,0.65),6));
addPlant("MUSHROOM_CUP_DIMPLE",glowPlants,lightSource(lightCell(0.03,0.03,0.9),3));
addPlant("CAVE MOSS",glowPlants,lightSource(lightCell(0.3,0.3,0.9),2));
addPlant("MUSHROOM_HELMET_PLUMP",glowPlants,lightSource(lightCell(0.5,0.2,0.9),2));
}
static size_t max_list_size = 100000; // Avoid iterating over huge lists
void lightingEngineViewscreen::doOcupancyAndLights()
{
lightSource sun(lightCell(1,1,1),15);
lightSource lava(lightCell(0.8f,0.2f,0.2f),5);
lightSource candle(lightCell(0.96f,0.84f,0.03f),5);
candle.flicker=true;
lightSource torch(lightCell(0.96f,0.5f,0.1f),8);
lightSource torch(lightCell(0.9f,0.75f,0.3f),8);
rect2d vp=getMapViewport();
int window_x=*df::global::window_x;
int window_y=*df::global::window_y;
int window_z=*df::global::window_z;
@ -371,12 +407,16 @@ void lightingEngineViewscreen::doOcupancyAndLights()
DFHack::MaterialInfo mat(bld->mat_index,bld->mat_type);
if(mat.isInorganic())
{
int color=mat.inorganic->material.basic_color[0];
curCell*=lightCell(df::global::enabler->ccolor[color][0]/255.0f,
df::global::enabler->ccolor[color][1]/255.0f,
df::global::enabler->ccolor[color][2]/255.0f);
int color=mat.inorganic->material.build_color[0]+8*mat.inorganic->material.build_color[2];
curCell*=getStandartColor(color);
}
}
if(type==df::enums::building_type::Door)
{
df::building_doorst* door=static_cast<df::building_doorst*>(bld);
if(door->door_flags.bits.closed)
curCell*=lightCell(0,0,0);
}
}
}
}
@ -440,6 +480,23 @@ void lightingEngineViewscreen::doOcupancyAndLights()
}
}
}
for(int i=0;i<block->plants.size();i++)
{
df::plant* cPlant=block->plants[i];
df::coord2d pos=cPlant->pos;
int wx=pos.x-window_x+vp.first.x;
int wy=pos.y-window_y+vp.first.y;
int tile=getIndex(wx,wy);
if(wx>=vp.first.x && wy>=vp.first.y && wx<=vp.second.x && wy<=vp.second.y)
{
auto it=glowPlants.find(cPlant->material);
if(it!=glowPlants.end())
{
addLight(tile,it->second);
}
}
}
}
if(df::global::cursor->x>-30000)
{

@ -2,7 +2,7 @@
#define RENDERER_LIGHT_INCLUDED
#include "renderer_opengl.hpp"
#include "Types.h"
#include <map>
struct renderer_light : public renderer_wrap {
private:
void colorizeTile(int x,int y)
@ -111,6 +111,7 @@ private:
void doFovs();
bool lightUpCell(lightCell& power,int dx,int dy,int tx,int ty);
bool addLight(int tileId,const lightSource& light);
void initRawSpecific();
size_t inline getIndex(int x,int y)
{
return x*h+y;
@ -118,6 +119,11 @@ private:
std::vector<lightCell> lightMap;
std::vector<lightCell> ocupancy;
std::vector<lightSource> lights;
std::map<int,lightSource> glowPlants;
std::map<int,lightSource> glowVeins;
int w,h;
DFHack::rect2d mapPort;
};

@ -20,7 +20,7 @@ using std::vector;
using std::string;
enum RENDERER_MODE
{
MODE_DEFAULT,MODE_TRIPPY,MODE_TRUECOLOR,MODE_LUA,MODE_LIGHT
MODE_DEFAULT,MODE_TRIPPY,MODE_TRUECOLOR,MODE_LUA,MODE_LIGHT,MODE_LIGHT_OFF
};
RENDERER_MODE current_mode=MODE_DEFAULT;
lightingEngine *engine=NULL;
@ -47,12 +47,6 @@ void removeOld()
if(current_mode!=MODE_DEFAULT)
delete df::global::enabler->renderer;
current_mode=MODE_DEFAULT;
if(current_mode==MODE_LIGHT)
{
if(engine)
delete engine;
engine=0;
}
}
void installNew(df::renderer* r,RENDERER_MODE newMode)
{
@ -241,6 +235,11 @@ static command_result rendermax(color_ostream &out, vector <string> & parameters
{
if(parameters.size()==0)
return CR_WRONG_USAGE;
if(!df::global::enabler->renderer->uses_opengl())
{
out.printerr("Sorry, this plugin needs open gl enabled printmode. Try STANDARD or other non-2d");
return CR_FAILURE;
}
string cmd=parameters[0];
if(cmd=="trippy")
{
@ -305,26 +304,20 @@ static command_result rendermax(color_ostream &out, vector <string> & parameters
}
else if(cmd=="light")
{
if(current_mode==MODE_LIGHT)
{
engine->calculate();
engine->updateWindow();
}
else
{
removeOld();
renderer_light *myRender=new renderer_light(df::global::enabler->renderer);
installNew(myRender,MODE_LIGHT);
engine=new lightingEngineViewscreen(myRender);
engine->calculate();
engine->updateWindow();
}
removeOld();
renderer_light *myRender=new renderer_light(df::global::enabler->renderer);
installNew(myRender,MODE_LIGHT);
engine=new lightingEngineViewscreen(myRender);
engine->calculate();
engine->updateWindow();
return CR_OK;
}
else if(cmd=="disable")
{
if(current_mode==MODE_DEFAULT)
out.print("%s\n","Not installed, doing nothing.");
else if(current_mode==MODE_LIGHT)
current_mode=MODE_LIGHT_OFF;
else
removeOld();
@ -336,9 +329,19 @@ DFhackCExport command_result plugin_onupdate (color_ostream &out)
{
if(engine)
{
engine->calculate();
engine->updateWindow();
if(current_mode==MODE_LIGHT_OFF)
{
delete engine;
engine=0;
removeOld();
}
else
{
engine->calculate();
engine->updateWindow();
}
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown(color_ostream &)