Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.09 KB | None | 0 0
  1.  
  2. #include "libtcod.hpp"
  3. #include "gEngine.h"
  4. #include "logging.h"
  5. #include <vector>
  6. #include <string>
  7. #include <iostream>
  8. #include <cmath>
  9. #include <chrono>
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. using namespace std;
  13. using namespace std::chrono;
  14.  
  15.  
  16.  
  17. FILE* UmbraLog::out = NULL;
  18. int UmbraLog::indent = 0;
  19. TCODList <UmbraLog::UmbraLogMessage*> UmbraLog::messages;
  20.  
  21. gEngine::gEngine(int w, int h, const char* name, bool fs, int fps, const char* font) {
  22.     UmbraLog::openBlock("gEngine Starting...");
  23.     TCODConsole::setCustomFont(font, TCOD_FONT_LAYOUT_TCOD | TCOD_FONT_TYPE_GRAYSCALE);
  24.     UmbraLog::info("loaded font [%s]", font);
  25.  
  26.     TCODConsole::initRoot(w, h, name, fs);
  27.     TCODSystem::setFps(fps);
  28.     UmbraLog::info("Root init with values: Width [%d], Height [%d], FPS [%d]", w, h, fps);
  29.     width = w;//Width of main window
  30.     height = h;//Height of main window
  31.     gEngineConsole rootOb;
  32.     rootOb.con = TCODConsole::root;
  33.     rootOb.id = 0;
  34.     mConsole.push_back(rootOb);
  35.     consoleId = 1; // set this to one, because root is always 0, will never get decremented
  36.     imageId = 0;
  37.     dijkstraMapId = 0;
  38.  
  39.     //mDungeonImage = NULL;
  40.     //mDungeonImage2x = NULL;
  41.     lightMaskHeight = 0;
  42.     lightMaskWidth = 0;
  43.     //mFovMap = NULL;
  44.     UmbraLog::info("Engine fully initialized.");
  45.     UmbraLog::closeBlock();
  46. }
  47.  
  48. TCOD_bkgnd_flag_t gEngine::mGetFlag(int flag) {//<- Protected
  49.     //Only setting up BKGND_NONE and SET for now
  50.     if (flag == 0) {
  51.         return TCOD_BKGND_NONE;
  52.     }
  53.     if (flag == 1) {
  54.         return TCOD_BKGND_SET;
  55.     }
  56.     else {
  57.         return TCOD_BKGND_NONE;//failsafe
  58.     }
  59. }
  60.  
  61. TCOD_alignment_t gEngine::mGetAlignmentFlag(int flag) {
  62.     if (flag == 0) {
  63.         return TCOD_LEFT;
  64.     }
  65.     else if (flag == 1) {
  66.         return TCOD_CENTER;
  67.     }
  68.     else if (flag == 2) {
  69.         return TCOD_RIGHT;
  70.     }
  71. }
  72.  
  73. int gEngine::mSysGetFPS() {
  74.     return TCODSystem::getFps();
  75. }
  76.  
  77. void gEngine::mSysSetFPS(int fps) {
  78.     TCODSystem::setFps(fps);
  79. }
  80.    
  81. void gEngine::mClear(int c) {
  82.     if (c == 0) {
  83.         TCODConsole::root->clear();
  84.     }
  85.     else {
  86.         TCODConsole* con = mGetConsole(c);
  87.         con->clear();
  88.     }
  89. }
  90.  
  91. void gEngine::mFlush() {
  92.     TCODConsole::flush();
  93. }
  94.  
  95. int gEngine::mGetMouseCX() {
  96.     TCOD_key_t key;
  97.     TCOD_mouse_t mouse;
  98.     TCODSystem::checkForEvent((TCOD_event_t)(TCOD_EVENT_MOUSE), &key, &mouse);
  99.     return mouse.cx;
  100. }
  101.  
  102. int gEngine::mGetMouseCY() {
  103.     TCOD_key_t key;
  104.     TCOD_mouse_t mouse;
  105.     TCODSystem::checkForEvent((TCOD_event_t)(TCOD_EVENT_MOUSE), &key, &mouse);
  106.     return mouse.cy;
  107. }
  108. int gEngine::mAddConsole(int w, int h) {
  109.     //Add an offscreen console.
  110.     UmbraLog::openBlock("Creating a new console...");  
  111.     gEngineConsole conOb;
  112.     TCODConsole* con = new TCODConsole(w, h);
  113.     conOb.con = con;
  114.     conOb.w = w;
  115.     conOb.h = h;
  116.     conOb.id = consoleId;
  117.     UmbraLog::info("Width [%d], Height [%d], ID [%d]", w, h, consoleId);
  118.     consoleId++;
  119.     mConsole.push_back(conOb);
  120.     UmbraLog::closeBlock();
  121.     return conOb.id;
  122. }
  123.  
  124. TCODConsole* gEngine::mGetConsole(int id) { // returns a console from supplied ID. For internal use only.
  125.     vector<gEngineConsole>::const_iterator iter;
  126.     TCODConsole* con;
  127.     for (iter = mConsole.begin(); iter != mConsole.end(); iter++) {
  128.         if (iter->id == id) {
  129.             con = iter->con;
  130.             return con;
  131.         }
  132.     }
  133. }
  134.  
  135. void gEngine::mDestroyConsole(int c) {
  136.     UmbraLog::openBlock("Deleting console with ID [%d]...", c);
  137.     vector<gEngineConsole>::const_iterator iter;
  138.     for (iter = mConsole.begin(); iter != mConsole.end(); iter++) {
  139.         if(iter->id == c){
  140.             delete iter->con;
  141.             mConsole.erase(iter);
  142.             UmbraLog::info("...Success.");
  143.             UmbraLog::closeBlock();
  144.             return;
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150. void gEngine::mBlit(int conSource, int conDest, int srcX, int srcY, int width, int height, int destX, int destY, float alphaFore, float alphaBack) {
  151.     TCODConsole* source = mGetConsole(conSource);  
  152.     TCODConsole* dest = mGetConsole(conDest);
  153.     TCODConsole::blit(source, srcX, srcY, width, height, dest, destX, destY, alphaFore, alphaBack);
  154. }
  155.  
  156. void gEngine::mSetKeyColor(int c, int r, int g, int b) {
  157.     TCODColor col(r, g, b);
  158.     TCODConsole* con = mGetConsole(c);
  159.     con->setKeyColor(col);
  160. }
  161. //////////////////////////////
  162. // BASIC PRINTING FUNCTIONS //
  163. //////////////////////////////
  164.  
  165. void gEngine::mSetForegroundColor(int c, int h, int s, int v) {
  166.     TCODColor col(h, s, v);
  167.     TCODConsole* con = mGetConsole(c);
  168.     con->setDefaultForeground(col);
  169. }
  170.  
  171. void gEngine::mSetBackgroundColor(int c, int h, int s, int v) {
  172.     TCODColor col(h, s, v);
  173.     TCODConsole* con = mGetConsole(c);
  174.     con->setDefaultBackground(col);
  175. }
  176.  
  177. void gEngine::mSetCharBackground(int c, int x, int y, int r, int g, int b, int flag) {
  178.     TCODColor col(r, g, b);
  179.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  180.     TCODConsole* con = mGetConsole(c);
  181.     con->setCharBackground(x, y, col, f);
  182. }
  183.  
  184. void gEngine::mSetCharForeground(int c, int x, int y, int r, int g, int b) {
  185.     TCODColor col(r, g, b);
  186.     TCODConsole* con = mGetConsole(c);
  187.     con->setCharForeground(x, y, col);
  188. }
  189.  
  190. void gEngine::mPutCharEx(int c, int x, int y, int ch, int fr, int fg, int fb, int br, int bg, int bb) {
  191.     TCODColor fore(fr, fg, fb);
  192.     TCODColor back(br, bg, bb);
  193.     TCODConsole* con = mGetConsole(c);
  194.     con->putCharEx(x, y, ch, fore, back);  
  195. }
  196.  
  197. void gEngine::mPutChar(int c, int x, int y, int ch, int flag) {
  198.     TCOD_bkgnd_flag_t f = gEngine::mGetFlag(flag); 
  199.     TCODConsole* con = mGetConsole(c);
  200.     con->putChar(x, y, ch, f); 
  201. }
  202.  
  203. void gEngine::mSetChar(int c, int x, int y, int ch) {  
  204.     TCODConsole* con = mGetConsole(c);
  205.     con->setChar(x, y, ch);
  206. }
  207.  
  208. //////////////////////////////
  209. // String Drawing Functions //
  210. //////////////////////////////
  211.  
  212. void gEngine::mSetAlignment(int c, int flag) {
  213.     TCOD_alignment_t f = mGetAlignmentFlag(flag);  
  214.     TCODConsole* con = mGetConsole(c);
  215.     con->setAlignment(f);
  216. }
  217.  
  218. void gEngine::mPrint(int c, int x, int y, const char* fmt, ...) {  
  219.     TCODConsole* con = mGetConsole(c);
  220.     con->printf(x, y, fmt);
  221. }
  222.  
  223. void gEngine::mPrintEx(int c, int x, int y, int bk, int al, const char* fmt, ...) {
  224.     TCOD_alignment_t a = mGetAlignmentFlag(al);
  225.     TCOD_bkgnd_flag_t b = mGetFlag(bk);
  226.     TCODConsole* con = mGetConsole(c);
  227.     con->printf(x, y, b, a, fmt);
  228. }
  229.  
  230. void gEngine::mPrintRect(int c, int x, int y, int w, int h, const char* fmt, ...) {
  231.     TCODConsole* con = mGetConsole(c);
  232.     con->printRect(x, y, w, h, fmt);
  233. }
  234.  
  235. void gEngine::mPrintRectEx(int c, int x, int y, int w, int h, int bk, int al, const char* fmt, ...) {
  236.     TCOD_alignment_t a = mGetAlignmentFlag(al);
  237.     TCOD_bkgnd_flag_t b = mGetFlag(bk);
  238.     TCODConsole* con = mGetConsole(c);
  239.     con->printRectEx(x, y, w, h, b, a, fmt);
  240. }
  241.  
  242. int gEngine::mGetHeightRect(int c, int x, int y, int w, int h, const char* fmt, ...) {
  243.     TCODConsole* con = mGetConsole(c);
  244.     return con->getHeightRect(x, y, w, h, fmt);
  245. }
  246. /////////////////////////////////
  247. // Advanced Printing Functions //
  248. /////////////////////////////////
  249. void gEngine::mHLine(int c, int x, int y, int l, int flag) {
  250.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  251.     TCODConsole* con = mGetConsole(c);
  252.     con->hline(x, y, l, f);
  253. }
  254.  
  255. void gEngine::mVLine(int c, int x, int y, int l, int flag) {
  256.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  257.     TCODConsole* con = mGetConsole(c);
  258.     con->vline(x, y, l, f);
  259. }
  260.  
  261. void gEngine::mPrintFrame(int c, int x, int y, int w, int h, bool clear, int flag, const char* fmt, ...) {
  262.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  263.     TCODConsole* con = mGetConsole(c);
  264.     if (strcmp(fmt, "NULL")==0) {
  265.         fmt = (const char*)0;
  266.     }
  267.  
  268.     con->printFrame(x, y, w, h, clear, f, fmt);
  269. }
  270. void gEngine::mSaveScreenshot(const char* filename) {
  271.     TCODSystem::saveScreenshot(filename);
  272. }
  273.  
  274. ///////////////////
  275. // Image Toolkit //
  276. ///////////////////
  277.  
  278. TCODImage* gEngine::mGetImage(int id) {
  279.     if (id == -1) {
  280.         return mDungeonImage;
  281.     }
  282.     vector<gEngineImage>::const_iterator iter;
  283.     TCODImage* img;
  284.     for (iter = mImages.begin(); iter != mImages.end(); iter++) {
  285.         if (iter->id == id) {
  286.             img = iter->img;
  287.             return img;
  288.         }
  289.     }
  290. }
  291. void gEngine::mDestroyImage(int id) {
  292.     UmbraLog::openBlock("Deleting Image with ID [%d]...", id);
  293.     vector<gEngineImage>::const_iterator iter;
  294.     for (iter = mImages.begin(); iter != mImages.end(); iter++) {
  295.         if (iter->id == id) {
  296.             delete iter->img;
  297.             mImages.erase(iter);
  298.             UmbraLog::info("...Success.");
  299.             UmbraLog::closeBlock();
  300.             return;
  301.         }
  302.     }
  303. }
  304.  
  305. int gEngine::mCreateImage(int w, int h) {
  306.     UmbraLog::openBlock("Creating a new blank image...");
  307.     TCODImage* img = new TCODImage(w, h);
  308.     gEngineImage imgObj;
  309.     imgObj.img = img;
  310.     imgObj.id = imageId;
  311.     imageId++;
  312.     UmbraLog::info("Image Width [%d], Height [%d], ID [%d]", w, h, imageId);
  313.     mImages.push_back(imgObj);
  314.     UmbraLog::closeBlock();
  315.     return imgObj.id;
  316. }
  317.  
  318. int gEngine::mLoadImage(const char* filename) {
  319.     UmbraLog::openBlock("Creating new Image from file [%s], Id [%d]", filename, imageId);
  320.     TCODImage* img = new TCODImage(filename);
  321.     gEngineImage imgObj;
  322.     imgObj.img = img;
  323.     imgObj.id = imageId;
  324.     imageId++;
  325.     mImages.push_back(imgObj);
  326.     UmbraLog::info("...Success.");
  327.     UmbraLog::closeBlock();
  328.     return imgObj.id;
  329. }
  330.  
  331. int gEngine::mImageGetHeight(int i) {
  332.     TCODImage* img = mGetImage(i);
  333.     int w, h;
  334.     img->getSize(&w, &h);
  335.     return h;
  336. }
  337.  
  338. int gEngine::mImageGetWidth(int i) {
  339.     TCODImage* img = mGetImage(i);
  340.     int w, h;
  341.     img->getSize(&w, &h);
  342.     return w;
  343. }
  344.  
  345. int gEngine::mImageGetR(int i, int x, int y) {
  346.     TCODImage* img = mGetImage(i);
  347.     TCODColor col = img->getPixel(x, y);   
  348.     return col.r;
  349. }
  350.  
  351. int gEngine::mImageGetG(int i, int x, int y) {
  352.     TCODImage* img = mGetImage(i);
  353.     TCODColor col = img->getPixel(x, y);
  354.     return col.g;
  355. }
  356.  
  357. int gEngine::mImageGetB(int i, int x, int y) {
  358.     TCODImage* img = mGetImage(i);
  359.     TCODColor col = img->getPixel(x, y);
  360.     return col.b;
  361. }
  362.  
  363. void gEngine::mImageClear(int i, int r, int g, int b) {
  364.     TCODImage* img = mGetImage(i);
  365.     TCODColor col = TCODColor(r, g, b);
  366.     img->clear(col);
  367. }
  368.  
  369. void gEngine::mImagePutPixel(int i, int x, int y, int r, int g, int b) {
  370.     TCODImage* img = mGetImage(i);
  371.     TCODColor col = TCODColor(r, g, b);
  372.     img->putPixel(x, y, col);
  373. }
  374.  
  375. void gEngine::mImageScale(int i, int newWidth, int newHeight) {
  376.     TCODImage* img = mGetImage(i);
  377.     img->scale(newWidth, newHeight);
  378. }
  379.  
  380. void gEngine::mImageHFlip(int i) {
  381.     TCODImage* img = mGetImage(i);
  382.     img->hflip();
  383. }
  384.  
  385. void gEngine::mImageVFlip(int i) {
  386.     TCODImage* img = mGetImage(i);
  387.     img->vflip();
  388. }
  389.  
  390. void gEngine::mImageBlitRect(int i, int c, int x, int y, int w, int h, int flag) {
  391.     TCODImage* img = mGetImage(i);
  392.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  393.     TCODConsole* con = mGetConsole(c);
  394.     img->blitRect(con, x, y, w, h, f);
  395. }
  396.  
  397. void gEngine::mImageBlit(int i, int c, int x, int y, int flag, float sx, float sy, float angle) {
  398.     TCODImage* img = mGetImage(i);
  399.     TCOD_bkgnd_flag_t f = mGetFlag(flag);
  400.     TCODConsole* con = mGetConsole(c);
  401.     img->blit(con, x, y, f, sx, sy, angle);
  402. }
  403.  
  404. void gEngine::mImageBlit2x(int i, int c, int x, int y, int sx, int sy, int w, int h) {
  405.     TCODImage* img = mGetImage(i);
  406.     TCODConsole* con = mGetConsole(c);
  407.     img->blit2x(con, x, y, sx, sy, w, h);
  408. }
  409. void gEngine::mImageSetKeyColor(int i, int r, int g, int b) {
  410.     TCODImage* img = mGetImage(i);
  411.     TCODColor col = TCODColor(r, g, b);
  412.     img->setKeyColor(col);
  413. }
  414.  
  415. void gEngine::mImageBatchShift(int i, int r1, int g1, int b1, int r2, int g2, int b2) {
  416.     TCODImage* img = mGetImage(i);
  417.     TCODColor col = TCODColor(r1, g1, b1);
  418.     TCODColor col2 = TCODColor(r2, g2, b2);
  419.     int w, h;
  420.     img->getSize(&w, &h);
  421.     for (int x = 0; x < w; x++) {
  422.         for (int y = 0; y < h; y++) {
  423.             TCODColor c = img->getPixel(x, y);
  424.             if (c == col) {
  425.                 img->putPixel(x, y, col2);
  426.             }
  427.         }
  428.     }
  429. }
  430.  
  431. ///////////////////////////////
  432. // Dungeon Drawing Functions //
  433. ///////////////////////////////
  434.  
  435. void gEngine::mDungeonNewMap(int w, int h) {
  436.     UmbraLog::openBlock("Initializing new dungeon map");
  437.     mFovMap = new TCODMap(w, h);
  438.     mDungeonImage = new TCODImage(w, h);
  439.     UmbraLog::info("Size Width[%d], Height [%d]", w, h);
  440.     UmbraLog::closeBlock();
  441. }
  442.  
  443. void gEngine::mDungeonReset() {
  444.     UmbraLog::openBlock("Resetting Dungeon...");
  445.     mDungeonMap.clear();
  446.     delete mFovMap;
  447.     delete mDungeonImage;
  448.     UmbraLog::info("...Done");
  449.     UmbraLog::closeBlock();
  450.     // light mask reset
  451. }
  452.  
  453. void gEngine::mDungeonAddTile(int x, int y, bool blocked, bool blockSight, int r, int g, int b) {
  454.     gEngineTile tile;
  455.     TCODColor col = TCODColor(r, g, b);
  456.     tile.x = x;
  457.     tile.y = y;
  458.     tile.blocked = blocked;
  459.     tile.blockSight = blockSight;
  460.     tile.color = col;
  461.     tile.explored = false;
  462.     mDungeonMap.push_back(tile);
  463.     mFovMap->setProperties(x, y, blockSight, blocked);
  464. }
  465.  
  466. void gEngine::mDungeonRenderStaticMap(int c, int x, int y) {
  467.     TCODConsole* con = mGetConsole(c);
  468.     mFovMap->computeFov(x, y, 0, true, FOV_SHADOW);
  469.  
  470.     vector<gEngineTile>::iterator iter;
  471.     int r = 0, g = 0, b = 0;
  472.  
  473.     for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {  
  474.         if (mFovMap->isInFov(iter->x, iter->y)) {
  475.             vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(iter->x, iter->y);
  476.             r = iter->color.r;
  477.             g = iter->color.g;
  478.             b = iter->color.b;
  479.             r = (r * lightIter->r);
  480.             g = (g * lightIter->g);
  481.             b = (b * lightIter->b);
  482.             //multiply by lightmask value
  483.             iter->setExplored();
  484.         }
  485.         else {
  486.             if (iter->explored) {
  487.                 r = iter->color.r * lightMaskAmbient;
  488.                 g = iter->color.g * lightMaskAmbient;
  489.                 b = iter->color.b * lightMaskAmbient;              
  490.             }
  491.             else {
  492.                 r=0, g=0, b = 0;
  493.             }
  494.         }
  495.         TCODColor col = TCODColor(r, g, b);
  496.         mDungeonImage->putPixel(iter->x, iter->y, col);
  497.     }
  498.     mDungeonImage->blitRect(con, 0, 0);
  499. }
  500.  
  501. void gEngine::mDungeonRenderStaticMap2x(int c, int x, int y) {
  502.     TCODConsole* con = mGetConsole(c);
  503.     mFovMap->computeFov(x, y, 0, true, FOV_SHADOW);
  504.  
  505.     vector<gEngineTile>::iterator iter;
  506.     int r = 0, g = 0, b = 0;
  507.  
  508.     for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {
  509.         if (mFovMap->isInFov(iter->x, iter->y)) {
  510.             vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(iter->x, iter->y);
  511.             r = iter->color.r;
  512.             r = iter->color.r;
  513.             g = iter->color.g;
  514.             b = iter->color.b;
  515.             r = (r * lightIter->r);
  516.             g = (g * lightIter->g);
  517.             b = (b * lightIter->b);
  518.             // multiply by lightmask value
  519.             iter->setExplored();
  520.         }
  521.         else {
  522.             if (iter->explored) {
  523.                 r = iter->color.r * lightMaskAmbient;
  524.                 g = iter->color.g * lightMaskAmbient;
  525.                 b = iter->color.b * lightMaskAmbient;
  526.             }
  527.             else {
  528.                 r = 0, g = 0, b = 0;
  529.             }
  530.         }
  531.         TCODColor col = TCODColor(r, g, b);
  532.         mDungeonImage->putPixel(iter->x, iter->y, col);
  533.     }  
  534. }
  535.  
  536. void gEngine::mDungeonRenderScrollingMap(int c, int x, int y) {
  537.    
  538. }
  539.  
  540. void gEngine::mDungeonRenderScrollingMap2x(int c, int x, int y) {
  541.  
  542. }
  543.  
  544. void gEngine::mDungeonBlit2x(int c) {
  545.     TCODConsole* con = mGetConsole(c);
  546.     mDungeonImage->blit2x(con, 0, 0);
  547. }
  548.  
  549. bool gEngine::mDungeonIsExplored(int x, int y) {
  550.     try {
  551.         vector<gEngineTile>::iterator iter;
  552.         if (mDungeonMap.size() < idx(x, y)) {
  553.             throw false;
  554.         }
  555.         iter = mDungeonMap.begin() + idx(x, y);    
  556.         return iter->explored;
  557.     }
  558.     catch (...) {
  559.         UmbraLog::openBlock("Exception in mDungeonIsExplored.");
  560.         UmbraLog::fatalError("");
  561.         UmbraLog::fatalError("mDungeonMap Size incorrect. Index point too large");
  562.         UmbraLog::fatalError("Size [%d], index [%d]", mDungeonMap.size(), idx(x, y));
  563.         UmbraLog::fatalError("X [%d], y [%d]", x, y);
  564.         UmbraLog::fatalError("Map Width [%d], Height [%d]", lightMaskWidth, lightMaskHeight);
  565.         UmbraLog::fatalError("Returning false. Verify map is initialized properly, or index is correct");
  566.         UmbraLog::fatalError("");
  567.         UmbraLog::closeBlock();
  568.         return false;
  569.     }
  570. }
  571.  
  572. bool gEngine::mDungeonIsWalkable(int x, int y) {
  573.     return mFovMap->isWalkable(x, y);
  574. }
  575.  
  576. bool gEngine::mDungeonIsTransparent(int x, int y) {
  577.     return mFovMap->isTransparent(x, y);
  578. }
  579.  
  580. void gEngine::mDungeonRevealMap() {
  581.     vector<gEngineTile>::iterator iter;
  582.     for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {
  583.         iter->setExplored();
  584.     }
  585. }
  586.  
  587. //////////////////////////
  588. // Light Mask Functions //
  589. //////////////////////////
  590.  
  591. void gEngine::mLightmaskInit(int w, int h) {
  592.     UmbraLog::openBlock("Initializing lightmask...");
  593.     lightMaskWidth = w;
  594.     lightMaskHeight = h;
  595.     mLightmaskReset();
  596.     UmbraLog::info("...Done");
  597.     UmbraLog::closeBlock();
  598. }
  599.  
  600. void gEngine::mLightmaskSetAmbient(float ambient) {
  601.     lightMaskAmbient = ambient;
  602. }
  603.  
  604. void gEngine::mLightmaskReset() {
  605.     //UmbraLog::openBlock("Resetting light mask...");
  606.     mLightMask.clear();
  607.     mLights.clear();
  608.     for (int x = 0; x <= lightMaskWidth; x++) {
  609.         for (int y = 0; y <= lightMaskHeight; y++) {
  610.             lightMaskLight light;
  611.             light.r = lightMaskAmbient;
  612.             light.g = lightMaskAmbient;
  613.             light.b = lightMaskAmbient;
  614.             mLightMask.push_back(light);
  615.         }
  616.     }
  617.     //UmbraLog::info("...Done. Lightmask set to ambient levels of [%f].", lightMaskAmbient);
  618.     //UmbraLog::closeBlock();
  619. }
  620.  
  621. void gEngine::mLightmaskCalculateLight(gEngineLight light) {
  622.     int top = max(0, light.y - light.radius);
  623.     int bottom = min(lightMaskHeight, light.y + light.radius);
  624.     int left = max(0, light.x - light.radius);
  625.     int right = min(lightMaskWidth, light.x + light.radius);
  626.     mLightmaskLightTile(light.x, light.y, 1.0f, light);
  627.  
  628.     TCOD_bresenham_data_t data;
  629.     int xx = 0, yy = 0;
  630.     for (int x = left; x <= right; x++) {
  631.         xx = 0, yy = 0;
  632.         TCODLine::init(light.x, light.y, x, top);
  633.         do {
  634.             if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
  635.                 if (top == 0 && left == 0) {
  636.                     float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  637.                     if (!mLightmaskLightTile(xx, yy, d, light)) {
  638.                         break;
  639.                     }
  640.                 }
  641.                 else {
  642.                     if (!mFovMap->isTransparent(xx, yy)) {
  643.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  644.                         mLightmaskLightTile(xx, yy, d, light);
  645.                         break;
  646.                     }
  647.                     else {
  648.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  649.                         if (!mLightmaskLightTile(xx, yy, d, light)) {
  650.                             break;
  651.                         }
  652.                     }
  653.                 }
  654.                
  655.             }
  656.         } while(!TCODLine::step(&xx, &yy));
  657.  
  658.         xx = 0, yy = 0;
  659.         TCODLine::init(light.x, light.y, x, bottom);
  660.         do {
  661.             if(mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
  662.                 if (top == 0 && left == 0) {
  663.                     float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  664.  
  665.                     if (!mLightmaskLightTile(xx, yy, d, light)) {
  666.                         break;
  667.                     }
  668.                 }
  669.                 else {
  670.                     if (!mFovMap->isTransparent(xx, yy)) {
  671.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  672.                         mLightmaskLightTile(xx, yy, d, light);
  673.                         break;
  674.                     }
  675.                     else {
  676.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  677.                         if (!mLightmaskLightTile(xx, yy, d, light)) {
  678.                             break;
  679.                         }
  680.                     }
  681.                 }
  682.                
  683.             }
  684.         } while (!TCODLine::step(&xx, &yy));
  685.     }
  686.  
  687.     for (int y = top; y <= bottom; y++) {
  688.         //int xx = 0, yy = 0;
  689.         xx = 0, yy = 0;
  690.         TCODLine::init(light.x, light.y, left, y);
  691.         do {
  692.             if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
  693.                 if (top == 0 && left == 0) {
  694.                     float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  695.                     if (!mLightmaskLightTile(xx, yy, d, light)) {
  696.                         break;
  697.                     }
  698.                 }
  699.                 else {
  700.                     if (!mFovMap->isTransparent(xx, yy)) {
  701.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  702.                         mLightmaskLightTile(xx, yy, d, light);
  703.                         break;
  704.                     }
  705.                     else {
  706.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  707.                         if (!mLightmaskLightTile(xx, yy, d, light)) {
  708.                             break;
  709.                         }
  710.                     }
  711.                 }
  712.                
  713.             }
  714.         } while (!TCODLine::step(&xx, &yy));
  715.  
  716.         xx = 0, yy = 0;
  717.         TCODLine::init(light.x, light.y, right, y);
  718.         do {
  719.             if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
  720.                 if (top == 0 && left == 0) {
  721.                     float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  722.                     if (!mLightmaskLightTile(xx, yy, d, light)) {
  723.                         break;
  724.                     }
  725.                 }
  726.                 else {
  727.                     if (!mFovMap->isTransparent(xx, yy)) {
  728.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  729.                         mLightmaskLightTile(xx, yy, d, light);
  730.                         break;
  731.                     }
  732.                     else {
  733.                         float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
  734.                         if (!mLightmaskLightTile(xx, yy, d, light)) {
  735.                             break;
  736.                         }
  737.                     }
  738.                 }
  739.                
  740.             }
  741.         } while (!TCODLine::step(&xx, &yy));
  742.     }
  743. }
  744.  
  745. void gEngine::mLightmaskCompute() {
  746.     vector<gEngineLight>::iterator light;
  747.     for (light = mLights.begin(); light != mLights.end(); light++) {       
  748.         this->mLightmaskCalculateLight(*light);    
  749.     }
  750. }
  751.  
  752. void gEngine::mLightmaskAddLight(int x, int y, int r, int g, int b, float intensity, float radius) {
  753.     //UmbraLog::openBlock("Adding light to lightmask.");
  754.     //UmbraLog::info("Light Container size before loading new light [%d]", mLights.size());
  755.     float newr = r / 255.0f;
  756.     float newg = g / 255.0f;
  757.     float newb = b / 255.0f;
  758.     gEngineLight light;
  759.     light.x = x;
  760.     light.y = y;
  761.     light.r = newr;
  762.     light.g = newg;
  763.     light.b = newb;
  764.     light.color = TCODColor(r, g, b);
  765.     light.radius = radius;
  766.     light.intensity = intensity;
  767.     mLights.push_back(light);
  768.     //UmbraLog::info("Done... New size [%d]", mLights.size());
  769.     //UmbraLog::closeBlock();
  770. }
  771.  
  772. bool gEngine::mLightmaskLightTile(int x, int y, float amount, gEngineLight light) {
  773.     float d = amount / light.radius;
  774.    
  775.     d = -d;
  776.     float c1 = (d + light.r);
  777.     float c2 = (d + light.g);
  778.     float c3 = (d + light.b);
  779.     vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(x, y);
  780.  
  781.     c1 = max(c1, lightIter->r);
  782.     c2 = max(c2, lightIter->g);
  783.     c3 = max(c3, lightIter->b);
  784.  
  785.     c1 = max(c1, lightMaskAmbient);
  786.     c2 = max(c2, lightMaskAmbient);
  787.     c3 = max(c3, lightMaskAmbient);
  788.     /*lightIter->r = c1;
  789.     lightIter->g = c2;
  790.     lightIter->b = c3;*/
  791.     lightIter->update(c1, c2, c3);
  792.     if (c1 == lightMaskAmbient && c2 == lightMaskAmbient && c3 == lightMaskAmbient) {
  793.         return false;
  794.     }
  795.     return true;
  796. }
  797.  
  798. float gEngine::mLightmaskGetValueR(int x, int y) {
  799.     vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
  800.     return light->r;
  801. }
  802.  
  803. float gEngine::mLightmaskGetValueG(int x, int y) {
  804.     vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
  805.     return light->g;
  806. }
  807.  
  808. float gEngine::mLightmaskGetValueB(int x, int y) {
  809.     vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
  810.     return light->g;
  811. }
  812.  
  813. bool gEngine::mLightmaskInCircle(int cx, int cy, int tx, int ty, float radius) {
  814.     float dx = tx - cx;
  815.     float dy = ty - cy;
  816.     float sq = dx * dx + dy * dy;
  817.     if (sq <= radius * radius) {
  818.         return true;
  819.     }
  820.     return false;
  821.     //return true;
  822. }
  823.  
  824. float gEngine::mLightmaskDistanceFromCenter(int cx, int cy, int tx, int ty, float radius) {
  825.     float dx = cx - tx;
  826.     float dy = cy - ty;
  827.     float sq = dx * dx + dy * dy;
  828.     float d = sqrt(sq);
  829.     return (float)d;
  830. }
  831.  
  832. void gEngine::mOpenBlock(const char* mes, ...) {
  833.     UmbraLog::openBlock(mes);
  834. }
  835.  
  836.  
  837. void gEngine::mCloseBlock() {
  838.     UmbraLog::closeBlock();
  839. }
  840.  
  841. void gEngine::mInfo(const char* mes, ...) {
  842.     UmbraLog::info(mes);
  843. }
  844.  
  845. void gEngine::mNotice(const char* mes, ...) {
  846.     UmbraLog::notice(mes);
  847. }
  848.  
  849. void gEngine::mError(const char* mes, ...) {
  850.     UmbraLog::error(mes);
  851. }
  852.  
  853. void gEngine::mFatalError(const char* mes, ...) {
  854.     UmbraLog::fatalError(mes);
  855. }
  856.  
  857. void gEngine::mMapAsciiCodeToFont(int code, int x, int y) {
  858.     TCODConsole::mapAsciiCodeToFont(code, x, y);
  859. }
  860.  
  861. int gEngine::mNewDijkstraMap(int w, int h) {
  862.     DijkstraMap m;
  863.     m.id = dijkstraMapId;
  864.     dijkstraMapId++;
  865.     m.w = w;
  866.     m.h = h;
  867.     m.maxValue = w * h;
  868.     for (int x = 0; x <= w; x++) {
  869.         for (int y = 0; y <= h; y++) {
  870.             m.tiles.push_back(DijkstraPoint(x, y, m.maxValue));
  871.         }
  872.     }
  873.     mDijkstraMaps.push_back(m);
  874.     return m.id;
  875. }
  876.  
  877. void gEngine::mDijkstraCompute(int id) {
  878.     DijkstraMap map = mDijkstraGetMap(id);
  879.     vector<DijkstraPoint>::iterator dmap_iter;
  880.     vector<DijkstraPoint>::iterator point_iter;
  881.  
  882.     vector<gEngineTile>::iterator iter;
  883.     vector<int> value;
  884.     int value2, dif;
  885.     bool changes = false;
  886.  
  887.     UmbraLog::openBlock("Computing Dijkstra Map....");
  888.     for (point_iter = map.points.begin(); point_iter != map.points.end(); point_iter++) {
  889.         dmap_iter = map.tiles.begin() + point_iter->x + point_iter->y * map.w;
  890.         UmbraLog::openBlock("Point Value %d", point_iter->value);
  891.         dmap_iter->set_value(point_iter->value);
  892.         UmbraLog::info("Map Value %d", dmap_iter->value);
  893.         UmbraLog::closeBlock();
  894.     }
  895.     //while (true) {
  896.     for(int z =0; z< 10; z++){
  897.         changes = false;
  898.         for (int x = 2; x < map.w - 2; x++) {
  899.             for (int y = 2; y < map.h - 2; y++) {
  900.                 //iter = (mDungeonMap.begin() + ((x * 2) + (y * 2)) * (map.w * 2));
  901.                 //if (!iter->blocked) {
  902.                     dmap_iter = map.tiles.begin() + ((x+1) + y) * map.w;
  903.                     value.push_back(dmap_iter->value);
  904.  
  905.                     dmap_iter = map.tiles.begin() + ((x-1) + y) * map.w;
  906.                     value.push_back(dmap_iter->value);
  907.  
  908.                     dmap_iter = map.tiles.begin() + (x + (y+1)) * map.w;
  909.                     value.push_back(dmap_iter->value);
  910.  
  911.                     dmap_iter = map.tiles.begin() + (x + (y-1)) * map.w;
  912.                     value.push_back(dmap_iter->value);
  913.  
  914.                     dmap_iter = map.tiles.begin() + ((x+1) + (y+1)) * map.w;
  915.                     value.push_back(dmap_iter->value);
  916.  
  917.                     dmap_iter = map.tiles.begin() + ((x-1) + (y-1)) * map.w;
  918.                     value.push_back(dmap_iter->value);
  919.  
  920.                     dmap_iter = map.tiles.begin() + ((x-1) + (y+1)) * map.w;
  921.                     value.push_back(dmap_iter->value);
  922.  
  923.                     dmap_iter = map.tiles.begin() + ((x+1) + (y-1)) * map.w;
  924.                     value.push_back(dmap_iter->value);
  925.  
  926.                     value2 = *min_element(value.begin(), value.end());
  927.                     value.clear();
  928.  
  929.                     dmap_iter = map.tiles.begin() + (x + y) * map.w;
  930.                     dif = dmap_iter->value - value2;
  931.  
  932.                     if (dif > 1) {
  933.                         dmap_iter->set_value(value2 + 1);
  934.                         changes = true;
  935.                     }
  936.                 //}
  937.             }
  938.         }
  939.         if (!changes) {
  940.             break;
  941.         }
  942.     }
  943.     UmbraLog::closeBlock();
  944. }
  945.  
  946. void gEngine::mDijkstraAddPoint(int id, int x, int y, int value) {
  947.     DijkstraMap map = mDijkstraGetMap(id);
  948.     vector<DijkstraPoint>::iterator dmap_iter;
  949.     dmap_iter = map.tiles.begin() + x + y * map.w;
  950.     dmap_iter->set_value(value);
  951.     map.points.push_back(DijkstraPoint(x, y, value));
  952. }
  953.  
  954. void gEngine::mDijkstraClearMap(int id) {
  955.     DijkstraMap map = mDijkstraGetMap(id);
  956.     vector<DijkstraPoint>::iterator iter;
  957.     for (iter = map.points.begin(); iter != map.points.end(); iter++) {
  958.         iter->value = map.maxValue;
  959.     }
  960. }
  961.  
  962. void gEngine::mDijkstraRemovePoint(int id, int x, int y) {
  963.     DijkstraMap map = mDijkstraGetMap(id);
  964.     vector<DijkstraPoint>::iterator iter;
  965.     for (iter = map.points.begin(); iter != map.points.end(); iter++) {
  966.         if (iter->x == x && iter->y == y) {
  967.             map.points.erase(iter);
  968.             return;
  969.         }
  970.     }
  971. }
  972.  
  973. int gEngine::mDijkstraGetValue(int id, int x, int y) {
  974.     DijkstraMap map = mDijkstraGetMap(id);
  975.     vector<DijkstraPoint>::iterator iter;
  976.     iter = map.tiles.begin() + x + y * map.w;
  977.     return iter->value;
  978.    
  979. }
  980.  
  981. void gEngine::mDijkstraRemoveMap(int id) {
  982.     vector<DijkstraMap>::const_iterator iter;
  983.     DijkstraMap map;
  984.     for (iter = mDijkstraMaps.begin(); iter != mDijkstraMaps.end(); iter++) {
  985.         if (iter->id == id) {
  986.             mDijkstraMaps.erase(iter);
  987.             return;
  988.         }
  989.     }
  990. }
  991.  
  992. DijkstraMap gEngine::mDijkstraGetMap(int id) {
  993.     vector<DijkstraMap>::const_iterator iter;
  994.     DijkstraMap map;
  995.     for (iter = mDijkstraMaps.begin(); iter != mDijkstraMaps.end(); iter++) {
  996.         if (iter->id == id) {
  997.             map = (*iter);
  998.             return map;
  999.         }
  1000.     }
  1001. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement