Skip to content

Commit

Permalink
ENGINES: Implement triggers for KotOR games
Browse files Browse the repository at this point in the history
  • Loading branch information
vkremianskii committed May 22, 2018
1 parent 198c2c4 commit 3b594cf
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/engines/aurora/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ src_engines_aurora_libaurora_la_SOURCES += \
src/engines/aurora/satellitecamera.h \
src/engines/aurora/walkmesh.h \
src/engines/aurora/walkeleveval.h \
src/engines/aurora/trigger.h \
$(EMPTY)

src_engines_aurora_libaurora_la_SOURCES += \
Expand All @@ -52,4 +53,5 @@ src_engines_aurora_libaurora_la_SOURCES += \
src/engines/aurora/satellitecamera.cpp \
src/engines/aurora/walkmesh.cpp \
src/engines/aurora/walkeleveval.cpp \
src/engines/aurora/trigger.cpp \
$(EMPTY)
93 changes: 93 additions & 0 deletions src/engines/aurora/trigger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos 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 3
* of the License, or (at your option) any later version.
*
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Generic trigger.
*/

#include "glm/gtc/type_ptr.hpp"

#include "src/common/intersect.h"

#include "src/aurora/gff3file.h"
#include "src/aurora/resman.h"

#include "src/engines/aurora/trigger.h"

namespace Engines {

Trigger::Trigger()
: Renderable(Graphics::kRenderableTypeObject),
_visible(false) {

}

void Trigger::setVisible(bool visible) {
_visible = visible;
}

bool Trigger::contains(float x, float y) const {
size_t vertexCount = _geometry.size();
if (vertexCount < 3)
return false;

glm::vec3 p0(x, y, 1000);
glm::vec3 p1(x, y, -1000);
glm::vec3 intersection;

for (size_t i = 2; i < vertexCount; ++i) {
if (Common::intersectRayTriangle(p0, p1,
_geometry[0],
_geometry[i - 1],
_geometry[i],
intersection) == 1) {
return true;
}
}

return false;
}

void Trigger::calculateDistance() {

}

void Trigger::render(Graphics::RenderPass pass) {
if (!_visible || pass != Graphics::kRenderPassTransparent)
return;

size_t vertexCount = _geometry.size();
if (vertexCount < 3)
return;

glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
glBegin(GL_TRIANGLES);

for (size_t i = 2; i < vertexCount; ++i) {
glVertex3fv(glm::value_ptr(_geometry[0]));
glVertex3fv(glm::value_ptr(_geometry[i - 1]));
glVertex3fv(glm::value_ptr(_geometry[i]));
}

glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

} // End of namespace Engines
54 changes: 54 additions & 0 deletions src/engines/aurora/trigger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos 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 3
* of the License, or (at your option) any later version.
*
* xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Generic trigger.
*/

#ifndef ENGINES_AURORA_TRIGGER_H
#define ENGINES_AURORA_TRIGGER_H

#include <vector>

#include "glm/vec3.hpp"

#include "src/graphics/renderable.h"

namespace Engines {

class Trigger : public Graphics::Renderable {
public:
Trigger();

void setVisible(bool visible);
bool contains(float x, float y) const;

// .--- Renderable
void calculateDistance();
void render(Graphics::RenderPass pass);
// '---
protected:
std::vector<glm::vec3> _geometry;
bool _visible;
};

} // End of namespace Engines

#endif // ENGINES_AURORA_TRIGGER_H
57 changes: 55 additions & 2 deletions src/engines/kotor/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@
#include "src/engines/kotor/placeable.h"
#include "src/engines/kotor/door.h"
#include "src/engines/kotor/creature.h"
#include "src/engines/kotor/trigger.h"

namespace Engines {

namespace KotOR {

Area::Area(Module &module, const Common::UString &resRef) : Object(kObjectTypeArea),
_module(&module), _resRef(resRef), _visible(false), _activeObject(0), _highlightAll(false) {
Area::Area(Module &module, const Common::UString &resRef)
: Object(kObjectTypeArea),
_module(&module),
_resRef(resRef),
_visible(false),
_activeObject(0),
_highlightAll(false),
_triggersVisible(false),
_activeTrigger(0) {

try {
load();
Expand Down Expand Up @@ -97,6 +105,8 @@ void Area::clear() {

_objects.clear();
_rooms.clear();
_triggers.clear();
_activeTrigger = 0;
}

uint32 Area::getMusicDayTrack() const {
Expand Down Expand Up @@ -318,6 +328,9 @@ void Area::loadGIT(const Aurora::GFF3Struct &git) {

if (git.hasField("Creature List"))
loadCreatures(git.getList("Creature List"));

if (git.hasField("TriggerList"))
loadTriggers(git.getList("TriggerList"));
}

void Area::loadProperties(const Aurora::GFF3Struct &props) {
Expand Down Expand Up @@ -415,6 +428,15 @@ void Area::loadCreatures(const Aurora::GFF3List &list) {
}
}

void Area::loadTriggers(const Aurora::GFF3List &list) {
for (Aurora::GFF3List::const_iterator t = list.begin(); t != list.end(); ++t) {
Trigger *trigger = new Trigger(**t);

loadObject(*trigger);
_triggers.push_back(trigger);
}
}

void Area::addEvent(const Events::Event &event) {
_eventQueue.push_back(event);
}
Expand Down Expand Up @@ -527,6 +549,37 @@ void Area::toggleWalkmesh() {
_walkmesh.show();
}

void Area::toggleTriggers() {
_triggersVisible = !_triggersVisible;
for (std::vector<Trigger *>::const_iterator it = _triggers.begin();
it != _triggers.end();
++it) {
(*it)->setVisible(_triggersVisible);
}
}

void Area::evaluateTriggers(float x, float y) {
Trigger *trigger = 0;

for (std::vector<Trigger *>::iterator it = _triggers.begin();
it != _triggers.end();
++it) {
Trigger *t = *it;
if (t->contains(x, y)) {
trigger = t;
break;
}
}

if (_activeTrigger != trigger) {
if (_activeTrigger)
_activeTrigger->runScript(kScriptExit, this, _module->getPC());
_activeTrigger = trigger;
if (_activeTrigger)
_activeTrigger->runScript(kScriptEnter, this, _module->getPC());
}
}

} // End of namespace KotOR

} // End of namespace Engines
12 changes: 12 additions & 0 deletions src/engines/kotor/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "src/engines/aurora/walkeleveval.h"
#include "src/engines/kotor/object.h"
#include "src/engines/kotor/walkmesh.h"
#include "src/engines/kotor/trigger.h"

namespace Engines {

Expand Down Expand Up @@ -122,6 +123,10 @@ class Area : public KotOR::Object, public Events::Notifyable {
float getElevationAt(float x, float y) const;
void toggleWalkmesh();

/// .--- Triggers
void toggleTriggers();
void evaluateTriggers(float x, float y);
/// '---

protected:
void notifyCameraMoved();
Expand Down Expand Up @@ -183,6 +188,12 @@ class Area : public KotOR::Object, public Events::Notifyable {
Common::Mutex _mutex; ///< Mutex securing access to the area.
KotOR::Walkmesh _walkmesh; ///< Walkmesh for collision detection.

/// .--- Triggers
std::vector<Trigger *> _triggers;
bool _triggersVisible;
Trigger *_activeTrigger;
/// '---


// Loading helpers

Expand All @@ -205,6 +216,7 @@ class Area : public KotOR::Object, public Events::Notifyable {
void loadPlaceables(const Aurora::GFF3List &list);
void loadDoors (const Aurora::GFF3List &list);
void loadCreatures (const Aurora::GFF3List &list);
void loadTriggers (const Aurora::GFF3List &list);

void unload();

Expand Down
21 changes: 14 additions & 7 deletions src/engines/kotor/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,26 @@ Console::Console(KotOREngine &engine) :
::Engines::Console(engine, Graphics::Aurora::kSystemFontMono, 13),
_engine(&engine), _maxSizeMusic(0) {

registerCommand("exitmodule" , boost::bind(&Console::cmdExitModule , this, _1),
registerCommand("exitmodule" , boost::bind(&Console::cmdExitModule , this, _1),
"Usage: exitmodule\nExit the module, returning to the main menu");
registerCommand("listmodules", boost::bind(&Console::cmdListModules , this, _1),
registerCommand("listmodules", boost::bind(&Console::cmdListModules , this, _1),
"Usage: listmodules\nList all modules");
registerCommand("loadmodule" , boost::bind(&Console::cmdLoadModule , this, _1),
registerCommand("loadmodule" , boost::bind(&Console::cmdLoadModule , this, _1),
"Usage: loadmodule <module>\nLoad and enter the specified module");
registerCommand("listmusic" , boost::bind(&Console::cmdListMusic , this, _1),
registerCommand("listmusic" , boost::bind(&Console::cmdListMusic , this, _1),
"Usage: listmusic\nList all available music resources");
registerCommand("stopmusic" , boost::bind(&Console::cmdStopMusic , this, _1),
registerCommand("stopmusic" , boost::bind(&Console::cmdStopMusic , this, _1),
"Usage: stopmusic\nStop the currently playing music resource");
registerCommand("playmusic" , boost::bind(&Console::cmdPlayMusic , this, _1),
registerCommand("playmusic" , boost::bind(&Console::cmdPlayMusic , this, _1),
"Usage: playmusic [<music>]\nPlay the specified music resource. "
"If none was specified, play the default area music.");
registerCommand("tfc" , boost::bind(&Console::cmdToggleFreeCam, this, _1),
registerCommand("tfc" , boost::bind(&Console::cmdToggleFreeCam , this, _1),
"Usage: tfc\nToggle free roam camera mode");
registerCommand("tw" , boost::bind(&Console::cmdToggleWalkmesh, this, _1),
"Usage: tw\nToggle walkmesh display");
registerCommand("tt" , boost::bind(&Console::cmdToggleTriggers, this, _1),
"Usage: tt\nToggle triggers display");

}

Console::~Console() {
Expand Down Expand Up @@ -153,6 +156,10 @@ void Console::cmdToggleWalkmesh(const CommandLine &UNUSED(cl)) {
_engine->getGame().getModule().toggleWalkmesh();
}

void Console::cmdToggleTriggers(const CommandLine &UNUSED(cl)) {
_engine->getGame().getModule().toggleTriggers();
}

} // End of namespace KotOR

} // End of namespace Engines
1 change: 1 addition & 0 deletions src/engines/kotor/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Console : public ::Engines::Console {
void cmdPlayMusic (const CommandLine &cl);
void cmdToggleFreeCam (const CommandLine &cl);
void cmdToggleWalkmesh(const CommandLine &cl);
void cmdToggleTriggers(const CommandLine &cl);
};

} // End of namespace KotOR
Expand Down
6 changes: 6 additions & 0 deletions src/engines/kotor/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ void Module::movedPC() {
CameraMan.setPosition(x, y, z + 1.8f);
CameraMan.update();
}

_area->evaluateTriggers(x, y);
}

void Module::setReturnStrref(uint32 id) {
Expand Down Expand Up @@ -769,6 +771,10 @@ void Module::toggleWalkmesh() {
_area->toggleWalkmesh();
}

void Module::toggleTriggers() {
_area->toggleTriggers();
}

void Module::loadSavedGame(SavedGame *save) {
try {
usePC(save->getPC());
Expand Down
1 change: 1 addition & 0 deletions src/engines/kotor/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Module : public KotOR::Object, public KotOR::ObjectContainer {

void toggleFreeRoamCamera();
void toggleWalkmesh();
void toggleTriggers();
void loadSavedGame(SavedGame *save);
void startConversation(const Common::UString &name);

Expand Down
2 changes: 2 additions & 0 deletions src/engines/kotor/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ src_engines_kotor_libkotor_la_SOURCES += \
src/engines/kotor/item.h \
src/engines/kotor/walkmesh.h \
src/engines/kotor/savedgame.h \
src/engines/kotor/trigger.h \
$(EMPTY)

src_engines_kotor_libkotor_la_SOURCES += \
Expand All @@ -63,6 +64,7 @@ src_engines_kotor_libkotor_la_SOURCES += \
src/engines/kotor/item.cpp \
src/engines/kotor/walkmesh.cpp \
src/engines/kotor/savedgame.cpp \
src/engines/kotor/trigger.cpp \
$(EMPTY)

include src/engines/kotor/script/rules.mk
Expand Down

0 comments on commit 3b594cf

Please sign in to comment.