From 64560867a9b6486d601784a2fe4ba6149d31b7aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Sat, 10 Dec 2016 01:14:42 +0700 Subject: [PATCH] Added a class for Hotspots to avoid using several arrays --- CMakeLists.txt | 2 + Source/ConsoleCmds.cpp | 36 ++++++++---------- Source/GameDraw.cpp | 31 ++++++--------- Source/GameTick.cpp | 86 +++++++++++++++++++----------------------- Source/Globals.cpp | 8 ---- Source/Hotspot.cpp | 38 +++++++++++++++++++ Source/Hotspot.h | 43 +++++++++++++++++++++ 7 files changed, 149 insertions(+), 95 deletions(-) create mode 100644 Source/Hotspot.cpp create mode 100644 Source/Hotspot.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c1a3e6b..0d3af5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ set(LUGARU_SRCS ${SRCDIR}/Account.cpp ${SRCDIR}/ConsoleCmds.cpp ${SRCDIR}/Dialog.cpp + ${SRCDIR}/Hotspot.cpp ${SRCDIR}/Game.cpp ${SRCDIR}/GameDraw.cpp ${SRCDIR}/GameInitDispose.cpp @@ -86,6 +87,7 @@ set(LUGARU_H ${SRCDIR}/Account.h ${SRCDIR}/ConsoleCmds.h ${SRCDIR}/Dialog.h + ${SRCDIR}/Hotspot.h ${SRCDIR}/Game.h ${SRCDIR}/Lights.h ${SRCDIR}/Menu.h diff --git a/Source/ConsoleCmds.cpp b/Source/ConsoleCmds.cpp index cdce93c..df26f82 100644 --- a/Source/ConsoleCmds.cpp +++ b/Source/ConsoleCmds.cpp @@ -21,6 +21,7 @@ along with Lugaru. If not, see . #include "ConsoleCmds.h" #include "Game.h" #include "Dialog.h" +#include "Hotspot.h" #include "Utils/Folders.h" const char *cmd_names[cmd_count] = { @@ -49,12 +50,7 @@ extern float slomospeed; extern float slomofreq; extern int tutoriallevel; extern int hostile; -extern XYZ hotspot[40]; -extern int hotspottype[40]; -extern float hotspotsize[40]; -extern char hotspottext[40][256]; extern int maptype; -extern int numhotspots; extern Objects objects; extern int slomo; extern float slomodelay; @@ -219,13 +215,13 @@ void ch_save(const char *args) fpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", objects.type[k], objects.yaw[k], objects.pitch[k], objects.position[k].x, objects.position[k].y, objects.position[k].z, objects.scale[k]); - fpackf(tfile, "Bi", numhotspots); - for (int i = 0; i < numhotspots; i++) { - fpackf(tfile, "Bi Bf Bf Bf Bf", hotspottype[i], hotspotsize[i], hotspot[i].x, hotspot[i].y, hotspot[i].z); - int templength = strlen(hotspottext[i]); + fpackf(tfile, "Bi", Hotspot::hotspots.size()); + for (int i = 0; i < Hotspot::hotspots.size(); i++) { + fpackf(tfile, "Bi Bf Bf Bf Bf", Hotspot::hotspots[i].type, Hotspot::hotspots[i].size, Hotspot::hotspots[i].position.x, Hotspot::hotspots[i].position.y, Hotspot::hotspots[i].position.z); + int templength = strlen(Hotspot::hotspots[i].text); fpackf(tfile, "Bi", templength); for (int l = 0; l < templength; l++) - fpackf(tfile, "Bb", hotspottext[i][l]); + fpackf(tfile, "Bb", Hotspot::hotspots[i].text[l]); } fpackf(tfile, "Bi", Person::players.size()); @@ -537,19 +533,14 @@ void ch_path(const char *args) void ch_hs(const char *args) { - hotspot[numhotspots] = Person::players[0]->coords; - float size; int type, shift; sscanf(args, "%f%d %n", &size, &type, &shift); - hotspotsize[numhotspots] = size; - hotspottype[numhotspots] = type; - - strcpy(hotspottext[numhotspots], args + shift); - strcat(hotspottext[numhotspots], "\n"); + Hotspot::hotspots.emplace_back(Person::players[0]->coords, type, size); - numhotspots++; + strcpy(Hotspot::hotspots.back().text, args + shift); + strcat(Hotspot::hotspots.back().text, "\n"); } void ch_dialogue(const char *args) @@ -593,13 +584,16 @@ void ch_fixrotation(const char *args) void ch_ddialogue(const char *args) { - Dialog::dialogs.pop_back(); + if (!Dialog::dialogs.empty()) { + Dialog::dialogs.pop_back(); + } } void ch_dhs(const char *args) { - if (numhotspots) - numhotspots--; + if (!Hotspot::hotspots.empty()) { + Hotspot::hotspots.pop_back(); + } } void ch_immobile(const char *args) diff --git a/Source/GameDraw.cpp b/Source/GameDraw.cpp index 45d2873..7ee322e 100644 --- a/Source/GameDraw.cpp +++ b/Source/GameDraw.cpp @@ -24,6 +24,7 @@ along with Lugaru. If not, see . #include "Awards.h" #include "Menu.h" #include "Dialog.h" +#include "Hotspot.h" extern XYZ viewer; extern int environment; @@ -77,14 +78,6 @@ extern bool againbonus; extern float damagedealt; extern bool invertmouse; -extern int numhotspots; -extern int killhotspot; -extern XYZ hotspot[40]; -extern int hotspottype[40]; -extern float hotspotsize[40]; -extern char hotspottext[40][256]; -extern int currenthotspot; - extern bool campaign; extern bool winfreeze; @@ -893,23 +886,23 @@ int Game::DrawGLScene(StereoSide side) } //Hot spots - if (numhotspots && (bonustime >= 1 || bonus <= 0 || bonustime < 0) && !tutoriallevel) { + if (Hotspot::hotspots.size() && (bonustime >= 1 || bonus <= 0 || bonustime < 0) && !tutoriallevel) { float closestdist = -1; float distance = 0; - int closest = currenthotspot; - for (int i = 0; i < numhotspots; i++) { - distance = distsq(&Person::players[0]->coords, &hotspot[i]); + int closest = Hotspot::current; + for (int i = 0; i < Hotspot::hotspots.size(); i++) { + distance = distsq(&Person::players[0]->coords, &Hotspot::hotspots[i].position); if (closestdist == -1 || distance < closestdist) { - if (distsq(&Person::players[0]->coords, &hotspot[i]) < hotspotsize[i] && ((hotspottype[i] <= 10 && hotspottype[i] >= 0) || (hotspottype[i] <= 40 && hotspottype[i] >= 20))) { + if (distsq(&Person::players[0]->coords, &Hotspot::hotspots[i].position) < Hotspot::hotspots[i].size && ((Hotspot::hotspots[i].type <= 10 && Hotspot::hotspots[i].type >= 0) || (Hotspot::hotspots[i].type <= 40 && Hotspot::hotspots[i].type >= 20))) { closestdist = distance; closest = i; } } } if (closest != -1) { - currenthotspot = closest; - if (hotspottype[closest] <= 10) { - if (distsq(&Person::players[0]->coords, &hotspot[closest]) < hotspotsize[closest]) + Hotspot::current = closest; + if (Hotspot::hotspots[closest].type <= 10) { + if (distsq(&Person::players[0]->coords, &Hotspot::hotspots[closest].position) < Hotspot::hotspots[closest].size) tutorialstagetime = 0; tutorialmaxtime = 1; tutorialopac = tutorialmaxtime - tutorialstagetime; @@ -918,7 +911,7 @@ int Game::DrawGLScene(StereoSide side) if (tutorialopac < 0) tutorialopac = 0; - sprintf (string, "%s", hotspottext[closest]); + sprintf (string, "%s", Hotspot::hotspots[closest].text); int lastline = 0; int line = 0; @@ -939,8 +932,8 @@ int Game::DrawGLScene(StereoSide side) done = 1; i++; } - } else if ((hotspottype[closest] >= 20) && (Dialog::dialogs[hotspottype[closest] - 20].gonethrough == 0)) { - Dialog::whichdialogue = hotspottype[closest] - 20; + } else if ((Hotspot::hotspots[closest].type >= 20) && (Dialog::dialogs[Hotspot::hotspots[closest].type - 20].gonethrough == 0)) { + Dialog::whichdialogue = Hotspot::hotspots[closest].type - 20; Dialog::currentDialog().play(); Dialog::currentDialog().gonethrough++; } diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 7ec218f..13eb1cb 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -44,6 +44,7 @@ along with Lugaru. If not, see . #include "ConsoleCmds.h" #include "Dialog.h" #include "Utils/Folders.h" +#include "Hotspot.h" #include #include @@ -130,14 +131,6 @@ extern float hostiletime; extern bool gamestarted; -extern int numhotspots; -extern int killhotspot; -extern XYZ hotspot[40]; -extern int hotspottype[40]; -extern float hotspotsize[40]; -extern char hotspottext[40][256]; -extern int currenthotspot; - extern int hostile; extern bool stillloading; @@ -840,8 +833,8 @@ void Game::Loadlevel(const std::string& name) if (accountactive) difficulty = accountactive->getDifficulty(); - numhotspots = 0; - currenthotspot = -1; + Hotspot::hotspots.clear(); + Hotspot::current = -1; bonustime = 1; skyboxtexture = 1; @@ -997,19 +990,22 @@ void Game::Loadlevel(const std::string& name) } if (mapvers >= 7) { + int numhotspots; funpackf(tfile, "Bi", &numhotspots); - for (int i = 0; i < numhotspots; i++) { - funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i], &hotspotsize[i], &hotspot[i].x, &hotspot[i].y, &hotspot[i].z); + Hotspot::hotspots.resize(numhotspots); + for (int i = 0; i < Hotspot::hotspots.size(); i++) { + funpackf(tfile, "Bi Bf Bf Bf Bf", &Hotspot::hotspots[i].type, &Hotspot::hotspots[i].size, &Hotspot::hotspots[i].position.x, &Hotspot::hotspots[i].position.y, &Hotspot::hotspots[i].position.z); funpackf(tfile, "Bi", &templength); if (templength) for (int l = 0; l < templength; l++) - funpackf(tfile, "Bb", &hotspottext[i][l]); - hotspottext[i][templength] = '\0'; - if (hotspottype[i] == -111) + funpackf(tfile, "Bb", &Hotspot::hotspots[i].text[l]); + Hotspot::hotspots[i].text[templength] = '\0'; + if (Hotspot::hotspots[i].type == -111) indemo = 1; } - } else - numhotspots = 0; + } else { + Hotspot::hotspots.clear(); + } if (visibleloading) LoadingScreen(); @@ -5438,27 +5434,27 @@ void Game::Tick() //hotspots static float hotspotvisual[40]; - if (numhotspots) { + if (Hotspot::hotspots.size()) { XYZ hotspotsprite; if (editorenabled) - for (int i = 0; i < numhotspots; i++) + for (int i = 0; i < Hotspot::hotspots.size(); i++) hotspotvisual[i] -= multiplier / 320; - for (int i = 0; i < numhotspots; i++) { + for (int i = 0; i < Hotspot::hotspots.size(); i++) { while (hotspotvisual[i] < 0) { hotspotsprite = 0; - hotspotsprite.x = float(abs(Random() % 100000)) / 100000 * hotspotsize[i]; + hotspotsprite.x = float(abs(Random() % 100000)) / 100000 * Hotspot::hotspots[i].size; hotspotsprite = DoRotation(hotspotsprite, 0, 0, Random() % 360); hotspotsprite = DoRotation(hotspotsprite, 0, Random() % 360, 0); - hotspotsprite += hotspot[i]; + hotspotsprite += Hotspot::hotspots[i].position; Sprite::MakeSprite(breathsprite, hotspotsprite, hotspotsprite * 0, 1, 0.5, 0, 7, 0.4); - hotspotvisual[i] += 0.1 / hotspotsize[i] / hotspotsize[i] / hotspotsize[i]; + hotspotvisual[i] += 0.1 / Hotspot::hotspots[i].size / Hotspot::hotspots[i].size / Hotspot::hotspots[i].size; } } - for (int i = 0; i < numhotspots; i++) { - if (hotspottype[i] <= 10 && hotspottype[i] > 0) { - hotspot[i] = Person::players[hotspottype[i]]->coords; + for (int i = 0; i < Hotspot::hotspots.size(); i++) { + if (Hotspot::hotspots[i].type <= 10 && Hotspot::hotspots[i].type > 0) { + Hotspot::hotspots[i].position = Person::players[Hotspot::hotspots[i].type]->coords; } } } @@ -5667,11 +5663,7 @@ void Game::Tick() if (Dialog::currentScene().sound != 0) { playdialoguescenesound(); if (Dialog::currentScene().sound == -5) { - hotspot[numhotspots] = Person::players[0]->coords; - hotspotsize[numhotspots] = 10; - hotspottype[numhotspots] = -1; - - numhotspots++; + Hotspot::hotspots.emplace_back(Person::players[0]->coords, -1, 10); } if (Dialog::currentScene().sound == -6) { hostile = 1; @@ -6911,23 +6903,23 @@ void Game::TickOnceAfter() } } - killhotspot = 2; - for (int i = 0; i < numhotspots; i++) { - if (hotspottype[i] > 10 && hotspottype[i] < 20) { - if (Person::players[hotspottype[i] - 10]->dead == 0) - killhotspot = 0; - else if (killhotspot == 2) - killhotspot = 1; + Hotspot::killhotspot = 2; + for (int i = 0; i < Hotspot::hotspots.size(); i++) { + if (Hotspot::hotspots[i].type > 10 && Hotspot::hotspots[i].type < 20) { + if (Person::players[Hotspot::hotspots[i].type - 10]->dead == 0) + Hotspot::killhotspot = 0; + else if (Hotspot::killhotspot == 2) + Hotspot::killhotspot = 1; } } - if (killhotspot == 2) - killhotspot = 0; + if (Hotspot::killhotspot == 2) + Hotspot::killhotspot = 0; winhotspot = false; - for (int i = 0; i < numhotspots; i++) - if (hotspottype[i] == -1) - if (distsq(&Person::players[0]->coords, &hotspot[i]) < hotspotsize[i]) + for (int i = 0; i < Hotspot::hotspots.size(); i++) + if (Hotspot::hotspots[i].type == -1) + if (distsq(&Person::players[0]->coords, &Hotspot::hotspots[i].position) < Hotspot::hotspots[i].size) winhotspot = true; int numalarmed = 0; @@ -6965,7 +6957,7 @@ void Game::TickOnceAfter() } - if (killhotspot) { + if (Hotspot::killhotspot) { changedelay = 1; targetlevel = whichlevel + 1; if (targetlevel > numchallengelevels - 1) @@ -6991,7 +6983,7 @@ void Game::TickOnceAfter() changedelay = .1; alldead = false; winhotspot = false; - killhotspot = 0; + Hotspot::killhotspot = 0; } if (!editorenabled && gameon && !mainmenu) { @@ -7033,13 +7025,13 @@ void Game::TickOnceAfter() (Person::players[0]->dead || (alldead && maptype == mapkilleveryone) || (winhotspot) || - (killhotspot))) + (Hotspot::killhotspot))) loading = 1; if ((Person::players[0]->dead || (alldead && maptype == mapkilleveryone) || (winhotspot) || (windialogue) || - (killhotspot)) && + (Hotspot::killhotspot)) && changedelay <= 0) { if (whichlevel != -2 && !loading && !Person::players[0]->dead) { winfreeze = true; diff --git a/Source/Globals.cpp b/Source/Globals.cpp index 8c09b7b..9c5d79d 100644 --- a/Source/Globals.cpp +++ b/Source/Globals.cpp @@ -108,14 +108,6 @@ int mainmenu = 0; int whichjointstartarray[26] = {0}; int whichjointendarray[26] = {0}; -int numhotspots = 0; -XYZ hotspot[40]; -int hotspottype[40] = {0}; -float hotspotsize[40] = {0}; -char hotspottext[40][256] = {0}; -int currenthotspot = 0; -int killhotspot = 0; - float smoketex = 0; float slomospeed = 0; diff --git a/Source/Hotspot.cpp b/Source/Hotspot.cpp new file mode 100644 index 0000000..03bf3a1 --- /dev/null +++ b/Source/Hotspot.cpp @@ -0,0 +1,38 @@ +/* +Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) + +This file is part of Lugaru. + +Lugaru is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +Lugaru is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Lugaru. If not, see . +*/ + +#include "Hotspot.h" + +std::vector Hotspot::hotspots; +int Hotspot::current = 0; +int Hotspot::killhotspot = 0; + +Hotspot::Hotspot() : + position(), + type(0), + size(0) +{ +} + +Hotspot::Hotspot(XYZ p, int t, float s) : + position(p), + type(t), + size(s) +{ +} diff --git a/Source/Hotspot.h b/Source/Hotspot.h new file mode 100644 index 0000000..3139f98 --- /dev/null +++ b/Source/Hotspot.h @@ -0,0 +1,43 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) + +This file is part of Lugaru. + +Lugaru is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +Lugaru is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Lugaru. If not, see . +*/ + +#ifndef _HOTSPOT_H_ +#define _HOTSPOT_H_ + +#include "Quaternions.h" +#include + +class Hotspot +{ +public: + static std::vector hotspots; + static int current; + static int killhotspot; + + Hotspot(); + Hotspot(XYZ position, int type, float size); + + XYZ position; + int type; + float size; + char text[256] = {0}; +}; + +#endif -- 2.39.5