|
|
@ -4,6 +4,7 @@
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <math.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "tinythread.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "LuaTools.h"
|
|
|
|
#include "LuaTools.h"
|
|
|
|
|
|
|
|
|
|
|
@ -23,13 +24,103 @@
|
|
|
|
#include "df/plant.h"
|
|
|
|
#include "df/plant.h"
|
|
|
|
#include "df/plant_raw.h"
|
|
|
|
#include "df/plant_raw.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
using df::global::gps;
|
|
|
|
using df::global::gps;
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace DFHack;
|
|
|
|
using df::coord2d;
|
|
|
|
using df::coord2d;
|
|
|
|
|
|
|
|
using namespace tthread;
|
|
|
|
|
|
|
|
|
|
|
|
const float RootTwo = 1.4142135623730950488016887242097f;
|
|
|
|
const float RootTwo = 1.4142135623730950488016887242097f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::lightWorkerThread(void * arg)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int thisIndex;
|
|
|
|
|
|
|
|
std::vector<lightCell> canvas;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
writeMutex.lock();
|
|
|
|
|
|
|
|
writeMutex.unlock(); //Don't start till write access is given.
|
|
|
|
|
|
|
|
indexMutex.lock();
|
|
|
|
|
|
|
|
if(nextIndex == -1) //The worker threads should keep going until, and including, index 0.
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
indexMutex.unlock();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(nextIndex == -2)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
indexMutex.unlock();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
thisIndex = nextIndex;
|
|
|
|
|
|
|
|
nextIndex--;
|
|
|
|
|
|
|
|
indexMutex.unlock();
|
|
|
|
|
|
|
|
if(canvas.size() != lightMap.size())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
canvas.resize(lightMap.size(), lightCell(0,0,0));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
doLight(canvas, thisIndex);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
writeMutex.lock();
|
|
|
|
|
|
|
|
for(int i = 0; i < canvas.size(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lightMap[i] = blend(lightMap[i], canvas[i]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
writeMutex.unlock();
|
|
|
|
|
|
|
|
canvas.assign(canvas.size(),lightCell(0,0,0));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lightingEngineViewscreen::~lightingEngineViewscreen()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
indexMutex.lock();
|
|
|
|
|
|
|
|
nextIndex = -2;
|
|
|
|
|
|
|
|
indexMutex.unlock();
|
|
|
|
|
|
|
|
writeMutex.unlock();
|
|
|
|
|
|
|
|
for(int i = 0; i < threadList.size(); i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(threadList[i])
|
|
|
|
|
|
|
|
threadList[i]->join();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void threadStub(void * arg)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(arg)
|
|
|
|
|
|
|
|
((lightingEngineViewscreen*)arg)->lightWorkerThread(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::doLightThreads()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
nextIndex = 0;
|
|
|
|
|
|
|
|
int num_threads = thread::hardware_concurrency();
|
|
|
|
|
|
|
|
if(num_threads < 1) num_threads = 1;
|
|
|
|
|
|
|
|
if(threadList.empty())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
threadList.resize(num_threads, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < num_threads; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
threadList[i] = new thread(threadStub, this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nextIndex = lightMap.size() - 1; //start at the largest valid index
|
|
|
|
|
|
|
|
writeMutex.unlock();
|
|
|
|
|
|
|
|
for(int i = 0; i < num_threads; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
threadList[i]->join();
|
|
|
|
|
|
|
|
delete threadList[i];
|
|
|
|
|
|
|
|
threadList[i]=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
writeMutex.lock();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lightSource::lightSource(lightCell power,int radius):power(power),flicker(false)
|
|
|
|
lightSource::lightSource(lightCell power,int radius):power(power),flicker(false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(radius >= 0)
|
|
|
|
if(radius >= 0)
|
|
|
@ -90,6 +181,7 @@ lightingEngineViewscreen::lightingEngineViewscreen(renderer_light* target):light
|
|
|
|
reinit();
|
|
|
|
reinit();
|
|
|
|
defaultSettings();
|
|
|
|
defaultSettings();
|
|
|
|
loadSettings();
|
|
|
|
loadSettings();
|
|
|
|
|
|
|
|
writeMutex.lock(); //This is needed for later when the threads will all want to write to the buffer.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::reinit()
|
|
|
|
void lightingEngineViewscreen::reinit()
|
|
|
@ -148,11 +240,15 @@ void plotLine(int x0, int y0, int x1, int y1,std::function<bool(int,int,int,int)
|
|
|
|
if (e2 <= dx) { err += dx; y0 += sy; rdy=sy;} /* e_xy+e_y < 0 */
|
|
|
|
if (e2 <= dx) { err += dx; y0 += sy; rdy=sy;} /* e_xy+e_y < 0 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lightCell blend(lightCell a,lightCell b)
|
|
|
|
lightCell blendMax(lightCell a,lightCell b)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return lightCell(std::max(a.r,b.r),std::max(a.g,b.g),std::max(a.b,b.b));
|
|
|
|
return lightCell(std::max(a.r,b.r),std::max(a.g,b.g),std::max(a.b,b.b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool lightingEngineViewscreen::lightUpCell(lightCell& power,int dx,int dy,int tx,int ty)
|
|
|
|
lightCell blend(lightCell a,lightCell b)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return blendMax(a,b);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lightingEngineViewscreen::lightUpCell(std::vector<lightCell> & target, lightCell& power,int dx,int dy,int tx,int ty)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(isInViewport(coord2d(tx,ty),mapPort))
|
|
|
|
if(isInViewport(coord2d(tx,ty),mapPort))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -183,9 +279,9 @@ bool lightingEngineViewscreen::lightUpCell(lightCell& power,int dx,int dy,int tx
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//float dt=sqrt(dsq);
|
|
|
|
//float dt=sqrt(dsq);
|
|
|
|
lightCell oldCol=lightMap[tile];
|
|
|
|
lightCell oldCol=target[tile];
|
|
|
|
lightCell ncol=blend(power,oldCol);
|
|
|
|
lightCell ncol=blendMax(power,oldCol);
|
|
|
|
lightMap[tile]=ncol;
|
|
|
|
target[tile]=ncol;
|
|
|
|
|
|
|
|
|
|
|
|
if(wallhack)
|
|
|
|
if(wallhack)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -195,50 +291,56 @@ bool lightingEngineViewscreen::lightUpCell(lightCell& power,int dx,int dy,int tx
|
|
|
|
else
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void lightingEngineViewscreen::doRay(lightCell power,int cx,int cy,int tx,int ty)
|
|
|
|
void lightingEngineViewscreen::doRay(std::vector<lightCell> & target, lightCell power,int cx,int cy,int tx,int ty)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
using namespace std::placeholders;
|
|
|
|
lightCell curPower=power;
|
|
|
|
lightCell curPower=power;
|
|
|
|
plotLine(cx,cy,tx,ty,std::bind(&lightingEngineViewscreen::lightUpCell,this,std::ref(curPower),_1,_2,_3,_4));
|
|
|
|
plotLine(cx,cy,tx,ty,std::bind(&lightingEngineViewscreen::lightUpCell,this,std::ref(target),std::ref(curPower),_1,_2,_3,_4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void lightingEngineViewscreen::doFovs()
|
|
|
|
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::doLight(std::vector<lightCell> & target, int index)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mapPort=getMapViewport();
|
|
|
|
|
|
|
|
using namespace std::placeholders;
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
|
|
|
lightSource& csource=lights[index];
|
|
|
|
for(int i=mapPort.first.x;i<mapPort.second.x;i++)
|
|
|
|
if(csource.radius>0)
|
|
|
|
for(int j=mapPort.first.y;j<mapPort.second.y;j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
coord2d coord = getCoords(index);
|
|
|
|
|
|
|
|
int i = coord.x;
|
|
|
|
|
|
|
|
int j = coord.y;
|
|
|
|
|
|
|
|
lightCell power=csource.power;
|
|
|
|
|
|
|
|
int radius =csource.radius;
|
|
|
|
|
|
|
|
if(csource.flicker)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lightSource& csource=lights[getIndex(i,j)];
|
|
|
|
float flicker=(rand()/(float)RAND_MAX)/2.0f+0.5f;
|
|
|
|
if(csource.radius>0)
|
|
|
|
radius*=flicker;
|
|
|
|
{
|
|
|
|
power=power*flicker;
|
|
|
|
lightCell power=csource.power;
|
|
|
|
}
|
|
|
|
int radius =csource.radius;
|
|
|
|
int surrounds = 0;
|
|
|
|
if(csource.flicker)
|
|
|
|
lightCell curPower;
|
|
|
|
{
|
|
|
|
|
|
|
|
float flicker=(rand()/(float)RAND_MAX)/2.0f+0.5f;
|
|
|
|
|
|
|
|
radius*=flicker;
|
|
|
|
|
|
|
|
power=power*flicker;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int surrounds = 0;
|
|
|
|
|
|
|
|
lightCell curPower;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lightUpCell(curPower = power, 0, 0,i+0, j+0);
|
|
|
|
lightUpCell(target, curPower = power, 0, 0,i+0, j+0);
|
|
|
|
{
|
|
|
|
{
|
|
|
|
surrounds += lightUpCell(curPower = power, 0, 1,i+0, j+1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power, 0, 1,i+0, j+1);
|
|
|
|
surrounds += lightUpCell(curPower = power, 1, 1,i+1, j+1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power, 1, 1,i+1, j+1);
|
|
|
|
surrounds += lightUpCell(curPower = power, 1, 0,i+1, j+0);
|
|
|
|
surrounds += lightUpCell(target, curPower = power, 1, 0,i+1, j+0);
|
|
|
|
surrounds += lightUpCell(curPower = power, 1,-1,i+1, j-1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power, 1,-1,i+1, j-1);
|
|
|
|
surrounds += lightUpCell(curPower = power, 0,-1,i+0, j-1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power, 0,-1,i+0, j-1);
|
|
|
|
surrounds += lightUpCell(curPower = power,-1,-1,i-1, j-1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power,-1,-1,i-1, j-1);
|
|
|
|
surrounds += lightUpCell(curPower = power,-1, 0,i-1, j+0);
|
|
|
|
surrounds += lightUpCell(target, curPower = power,-1, 0,i-1, j+0);
|
|
|
|
surrounds += lightUpCell(curPower = power,-1, 1,i-1, j+1);
|
|
|
|
surrounds += lightUpCell(target, curPower = power,-1, 1,i-1, j+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(surrounds)
|
|
|
|
|
|
|
|
plotSquare(i,j,radius,
|
|
|
|
|
|
|
|
std::bind(&lightingEngineViewscreen::doRay,this,power,i,j,_1,_2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(surrounds)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
plotSquare(i,j,radius,
|
|
|
|
|
|
|
|
std::bind(&lightingEngineViewscreen::doRay,this,std::ref(target),power,i,j,_1,_2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void lightingEngineViewscreen::doFovs()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mapPort=getMapViewport();
|
|
|
|
|
|
|
|
doLightThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void lightingEngineViewscreen::clear()
|
|
|
|
void lightingEngineViewscreen::clear()
|
|
|
|
{
|
|
|
|
{
|
|
|
|