Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libtcod.hpp"
- #include "gEngine.h"
- #include "logging.h"
- #include <vector>
- #include <string>
- #include <iostream>
- #include <cmath>
- #include <chrono>
- #include <stdarg.h>
- #include <stdio.h>
- using namespace std;
- using namespace std::chrono;
- FILE* UmbraLog::out = NULL;
- int UmbraLog::indent = 0;
- TCODList <UmbraLog::UmbraLogMessage*> UmbraLog::messages;
- gEngine::gEngine(int w, int h, const char* name, bool fs, int fps, const char* font) {
- UmbraLog::openBlock("gEngine Starting...");
- TCODConsole::setCustomFont(font, TCOD_FONT_LAYOUT_TCOD | TCOD_FONT_TYPE_GRAYSCALE);
- UmbraLog::info("loaded font [%s]", font);
- TCODConsole::initRoot(w, h, name, fs);
- TCODSystem::setFps(fps);
- UmbraLog::info("Root init with values: Width [%d], Height [%d], FPS [%d]", w, h, fps);
- width = w;//Width of main window
- height = h;//Height of main window
- gEngineConsole rootOb;
- rootOb.con = TCODConsole::root;
- rootOb.id = 0;
- mConsole.push_back(rootOb);
- consoleId = 1; // set this to one, because root is always 0, will never get decremented
- imageId = 0;
- dijkstraMapId = 0;
- //mDungeonImage = NULL;
- //mDungeonImage2x = NULL;
- lightMaskHeight = 0;
- lightMaskWidth = 0;
- //mFovMap = NULL;
- UmbraLog::info("Engine fully initialized.");
- UmbraLog::closeBlock();
- }
- TCOD_bkgnd_flag_t gEngine::mGetFlag(int flag) {//<- Protected
- //Only setting up BKGND_NONE and SET for now
- if (flag == 0) {
- return TCOD_BKGND_NONE;
- }
- if (flag == 1) {
- return TCOD_BKGND_SET;
- }
- else {
- return TCOD_BKGND_NONE;//failsafe
- }
- }
- TCOD_alignment_t gEngine::mGetAlignmentFlag(int flag) {
- if (flag == 0) {
- return TCOD_LEFT;
- }
- else if (flag == 1) {
- return TCOD_CENTER;
- }
- else if (flag == 2) {
- return TCOD_RIGHT;
- }
- }
- int gEngine::mSysGetFPS() {
- return TCODSystem::getFps();
- }
- void gEngine::mSysSetFPS(int fps) {
- TCODSystem::setFps(fps);
- }
- void gEngine::mClear(int c) {
- if (c == 0) {
- TCODConsole::root->clear();
- }
- else {
- TCODConsole* con = mGetConsole(c);
- con->clear();
- }
- }
- void gEngine::mFlush() {
- TCODConsole::flush();
- }
- int gEngine::mGetMouseCX() {
- TCOD_key_t key;
- TCOD_mouse_t mouse;
- TCODSystem::checkForEvent((TCOD_event_t)(TCOD_EVENT_MOUSE), &key, &mouse);
- return mouse.cx;
- }
- int gEngine::mGetMouseCY() {
- TCOD_key_t key;
- TCOD_mouse_t mouse;
- TCODSystem::checkForEvent((TCOD_event_t)(TCOD_EVENT_MOUSE), &key, &mouse);
- return mouse.cy;
- }
- int gEngine::mAddConsole(int w, int h) {
- //Add an offscreen console.
- UmbraLog::openBlock("Creating a new console...");
- gEngineConsole conOb;
- TCODConsole* con = new TCODConsole(w, h);
- conOb.con = con;
- conOb.w = w;
- conOb.h = h;
- conOb.id = consoleId;
- UmbraLog::info("Width [%d], Height [%d], ID [%d]", w, h, consoleId);
- consoleId++;
- mConsole.push_back(conOb);
- UmbraLog::closeBlock();
- return conOb.id;
- }
- TCODConsole* gEngine::mGetConsole(int id) { // returns a console from supplied ID. For internal use only.
- vector<gEngineConsole>::const_iterator iter;
- TCODConsole* con;
- for (iter = mConsole.begin(); iter != mConsole.end(); iter++) {
- if (iter->id == id) {
- con = iter->con;
- return con;
- }
- }
- }
- void gEngine::mDestroyConsole(int c) {
- UmbraLog::openBlock("Deleting console with ID [%d]...", c);
- vector<gEngineConsole>::const_iterator iter;
- for (iter = mConsole.begin(); iter != mConsole.end(); iter++) {
- if(iter->id == c){
- delete iter->con;
- mConsole.erase(iter);
- UmbraLog::info("...Success.");
- UmbraLog::closeBlock();
- return;
- }
- }
- }
- void gEngine::mBlit(int conSource, int conDest, int srcX, int srcY, int width, int height, int destX, int destY, float alphaFore, float alphaBack) {
- TCODConsole* source = mGetConsole(conSource);
- TCODConsole* dest = mGetConsole(conDest);
- TCODConsole::blit(source, srcX, srcY, width, height, dest, destX, destY, alphaFore, alphaBack);
- }
- void gEngine::mSetKeyColor(int c, int r, int g, int b) {
- TCODColor col(r, g, b);
- TCODConsole* con = mGetConsole(c);
- con->setKeyColor(col);
- }
- //////////////////////////////
- // BASIC PRINTING FUNCTIONS //
- //////////////////////////////
- void gEngine::mSetForegroundColor(int c, int h, int s, int v) {
- TCODColor col(h, s, v);
- TCODConsole* con = mGetConsole(c);
- con->setDefaultForeground(col);
- }
- void gEngine::mSetBackgroundColor(int c, int h, int s, int v) {
- TCODColor col(h, s, v);
- TCODConsole* con = mGetConsole(c);
- con->setDefaultBackground(col);
- }
- void gEngine::mSetCharBackground(int c, int x, int y, int r, int g, int b, int flag) {
- TCODColor col(r, g, b);
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- con->setCharBackground(x, y, col, f);
- }
- void gEngine::mSetCharForeground(int c, int x, int y, int r, int g, int b) {
- TCODColor col(r, g, b);
- TCODConsole* con = mGetConsole(c);
- con->setCharForeground(x, y, col);
- }
- void gEngine::mPutCharEx(int c, int x, int y, int ch, int fr, int fg, int fb, int br, int bg, int bb) {
- TCODColor fore(fr, fg, fb);
- TCODColor back(br, bg, bb);
- TCODConsole* con = mGetConsole(c);
- con->putCharEx(x, y, ch, fore, back);
- }
- void gEngine::mPutChar(int c, int x, int y, int ch, int flag) {
- TCOD_bkgnd_flag_t f = gEngine::mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- con->putChar(x, y, ch, f);
- }
- void gEngine::mSetChar(int c, int x, int y, int ch) {
- TCODConsole* con = mGetConsole(c);
- con->setChar(x, y, ch);
- }
- //////////////////////////////
- // String Drawing Functions //
- //////////////////////////////
- void gEngine::mSetAlignment(int c, int flag) {
- TCOD_alignment_t f = mGetAlignmentFlag(flag);
- TCODConsole* con = mGetConsole(c);
- con->setAlignment(f);
- }
- void gEngine::mPrint(int c, int x, int y, const char* fmt, ...) {
- TCODConsole* con = mGetConsole(c);
- con->printf(x, y, fmt);
- }
- void gEngine::mPrintEx(int c, int x, int y, int bk, int al, const char* fmt, ...) {
- TCOD_alignment_t a = mGetAlignmentFlag(al);
- TCOD_bkgnd_flag_t b = mGetFlag(bk);
- TCODConsole* con = mGetConsole(c);
- con->printf(x, y, b, a, fmt);
- }
- void gEngine::mPrintRect(int c, int x, int y, int w, int h, const char* fmt, ...) {
- TCODConsole* con = mGetConsole(c);
- con->printRect(x, y, w, h, fmt);
- }
- void gEngine::mPrintRectEx(int c, int x, int y, int w, int h, int bk, int al, const char* fmt, ...) {
- TCOD_alignment_t a = mGetAlignmentFlag(al);
- TCOD_bkgnd_flag_t b = mGetFlag(bk);
- TCODConsole* con = mGetConsole(c);
- con->printRectEx(x, y, w, h, b, a, fmt);
- }
- int gEngine::mGetHeightRect(int c, int x, int y, int w, int h, const char* fmt, ...) {
- TCODConsole* con = mGetConsole(c);
- return con->getHeightRect(x, y, w, h, fmt);
- }
- /////////////////////////////////
- // Advanced Printing Functions //
- /////////////////////////////////
- void gEngine::mHLine(int c, int x, int y, int l, int flag) {
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- con->hline(x, y, l, f);
- }
- void gEngine::mVLine(int c, int x, int y, int l, int flag) {
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- con->vline(x, y, l, f);
- }
- void gEngine::mPrintFrame(int c, int x, int y, int w, int h, bool clear, int flag, const char* fmt, ...) {
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- if (strcmp(fmt, "NULL")==0) {
- fmt = (const char*)0;
- }
- con->printFrame(x, y, w, h, clear, f, fmt);
- }
- void gEngine::mSaveScreenshot(const char* filename) {
- TCODSystem::saveScreenshot(filename);
- }
- ///////////////////
- // Image Toolkit //
- ///////////////////
- TCODImage* gEngine::mGetImage(int id) {
- if (id == -1) {
- return mDungeonImage;
- }
- vector<gEngineImage>::const_iterator iter;
- TCODImage* img;
- for (iter = mImages.begin(); iter != mImages.end(); iter++) {
- if (iter->id == id) {
- img = iter->img;
- return img;
- }
- }
- }
- void gEngine::mDestroyImage(int id) {
- UmbraLog::openBlock("Deleting Image with ID [%d]...", id);
- vector<gEngineImage>::const_iterator iter;
- for (iter = mImages.begin(); iter != mImages.end(); iter++) {
- if (iter->id == id) {
- delete iter->img;
- mImages.erase(iter);
- UmbraLog::info("...Success.");
- UmbraLog::closeBlock();
- return;
- }
- }
- }
- int gEngine::mCreateImage(int w, int h) {
- UmbraLog::openBlock("Creating a new blank image...");
- TCODImage* img = new TCODImage(w, h);
- gEngineImage imgObj;
- imgObj.img = img;
- imgObj.id = imageId;
- imageId++;
- UmbraLog::info("Image Width [%d], Height [%d], ID [%d]", w, h, imageId);
- mImages.push_back(imgObj);
- UmbraLog::closeBlock();
- return imgObj.id;
- }
- int gEngine::mLoadImage(const char* filename) {
- UmbraLog::openBlock("Creating new Image from file [%s], Id [%d]", filename, imageId);
- TCODImage* img = new TCODImage(filename);
- gEngineImage imgObj;
- imgObj.img = img;
- imgObj.id = imageId;
- imageId++;
- mImages.push_back(imgObj);
- UmbraLog::info("...Success.");
- UmbraLog::closeBlock();
- return imgObj.id;
- }
- int gEngine::mImageGetHeight(int i) {
- TCODImage* img = mGetImage(i);
- int w, h;
- img->getSize(&w, &h);
- return h;
- }
- int gEngine::mImageGetWidth(int i) {
- TCODImage* img = mGetImage(i);
- int w, h;
- img->getSize(&w, &h);
- return w;
- }
- int gEngine::mImageGetR(int i, int x, int y) {
- TCODImage* img = mGetImage(i);
- TCODColor col = img->getPixel(x, y);
- return col.r;
- }
- int gEngine::mImageGetG(int i, int x, int y) {
- TCODImage* img = mGetImage(i);
- TCODColor col = img->getPixel(x, y);
- return col.g;
- }
- int gEngine::mImageGetB(int i, int x, int y) {
- TCODImage* img = mGetImage(i);
- TCODColor col = img->getPixel(x, y);
- return col.b;
- }
- void gEngine::mImageClear(int i, int r, int g, int b) {
- TCODImage* img = mGetImage(i);
- TCODColor col = TCODColor(r, g, b);
- img->clear(col);
- }
- void gEngine::mImagePutPixel(int i, int x, int y, int r, int g, int b) {
- TCODImage* img = mGetImage(i);
- TCODColor col = TCODColor(r, g, b);
- img->putPixel(x, y, col);
- }
- void gEngine::mImageScale(int i, int newWidth, int newHeight) {
- TCODImage* img = mGetImage(i);
- img->scale(newWidth, newHeight);
- }
- void gEngine::mImageHFlip(int i) {
- TCODImage* img = mGetImage(i);
- img->hflip();
- }
- void gEngine::mImageVFlip(int i) {
- TCODImage* img = mGetImage(i);
- img->vflip();
- }
- void gEngine::mImageBlitRect(int i, int c, int x, int y, int w, int h, int flag) {
- TCODImage* img = mGetImage(i);
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- img->blitRect(con, x, y, w, h, f);
- }
- void gEngine::mImageBlit(int i, int c, int x, int y, int flag, float sx, float sy, float angle) {
- TCODImage* img = mGetImage(i);
- TCOD_bkgnd_flag_t f = mGetFlag(flag);
- TCODConsole* con = mGetConsole(c);
- img->blit(con, x, y, f, sx, sy, angle);
- }
- void gEngine::mImageBlit2x(int i, int c, int x, int y, int sx, int sy, int w, int h) {
- TCODImage* img = mGetImage(i);
- TCODConsole* con = mGetConsole(c);
- img->blit2x(con, x, y, sx, sy, w, h);
- }
- void gEngine::mImageSetKeyColor(int i, int r, int g, int b) {
- TCODImage* img = mGetImage(i);
- TCODColor col = TCODColor(r, g, b);
- img->setKeyColor(col);
- }
- void gEngine::mImageBatchShift(int i, int r1, int g1, int b1, int r2, int g2, int b2) {
- TCODImage* img = mGetImage(i);
- TCODColor col = TCODColor(r1, g1, b1);
- TCODColor col2 = TCODColor(r2, g2, b2);
- int w, h;
- img->getSize(&w, &h);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- TCODColor c = img->getPixel(x, y);
- if (c == col) {
- img->putPixel(x, y, col2);
- }
- }
- }
- }
- ///////////////////////////////
- // Dungeon Drawing Functions //
- ///////////////////////////////
- void gEngine::mDungeonNewMap(int w, int h) {
- UmbraLog::openBlock("Initializing new dungeon map");
- mFovMap = new TCODMap(w, h);
- mDungeonImage = new TCODImage(w, h);
- UmbraLog::info("Size Width[%d], Height [%d]", w, h);
- UmbraLog::closeBlock();
- }
- void gEngine::mDungeonReset() {
- UmbraLog::openBlock("Resetting Dungeon...");
- mDungeonMap.clear();
- delete mFovMap;
- delete mDungeonImage;
- UmbraLog::info("...Done");
- UmbraLog::closeBlock();
- // light mask reset
- }
- void gEngine::mDungeonAddTile(int x, int y, bool blocked, bool blockSight, int r, int g, int b) {
- gEngineTile tile;
- TCODColor col = TCODColor(r, g, b);
- tile.x = x;
- tile.y = y;
- tile.blocked = blocked;
- tile.blockSight = blockSight;
- tile.color = col;
- tile.explored = false;
- mDungeonMap.push_back(tile);
- mFovMap->setProperties(x, y, blockSight, blocked);
- }
- void gEngine::mDungeonRenderStaticMap(int c, int x, int y) {
- TCODConsole* con = mGetConsole(c);
- mFovMap->computeFov(x, y, 0, true, FOV_SHADOW);
- vector<gEngineTile>::iterator iter;
- int r = 0, g = 0, b = 0;
- for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {
- if (mFovMap->isInFov(iter->x, iter->y)) {
- vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(iter->x, iter->y);
- r = iter->color.r;
- g = iter->color.g;
- b = iter->color.b;
- r = (r * lightIter->r);
- g = (g * lightIter->g);
- b = (b * lightIter->b);
- //multiply by lightmask value
- iter->setExplored();
- }
- else {
- if (iter->explored) {
- r = iter->color.r * lightMaskAmbient;
- g = iter->color.g * lightMaskAmbient;
- b = iter->color.b * lightMaskAmbient;
- }
- else {
- r=0, g=0, b = 0;
- }
- }
- TCODColor col = TCODColor(r, g, b);
- mDungeonImage->putPixel(iter->x, iter->y, col);
- }
- mDungeonImage->blitRect(con, 0, 0);
- }
- void gEngine::mDungeonRenderStaticMap2x(int c, int x, int y) {
- TCODConsole* con = mGetConsole(c);
- mFovMap->computeFov(x, y, 0, true, FOV_SHADOW);
- vector<gEngineTile>::iterator iter;
- int r = 0, g = 0, b = 0;
- for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {
- if (mFovMap->isInFov(iter->x, iter->y)) {
- vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(iter->x, iter->y);
- r = iter->color.r;
- r = iter->color.r;
- g = iter->color.g;
- b = iter->color.b;
- r = (r * lightIter->r);
- g = (g * lightIter->g);
- b = (b * lightIter->b);
- // multiply by lightmask value
- iter->setExplored();
- }
- else {
- if (iter->explored) {
- r = iter->color.r * lightMaskAmbient;
- g = iter->color.g * lightMaskAmbient;
- b = iter->color.b * lightMaskAmbient;
- }
- else {
- r = 0, g = 0, b = 0;
- }
- }
- TCODColor col = TCODColor(r, g, b);
- mDungeonImage->putPixel(iter->x, iter->y, col);
- }
- }
- void gEngine::mDungeonRenderScrollingMap(int c, int x, int y) {
- }
- void gEngine::mDungeonRenderScrollingMap2x(int c, int x, int y) {
- }
- void gEngine::mDungeonBlit2x(int c) {
- TCODConsole* con = mGetConsole(c);
- mDungeonImage->blit2x(con, 0, 0);
- }
- bool gEngine::mDungeonIsExplored(int x, int y) {
- try {
- vector<gEngineTile>::iterator iter;
- if (mDungeonMap.size() < idx(x, y)) {
- throw false;
- }
- iter = mDungeonMap.begin() + idx(x, y);
- return iter->explored;
- }
- catch (...) {
- UmbraLog::openBlock("Exception in mDungeonIsExplored.");
- UmbraLog::fatalError("");
- UmbraLog::fatalError("mDungeonMap Size incorrect. Index point too large");
- UmbraLog::fatalError("Size [%d], index [%d]", mDungeonMap.size(), idx(x, y));
- UmbraLog::fatalError("X [%d], y [%d]", x, y);
- UmbraLog::fatalError("Map Width [%d], Height [%d]", lightMaskWidth, lightMaskHeight);
- UmbraLog::fatalError("Returning false. Verify map is initialized properly, or index is correct");
- UmbraLog::fatalError("");
- UmbraLog::closeBlock();
- return false;
- }
- }
- bool gEngine::mDungeonIsWalkable(int x, int y) {
- return mFovMap->isWalkable(x, y);
- }
- bool gEngine::mDungeonIsTransparent(int x, int y) {
- return mFovMap->isTransparent(x, y);
- }
- void gEngine::mDungeonRevealMap() {
- vector<gEngineTile>::iterator iter;
- for (iter = mDungeonMap.begin(); iter != mDungeonMap.end(); iter++) {
- iter->setExplored();
- }
- }
- //////////////////////////
- // Light Mask Functions //
- //////////////////////////
- void gEngine::mLightmaskInit(int w, int h) {
- UmbraLog::openBlock("Initializing lightmask...");
- lightMaskWidth = w;
- lightMaskHeight = h;
- mLightmaskReset();
- UmbraLog::info("...Done");
- UmbraLog::closeBlock();
- }
- void gEngine::mLightmaskSetAmbient(float ambient) {
- lightMaskAmbient = ambient;
- }
- void gEngine::mLightmaskReset() {
- //UmbraLog::openBlock("Resetting light mask...");
- mLightMask.clear();
- mLights.clear();
- for (int x = 0; x <= lightMaskWidth; x++) {
- for (int y = 0; y <= lightMaskHeight; y++) {
- lightMaskLight light;
- light.r = lightMaskAmbient;
- light.g = lightMaskAmbient;
- light.b = lightMaskAmbient;
- mLightMask.push_back(light);
- }
- }
- //UmbraLog::info("...Done. Lightmask set to ambient levels of [%f].", lightMaskAmbient);
- //UmbraLog::closeBlock();
- }
- void gEngine::mLightmaskCalculateLight(gEngineLight light) {
- int top = max(0, light.y - light.radius);
- int bottom = min(lightMaskHeight, light.y + light.radius);
- int left = max(0, light.x - light.radius);
- int right = min(lightMaskWidth, light.x + light.radius);
- mLightmaskLightTile(light.x, light.y, 1.0f, light);
- TCOD_bresenham_data_t data;
- int xx = 0, yy = 0;
- for (int x = left; x <= right; x++) {
- xx = 0, yy = 0;
- TCODLine::init(light.x, light.y, x, top);
- do {
- if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
- if (top == 0 && left == 0) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- else {
- if (!mFovMap->isTransparent(xx, yy)) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- mLightmaskLightTile(xx, yy, d, light);
- break;
- }
- else {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- }
- }
- } while(!TCODLine::step(&xx, &yy));
- xx = 0, yy = 0;
- TCODLine::init(light.x, light.y, x, bottom);
- do {
- if(mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
- if (top == 0 && left == 0) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- else {
- if (!mFovMap->isTransparent(xx, yy)) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- mLightmaskLightTile(xx, yy, d, light);
- break;
- }
- else {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- }
- }
- } while (!TCODLine::step(&xx, &yy));
- }
- for (int y = top; y <= bottom; y++) {
- //int xx = 0, yy = 0;
- xx = 0, yy = 0;
- TCODLine::init(light.x, light.y, left, y);
- do {
- if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
- if (top == 0 && left == 0) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- else {
- if (!mFovMap->isTransparent(xx, yy)) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- mLightmaskLightTile(xx, yy, d, light);
- break;
- }
- else {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- }
- }
- } while (!TCODLine::step(&xx, &yy));
- xx = 0, yy = 0;
- TCODLine::init(light.x, light.y, right, y);
- do {
- if (mLightmaskInCircle(light.x, light.y, xx, yy, light.radius)) {
- if (top == 0 && left == 0) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- else {
- if (!mFovMap->isTransparent(xx, yy)) {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- mLightmaskLightTile(xx, yy, d, light);
- break;
- }
- else {
- float d = mLightmaskDistanceFromCenter(light.x, light.y, xx, yy, light.radius);
- if (!mLightmaskLightTile(xx, yy, d, light)) {
- break;
- }
- }
- }
- }
- } while (!TCODLine::step(&xx, &yy));
- }
- }
- void gEngine::mLightmaskCompute() {
- vector<gEngineLight>::iterator light;
- for (light = mLights.begin(); light != mLights.end(); light++) {
- this->mLightmaskCalculateLight(*light);
- }
- }
- void gEngine::mLightmaskAddLight(int x, int y, int r, int g, int b, float intensity, float radius) {
- //UmbraLog::openBlock("Adding light to lightmask.");
- //UmbraLog::info("Light Container size before loading new light [%d]", mLights.size());
- float newr = r / 255.0f;
- float newg = g / 255.0f;
- float newb = b / 255.0f;
- gEngineLight light;
- light.x = x;
- light.y = y;
- light.r = newr;
- light.g = newg;
- light.b = newb;
- light.color = TCODColor(r, g, b);
- light.radius = radius;
- light.intensity = intensity;
- mLights.push_back(light);
- //UmbraLog::info("Done... New size [%d]", mLights.size());
- //UmbraLog::closeBlock();
- }
- bool gEngine::mLightmaskLightTile(int x, int y, float amount, gEngineLight light) {
- float d = amount / light.radius;
- d = -d;
- float c1 = (d + light.r);
- float c2 = (d + light.g);
- float c3 = (d + light.b);
- vector<lightMaskLight>::iterator lightIter = mLightMask.begin() + idx(x, y);
- c1 = max(c1, lightIter->r);
- c2 = max(c2, lightIter->g);
- c3 = max(c3, lightIter->b);
- c1 = max(c1, lightMaskAmbient);
- c2 = max(c2, lightMaskAmbient);
- c3 = max(c3, lightMaskAmbient);
- /*lightIter->r = c1;
- lightIter->g = c2;
- lightIter->b = c3;*/
- lightIter->update(c1, c2, c3);
- if (c1 == lightMaskAmbient && c2 == lightMaskAmbient && c3 == lightMaskAmbient) {
- return false;
- }
- return true;
- }
- float gEngine::mLightmaskGetValueR(int x, int y) {
- vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
- return light->r;
- }
- float gEngine::mLightmaskGetValueG(int x, int y) {
- vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
- return light->g;
- }
- float gEngine::mLightmaskGetValueB(int x, int y) {
- vector<lightMaskLight>::iterator light = mLightMask.begin() + idx(x, y);
- return light->g;
- }
- bool gEngine::mLightmaskInCircle(int cx, int cy, int tx, int ty, float radius) {
- float dx = tx - cx;
- float dy = ty - cy;
- float sq = dx * dx + dy * dy;
- if (sq <= radius * radius) {
- return true;
- }
- return false;
- //return true;
- }
- float gEngine::mLightmaskDistanceFromCenter(int cx, int cy, int tx, int ty, float radius) {
- float dx = cx - tx;
- float dy = cy - ty;
- float sq = dx * dx + dy * dy;
- float d = sqrt(sq);
- return (float)d;
- }
- void gEngine::mOpenBlock(const char* mes, ...) {
- UmbraLog::openBlock(mes);
- }
- void gEngine::mCloseBlock() {
- UmbraLog::closeBlock();
- }
- void gEngine::mInfo(const char* mes, ...) {
- UmbraLog::info(mes);
- }
- void gEngine::mNotice(const char* mes, ...) {
- UmbraLog::notice(mes);
- }
- void gEngine::mError(const char* mes, ...) {
- UmbraLog::error(mes);
- }
- void gEngine::mFatalError(const char* mes, ...) {
- UmbraLog::fatalError(mes);
- }
- void gEngine::mMapAsciiCodeToFont(int code, int x, int y) {
- TCODConsole::mapAsciiCodeToFont(code, x, y);
- }
- int gEngine::mNewDijkstraMap(int w, int h) {
- DijkstraMap m;
- m.id = dijkstraMapId;
- dijkstraMapId++;
- m.w = w;
- m.h = h;
- m.maxValue = w * h;
- for (int x = 0; x <= w; x++) {
- for (int y = 0; y <= h; y++) {
- m.tiles.push_back(DijkstraPoint(x, y, m.maxValue));
- }
- }
- mDijkstraMaps.push_back(m);
- return m.id;
- }
- void gEngine::mDijkstraCompute(int id) {
- DijkstraMap map = mDijkstraGetMap(id);
- vector<DijkstraPoint>::iterator dmap_iter;
- vector<DijkstraPoint>::iterator point_iter;
- vector<gEngineTile>::iterator iter;
- vector<int> value;
- int value2, dif;
- bool changes = false;
- UmbraLog::openBlock("Computing Dijkstra Map....");
- for (point_iter = map.points.begin(); point_iter != map.points.end(); point_iter++) {
- dmap_iter = map.tiles.begin() + point_iter->x + point_iter->y * map.w;
- UmbraLog::openBlock("Point Value %d", point_iter->value);
- dmap_iter->set_value(point_iter->value);
- UmbraLog::info("Map Value %d", dmap_iter->value);
- UmbraLog::closeBlock();
- }
- //while (true) {
- for(int z =0; z< 10; z++){
- changes = false;
- for (int x = 2; x < map.w - 2; x++) {
- for (int y = 2; y < map.h - 2; y++) {
- //iter = (mDungeonMap.begin() + ((x * 2) + (y * 2)) * (map.w * 2));
- //if (!iter->blocked) {
- dmap_iter = map.tiles.begin() + ((x+1) + y) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + ((x-1) + y) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + (x + (y+1)) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + (x + (y-1)) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + ((x+1) + (y+1)) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + ((x-1) + (y-1)) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + ((x-1) + (y+1)) * map.w;
- value.push_back(dmap_iter->value);
- dmap_iter = map.tiles.begin() + ((x+1) + (y-1)) * map.w;
- value.push_back(dmap_iter->value);
- value2 = *min_element(value.begin(), value.end());
- value.clear();
- dmap_iter = map.tiles.begin() + (x + y) * map.w;
- dif = dmap_iter->value - value2;
- if (dif > 1) {
- dmap_iter->set_value(value2 + 1);
- changes = true;
- }
- //}
- }
- }
- if (!changes) {
- break;
- }
- }
- UmbraLog::closeBlock();
- }
- void gEngine::mDijkstraAddPoint(int id, int x, int y, int value) {
- DijkstraMap map = mDijkstraGetMap(id);
- vector<DijkstraPoint>::iterator dmap_iter;
- dmap_iter = map.tiles.begin() + x + y * map.w;
- dmap_iter->set_value(value);
- map.points.push_back(DijkstraPoint(x, y, value));
- }
- void gEngine::mDijkstraClearMap(int id) {
- DijkstraMap map = mDijkstraGetMap(id);
- vector<DijkstraPoint>::iterator iter;
- for (iter = map.points.begin(); iter != map.points.end(); iter++) {
- iter->value = map.maxValue;
- }
- }
- void gEngine::mDijkstraRemovePoint(int id, int x, int y) {
- DijkstraMap map = mDijkstraGetMap(id);
- vector<DijkstraPoint>::iterator iter;
- for (iter = map.points.begin(); iter != map.points.end(); iter++) {
- if (iter->x == x && iter->y == y) {
- map.points.erase(iter);
- return;
- }
- }
- }
- int gEngine::mDijkstraGetValue(int id, int x, int y) {
- DijkstraMap map = mDijkstraGetMap(id);
- vector<DijkstraPoint>::iterator iter;
- iter = map.tiles.begin() + x + y * map.w;
- return iter->value;
- }
- void gEngine::mDijkstraRemoveMap(int id) {
- vector<DijkstraMap>::const_iterator iter;
- DijkstraMap map;
- for (iter = mDijkstraMaps.begin(); iter != mDijkstraMaps.end(); iter++) {
- if (iter->id == id) {
- mDijkstraMaps.erase(iter);
- return;
- }
- }
- }
- DijkstraMap gEngine::mDijkstraGetMap(int id) {
- vector<DijkstraMap>::const_iterator iter;
- DijkstraMap map;
- for (iter = mDijkstraMaps.begin(); iter != mDijkstraMaps.end(); iter++) {
- if (iter->id == id) {
- map = (*iter);
- return map;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement