refator: Remove IRoutineCallbacks interface

This commit is contained in:
Vsevolod Kremianskii 2020-09-28 16:18:05 +07:00
parent 1162a0371a
commit 72bba03898
10 changed files with 71 additions and 182 deletions

View file

@ -93,7 +93,6 @@ set(HEADERS
src/game/objectcontainer.h
src/game/paths.h
src/game/room.h
src/game/script/callbacks.h
src/game/script/routines.h
src/game/script/util.h
src/game/types.h

View file

@ -393,12 +393,12 @@ void Game::loadModule(const string &name, const PartyConfiguration &party, strin
_module->load(name, *ifo);
_module->loadParty(party, entry);
_module->area().loadState(_state);
_module->area()->loadState(_state);
if (_music) {
_music->stop();
}
string musicName(_module->area().music());
string musicName(_module->area()->music());
if (!musicName.empty()) {
_music = playMusic(musicName);
}
@ -420,7 +420,7 @@ void Game::loadHUD() {
_hud->resetFocus();
shared_ptr<SpatialObject> player(_module->area().player());
shared_ptr<SpatialObject> player(_module->area()->player());
_equipmentGui->open(player.get());
_screen = Screen::Equipment;
@ -440,7 +440,7 @@ void Game::loadDialogGui() {
dialog->load(_version);
dialog->setPickReplyEnabled(_pickDialogReplyEnabled);
dialog->setGetObjectIdByTagFunc([this](const string &tag) {
shared_ptr<Object> object(_module->area().find(tag));
shared_ptr<Object> object(_module->area()->find(tag));
return object ? object->id() : 0;
});
dialog->setOnReplyPicked(bind(&Game::onDialogReplyPicked, this, _1));
@ -453,7 +453,7 @@ void Game::loadContainerGui() {
unique_ptr<ContainerGui> container(new ContainerGui(_options.graphics));
container->load(_version);
container->setOnGetItems([this]() {
shared_ptr<SpatialObject> player(_module->area().player());
shared_ptr<SpatialObject> player(_module->area()->player());
SpatialObject &container = _containerGui->container();
container.moveItemsTo(*player);
@ -477,10 +477,10 @@ void Game::onDialogReplyPicked(uint32_t index) {
}
void Game::onDialogSpeakerChanged(uint32_t from, uint32_t to) {
shared_ptr<SpatialObject> player(_module->area().player());
shared_ptr<SpatialObject> partyLeader(_module->area().partyLeader());
shared_ptr<SpatialObject> prevSpeaker = from != 0 ? _module->area().find(from) : nullptr;
shared_ptr<SpatialObject> speaker = to != 0 ? _module->area().find(to) : nullptr;
shared_ptr<SpatialObject> player(_module->area()->player());
shared_ptr<SpatialObject> partyLeader(_module->area()->partyLeader());
shared_ptr<SpatialObject> prevSpeaker = from != 0 ? _module->area()->find(from) : nullptr;
shared_ptr<SpatialObject> speaker = to != 0 ? _module->area()->find(to) : nullptr;
if (speaker == partyLeader) return;
debug(boost::format("Game: dialog speaker: \"%s\"") % (speaker ? speaker->tag() : ""));
@ -570,46 +570,8 @@ bool Game::handle(const SDL_Event &event) {
return false;
}
void Game::delayCommand(uint32_t timestamp, const ExecutionContext &ctx) {
_module->area().delayCommand(timestamp, ctx);
}
Module *Game::getModule() {
return _module.get();
}
Area *Game::getArea() {
return &_module->area();
}
shared_ptr<Object> Game::getObjectById(uint32_t id) {
return _module->area().find(id);
}
shared_ptr<Object> Game::getObjectByTag(const string &tag, int nth) {
return _module->area().find(tag, nth);
}
shared_ptr<Object> Game::getWaypointByTag(const string &tag) {
return _module->area().find(tag);
}
shared_ptr<Object> Game::getPlayer() {
return _module->area().player();
}
int Game::eventUserDefined(int eventNumber) {
return _module->area().eventUserDefined(eventNumber);
}
void Game::signalEvent(uint32_t objectId, int eventId) {
assert(objectId >= 2);
Area &area = _module->area();
if (objectId != area.id()) {
warn("Game: event object is not an area");
return;
}
area.signalEvent(eventId);
shared_ptr<Module> Game::module() const {
return _module;
}
bool Game::getGlobalBoolean(const string &name) const {

View file

@ -48,7 +48,7 @@ namespace game {
* @see game::Module
* @see gui::GUI
*/
class Game : public render::IEventHandler, public IRoutineCallbacks {
class Game : public render::IEventHandler {
public:
Game(const boost::filesystem::path &path, const Options &opts);
@ -56,39 +56,22 @@ public:
void loadModule(const std::string &name, const PartyConfiguration &party, std::string entry = "");
bool handle(const SDL_Event &event) override;
// Routine callbacks
void delayCommand(uint32_t timestamp, const script::ExecutionContext &ctx) override;
int eventUserDefined(int eventNumber) override;
void signalEvent(uint32_t objectId, int eventId) override;
// Objects
Module *getModule() override;
Area *getArea() override;
std::shared_ptr<Object> getObjectById(uint32_t id) override;
std::shared_ptr<Object> getObjectByTag(const std::string &tag, int nth) override;
std::shared_ptr<Object> getWaypointByTag(const std::string &tag) override;
std::shared_ptr<Object> getPlayer() override;
// END Objects
std::shared_ptr<Module> module() const;
// Globals/locals
bool getGlobalBoolean(const std::string &name) const override;
int getGlobalNumber(const std::string &name) const override;
bool getLocalBoolean(uint32_t objectId, int index) const override;
int getLocalNumber(uint32_t objectId, int index) const override;
bool getGlobalBoolean(const std::string &name) const;
int getGlobalNumber(const std::string &name) const;
bool getLocalBoolean(uint32_t objectId, int idx) const;
int getLocalNumber(uint32_t objectId, int idx) const;
void setGlobalBoolean(const std::string &name, bool value) override;
void setGlobalNumber(const std::string &name, int value) override;
void setLocalBoolean(uint32_t objectId, int index, bool value) override;
void setLocalNumber(uint32_t objectId, int index, int value) override;
void setGlobalBoolean(const std::string &name, bool value);
void setGlobalNumber(const std::string &name, int value);
void setLocalBoolean(uint32_t objectId, int idx, bool value);
void setLocalNumber(uint32_t objectId, int idx, int value);
// END Globals/locals
// END Routine callbacks
protected:
enum class Screen {
None,

View file

@ -500,8 +500,8 @@ const ModuleInfo &Module::info() const {
return _info;
}
Area &Module::area() const {
return *_area;
shared_ptr<Area> Module::area() const {
return _area;
}
CameraType Module::cameraType() const {

View file

@ -26,7 +26,6 @@
#include "../render/camera/thirdperson.h"
#include "area.h"
#include "script/callbacks.h"
namespace reone {
@ -58,7 +57,7 @@ public:
bool loaded() const;
std::shared_ptr<render::Camera> getCamera() const;
const ModuleInfo &info() const;
Area &area() const;
std::shared_ptr<Area> area() const;
render::CameraType cameraType() const;
// Callbacks

View file

@ -77,16 +77,16 @@ void MultiplayerGame::synchronizeClient(const string &tag) {
lock_guard<recursive_mutex> syncLock(_syncMutex);
sendLoadModule(tag, _module->name());
shared_ptr<Object> partyLeader(_module->area().partyLeader());
shared_ptr<Object> partyLeader(_module->area()->partyLeader());
sendLoadCreature(tag, CreatureRole::PartyLeader, static_cast<Creature &>(*partyLeader));
shared_ptr<Object> partyMember1(_module->area().partyMember1());
shared_ptr<Object> partyMember1(_module->area()->partyMember1());
if (partyMember1) {
Creature &creature = static_cast<Creature &>(*partyMember1);
sendLoadCreature(tag, CreatureRole::PartyMember1, creature);
}
shared_ptr<Object> partyMember2(_module->area().partyMember2());
shared_ptr<Object> partyMember2(_module->area()->partyMember2());
if (partyMember2) {
Creature &creature = static_cast<Creature &>(*partyMember2);
sendLoadCreature(tag, CreatureRole::PartyMember2, creature);
@ -157,8 +157,8 @@ void MultiplayerGame::sendSetPlayerRole(const string &client, CreatureRole role)
void MultiplayerGame::onClientDisconnected(const string tag) {
if (!_module || !_module->loaded()) return;
const MultiplayerArea &area = static_cast<MultiplayerArea &>(_module->area());
shared_ptr<Object> object(area.findCreatureByClientTag(tag));
shared_ptr<Area> area(_module->area());
shared_ptr<Object> object(static_cast<MultiplayerArea &>(*area).findCreatureByClientTag(tag));
if (object) {
static_cast<MultiplayerCreature &>(*object).setClientTag("");
}
@ -201,7 +201,7 @@ void MultiplayerGame::update() {
_screen = Screen::InGame;
break;
default:
static_cast<MultiplayerArea &>(_module->area()).execute(cmd);
static_cast<MultiplayerArea &>(*_module->area()).execute(cmd);
break;
}
_commandsIn.pop();
@ -234,7 +234,7 @@ void MultiplayerGame::onObjectTransformChanged(const Object &object, const glm::
bool MultiplayerGame::shouldSendObjectUpdates(uint32_t objectId) const {
if (!_module || !_module->loaded()) return false;
shared_ptr<Object> player(_module->area().player());
shared_ptr<Object> player(_module->area()->player());
switch (_mode) {
case MultiplayerMode::Server:

View file

@ -1,70 +0,0 @@
/*
* Copyright © 2020 Vsevolod Kremianskii
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include "../../script/types.h"
#include "../object/object.h"
namespace reone {
namespace game {
class Area;
class Module;
class IRoutineCallbacks {
public:
virtual ~IRoutineCallbacks() = default;
virtual void delayCommand(uint32_t timestamp, const script::ExecutionContext &ctx) = 0;
virtual int eventUserDefined(int eventNumber) = 0;
virtual void signalEvent(uint32_t objectId, int eventId) = 0;
// Objects
virtual Module *getModule() = 0;
virtual Area *getArea() = 0;
virtual std::shared_ptr<Object> getObjectById(uint32_t id) = 0;
virtual std::shared_ptr<Object> getObjectByTag(const std::string &tag, int nth = 0) = 0;
virtual std::shared_ptr<Object> getWaypointByTag(const std::string &tag) = 0;
virtual std::shared_ptr<Object> getPlayer() = 0;
// END Objects
// Globals/locals
virtual bool getGlobalBoolean(const std::string &name) const = 0;
virtual int getGlobalNumber(const std::string &name) const = 0;
virtual bool getLocalBoolean(uint32_t objectId, int index) const = 0;
virtual int getLocalNumber(uint32_t objectId, int index) const = 0;
virtual void setGlobalBoolean(const std::string &name, bool value) = 0;
virtual void setGlobalNumber(const std::string &name, int value) = 0;
virtual void setLocalBoolean(uint32_t objectId, int index, bool value) = 0;
virtual void setLocalNumber(uint32_t objectId, int index, int value) = 0;
// END Globals/locals
};
} // namespace game
} // namespace reone

View file

@ -37,8 +37,8 @@ RoutineManager &RoutineManager::instance() {
return instance;
}
void RoutineManager::init(GameVersion version, IRoutineCallbacks *callbacks) {
_callbacks = callbacks;
void RoutineManager::init(GameVersion version, Game *game) {
_game = game;
switch (version) {
case GameVersion::KotOR:
@ -78,7 +78,6 @@ const Routine &RoutineManager::get(int index) {
shared_ptr<Object> RoutineManager::getObjectById(uint32_t id, const ExecutionContext &ctx) const {
uint32_t finalId = 0;
switch (id) {
case kObjectSelf:
finalId = ctx.callerId;
@ -91,7 +90,17 @@ shared_ptr<Object> RoutineManager::getObjectById(uint32_t id, const ExecutionCon
break;
}
return _callbacks->getObjectById(finalId);
shared_ptr<Module> module(_game->module());
if (finalId == module->id()) {
return module;
}
shared_ptr<Area> area(module->area());
if (finalId == area->id()) {
return area;
}
return area->find(finalId);
}
} // namespace game

View file

@ -26,23 +26,25 @@
#include "../../script/types.h"
#include "../../script/variable.h"
#include "callbacks.h"
#include "../object/object.h"
namespace reone {
namespace game {
class Game;
class RoutineManager : public script::IRoutineProvider {
public:
static RoutineManager &instance();
void init(resources::GameVersion version, IRoutineCallbacks *callbacks);
void init(resources::GameVersion version, Game *game);
void deinit();
const script::Routine &get(int index) override;
private:
IRoutineCallbacks *_callbacks { nullptr };
Game *_game { nullptr };
std::vector<script::Routine> _routines;
RoutineManager() = default;

View file

@ -23,6 +23,7 @@
#include "../../core/random.h"
#include "../area.h"
#include "../game.h"
#include "../object/creature.h"
#include "../object/door.h"
@ -52,7 +53,7 @@ Variable RoutineManager::getEnteringObject(const vector<Variable> &args, Executi
Variable RoutineManager::getIsPC(const vector<Variable> &args, ExecutionContext &ctx) {
assert(!args.empty() && args[0].type == VariableType::Object);
shared_ptr<Object> player(_callbacks->getPlayer());
shared_ptr<Object> player(_game->module()->area()->player());
return Variable(args[0].objectId == player->id());
}
@ -63,7 +64,7 @@ Variable RoutineManager::getIsObjectValid(const vector<Variable> &args, Executio
}
Variable RoutineManager::getFirstPC(const vector<Variable> &args, ExecutionContext &ctx) {
shared_ptr<Object> player(_callbacks->getPlayer());
shared_ptr<Object> player(_game->module()->area()->player());
Variable result(VariableType::Object);
result.objectId = player->id();
@ -82,7 +83,7 @@ Variable RoutineManager::getObjectByTag(const vector<Variable> &args, ExecutionC
tag = "party-leader";
}
int nth = args.size() >= 2 ? args[1].intValue : 0;
shared_ptr<Object> object(_callbacks->getObjectByTag(tag, nth));
shared_ptr<Object> object(_game->module()->area()->find(tag, nth));
Variable result(VariableType::Object);
result.objectId = object ? object->id() : kObjectInvalid;
@ -92,7 +93,7 @@ Variable RoutineManager::getObjectByTag(const vector<Variable> &args, ExecutionC
Variable RoutineManager::getWaypointByTag(const vector<Variable> &args, ExecutionContext &ctx) {
assert(!args.empty() && args[0].type == VariableType::String);
shared_ptr<Object> object(_callbacks->getWaypointByTag(args[0].strValue));
shared_ptr<Object> object(_game->module()->area()->find(args[0].strValue));
Variable result(VariableType::Object);
result.objectId = object ? object->id() : kObjectInvalid;
@ -134,7 +135,7 @@ Variable RoutineManager::getArea(const vector<Variable> &args, ExecutionContext
assert(!args.empty() && args[0].type == VariableType::Object);
Variable result(VariableType::Object);
result.objectId = _callbacks->getArea()->id();
result.objectId = _game->module()->area()->id();
return move(result);
}
@ -158,12 +159,12 @@ Variable RoutineManager::getItemInSlot(const vector<Variable> &args, ExecutionCo
Variable RoutineManager::getGlobalBoolean(const vector<Variable> &args, ExecutionContext &ctx) {
assert(!args.empty() && args[0].type == VariableType::String);
return _callbacks->getGlobalBoolean(args[0].strValue);
return _game->getGlobalBoolean(args[0].strValue);
}
Variable RoutineManager::getGlobalNumber(const vector<Variable> &args, ExecutionContext &ctx) {
assert(!args.empty() && args[0].type == VariableType::String);
return _callbacks->getGlobalNumber(args[0].strValue);
return _game->getGlobalNumber(args[0].strValue);
}
Variable RoutineManager::getLocalBoolean(const vector<Variable> &args, ExecutionContext &ctx) {
@ -172,7 +173,7 @@ Variable RoutineManager::getLocalBoolean(const vector<Variable> &args, Execution
args[0].type == VariableType::Object &&
args[1].type == VariableType::Int);
return _callbacks->getLocalBoolean(args[0].objectId, args[1].intValue);
return _game->getLocalBoolean(args[0].objectId, args[1].intValue);
}
Variable RoutineManager::getLocalNumber(const vector<Variable> &args, ExecutionContext &ctx) {
@ -181,7 +182,7 @@ Variable RoutineManager::getLocalNumber(const vector<Variable> &args, ExecutionC
args[0].type == VariableType::Object &&
args[1].type == VariableType::Int);
return _callbacks->getLocalNumber(args[0].objectId, args[1].intValue);
return _game->getLocalNumber(args[0].objectId, args[1].intValue);
}
Variable RoutineManager::setGlobalBoolean(const vector<Variable> &args, ExecutionContext &ctx) {
@ -190,7 +191,7 @@ Variable RoutineManager::setGlobalBoolean(const vector<Variable> &args, Executio
args[0].type == VariableType::String &&
args[1].type == VariableType::Int);
_callbacks->setGlobalBoolean(args[0].strValue, args[1].intValue);
_game->setGlobalBoolean(args[0].strValue, args[1].intValue);
return Variable();
}
@ -201,7 +202,7 @@ Variable RoutineManager::setGlobalNumber(const vector<Variable> &args, Execution
args[0].type == VariableType::String &&
args[1].type == VariableType::Int);
_callbacks->setGlobalNumber(args[0].strValue, args[1].intValue);
_game->setGlobalNumber(args[0].strValue, args[1].intValue);
return Variable();
}
@ -213,7 +214,7 @@ Variable RoutineManager::setLocalBoolean(const vector<Variable> &args, Execution
args[1].type == VariableType::Int && args[1].intValue >= 0 && args[1].intValue <= 63 &&
args[2].type == VariableType::Int);
_callbacks->setLocalBoolean(args[0].objectId, args[1].intValue, args[2].intValue);
_game->setLocalBoolean(args[0].objectId, args[1].intValue, args[2].intValue);
return Variable();
}
@ -225,7 +226,7 @@ Variable RoutineManager::setLocalNumber(const vector<Variable> &args, ExecutionC
args[1].type == VariableType::Int &&
args[2].type == VariableType::Int);
_callbacks->setLocalNumber(args[0].objectId, args[1].intValue, args[2].intValue);
_game->setLocalNumber(args[0].objectId, args[1].intValue, args[2].intValue);
return Variable();
}
@ -237,7 +238,7 @@ Variable RoutineManager::delayCommand(const vector<Variable> &args, ExecutionCon
args[1].type == VariableType::Action);
uint32_t timestamp = SDL_GetTicks() + static_cast<int>(args[0].floatValue * 1000.0f);
_callbacks->delayCommand(timestamp, args[1].context);
_game->module()->area()->delayCommand(timestamp, args[1].context);
return Variable();
}
@ -252,7 +253,7 @@ Variable RoutineManager::assignCommand(const vector<Variable> &args, ExecutionCo
newCtx.callerId = args[0].objectId;
newCtx.triggererId = kObjectInvalid;
_callbacks->delayCommand(SDL_GetTicks(), move(newCtx));
_game->module()->area()->delayCommand(SDL_GetTicks(), move(newCtx));
return Variable();
}
@ -261,7 +262,7 @@ Variable RoutineManager::eventUserDefined(const vector<Variable> &args, Executio
assert(!args.empty() && args[0].type == VariableType::Int);
Variable result(VariableType::Event);
result.engineTypeId = _callbacks->eventUserDefined(args[0].intValue);
result.engineTypeId = _game->module()->area()->eventUserDefined(args[0].intValue);
return move(result);
}
@ -274,7 +275,11 @@ Variable RoutineManager::signalEvent(const vector<Variable> &args, ExecutionCont
shared_ptr<Object> subject(getObjectById(args[0].objectId, ctx));
if (subject) {
_callbacks->signalEvent(subject->id(), args[1].engineTypeId);
if (subject->type() == ObjectType::Area) {
static_cast<Area &>(*subject).signalEvent(args[1].engineTypeId);
} else {
warn("Routine: event object is not an area");
}
} else {
warn("Routine: object not found by id: " + to_string(args[0].objectId));
}