2013-06-23 10:25:42 -06:00
|
|
|
#include "renderer_light.hpp"
|
|
|
|
|
|
|
|
#include <functional>
|
2013-06-23 14:56:01 -06:00
|
|
|
#include <string>
|
2013-06-23 16:45:45 -06:00
|
|
|
#include <math.h>
|
2013-06-23 10:25:42 -06:00
|
|
|
|
2013-06-26 07:42:14 -06:00
|
|
|
#include "tinythread.h"
|
2013-06-24 15:59:32 -06:00
|
|
|
|
2013-06-24 09:18:57 -06:00
|
|
|
#include "LuaTools.h"
|
2013-06-23 10:25:42 -06:00
|
|
|
|
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Screen.h"
|
|
|
|
#include "modules/Maps.h"
|
2013-06-24 15:59:32 -06:00
|
|
|
|
2013-06-23 16:45:45 -06:00
|
|
|
#include "modules/Units.h"
|
2013-06-23 10:25:42 -06:00
|
|
|
|
|
|
|
#include "df/graphic.h"
|
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2013-06-23 18:47:42 -06:00
|
|
|
#include "df/viewscreen_dungeonmodest.h"
|
2013-06-23 10:25:42 -06:00
|
|
|
#include "df/flow_info.h"
|
2013-06-23 12:21:25 -06:00
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/building.h"
|
2013-06-23 14:56:01 -06:00
|
|
|
#include "df/building_doorst.h"
|
2013-06-28 12:17:54 -06:00
|
|
|
#include "df/building_floodgatest.h"
|
2013-06-23 14:56:01 -06:00
|
|
|
#include "df/plant.h"
|
|
|
|
#include "df/plant_raw.h"
|
2013-09-22 05:50:03 -06:00
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/items_other_id.h"
|
|
|
|
#include "df/unit.h"
|
2013-06-23 10:25:42 -06:00
|
|
|
|
2013-06-26 07:42:14 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
using df::global::gps;
|
|
|
|
using namespace DFHack;
|
|
|
|
using df::coord2d;
|
2013-06-26 07:42:14 -06:00
|
|
|
using namespace tthread;
|
2013-06-25 10:34:38 -06:00
|
|
|
|
2013-06-24 02:50:22 -06:00
|
|
|
const float RootTwo = 1.4142135623730950488016887242097f;
|
2013-06-23 10:25:42 -06:00
|
|
|
|
2013-06-26 07:42:14 -06:00
|
|
|
|
2013-06-29 11:55:07 -06:00
|
|
|
bool isInRect(const coord2d& pos,const rect2d& rect)
|
2013-06-26 07:42:14 -06:00
|
|
|
{
|
2013-06-29 11:55:07 -06:00
|
|
|
if(pos.x>=rect.first.x && pos.y>=rect.first.y && pos.x<rect.second.x && pos.y<rect.second.y)
|
|
|
|
return true;
|
|
|
|
return false;
|
2013-06-26 07:42:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
lightingEngineViewscreen::~lightingEngineViewscreen()
|
|
|
|
{
|
2013-06-29 11:55:07 -06:00
|
|
|
threading.shutdown();
|
2013-06-26 07:42:14 -06:00
|
|
|
}
|
|
|
|
|
2013-06-30 06:05:59 -06:00
|
|
|
lightSource::lightSource(rgbf power,int radius):power(power),flicker(false)
|
2013-06-25 11:43:55 -06:00
|
|
|
{
|
|
|
|
if(radius >= 0)
|
|
|
|
this->radius = radius;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float totalPower = power.r;
|
|
|
|
if(totalPower < power.g)totalPower = power.g;
|
|
|
|
if(totalPower < power.b)totalPower = power.b;
|
|
|
|
if(totalPower > 0 && levelDim > 0)
|
|
|
|
this->radius = (int)((log(levelDim/totalPower)/log(0.85f))) + 1;
|
|
|
|
else
|
|
|
|
this->radius = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
rect2d getMapViewport()
|
|
|
|
{
|
|
|
|
const int AREA_MAP_WIDTH = 23;
|
|
|
|
const int MENU_WIDTH = 30;
|
|
|
|
if(!gps || !df::viewscreen_dwarfmodest::_identity.is_instance(DFHack::Gui::getCurViewscreen()))
|
2013-06-23 18:47:42 -06:00
|
|
|
{
|
|
|
|
if(gps && df::viewscreen_dungeonmodest::_identity.is_instance(DFHack::Gui::getCurViewscreen()))
|
|
|
|
{
|
|
|
|
return mkrect_wh(0,0,gps->dimx,gps->dimy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return mkrect_wh(0,0,0,0);
|
|
|
|
|
|
|
|
}
|
2013-06-23 10:25:42 -06:00
|
|
|
int w=gps->dimx;
|
|
|
|
int h=gps->dimy;
|
|
|
|
int view_height=h-2;
|
|
|
|
int area_x2 = w-AREA_MAP_WIDTH-2;
|
|
|
|
int menu_x2=w-MENU_WIDTH-2;
|
|
|
|
int menu_x1=area_x2-MENU_WIDTH-1;
|
|
|
|
int view_rb=w-1;
|
|
|
|
|
|
|
|
int area_pos=*df::global::ui_area_map_width;
|
|
|
|
int menu_pos=*df::global::ui_menu_width;
|
|
|
|
if(area_pos<3)
|
|
|
|
{
|
|
|
|
view_rb=area_x2;
|
|
|
|
}
|
|
|
|
if (menu_pos<area_pos || df::global::ui->main.mode!=0)
|
|
|
|
{
|
|
|
|
if (menu_pos >= area_pos)
|
|
|
|
menu_pos = area_pos-1;
|
|
|
|
int menu_x = menu_x2;
|
|
|
|
if(menu_pos < 2) menu_x = menu_x1;
|
|
|
|
view_rb = menu_x;
|
|
|
|
}
|
2013-06-24 16:25:38 -06:00
|
|
|
return mkrect_wh(1,1,view_rb,view_height+1);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-06-29 11:55:07 -06:00
|
|
|
lightingEngineViewscreen::lightingEngineViewscreen(renderer_light* target):lightingEngine(target),doDebug(false),threading(this)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
|
|
|
reinit();
|
2013-06-24 09:18:57 -06:00
|
|
|
defaultSettings();
|
2013-06-29 11:55:07 -06:00
|
|
|
int numTreads=tthread::thread::hardware_concurrency();
|
|
|
|
if(numTreads==0)numTreads=1;
|
|
|
|
threading.start(numTreads);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::reinit()
|
|
|
|
{
|
|
|
|
if(!gps)
|
|
|
|
return;
|
|
|
|
w=gps->dimx;
|
|
|
|
h=gps->dimy;
|
|
|
|
size_t size=w*h;
|
2013-06-30 06:05:59 -06:00
|
|
|
lightMap.resize(size,rgbf(1,1,1));
|
2013-06-23 10:25:42 -06:00
|
|
|
ocupancy.resize(size);
|
2013-06-23 11:45:05 -06:00
|
|
|
lights.resize(size);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-07-03 09:47:51 -06:00
|
|
|
void plotCircle(int xm, int ym, int r,const std::function<void(int,int)>& setPixel)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
|
|
|
int x = -r, y = 0, err = 2-2*r; /* II. Quadrant */
|
|
|
|
do {
|
|
|
|
setPixel(xm-x, ym+y); /* I. Quadrant */
|
|
|
|
setPixel(xm-y, ym-x); /* II. Quadrant */
|
|
|
|
setPixel(xm+x, ym-y); /* III. Quadrant */
|
|
|
|
setPixel(xm+y, ym+x); /* IV. Quadrant */
|
|
|
|
r = err;
|
|
|
|
if (r <= y) err += ++y*2+1; /* e_xy+e_y < 0 */
|
|
|
|
if (r > x || err > y) err += ++x*2+1; /* e_xy+e_x > 0 or no 2nd y-step */
|
|
|
|
} while (x < 0);
|
|
|
|
}
|
2013-07-03 09:47:51 -06:00
|
|
|
void plotSquare(int xm, int ym, int r,const std::function<void(int,int)>& setPixel)
|
2013-06-25 11:43:55 -06:00
|
|
|
{
|
|
|
|
for(int x = 0; x <= r; x++)
|
|
|
|
{
|
|
|
|
setPixel(xm+r, ym+x); /* I.1 Quadrant */
|
|
|
|
setPixel(xm+x, ym+r); /* I.2 Quadrant */
|
|
|
|
setPixel(xm+r, ym-x); /* II.1 Quadrant */
|
|
|
|
setPixel(xm+x, ym-r); /* II.2 Quadrant */
|
|
|
|
setPixel(xm-r, ym-x); /* III.1 Quadrant */
|
|
|
|
setPixel(xm-x, ym-r); /* III.2 Quadrant */
|
|
|
|
setPixel(xm-r, ym+x); /* IV.1 Quadrant */
|
|
|
|
setPixel(xm-x, ym+r); /* IV.2 Quadrant */
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 09:47:51 -06:00
|
|
|
void plotLine(int x0, int y0, int x1, int y1,rgbf power,const std::function<rgbf(rgbf,int,int,int,int)>& setPixel)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
|
|
|
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
|
|
|
|
int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
|
|
|
|
int err = dx+dy, e2; /* error value e_xy */
|
|
|
|
int rdx=0;
|
|
|
|
int rdy=0;
|
|
|
|
for(;;){ /* loop */
|
2013-06-24 02:50:22 -06:00
|
|
|
if(rdx!=0 || rdy!=0) //dirty hack to skip occlusion on the first tile.
|
2013-06-27 07:11:29 -06:00
|
|
|
{
|
|
|
|
power=setPixel(power,rdx,rdy,x0,y0);
|
|
|
|
if(power.dot(power)<0.00001f)
|
2013-06-24 02:50:22 -06:00
|
|
|
return;
|
2013-06-27 07:11:29 -06:00
|
|
|
}
|
2013-06-23 10:25:42 -06:00
|
|
|
if (x0==x1 && y0==y1) break;
|
|
|
|
e2 = 2*err;
|
|
|
|
rdx=rdy=0;
|
|
|
|
if (e2 >= dy) { err += dy; x0 += sx; rdx=sx;} /* e_xy+e_x > 0 */
|
|
|
|
if (e2 <= dx) { err += dx; y0 += sy; rdy=sy;} /* e_xy+e_y < 0 */
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 07:11:29 -06:00
|
|
|
|
2013-07-03 09:47:51 -06:00
|
|
|
void plotLineAA(int x0, int y0, int x1, int y1,rgbf power,const std::function<rgbf(rgbf,int,int,int,int)>& setPixelAA)
|
2013-06-27 07:11:29 -06:00
|
|
|
{
|
|
|
|
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
|
|
|
|
int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
|
|
|
|
int err = dx-dy, e2, x2; /* error value e_xy */
|
|
|
|
int ed = dx+dy == 0 ? 1 : sqrt((float)dx*dx+(float)dy*dy);
|
|
|
|
int rdx=0;
|
|
|
|
int rdy=0;
|
|
|
|
int lrdx,lrdy;
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf sumPower;
|
2013-06-27 07:11:29 -06:00
|
|
|
for ( ; ; ){ /* pixel loop */
|
|
|
|
float strsum=0;
|
|
|
|
float str=1-abs(err-dx+dy)/(float)ed;
|
|
|
|
strsum=str;
|
|
|
|
sumPower=setPixelAA(power*str,rdx,rdy,x0,y0);
|
|
|
|
e2 = err; x2 = x0;
|
|
|
|
lrdx=rdx;
|
|
|
|
lrdy=rdy;
|
|
|
|
rdx=rdy=0;
|
|
|
|
if (2*e2 >= -dx) { /* x step */
|
|
|
|
if (x0 == x1) break;
|
|
|
|
|
|
|
|
if (e2+dy < ed)
|
|
|
|
{
|
|
|
|
str=1-(e2+dy)/(float)ed;
|
|
|
|
sumPower+=setPixelAA(power*str,lrdx,lrdy,x0,y0+sy);
|
|
|
|
strsum+=str;
|
|
|
|
}
|
|
|
|
err -= dy; x0 += sx; rdx=sx;
|
|
|
|
}
|
|
|
|
if (2*e2 <= dy) { /* y step */
|
|
|
|
if (y0 == y1) break;
|
|
|
|
|
|
|
|
if (dx-e2 < ed)
|
|
|
|
{
|
|
|
|
str=1-(dx-e2)/(float)ed;
|
|
|
|
sumPower+=setPixelAA(power*str,lrdx,lrdy,x2+sx,y0);
|
|
|
|
strsum+=str;
|
|
|
|
}
|
|
|
|
err += dx; y0 += sy; rdy=sy;
|
|
|
|
}
|
|
|
|
if(strsum<0.001f)
|
|
|
|
return;
|
|
|
|
sumPower=sumPower/strsum;
|
|
|
|
if(sumPower.dot(sumPower)<0.00001f)
|
|
|
|
return;
|
|
|
|
power=sumPower;
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
rgbf blendMax(const rgbf& a,const rgbf& b)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf(std::max(a.r,b.r),std::max(a.g,b.g),std::max(a.b,b.b));
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
rgbf blend(const rgbf& a,const rgbf& b)
|
2013-06-26 07:42:14 -06:00
|
|
|
{
|
|
|
|
return blendMax(a,b);
|
|
|
|
}
|
2013-06-25 11:36:53 -06:00
|
|
|
void lightingEngineViewscreen::clear()
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
lightMap.assign(lightMap.size(),rgbf(1,1,1));
|
2013-06-25 11:36:53 -06:00
|
|
|
tthread::lock_guard<tthread::fast_mutex> guard(myRenderer->dataMutex);
|
|
|
|
if(lightMap.size()==myRenderer->lightGrid.size())
|
|
|
|
{
|
|
|
|
std::swap(myRenderer->lightGrid,lightMap);
|
|
|
|
myRenderer->invalidate();
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 10:25:42 -06:00
|
|
|
void lightingEngineViewscreen::calculate()
|
|
|
|
{
|
2013-07-03 01:44:23 -06:00
|
|
|
if(lightMap.size()!=myRenderer->lightGrid.size())
|
|
|
|
{
|
|
|
|
reinit();
|
2013-07-03 09:47:51 -06:00
|
|
|
myRenderer->invalidate();//needs a lock?
|
2013-07-03 01:44:23 -06:00
|
|
|
}
|
2013-06-23 10:25:42 -06:00
|
|
|
rect2d vp=getMapViewport();
|
2013-06-30 06:05:59 -06:00
|
|
|
const rgbf dim(levelDim,levelDim,levelDim);
|
|
|
|
lightMap.assign(lightMap.size(),rgbf(1,1,1));
|
2013-06-23 11:45:05 -06:00
|
|
|
lights.assign(lights.size(),lightSource());
|
2013-06-23 10:25:42 -06:00
|
|
|
for(int i=vp.first.x;i<vp.second.x;i++)
|
|
|
|
for(int j=vp.first.y;j<vp.second.y;j++)
|
|
|
|
{
|
|
|
|
lightMap[getIndex(i,j)]=dim;
|
|
|
|
}
|
|
|
|
doOcupancyAndLights();
|
2013-06-29 11:55:07 -06:00
|
|
|
threading.signalDoneOcclusion();
|
|
|
|
threading.waitForWrites();
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
void lightingEngineViewscreen::updateWindow()
|
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::fast_mutex> guard(myRenderer->dataMutex);
|
|
|
|
if(lightMap.size()!=myRenderer->lightGrid.size())
|
|
|
|
{
|
|
|
|
reinit();
|
2013-06-23 12:29:03 -06:00
|
|
|
myRenderer->invalidate();
|
2013-06-23 10:25:42 -06:00
|
|
|
return;
|
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
|
2013-06-28 14:34:47 -06:00
|
|
|
if(doDebug)
|
|
|
|
std::swap(ocupancy,myRenderer->lightGrid);
|
|
|
|
else
|
|
|
|
std::swap(lightMap,myRenderer->lightGrid);
|
2013-06-23 10:25:42 -06:00
|
|
|
rect2d vp=getMapViewport();
|
|
|
|
|
2013-06-25 11:36:53 -06:00
|
|
|
myRenderer->invalidateRect(vp.first.x,vp.first.y,vp.second.x-vp.first.x,vp.second.y-vp.first.y);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-06-23 12:21:25 -06:00
|
|
|
|
|
|
|
static size_t max_list_size = 100000; // Avoid iterating over huge lists
|
|
|
|
|
2013-06-23 11:45:05 -06:00
|
|
|
void lightSource::combine(const lightSource& other)
|
|
|
|
{
|
|
|
|
power=blend(power,other.power);
|
|
|
|
radius=std::max(other.radius,radius);//hack... but who cares
|
|
|
|
}
|
|
|
|
bool lightingEngineViewscreen::addLight(int tileId,const lightSource& light)
|
|
|
|
{
|
|
|
|
bool wasLight=lights[tileId].radius>0;
|
|
|
|
lights[tileId].combine(light);
|
|
|
|
if(light.flicker)
|
|
|
|
lights[tileId].flicker=true;
|
|
|
|
return wasLight;
|
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
void lightingEngineViewscreen::addOclusion(int tileId,const rgbf& c,float thickness)
|
2013-06-26 11:19:30 -06:00
|
|
|
{
|
|
|
|
if(thickness > 0.999 && thickness < 1.001)
|
|
|
|
ocupancy[tileId]*=c;
|
|
|
|
else
|
2013-06-27 07:11:29 -06:00
|
|
|
ocupancy[tileId]*=(c.pow(thickness));
|
2013-06-26 11:19:30 -06:00
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf getStandartColor(int colorId)
|
2013-06-23 14:56:01 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf(df::global::enabler->ccolor[colorId][0]/255.0f,
|
2013-06-23 14:56:01 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
matLightDef* lightingEngineViewscreen::getMaterialDef( int matType,int matIndex )
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
|
|
|
auto it=matDefs.find(std::make_pair(matType,matIndex));
|
|
|
|
if(it!=matDefs.end())
|
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
buildingLightDef* lightingEngineViewscreen::getBuildingDef( df::building* bld )
|
2013-06-25 16:18:26 -06:00
|
|
|
{
|
|
|
|
auto it=buildingDefs.find(std::make_tuple((int)bld->getType(),(int)bld->getSubtype(),(int)bld->getCustomType()));
|
|
|
|
if(it!=buildingDefs.end())
|
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
creatureLightDef* lightingEngineViewscreen::getCreatureDef(df::unit* u)
|
|
|
|
{
|
|
|
|
auto it=creatureDefs.find(std::make_pair(int(u->race),int(u->caste)));
|
|
|
|
if(it!=creatureDefs.end())
|
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto it2=creatureDefs.find(std::make_pair(int(u->race),int(-1)));
|
|
|
|
if(it2!=creatureDefs.end())
|
|
|
|
return &it2->second;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemLightDef* lightingEngineViewscreen::getItemDef(df::item* it)
|
|
|
|
{
|
|
|
|
auto iter=itemDefs.find(std::make_pair(int(it->getType()),int(it->getSubtype())));
|
|
|
|
if(iter!=itemDefs.end())
|
|
|
|
return &iter->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto iter2=itemDefs.find(std::make_pair(int(it->getType()),int(-1)));
|
|
|
|
if(iter2!=itemDefs.end())
|
|
|
|
return &iter2->second;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 05:03:01 -06:00
|
|
|
void lightingEngineViewscreen::applyMaterial(int tileId,const matLightDef& mat,float size, float thickness)
|
2013-06-24 09:18:57 -06:00
|
|
|
{
|
|
|
|
if(mat.isTransparent)
|
2013-06-25 05:03:01 -06:00
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
addOclusion(tileId,mat.transparency,thickness);
|
2013-06-25 05:03:01 -06:00
|
|
|
}
|
2013-06-24 09:18:57 -06:00
|
|
|
else
|
2013-06-30 06:05:59 -06:00
|
|
|
ocupancy[tileId]=rgbf(0,0,0);
|
2013-06-24 09:18:57 -06:00
|
|
|
if(mat.isEmiting)
|
|
|
|
addLight(tileId,mat.makeSource(size));
|
|
|
|
}
|
2013-06-26 11:19:30 -06:00
|
|
|
bool lightingEngineViewscreen::applyMaterial(int tileId,int matType,int matIndex,float size,float thickness,const matLightDef* def)
|
2013-06-24 09:18:57 -06:00
|
|
|
{
|
2013-09-22 05:50:03 -06:00
|
|
|
matLightDef* m=getMaterialDef(matType,matIndex);
|
2013-06-24 15:59:32 -06:00
|
|
|
if(m)
|
2013-06-24 09:18:57 -06:00
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
applyMaterial(tileId,*m,size,thickness);
|
2013-06-24 09:18:57 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(def)
|
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
applyMaterial(tileId,*def,size,thickness);
|
2013-06-24 09:18:57 -06:00
|
|
|
}
|
|
|
|
return false;
|
2013-06-23 14:56:01 -06:00
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf lightingEngineViewscreen::propogateSun(MapExtras::Block* b, int x,int y,const rgbf& in,bool lastLevel)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
//TODO unify under addLight/addOclusion
|
2013-06-30 06:05:59 -06:00
|
|
|
const rgbf matStairCase(0.9f,0.9f,0.9f);
|
|
|
|
rgbf ret=in;
|
2013-06-24 15:59:32 -06:00
|
|
|
coord2d innerCoord(x,y);
|
2013-06-25 13:49:52 -06:00
|
|
|
df::tiletype type = b->staticTiletypeAt(innerCoord);
|
2013-06-24 15:59:32 -06:00
|
|
|
df::tile_designation d = b->DesignationAt(innerCoord);
|
|
|
|
//df::tile_occupancy o = b->OccupancyAt(innerCoord);
|
|
|
|
df::tiletype_shape shape = ENUM_ATTR(tiletype,shape,type);
|
|
|
|
df::tiletype_shape_basic basic_shape = ENUM_ATTR(tiletype_shape, basic_shape, shape);
|
|
|
|
DFHack::t_matpair mat=b->staticMaterialAt(innerCoord);
|
|
|
|
df::tiletype_material tileMat= ENUM_ATTR(tiletype,material,type);
|
2013-06-23 17:54:09 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
matLightDef* lightDef;
|
|
|
|
if(tileMat==df::tiletype_material::FROZEN_LIQUID)
|
2013-06-25 13:49:52 -06:00
|
|
|
{
|
|
|
|
df::tiletype typeIce = b->tiletypeAt(innerCoord);
|
|
|
|
df::tiletype_shape shapeIce = ENUM_ATTR(tiletype,shape,typeIce);
|
|
|
|
df::tiletype_shape_basic basicShapeIce = ENUM_ATTR(tiletype_shape,basic_shape,shapeIce);
|
|
|
|
if(basicShapeIce==df::tiletype_shape_basic::Wall)
|
|
|
|
ret*=matIce.transparency;
|
|
|
|
else if(basicShapeIce==df::tiletype_shape_basic::Floor || basicShapeIce==df::tiletype_shape_basic::Ramp || shapeIce==df::tiletype_shape::STAIR_UP)
|
|
|
|
if(!lastLevel)
|
2013-06-27 07:11:29 -06:00
|
|
|
ret*=matIce.transparency.pow(1.0f/7.0f);
|
2013-06-25 13:49:52 -06:00
|
|
|
}
|
|
|
|
|
2013-09-22 05:50:03 -06:00
|
|
|
lightDef=getMaterialDef(mat.mat_type,mat.mat_index);
|
2013-06-24 15:59:32 -06:00
|
|
|
|
|
|
|
if(!lightDef || !lightDef->isTransparent)
|
|
|
|
lightDef=&matWall;
|
|
|
|
if(basic_shape==df::tiletype_shape_basic::Wall)
|
|
|
|
{
|
|
|
|
ret*=lightDef->transparency;
|
|
|
|
}
|
|
|
|
else if(basic_shape==df::tiletype_shape_basic::Floor || basic_shape==df::tiletype_shape_basic::Ramp || shape==df::tiletype_shape::STAIR_UP)
|
|
|
|
{
|
2013-06-25 13:49:52 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
if(!lastLevel)
|
2013-06-27 07:11:29 -06:00
|
|
|
ret*=lightDef->transparency.pow(1.0f/7.0f);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
else if(shape==df::tiletype_shape::STAIR_DOWN || shape==df::tiletype_shape::STAIR_UPDOWN)
|
|
|
|
{
|
|
|
|
ret*=matStairCase;
|
|
|
|
}
|
2013-06-25 05:03:01 -06:00
|
|
|
if(d.bits.liquid_type == df::enums::tile_liquid::Water && d.bits.flow_size > 0)
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-27 07:11:29 -06:00
|
|
|
ret *=matWater.transparency.pow((float)d.bits.flow_size/7.0f);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
2013-06-25 05:03:01 -06:00
|
|
|
else if(d.bits.liquid_type == df::enums::tile_liquid::Magma && d.bits.flow_size > 0)
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-27 07:11:29 -06:00
|
|
|
ret *=matLava.transparency.pow((float)d.bits.flow_size/7.0f);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
coord2d lightingEngineViewscreen::worldToViewportCoord(const coord2d& in,const rect2d& r,const coord2d& window2d)
|
|
|
|
{
|
|
|
|
return in-window2d+r.first;
|
|
|
|
}
|
2013-06-29 11:55:07 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
static size_t max_list_size = 100000; // Avoid iterating over huge lists
|
|
|
|
void lightingEngineViewscreen::doSun(const lightSource& sky,MapExtras::MapCache& map)
|
|
|
|
{
|
|
|
|
//TODO fix this mess
|
2013-06-23 10:25:42 -06:00
|
|
|
int window_x=*df::global::window_x;
|
|
|
|
int window_y=*df::global::window_y;
|
2013-06-24 15:59:32 -06:00
|
|
|
coord2d window2d(window_x,window_y);
|
2013-06-23 10:25:42 -06:00
|
|
|
int window_z=*df::global::window_z;
|
2013-06-24 15:59:32 -06:00
|
|
|
rect2d vp=getMapViewport();
|
|
|
|
coord2d vpSize=rect_size(vp);
|
|
|
|
rect2d blockVp;
|
|
|
|
blockVp.first=window2d/16;
|
|
|
|
blockVp.second=(window2d+vpSize)/16;
|
|
|
|
blockVp.second.x=std::min(blockVp.second.x,(int16_t)df::global::world->map.x_count_block);
|
|
|
|
blockVp.second.y=std::min(blockVp.second.y,(int16_t)df::global::world->map.y_count_block);
|
|
|
|
//endof mess
|
|
|
|
for(int blockX=blockVp.first.x;blockX<=blockVp.second.x;blockX++)
|
|
|
|
for(int blockY=blockVp.first.y;blockY<=blockVp.second.y;blockY++)
|
2013-06-23 15:05:03 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf cellArray[16][16];
|
2013-06-23 15:05:03 -06:00
|
|
|
for(int block_x = 0; block_x < 16; block_x++)
|
|
|
|
for(int block_y = 0; block_y < 16; block_y++)
|
2013-06-24 15:59:32 -06:00
|
|
|
cellArray[block_x][block_y] = sky.power;
|
|
|
|
|
|
|
|
int emptyCell=0;
|
2013-06-26 15:19:18 -06:00
|
|
|
for(int z=window_z;z< df::global::world->map.z_count && emptyCell<256;z++)
|
2013-06-23 15:05:03 -06:00
|
|
|
{
|
2013-06-24 15:59:32 -06:00
|
|
|
MapExtras::Block* b=map.BlockAt(DFCoord(blockX,blockY,z));
|
|
|
|
if(!b)
|
|
|
|
continue;
|
|
|
|
emptyCell=0;
|
2013-06-23 15:05:03 -06:00
|
|
|
for(int block_x = 0; block_x < 16; block_x++)
|
|
|
|
for(int block_y = 0; block_y < 16; block_y++)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf& curCell=cellArray[block_x][block_y];
|
2013-06-24 15:59:32 -06:00
|
|
|
curCell=propogateSun(b,block_x,block_y,curCell,z==window_z);
|
|
|
|
if(curCell.dot(curCell)<0.003f)
|
|
|
|
emptyCell++;
|
2013-06-23 15:05:03 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
if(emptyCell==256)
|
|
|
|
continue;
|
2013-06-23 15:05:03 -06:00
|
|
|
for(int block_x = 0; block_x < 16; block_x++)
|
|
|
|
for(int block_y = 0; block_y < 16; block_y++)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf& curCell=cellArray[block_x][block_y];
|
2013-06-23 15:05:03 -06:00
|
|
|
df::coord2d pos;
|
2013-06-24 15:59:32 -06:00
|
|
|
pos.x = blockX*16+block_x;
|
|
|
|
pos.y = blockY*16+block_y;
|
|
|
|
pos=worldToViewportCoord(pos,vp,window2d);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(pos,vp) && curCell.dot(curCell)>0.003f)
|
2013-06-23 15:05:03 -06:00
|
|
|
{
|
2013-06-24 15:59:32 -06:00
|
|
|
lightSource sun=lightSource(curCell,15);
|
|
|
|
addLight(getIndex(pos.x,pos.y),sun);
|
2013-06-23 15:05:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf lightingEngineViewscreen::getSkyColor(float v)
|
2013-06-26 15:14:03 -06:00
|
|
|
{
|
|
|
|
if(dayColors.size()<2)
|
|
|
|
{
|
|
|
|
v=abs(fmod(v+0.5,1)-0.5)*2;
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf(v,v,v);
|
2013-06-26 15:14:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float pos=v*(dayColors.size()-1);
|
|
|
|
int pre=floor(pos);
|
|
|
|
pos-=pre;
|
|
|
|
if(pre==dayColors.size()-1)
|
|
|
|
return dayColors[pre];
|
|
|
|
return dayColors[pre]*(1-pos)+dayColors[pre+1]*pos;
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
void lightingEngineViewscreen::doOcupancyAndLights()
|
|
|
|
{
|
2013-06-26 13:05:22 -06:00
|
|
|
float daycol;
|
|
|
|
if(dayHour<0)
|
2013-06-26 15:14:03 -06:00
|
|
|
{
|
|
|
|
int length=1200/daySpeed;
|
|
|
|
daycol= (*df::global::cur_year_tick % length)/ (float)length;
|
|
|
|
}
|
2013-06-26 13:05:22 -06:00
|
|
|
else
|
2013-06-26 15:14:03 -06:00
|
|
|
daycol= fmod(dayHour,24.0f)/24.0f; //1->12h 0->24h
|
2013-06-26 13:05:22 -06:00
|
|
|
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf sky_col=getSkyColor(daycol);
|
2013-06-27 07:11:29 -06:00
|
|
|
lightSource sky(sky_col, -1);//auto calculate best size
|
2013-06-24 15:59:32 -06:00
|
|
|
|
|
|
|
MapExtras::MapCache cache;
|
|
|
|
doSun(sky,cache);
|
|
|
|
|
|
|
|
int window_x=*df::global::window_x;
|
|
|
|
int window_y=*df::global::window_y;
|
|
|
|
coord2d window2d(window_x,window_y);
|
|
|
|
int window_z=*df::global::window_z;
|
|
|
|
rect2d vp=getMapViewport();
|
|
|
|
coord2d vpSize=rect_size(vp);
|
|
|
|
rect2d blockVp;
|
|
|
|
blockVp.first=coord2d(window_x,window_y)/16;
|
|
|
|
blockVp.second=(window2d+vpSize)/16;
|
|
|
|
blockVp.second.x=std::min(blockVp.second.x,(int16_t)df::global::world->map.x_count_block);
|
|
|
|
blockVp.second.y=std::min(blockVp.second.y,(int16_t)df::global::world->map.y_count_block);
|
|
|
|
|
|
|
|
for(int blockX=blockVp.first.x;blockX<=blockVp.second.x;blockX++)
|
|
|
|
for(int blockY=blockVp.first.y;blockY<=blockVp.second.y;blockY++)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
2013-06-24 15:59:32 -06:00
|
|
|
MapExtras::Block* b=cache.BlockAt(DFCoord(blockX,blockY,window_z));
|
|
|
|
MapExtras::Block* bDown=cache.BlockAt(DFCoord(blockX,blockY,window_z-1));
|
|
|
|
if(!b)
|
|
|
|
continue; //empty blocks fixed by sun propagation
|
2013-09-22 05:50:03 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
for(int block_x = 0; block_x < 16; block_x++)
|
|
|
|
for(int block_y = 0; block_y < 16; block_y++)
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
2013-06-24 15:59:32 -06:00
|
|
|
df::coord2d pos;
|
|
|
|
pos.x = blockX*16+block_x;
|
|
|
|
pos.y = blockY*16+block_y;
|
|
|
|
df::coord2d gpos=pos;
|
|
|
|
pos=worldToViewportCoord(pos,vp,window2d);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(!isInRect(pos,vp))
|
2013-06-24 15:59:32 -06:00
|
|
|
continue;
|
|
|
|
int tile=getIndex(pos.x,pos.y);
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf& curCell=ocupancy[tile];
|
2013-06-24 15:59:32 -06:00
|
|
|
curCell=matAmbience.transparency;
|
|
|
|
|
|
|
|
|
|
|
|
df::tiletype type = b->tiletypeAt(gpos);
|
|
|
|
df::tile_designation d = b->DesignationAt(gpos);
|
2013-06-25 16:18:26 -06:00
|
|
|
if(d.bits.hidden)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
curCell=rgbf(0,0,0);
|
2013-06-26 11:19:30 -06:00
|
|
|
continue; // do not process hidden stuff, TODO other hidden stuff
|
2013-06-25 16:18:26 -06:00
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
//df::tile_occupancy o = b->OccupancyAt(gpos);
|
|
|
|
df::tiletype_shape shape = ENUM_ATTR(tiletype,shape,type);
|
|
|
|
df::tiletype_shape_basic basic_shape = ENUM_ATTR(tiletype_shape, basic_shape, shape);
|
|
|
|
df::tiletype_material tileMat= ENUM_ATTR(tiletype,material,type);
|
2013-06-25 13:49:52 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
DFHack::t_matpair mat=b->staticMaterialAt(gpos);
|
2013-06-25 13:49:52 -06:00
|
|
|
|
2013-09-22 05:50:03 -06:00
|
|
|
matLightDef* lightDef=getMaterialDef(mat.mat_type,mat.mat_index);
|
2013-06-24 15:59:32 -06:00
|
|
|
if(!lightDef || !lightDef->isTransparent)
|
|
|
|
lightDef=&matWall;
|
2013-06-25 16:18:26 -06:00
|
|
|
if(shape==df::tiletype_shape::BROOK_BED )
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
curCell=rgbf(0,0,0);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
else if(shape==df::tiletype_shape::WALL)
|
|
|
|
{
|
|
|
|
if(tileMat==df::tiletype_material::FROZEN_LIQUID)
|
|
|
|
applyMaterial(tile,matIce);
|
|
|
|
else
|
|
|
|
applyMaterial(tile,*lightDef);
|
|
|
|
}
|
2013-06-25 05:03:01 -06:00
|
|
|
else if(!d.bits.liquid_type && d.bits.flow_size>0 )
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
applyMaterial(tile,matWater, (float)d.bits.flow_size/7.0f, (float)d.bits.flow_size/7.0f);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
if(d.bits.liquid_type && d.bits.flow_size>0)
|
2013-06-23 12:21:25 -06:00
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
applyMaterial(tile,matLava,(float)d.bits.flow_size/7.0f,(float)d.bits.flow_size/7.0f);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
else if(shape==df::tiletype_shape::EMPTY || shape==df::tiletype_shape::RAMP_TOP
|
|
|
|
|| shape==df::tiletype_shape::STAIR_DOWN || shape==df::tiletype_shape::STAIR_UPDOWN)
|
|
|
|
{
|
|
|
|
if(bDown)
|
2013-06-23 12:21:25 -06:00
|
|
|
{
|
2013-06-24 15:59:32 -06:00
|
|
|
df::tile_designation d2=bDown->DesignationAt(gpos);
|
|
|
|
if(d2.bits.liquid_type && d2.bits.flow_size>0)
|
|
|
|
{
|
|
|
|
applyMaterial(tile,matLava);
|
|
|
|
}
|
2013-06-23 12:21:25 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 13:49:52 -06:00
|
|
|
|
|
|
|
|
2013-06-23 12:21:25 -06:00
|
|
|
}
|
2013-06-24 16:25:38 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
df::map_block* block=b->getRaw();
|
2013-06-24 16:25:38 -06:00
|
|
|
if(!block)
|
|
|
|
continue;
|
|
|
|
//flows
|
2013-06-23 10:25:42 -06:00
|
|
|
for(int i=0;i<block->flows.size();i++)
|
|
|
|
{
|
|
|
|
df::flow_info* f=block->flows[i];
|
|
|
|
if(f && f->density>0 && f->type==df::flow_type::Dragonfire || f->type==df::flow_type::Fire)
|
|
|
|
{
|
|
|
|
df::coord2d pos=f->pos;
|
2013-06-24 15:59:32 -06:00
|
|
|
pos=worldToViewportCoord(pos,vp,window2d);
|
|
|
|
int tile=getIndex(pos.x,pos.y);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(pos,vp))
|
2013-06-23 10:25:42 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf fireColor;
|
2013-06-23 10:25:42 -06:00
|
|
|
if(f->density>60)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
fireColor=rgbf(0.98f,0.91f,0.30f);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
else if(f->density>30)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
fireColor=rgbf(0.93f,0.16f,0.16f);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
fireColor=rgbf(0.64f,0.0f,0.0f);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-06-23 11:45:05 -06:00
|
|
|
lightSource fire(fireColor,f->density/5);
|
|
|
|
addLight(tile,fire);
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
|
2013-06-24 16:25:38 -06:00
|
|
|
//plants
|
2013-06-23 14:56:01 -06:00
|
|
|
for(int i=0;i<block->plants.size();i++)
|
|
|
|
{
|
|
|
|
df::plant* cPlant=block->plants[i];
|
2013-06-26 11:19:30 -06:00
|
|
|
if (cPlant->grow_counter <180000) //todo maybe smaller light/oclusion?
|
|
|
|
continue;
|
2013-06-23 14:56:01 -06:00
|
|
|
df::coord2d pos=cPlant->pos;
|
2013-06-24 15:59:32 -06:00
|
|
|
pos=worldToViewportCoord(pos,vp,window2d);
|
|
|
|
int tile=getIndex(pos.x,pos.y);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(pos,vp))
|
2013-06-23 14:56:01 -06:00
|
|
|
{
|
2013-06-24 16:25:38 -06:00
|
|
|
applyMaterial(tile,419,cPlant->material);
|
2013-06-23 14:56:01 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 13:49:52 -06:00
|
|
|
//blood and other goo
|
|
|
|
for(int i=0;i<block->block_events.size();i++)
|
|
|
|
{
|
|
|
|
df::block_square_event* ev=block->block_events[i];
|
|
|
|
df::block_square_event_type ev_type=ev->getType();
|
|
|
|
if(ev_type==df::block_square_event_type::material_spatter)
|
|
|
|
{
|
|
|
|
df::block_square_event_material_spatterst* spatter=static_cast<df::block_square_event_material_spatterst*>(ev);
|
2013-09-22 05:50:03 -06:00
|
|
|
matLightDef* m=getMaterialDef(spatter->mat_type,spatter->mat_index);
|
2013-06-25 13:49:52 -06:00
|
|
|
if(!m)
|
|
|
|
continue;
|
|
|
|
if(!m->isEmiting)
|
|
|
|
continue;
|
|
|
|
for(int x=0;x<16;x++)
|
|
|
|
for(int y=0;y<16;y++)
|
|
|
|
{
|
|
|
|
df::coord2d pos;
|
|
|
|
pos.x = blockX*16+x;
|
|
|
|
pos.y = blockY*16+y;
|
|
|
|
int16_t amount=spatter->amount[x][y];
|
|
|
|
if(amount<=0)
|
|
|
|
continue;
|
|
|
|
pos=worldToViewportCoord(pos,vp,window2d);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(pos,vp))
|
2013-06-25 13:49:52 -06:00
|
|
|
{
|
|
|
|
addLight(getIndex(pos.x,pos.y),m->makeSource((float)amount/100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
2013-06-23 11:45:05 -06:00
|
|
|
if(df::global::cursor->x>-30000)
|
|
|
|
{
|
|
|
|
int wx=df::global::cursor->x-window_x+vp.first.x;
|
|
|
|
int wy=df::global::cursor->y-window_y+vp.first.y;
|
|
|
|
int tile=getIndex(wx,wy);
|
2013-06-24 09:18:57 -06:00
|
|
|
applyMaterial(tile,matCursor);
|
2013-06-23 11:45:05 -06:00
|
|
|
}
|
2013-06-25 10:34:38 -06:00
|
|
|
//citizen only emit light, if defined
|
2013-09-22 05:50:03 -06:00
|
|
|
//or other creatures
|
|
|
|
if(matCitizen.isEmiting || creatureDefs.size()>0)
|
2013-06-23 16:45:45 -06:00
|
|
|
for (int i=0;i<df::global::world->units.active.size();++i)
|
|
|
|
{
|
|
|
|
df::unit *u = df::global::world->units.active[i];
|
2013-06-24 15:59:32 -06:00
|
|
|
coord2d pos=worldToViewportCoord(coord2d(u->pos.x,u->pos.y),vp,window2d);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(u->pos.z==window_z && isInRect(pos,vp))
|
2013-09-22 05:50:03 -06:00
|
|
|
{
|
|
|
|
if (DFHack::Units::isCitizen(u) && !u->counters.unconscious)
|
|
|
|
addLight(getIndex(pos.x,pos.y),matCitizen.makeSource());
|
|
|
|
creatureLightDef *def=getCreatureDef(u);
|
|
|
|
if(def && !u->flags1.bits.dead)
|
|
|
|
{
|
|
|
|
addLight(getIndex(pos.x,pos.y),def->light.makeSource());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//items
|
|
|
|
if(itemDefs.size()>0)
|
|
|
|
{
|
|
|
|
std::vector<df::item*>& vec=df::global::world->items.other[items_other_id::IN_PLAY];
|
|
|
|
for(size_t i=0;i<vec.size();i++)
|
|
|
|
{
|
|
|
|
df::item* curItem=vec[i];
|
|
|
|
df::coord itemPos=DFHack::Items::getPosition(curItem);
|
|
|
|
coord2d pos=worldToViewportCoord(itemPos,vp,window2d);
|
|
|
|
itemLightDef* mat=0;
|
|
|
|
if( itemPos.z==window_z && isInRect(pos,vp) && (mat=getItemDef(curItem)) )
|
|
|
|
{
|
|
|
|
if( ((mat->equiped || mat->haul ||mat->inBuilding ||mat->inContainer) && curItem->flags.bits.in_inventory)|| //TODO split this up
|
|
|
|
(mat->onGround && curItem->flags.bits.on_ground) )
|
|
|
|
{
|
|
|
|
if(mat->light.isEmiting)
|
|
|
|
addLight(getIndex(pos.x,pos.y),mat->light.makeSource());
|
|
|
|
if(!mat->light.isTransparent)
|
|
|
|
addOclusion(getIndex(pos.x,pos.y),mat->light.transparency,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
//buildings
|
|
|
|
for(size_t i = 0; i < df::global::world->buildings.all.size(); i++)
|
|
|
|
{
|
|
|
|
df::building *bld = df::global::world->buildings.all[i];
|
2013-06-25 16:18:26 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
if(window_z!=bld->z)
|
|
|
|
continue;
|
2013-06-25 16:18:26 -06:00
|
|
|
if(bld->getBuildStage()<bld->getMaxBuildStage()) //only work if fully built
|
|
|
|
continue;
|
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
df::coord2d p1(bld->x1,bld->y1);
|
|
|
|
df::coord2d p2(bld->x2,bld->y2);
|
|
|
|
p1=worldToViewportCoord(p1,vp,window2d);
|
2013-06-27 09:11:50 -06:00
|
|
|
p2=worldToViewportCoord(p2,vp,window2d);
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(p1,vp)||isInRect(p2,vp))
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-25 16:18:26 -06:00
|
|
|
|
2013-06-27 09:11:50 -06:00
|
|
|
int tile;
|
2013-06-29 11:55:07 -06:00
|
|
|
if(isInRect(p1,vp))
|
2013-06-27 09:11:50 -06:00
|
|
|
tile=getIndex(p1.x,p1.y); //TODO multitile buildings. How they would work?
|
|
|
|
else
|
|
|
|
tile=getIndex(p2.x,p2.y);
|
2013-06-24 15:59:32 -06:00
|
|
|
df::building_type type = bld->getType();
|
2013-09-22 05:50:03 -06:00
|
|
|
buildingLightDef* def=getBuildingDef(bld);
|
2013-06-25 16:18:26 -06:00
|
|
|
if(!def)
|
|
|
|
continue;
|
|
|
|
if(type==df::enums::building_type::Door)
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-25 16:18:26 -06:00
|
|
|
df::building_doorst* door=static_cast<df::building_doorst*>(bld);
|
|
|
|
if(!door->door_flags.bits.closed)
|
|
|
|
continue;
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
2013-06-28 12:17:54 -06:00
|
|
|
else if(type==df::enums::building_type::Floodgate)
|
|
|
|
{
|
|
|
|
df::building_floodgatest* gate=static_cast<df::building_floodgatest*>(bld);
|
|
|
|
if(!gate->gate_flags.bits.closed)
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
if(def->useMaterial)
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-09-22 05:50:03 -06:00
|
|
|
matLightDef* mat=getMaterialDef(bld->mat_type,bld->mat_index);
|
2013-06-25 16:18:26 -06:00
|
|
|
if(!mat)mat=&matWall;
|
2013-06-28 14:51:52 -06:00
|
|
|
if(!def->poweredOnly || !bld->isUnpowered()) //not powered. Add occlusion only.
|
2013-06-25 16:18:26 -06:00
|
|
|
{
|
2013-06-28 14:51:52 -06:00
|
|
|
if(def->light.isEmiting)
|
|
|
|
{
|
|
|
|
addLight(tile,def->light.makeSource(def->size));
|
|
|
|
}
|
|
|
|
else if(mat->isEmiting)
|
|
|
|
{
|
|
|
|
addLight(tile,mat->makeSource(def->size));
|
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
}
|
|
|
|
if(def->light.isTransparent)
|
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
addOclusion(tile,def->light.transparency,def->size);
|
2013-06-25 16:18:26 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-26 11:19:30 -06:00
|
|
|
addOclusion(tile,mat->transparency,def->size);
|
2013-06-25 16:18:26 -06:00
|
|
|
}
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
else
|
2013-06-24 15:59:32 -06:00
|
|
|
{
|
2013-06-28 14:51:52 -06:00
|
|
|
if(!def->poweredOnly || !bld->isUnpowered())//not powered. Add occlusion only.
|
|
|
|
addOclusion(tile,def->light.transparency,def->size);
|
|
|
|
else
|
|
|
|
applyMaterial(tile,def->light,def->size,def->thickness);
|
2013-06-24 15:59:32 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 16:45:45 -06:00
|
|
|
}
|
2013-06-25 13:49:52 -06:00
|
|
|
|
2013-06-23 16:45:45 -06:00
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf lua_parseLightCell(lua_State* L)
|
2013-06-24 09:18:57 -06:00
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf ret;
|
2013-06-24 09:18:57 -06:00
|
|
|
|
|
|
|
lua_pushnumber(L,1);
|
|
|
|
lua_gettable(L,-2);
|
|
|
|
ret.r=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
|
|
|
lua_pushnumber(L,2);
|
|
|
|
lua_gettable(L,-2);
|
|
|
|
ret.g=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
|
|
|
lua_pushnumber(L,3);
|
|
|
|
lua_gettable(L,-2);
|
|
|
|
ret.b=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
//Lua::GetOutput(L)->print("got cell(%f,%f,%f)\n",ret.r,ret.g,ret.b);
|
2013-06-24 09:18:57 -06:00
|
|
|
return ret;
|
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
#define GETLUAFLAG(field,name) lua_getfield(L,-1,"flags");\
|
|
|
|
if(lua_isnil(L,-1)){field=false;}\
|
|
|
|
else{lua_getfield(L,-1,#name);field=lua_isnil(L,-1);lua_pop(L,1);}\
|
|
|
|
lua_pop(L,1)
|
2013-06-26 13:05:22 -06:00
|
|
|
|
|
|
|
#define GETLUANUMBER(field,name) lua_getfield(L,-1,#name);\
|
|
|
|
if(!lua_isnil(L,-1) && lua_isnumber(L,-1))field=lua_tonumber(L,-1);\
|
|
|
|
lua_pop(L,1)
|
2013-06-24 09:18:57 -06:00
|
|
|
matLightDef lua_parseMatDef(lua_State* L)
|
|
|
|
{
|
|
|
|
|
|
|
|
matLightDef ret;
|
|
|
|
lua_getfield(L,-1,"tr");
|
|
|
|
if(ret.isTransparent=!lua_isnil(L,-1))
|
|
|
|
{
|
|
|
|
ret.transparency=lua_parseLightCell(L);
|
|
|
|
}
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
|
|
|
lua_getfield(L,-1,"em");
|
|
|
|
if(ret.isEmiting=!lua_isnil(L,-1))
|
|
|
|
{
|
|
|
|
ret.emitColor=lua_parseLightCell(L);
|
|
|
|
lua_pop(L,1);
|
|
|
|
lua_getfield(L,-1,"rad");
|
|
|
|
if(lua_isnil(L,-1))
|
|
|
|
{
|
|
|
|
lua_pop(L,1);
|
|
|
|
luaL_error(L,"Material has emittance but no radius");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret.radius=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pop(L,1);
|
2013-06-25 16:18:26 -06:00
|
|
|
GETLUAFLAG(ret.flicker,"flicker");
|
2013-06-24 09:18:57 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int lightingEngineViewscreen::parseMaterials(lua_State* L)
|
|
|
|
{
|
|
|
|
auto engine= (lightingEngineViewscreen*)lua_touserdata(L, 1);
|
|
|
|
engine->matDefs.clear();
|
|
|
|
//color_ostream* os=Lua::GetOutput(L);
|
|
|
|
Lua::StackUnwinder unwinder(L);
|
|
|
|
lua_getfield(L,2,"materials");
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Materials table not found.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
int type=lua_tonumber(L,-2);
|
|
|
|
//os->print("Processing type:%d\n",type);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
int index=lua_tonumber(L,-2);
|
|
|
|
//os->print("\tProcessing index:%d\n",index);
|
|
|
|
engine->matDefs[std::make_pair(type,index)]=lua_parseMatDef(L);
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#define LOAD_SPECIAL(lua_name,class_name) \
|
|
|
|
lua_getfield(L,-1,#lua_name);\
|
|
|
|
if(!lua_isnil(L,-1))engine->class_name=lua_parseMatDef(L);\
|
|
|
|
lua_pop(L,1)
|
|
|
|
int lightingEngineViewscreen::parseSpecial(lua_State* L)
|
|
|
|
{
|
|
|
|
auto engine= (lightingEngineViewscreen*)lua_touserdata(L, 1);
|
|
|
|
Lua::StackUnwinder unwinder(L);
|
|
|
|
lua_getfield(L,2,"special");
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Special table not found.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
LOAD_SPECIAL(LAVA,matLava);
|
2013-06-24 15:59:32 -06:00
|
|
|
LOAD_SPECIAL(WATER,matWater);
|
2013-06-24 09:18:57 -06:00
|
|
|
LOAD_SPECIAL(FROZEN_LIQUID,matIce);
|
|
|
|
LOAD_SPECIAL(AMBIENT,matAmbience);
|
|
|
|
LOAD_SPECIAL(CURSOR,matCursor);
|
2013-06-25 10:34:38 -06:00
|
|
|
LOAD_SPECIAL(CITIZEN,matCitizen);
|
2013-06-26 13:05:22 -06:00
|
|
|
GETLUANUMBER(engine->levelDim,levelDim);
|
|
|
|
GETLUANUMBER(engine->dayHour,dayHour);
|
2013-06-26 15:14:03 -06:00
|
|
|
GETLUANUMBER(engine->daySpeed,daySpeed);
|
|
|
|
lua_getfield(L,-1,"dayColors");
|
|
|
|
if(lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
engine->dayColors.clear();
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
engine->dayColors.push_back(lua_parseLightCell(L));
|
|
|
|
lua_pop(L,1);
|
|
|
|
}
|
|
|
|
lua_pop(L,1);
|
|
|
|
}
|
2013-06-24 09:18:57 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef LOAD_SPECIAL
|
2013-09-22 05:50:03 -06:00
|
|
|
int lightingEngineViewscreen::parseItems(lua_State* L)
|
|
|
|
{
|
|
|
|
auto engine= (lightingEngineViewscreen*)lua_touserdata(L, 1);
|
|
|
|
engine->itemDefs.clear();
|
|
|
|
Lua::StackUnwinder unwinder(L);
|
|
|
|
lua_getfield(L,2,"items");
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Items table not found.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
luaL_error(L,"Broken item definitions.");
|
|
|
|
lua_getfield(L,-1,"type");
|
|
|
|
int type=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
lua_getfield(L,-1,"subtype");
|
|
|
|
int subtype=luaL_optinteger(L,-1,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
itemLightDef item;
|
|
|
|
lua_getfield(L,-1,"light");
|
|
|
|
item.light=lua_parseMatDef(L);
|
|
|
|
GETLUAFLAG(item.haul,"hauling");
|
|
|
|
GETLUAFLAG(item.equiped,"equiped");
|
|
|
|
GETLUAFLAG(item.inBuilding,"inBuilding");
|
|
|
|
GETLUAFLAG(item.inContainer,"contained");
|
|
|
|
GETLUAFLAG(item.onGround,"onGround");
|
|
|
|
GETLUAFLAG(item.useMaterial,"useMaterial");
|
|
|
|
engine->itemDefs[std::make_pair(type,subtype)]=item;
|
|
|
|
lua_pop(L,2);
|
|
|
|
}
|
|
|
|
lua_pop(L,1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int lightingEngineViewscreen::parseCreatures(lua_State* L)
|
|
|
|
{
|
|
|
|
auto engine= (lightingEngineViewscreen*)lua_touserdata(L, 1);
|
|
|
|
engine->creatureDefs.clear();
|
|
|
|
Lua::StackUnwinder unwinder(L);
|
|
|
|
lua_getfield(L,2,"creatures");
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Creatures table not found.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
luaL_error(L,"Broken creature definitions.");
|
|
|
|
lua_getfield(L,-1,"race");
|
|
|
|
int race=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
lua_getfield(L,-1,"caste");
|
|
|
|
int caste=lua_tonumber(L,-1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
creatureLightDef cr;
|
|
|
|
lua_getfield(L,-1,"light");
|
|
|
|
cr.light=lua_parseMatDef(L);
|
|
|
|
engine->creatureDefs[std::make_pair(race,caste)]=cr;
|
|
|
|
lua_pop(L,2);
|
|
|
|
}
|
|
|
|
lua_pop(L,1);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
int lightingEngineViewscreen::parseBuildings(lua_State* L)
|
|
|
|
{
|
|
|
|
auto engine= (lightingEngineViewscreen*)lua_touserdata(L, 1);
|
|
|
|
engine->buildingDefs.clear();
|
|
|
|
Lua::StackUnwinder unwinder(L);
|
|
|
|
lua_getfield(L,2,"buildings");
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Buildings table not found.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
int type=lua_tonumber(L,-2);
|
|
|
|
if(!lua_istable(L,-1))
|
|
|
|
{
|
|
|
|
luaL_error(L,"Broken building definitions.");
|
|
|
|
}
|
|
|
|
//os->print("Processing type:%d\n",type);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
int subtype=lua_tonumber(L,-2);
|
|
|
|
//os->print("\tProcessing subtype:%d\n",index);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
int custom=lua_tonumber(L,-2);
|
|
|
|
//os->print("\tProcessing custom:%d\n",index);
|
|
|
|
buildingLightDef current;
|
|
|
|
current.light=lua_parseMatDef(L);
|
|
|
|
engine->buildingDefs[std::make_tuple(type,subtype,custom)]=current;
|
|
|
|
GETLUAFLAG(current.poweredOnly,"poweredOnly");
|
|
|
|
GETLUAFLAG(current.useMaterial,"useMaterial");
|
2013-06-26 11:19:30 -06:00
|
|
|
|
|
|
|
lua_getfield(L,-1,"size");
|
|
|
|
current.size=luaL_optnumber(L,-1,1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
|
|
|
lua_getfield(L,-1,"thickness");
|
|
|
|
current.thickness=luaL_optnumber(L,-1,1);
|
|
|
|
lua_pop(L,1);
|
|
|
|
|
2013-06-25 16:18:26 -06:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
lua_pop(L,1);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-24 09:18:57 -06:00
|
|
|
void lightingEngineViewscreen::defaultSettings()
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
matAmbience=matLightDef(rgbf(0.85f,0.85f,0.85f));
|
|
|
|
matLava=matLightDef(rgbf(0.8f,0.2f,0.2f),rgbf(0.8f,0.2f,0.2f),5);
|
|
|
|
matWater=matLightDef(rgbf(0.6f,0.6f,0.8f));
|
|
|
|
matIce=matLightDef(rgbf(0.7f,0.7f,0.9f));
|
|
|
|
matCursor=matLightDef(rgbf(0.96f,0.84f,0.03f),11);
|
2013-06-24 09:18:57 -06:00
|
|
|
matCursor.flicker=true;
|
2013-06-30 06:05:59 -06:00
|
|
|
matWall=matLightDef(rgbf(0,0,0));
|
|
|
|
matCitizen=matLightDef(rgbf(0.8f,0.8f,0.9f),6);
|
2013-06-25 10:34:38 -06:00
|
|
|
levelDim=0.2f;
|
2013-06-26 13:05:22 -06:00
|
|
|
dayHour=-1;
|
2013-06-26 15:14:03 -06:00
|
|
|
daySpeed=1;
|
2013-06-30 06:05:59 -06:00
|
|
|
dayColors.push_back(rgbf(0,0,0));
|
|
|
|
dayColors.push_back(rgbf(1,1,1));
|
|
|
|
dayColors.push_back(rgbf(0,0,0));
|
2013-06-24 09:18:57 -06:00
|
|
|
}
|
|
|
|
void lightingEngineViewscreen::loadSettings()
|
|
|
|
{
|
|
|
|
|
|
|
|
const std::string settingsfile="rendermax.lua";
|
|
|
|
CoreSuspender lock;
|
|
|
|
color_ostream_proxy out(Core::getInstance().getConsole());
|
|
|
|
|
|
|
|
lua_State* s=DFHack::Lua::Core::State;
|
|
|
|
lua_newtable(s);
|
|
|
|
int env=lua_gettop(s);
|
|
|
|
try{
|
|
|
|
int ret=luaL_loadfile(s,settingsfile.c_str());
|
|
|
|
if(ret==LUA_ERRFILE)
|
|
|
|
{
|
|
|
|
out.printerr("File not found:%s\n",settingsfile.c_str());
|
2013-06-25 10:34:38 -06:00
|
|
|
lua_pop(s,1);
|
2013-06-24 09:18:57 -06:00
|
|
|
}
|
|
|
|
else if(ret==LUA_ERRSYNTAX)
|
|
|
|
{
|
|
|
|
out.printerr("Syntax error:\n\t%s\n",lua_tostring(s,-1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
|
|
|
|
if(Lua::SafeCall(out,s,1,0))
|
|
|
|
{
|
|
|
|
lua_pushcfunction(s, parseMaterials);
|
|
|
|
lua_pushlightuserdata(s, this);
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
Lua::SafeCall(out,s,2,0);
|
|
|
|
out.print("%d materials loaded\n",matDefs.size());
|
|
|
|
|
|
|
|
lua_pushcfunction(s, parseSpecial);
|
|
|
|
lua_pushlightuserdata(s, this);
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
Lua::SafeCall(out,s,2,0);
|
2013-06-26 15:14:03 -06:00
|
|
|
out.print("%d day light colors loaded\n",dayColors.size());
|
2013-06-25 16:18:26 -06:00
|
|
|
|
|
|
|
lua_pushcfunction(s, parseBuildings);
|
|
|
|
lua_pushlightuserdata(s, this);
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
Lua::SafeCall(out,s,2,0);
|
|
|
|
out.print("%d buildings loaded\n",buildingDefs.size());
|
2013-09-22 05:50:03 -06:00
|
|
|
|
|
|
|
lua_pushcfunction(s, parseCreatures);
|
|
|
|
lua_pushlightuserdata(s, this);
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
Lua::SafeCall(out,s,2,0);
|
|
|
|
out.print("%d creatures loaded\n",creatureDefs.size());
|
|
|
|
|
|
|
|
lua_pushcfunction(s, parseItems);
|
|
|
|
lua_pushlightuserdata(s, this);
|
|
|
|
lua_pushvalue(s,env);
|
|
|
|
Lua::SafeCall(out,s,2,0);
|
|
|
|
out.print("%d items loaded\n",itemDefs.size());
|
2013-06-24 09:18:57 -06:00
|
|
|
}
|
2013-06-25 10:34:38 -06:00
|
|
|
|
2013-06-24 09:18:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
out.printerr("%s",e.what());
|
|
|
|
}
|
|
|
|
lua_pop(s,1);
|
|
|
|
}
|
2013-06-25 16:18:26 -06:00
|
|
|
#undef GETLUAFLAG
|
2013-06-26 15:19:18 -06:00
|
|
|
#undef GETLUANUMBER
|
2013-06-29 11:55:07 -06:00
|
|
|
/*
|
|
|
|
* Threading stuff
|
|
|
|
*/
|
|
|
|
lightThread::lightThread( lightThreadDispatch& dispatch ):dispatch(dispatch),isDone(false),myThread(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
lightThread::~lightThread()
|
|
|
|
{
|
2013-07-03 01:44:23 -06:00
|
|
|
delete myThread;
|
2013-06-29 11:55:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void lightThread::run()
|
|
|
|
{
|
|
|
|
while(!isDone)
|
|
|
|
{
|
2013-06-30 13:03:07 -06:00
|
|
|
{//wait for occlusion (and lights) to be ready
|
|
|
|
tthread::lock_guard<tthread::mutex> guard(dispatch.occlusionMutex);
|
|
|
|
if(!dispatch.occlusionReady)
|
2013-06-29 11:55:07 -06:00
|
|
|
dispatch.occlusionDone.wait(dispatch.occlusionMutex);//wait for work
|
2013-06-30 13:03:07 -06:00
|
|
|
if(dispatch.unprocessed.size()==0 || !dispatch.occlusionReady) //spurious wake-up
|
2013-06-29 11:55:07 -06:00
|
|
|
continue;
|
|
|
|
if(dispatch.occlusion.size()!=canvas.size()) //oh no somebody resized stuff
|
|
|
|
canvas.resize(dispatch.occlusion.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{ //get my rectangle (any will do)
|
|
|
|
tthread::lock_guard<tthread::mutex> guard(dispatch.unprocessedMutex);
|
2013-06-30 13:03:07 -06:00
|
|
|
if (dispatch.unprocessed.size()==0)
|
|
|
|
{
|
|
|
|
//wtf?? why?!
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-29 11:55:07 -06:00
|
|
|
myRect=dispatch.unprocessed.top();
|
|
|
|
dispatch.unprocessed.pop();
|
2013-06-30 13:03:07 -06:00
|
|
|
if (dispatch.unprocessed.size()==0)
|
|
|
|
{
|
|
|
|
dispatch.occlusionReady=false;
|
|
|
|
}
|
|
|
|
|
2013-06-29 11:55:07 -06:00
|
|
|
}
|
|
|
|
work();
|
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::mutex> guard(dispatch.writeLock);
|
|
|
|
combine();//write it back
|
|
|
|
dispatch.writeCount++;
|
|
|
|
}
|
|
|
|
dispatch.writesDone.notify_one();//tell about it to the dispatch.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightThread::work()
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
canvas.assign(canvas.size(),rgbf(0,0,0));
|
2013-06-29 11:55:07 -06:00
|
|
|
for(int i=myRect.first.x;i<myRect.second.x;i++)
|
|
|
|
for(int j=myRect.first.y;j<myRect.second.y;j++)
|
|
|
|
{
|
|
|
|
doLight(i,j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightThread::combine()
|
|
|
|
{
|
|
|
|
for(int i=0;i<canvas.size();i++)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf& c=dispatch.lightMap[i];
|
2013-06-29 11:55:07 -06:00
|
|
|
c=blend(c,canvas[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf lightThread::lightUpCell(rgbf power,int dx,int dy,int tx,int ty)
|
2013-06-29 11:55:07 -06:00
|
|
|
{
|
|
|
|
int h=dispatch.getH();
|
|
|
|
if(isInRect(coord2d(tx,ty),dispatch.viewPort))
|
|
|
|
{
|
|
|
|
size_t tile=tx*h+ty;
|
|
|
|
int dsq=dx*dx+dy*dy;
|
|
|
|
float dt=1;
|
|
|
|
if(dsq == 1)
|
|
|
|
dt=1;
|
|
|
|
else if(dsq == 2)
|
|
|
|
dt = RootTwo;
|
|
|
|
else if(dsq == 0)
|
|
|
|
dt = 0;
|
|
|
|
else
|
|
|
|
dt=sqrt((float)dsq);
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf& v=dispatch.occlusion[tile];
|
2013-06-29 11:55:07 -06:00
|
|
|
lightSource& ls=dispatch.lights[tile];
|
|
|
|
bool wallhack=false;
|
|
|
|
if(v.r+v.g+v.b==0)
|
|
|
|
wallhack=true;
|
|
|
|
|
|
|
|
if (dsq>0 && !wallhack)
|
|
|
|
{
|
|
|
|
power*=v.pow(dt);
|
|
|
|
}
|
|
|
|
if(ls.radius>0 && dsq>0)
|
|
|
|
{
|
|
|
|
if(power<=ls.power)
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf();
|
2013-06-29 11:55:07 -06:00
|
|
|
}
|
|
|
|
//float dt=sqrt(dsq);
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf oldCol=canvas[tile];
|
|
|
|
rgbf ncol=blendMax(power,oldCol);
|
2013-06-29 11:55:07 -06:00
|
|
|
canvas[tile]=ncol;
|
|
|
|
|
|
|
|
if(wallhack)
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf();
|
2013-06-29 11:55:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
return power;
|
|
|
|
}
|
|
|
|
else
|
2013-06-30 06:05:59 -06:00
|
|
|
return rgbf();
|
2013-06-29 11:55:07 -06:00
|
|
|
}
|
2013-09-22 05:50:03 -06:00
|
|
|
void lightThread::doRay(const rgbf& power,int cx,int cy,int tx,int ty)
|
2013-06-29 11:55:07 -06:00
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
|
|
|
plotLine(cx,cy,tx,ty,power,std::bind(&lightThread::lightUpCell,this,_1,_2,_3,_4,_5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightThread::doLight( int x,int y )
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
lightSource& csource=dispatch.lights[x*dispatch.getH()+y];
|
|
|
|
if(csource.radius>0)
|
|
|
|
{
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf power=csource.power;
|
2013-06-29 11:55:07 -06:00
|
|
|
int radius =csource.radius;
|
|
|
|
if(csource.flicker)
|
|
|
|
{
|
|
|
|
float flicker=(rand()/(float)RAND_MAX)/2.0f+0.5f;
|
|
|
|
radius*=flicker;
|
|
|
|
power=power*flicker;
|
|
|
|
}
|
2013-06-30 06:05:59 -06:00
|
|
|
rgbf surrounds;
|
2013-06-29 11:55:07 -06:00
|
|
|
lightUpCell( power, 0, 0,x, y);
|
|
|
|
for(int i=-1;i<2;i++)
|
|
|
|
for(int j=-1;j<2;j++)
|
|
|
|
if(i!=0||j!=0)
|
|
|
|
surrounds += lightUpCell( power, i, j,x+i, y+j);
|
|
|
|
if(surrounds.dot(surrounds)>0.00001f)
|
|
|
|
{
|
|
|
|
plotSquare(x,y,radius,
|
|
|
|
std::bind(&lightThread::doRay,this,power,x,y,_1,_2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void lightThreadDispatch::signalDoneOcclusion()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::mutex> guardWrite(writeLock);
|
|
|
|
writeCount=0;
|
|
|
|
}
|
2013-06-30 13:03:07 -06:00
|
|
|
tthread::lock_guard<tthread::mutex> guard1(occlusionMutex);
|
|
|
|
tthread::lock_guard<tthread::mutex> guard2(unprocessedMutex);
|
|
|
|
while(!unprocessed.empty())
|
|
|
|
unprocessed.pop();
|
2013-06-29 11:55:07 -06:00
|
|
|
viewPort=getMapViewport();
|
|
|
|
int threadCount=threadPool.size();
|
|
|
|
int w=viewPort.second.x-viewPort.first.x;
|
|
|
|
int slicew=w/threadCount;
|
|
|
|
for(int i=0;i<threadCount;i++)
|
|
|
|
{
|
|
|
|
rect2d area=viewPort;
|
|
|
|
area.first.x=viewPort.first.x+i*slicew;
|
|
|
|
if(i==threadCount-1)
|
|
|
|
area.second.x=viewPort.second.x; //fix, when area is even number or sth.
|
|
|
|
else
|
|
|
|
area.second.x=viewPort.first.x+(i+1)*slicew;
|
|
|
|
unprocessed.push(area);
|
|
|
|
}
|
2013-06-30 13:03:07 -06:00
|
|
|
occlusionReady=true;
|
2013-06-29 11:55:07 -06:00
|
|
|
occlusionDone.notify_all();
|
|
|
|
}
|
|
|
|
|
2013-06-30 13:03:07 -06:00
|
|
|
lightThreadDispatch::lightThreadDispatch( lightingEngineViewscreen* p ):parent(p),lights(parent->lights),occlusion(parent->ocupancy),
|
|
|
|
lightMap(parent->lightMap),writeCount(0),occlusionReady(false)
|
2013-06-29 11:55:07 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightThreadDispatch::shutdown()
|
|
|
|
{
|
|
|
|
for(int i=0;i<threadPool.size();i++)
|
|
|
|
{
|
|
|
|
threadPool[i]->isDone=true;
|
2013-06-30 13:03:07 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
occlusionDone.notify_all();//if stuck signal that you are done with stuff.
|
|
|
|
for(int i=0;i<threadPool.size();i++)
|
|
|
|
{
|
2013-06-29 11:55:07 -06:00
|
|
|
threadPool[i]->myThread->join();
|
|
|
|
}
|
|
|
|
threadPool.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int lightThreadDispatch::getW()
|
|
|
|
{
|
|
|
|
return parent->getW();
|
|
|
|
}
|
|
|
|
|
|
|
|
int lightThreadDispatch::getH()
|
|
|
|
{
|
|
|
|
return parent->getH();
|
|
|
|
}
|
|
|
|
void threadStub(void * arg)
|
|
|
|
{
|
|
|
|
if(arg)
|
|
|
|
((lightThread*)arg)->run();
|
|
|
|
}
|
|
|
|
void lightThreadDispatch::start(int count)
|
|
|
|
{
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
{
|
|
|
|
std::unique_ptr<lightThread> nthread(new lightThread(*this));
|
|
|
|
nthread->myThread=new tthread::thread(&threadStub,nthread.get());
|
|
|
|
threadPool.push_back(std::move(nthread));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lightThreadDispatch::waitForWrites()
|
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::mutex> guard(writeLock);
|
|
|
|
while(threadPool.size()>writeCount)//missed it somehow already.
|
|
|
|
{
|
|
|
|
writesDone.wait(writeLock); //if not, wait a bit
|
|
|
|
}
|
|
|
|
}
|
2013-06-30 13:03:07 -06:00
|
|
|
|
|
|
|
lightThreadDispatch::~lightThreadDispatch()
|
|
|
|
{
|
|
|
|
shutdown();
|
|
|
|
}
|