diff --git a/src/simulation/elements/HEAC.cpp b/src/simulation/elements/HEAC.cpp index b16d6e9b2..9bdeba37d 100644 --- a/src/simulation/elements/HEAC.cpp +++ b/src/simulation/elements/HEAC.cpp @@ -1,3 +1,5 @@ +#include +#include #include "simulation/Elements.h" #include "simulation/Air.h" //#TPT-Directive ElementClass Element_HEAC PT_HEAC 180 @@ -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 { + 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 static bool CheckLine(Simulation* sim, int x1, int y1, int x2, int y2, BinaryPredicate func) +template +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 = (y1pmap[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)) + { + 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) { @@ -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(sim, x, y, x+rrx, y+rry, isInsulator)) { r = pmap[y+rry][x+rrx]; if (r && sim->elements[r&0xFF].HeatConduct > 0) @@ -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(sim, x, y, x+rrx, y+rry, isInsulator)) { r = pmap[y+rry][x+rrx]; if (r && sim->elements[r&0xFF].HeatConduct > 0)