Skip to content

Commit

Permalink
HEAC now checks for heat insulators
Browse files Browse the repository at this point in the history
Also testing c++ stuff, maybe reuse this function for GOLD later
  • Loading branch information
jacob1 committed Jun 14, 2017
1 parent d8edb3c commit 4214f85
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions src/simulation/elements/HEAC.cpp
@@ -1,3 +1,5 @@
#include <algorithm>
#include <functional>
#include "simulation/Elements.h"
#include "simulation/Air.h"
//#TPT-Directive ElementClass Element_HEAC PT_HEAC 180
Expand Down Expand Up @@ -46,6 +48,79 @@ Element_HEAC::Element_HEAC()
Update = &Element_HEAC::update;
}

//#TPT-Directive ElementHeader Element_HEAC struct IsInsulator
struct Element_HEAC::IsInsulator : public std::binary_function<Simulation*,int,bool> {
bool operator() (Simulation* a, int b) {return b && a->elements[b].HeatConduct == 0;}
};
//#TPT-Directive ElementHeader Element_HEAC static IsInsulator isInsulator
Element_HEAC::IsInsulator Element_HEAC::isInsulator = Element_HEAC::IsInsulator();

// If this is used elsewhere (GOLD), it should be moved into Simulation.h
//#TPT-Directive ElementHeader Element_HEAC template<class BinaryPredicate> static bool CheckLine(Simulation* sim, int x1, int y1, int x2, int y2, BinaryPredicate func)
template<class BinaryPredicate>
bool Element_HEAC::CheckLine(Simulation* sim, int x1, int y1, int x2, int y2, BinaryPredicate func)
{
bool reverseXY = abs(y2-y1) > abs(x2-x1);
int x, y, dx, dy, sy;
float e, de;
if (reverseXY)
{
y = x1;
x1 = y1;
y1 = y;
y = x2;
x2 = y2;
y2 = y;
}
if (x1 > x2)
{
y = x1;
x1 = x2;
x2 = y;
y = y1;
y1 = y2;
y2 = y;
}
dx = x2 - x1;
dy = abs(y2 - y1);
e = 0.0f;
if (dx)
de = dy/(float)dx;
else
de = 0.0f;
y = y1;
sy = (y1<y2) ? 1 : -1;
for (x=x1; x<=x2; x++)
{
if (reverseXY)
{
if (func(sim, sim->pmap[x][y]&0xFF)) return true;
}
else
{
if (func(sim, sim->pmap[y][x]&0xFF)) return true;
}
e += de;
if (e >= 0.5f)
{
y += sy;
if ((y1<y2) ? (y<=y2) : (y>=y2))
{
if (reverseXY)
{
if (func(sim, sim->pmap[x][y]&0xFF)) return true;
}
else
{
if (func(sim, sim->pmap[y][x]&0xFF)) return true;
}
}
e -= 1.0f;
}
}
return false;
}

//#TPT-Directive ElementHeader Element_HEAC static int update(UPDATE_FUNC_ARGS)
int Element_HEAC::update(UPDATE_FUNC_ARGS)
{
Expand All @@ -58,7 +133,7 @@ int Element_HEAC::update(UPDATE_FUNC_ARGS)
{
rry = ry * rad;
rrx = rx * rad;
if (x+rrx >= 0 && x+rrx < XRES && y+rry >= 0 && y+rry < YRES)
if (x+rrx >= 0 && x+rrx < XRES && y+rry >= 0 && y+rry < YRES && !Element_HEAC::CheckLine<Element_HEAC::IsInsulator>(sim, x, y, x+rrx, y+rry, isInsulator))
{
r = pmap[y+rry][x+rrx];
if (r && sim->elements[r&0xFF].HeatConduct > 0)
Expand Down Expand Up @@ -86,7 +161,7 @@ int Element_HEAC::update(UPDATE_FUNC_ARGS)
{
rry = ry * rad;
rrx = rx * rad;
if (x+rrx >= 0 && x+rrx < XRES && y+rry >= 0 && y+rry < YRES)
if (x+rrx >= 0 && x+rrx < XRES && y+rry >= 0 && y+rry < YRES && !Element_HEAC::CheckLine<Element_HEAC::IsInsulator>(sim, x, y, x+rrx, y+rry, isInsulator))
{
r = pmap[y+rry][x+rrx];
if (r && sim->elements[r&0xFF].HeatConduct > 0)
Expand Down

0 comments on commit 4214f85

Please sign in to comment.