1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 22:19:54 +00:00
openmw-tes3mp/apps/openmw/mwworld/world.cpp

734 lines
22 KiB
C++
Raw Normal View History

#include "world.hpp"
2010-07-18 16:29:16 +00:00
#include <cmath>
#include <iostream>
#include <components/bsa/bsa_archive.hpp>
#include "../mwrender/sky.hpp"
#include "../mwrender/interior.hpp"
#include "../mwrender/exterior.hpp"
#include "../mwmechanics/mechanicsmanager.hpp"
2010-08-14 07:29:38 +00:00
#include "../mwsound/soundmanager.hpp"
2010-07-09 14:07:03 +00:00
#include "ptr.hpp"
#include "environment.hpp"
#include "class.hpp"
2010-07-09 14:07:03 +00:00
namespace
{
template<typename T>
2010-07-02 15:21:27 +00:00
void listCellScripts (const ESMS::ESMStore& store,
ESMS::CellRefList<T, MWWorld::RefData>& cellRefList, MWWorld::World::ScriptList& scriptList,
MWWorld::Ptr::CellStore *cell)
{
2010-07-03 13:41:20 +00:00
for (typename ESMS::CellRefList<T, MWWorld::RefData>::List::iterator iter (
cellRefList.list.begin());
iter!=cellRefList.list.end(); ++iter)
{
2010-08-04 12:04:22 +00:00
if (!iter->base->script.empty() && iter->mData.getCount())
2010-07-02 15:21:27 +00:00
{
if (const ESM::Script *script = store.scripts.find (iter->base->script))
{
2010-07-02 15:21:27 +00:00
iter->mData.setLocals (*script);
2010-07-02 15:21:27 +00:00
scriptList.push_back (
std::make_pair (iter->base->script, MWWorld::Ptr (&*iter, cell)));
2010-07-02 15:21:27 +00:00
}
}
}
}
template<typename T>
ESMS::LiveCellRef<T, MWWorld::RefData> *searchViaHandle (const std::string& handle,
ESMS::CellRefList<T, MWWorld::RefData>& refList)
{
typedef typename ESMS::CellRefList<T, MWWorld::RefData>::List::iterator iterator;
for (iterator iter (refList.list.begin()); iter!=refList.list.end(); ++iter)
{
if (iter->mData.getHandle()==handle)
{
return &*iter;
}
}
return 0;
}
}
2010-07-03 13:41:20 +00:00
namespace MWWorld
{
2010-07-03 13:41:20 +00:00
void World::insertInteriorScripts (ESMS::CellStore<RefData>& cell)
{
listCellScripts (mStore, cell.activators, mLocalScripts, &cell);
listCellScripts (mStore, cell.potions, mLocalScripts, &cell);
listCellScripts (mStore, cell.appas, mLocalScripts, &cell);
listCellScripts (mStore, cell.armors, mLocalScripts, &cell);
listCellScripts (mStore, cell.books, mLocalScripts, &cell);
listCellScripts (mStore, cell.clothes, mLocalScripts, &cell);
listCellScripts (mStore, cell.containers, mLocalScripts, &cell);
listCellScripts (mStore, cell.creatures, mLocalScripts, &cell);
listCellScripts (mStore, cell.doors, mLocalScripts, &cell);
listCellScripts (mStore, cell.ingreds, mLocalScripts, &cell);
listCellScripts (mStore, cell.lights, mLocalScripts, &cell);
listCellScripts (mStore, cell.lockpicks, mLocalScripts, &cell);
listCellScripts (mStore, cell.miscItems, mLocalScripts, &cell);
listCellScripts (mStore, cell.npcs, mLocalScripts, &cell);
listCellScripts (mStore, cell.probes, mLocalScripts, &cell);
listCellScripts (mStore, cell.repairs, mLocalScripts, &cell);
listCellScripts (mStore, cell.weapons, mLocalScripts, &cell);
}
Ptr World::getPtr (const std::string& name, Ptr::CellStore& cell)
{
if (ESMS::LiveCellRef<ESM::Activator, RefData> *ref = cell.activators.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Potion, RefData> *ref = cell.potions.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Apparatus, RefData> *ref = cell.appas.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Armor, RefData> *ref = cell.armors.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Book, RefData> *ref = cell.books.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Clothing, RefData> *ref = cell.clothes.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Container, RefData> *ref = cell.containers.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Creature, RefData> *ref = cell.creatures.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Door, RefData> *ref = cell.doors.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Ingredient, RefData> *ref = cell.ingreds.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::CreatureLevList, RefData> *ref = cell.creatureLists.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::ItemLevList, RefData> *ref = cell.itemLists.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Light, RefData> *ref = cell.lights.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Tool, RefData> *ref = cell.lockpicks.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Misc, RefData> *ref = cell.miscItems.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::NPC, RefData> *ref = cell.npcs.find (name))
return Ptr (ref, &cell);
2010-08-03 13:24:44 +00:00
if (ESMS::LiveCellRef<ESM::Probe, RefData> *ref = cell.probes.find (name))
return Ptr (ref, &cell);
2010-08-03 13:24:44 +00:00
if (ESMS::LiveCellRef<ESM::Repair, RefData> *ref = cell.repairs.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Static, RefData> *ref = cell.statics.find (name))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Weapon, RefData> *ref = cell.weapons.find (name))
return Ptr (ref, &cell);
return Ptr();
}
Ptr World::getPtrViaHandle (const std::string& handle, Ptr::CellStore& cell)
{
if (ESMS::LiveCellRef<ESM::Activator, RefData> *ref =
searchViaHandle (handle, cell.activators))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Potion, RefData> *ref = searchViaHandle (handle, cell.potions))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Apparatus, RefData> *ref = searchViaHandle (handle, cell.appas))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Armor, RefData> *ref = searchViaHandle (handle, cell.armors))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Book, RefData> *ref = searchViaHandle (handle, cell.books))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Clothing, RefData> *ref = searchViaHandle (handle, cell.clothes))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Container, RefData> *ref =
searchViaHandle (handle, cell.containers))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Creature, RefData> *ref =
searchViaHandle (handle, cell.creatures))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Door, RefData> *ref = searchViaHandle (handle, cell.doors))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Ingredient, RefData> *ref =
searchViaHandle (handle, cell.ingreds))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Light, RefData> *ref = searchViaHandle (handle, cell.lights))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Tool, RefData> *ref = searchViaHandle (handle, cell.lockpicks))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Misc, RefData> *ref = searchViaHandle (handle, cell.miscItems))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::NPC, RefData> *ref = searchViaHandle (handle, cell.npcs))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Probe, RefData> *ref = searchViaHandle (handle, cell.probes))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Repair, RefData> *ref = searchViaHandle (handle, cell.repairs))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Static, RefData> *ref = searchViaHandle (handle, cell.statics))
return Ptr (ref, &cell);
if (ESMS::LiveCellRef<ESM::Weapon, RefData> *ref = searchViaHandle (handle, cell.weapons))
return Ptr (ref, &cell);
return Ptr();
}
MWRender::CellRender *World::searchRender (Ptr::CellStore *store)
2010-07-09 14:07:03 +00:00
{
CellRenderCollection::iterator iter = mActiveCells.find (store);
2010-07-09 14:07:03 +00:00
if (iter!=mActiveCells.end())
{
return iter->second;
}
else
{
iter = mBufferedCells.find (store);
if (iter!=mBufferedCells.end())
return iter->second;
}
2010-07-09 14:07:03 +00:00
return 0;
}
int World::getDaysPerMonth (int month) const
{
switch (month)
{
case 0: return 31;
case 1: return 28;
case 2: return 31;
case 3: return 30;
case 4: return 31;
case 5: return 30;
case 6: return 31;
case 7: return 31;
case 8: return 30;
case 9: return 31;
case 10: return 30;
case 11: return 31;
}
throw std::runtime_error ("month out of range");
}
2010-08-21 09:43:07 +00:00
void World::removeScripts (Ptr::CellStore *cell)
{
ScriptList::iterator iter = mLocalScripts.begin();
while (iter!=mLocalScripts.end())
{
if (iter->second.getCell()==cell)
mLocalScripts.erase (iter++);
else
++iter;
}
}
void World::unloadCell (CellRenderCollection::iterator iter)
{
removeScripts (iter->first);
mEnvironment.mMechanicsManager->dropActors (iter->first);
iter->second->destroy();
mEnvironment.mSoundManager->stopSound (iter->first);
delete iter->second;
mActiveCells.erase (iter);
}
2010-08-22 18:55:22 +00:00
void World::loadCell (Ptr::CellStore *cell, MWRender::CellRender *render)
{
// register local scripts
insertInteriorScripts (*cell);
// This connects the cell data with the rendering scene.
std::pair<CellRenderCollection::iterator, bool> result =
mActiveCells.insert (std::make_pair (cell, render));
if (result.second)
{
// Load the cell and insert it into the renderer
result.first->second->show();
}
}
void World::playerCellChange (Ptr::CellStore *cell, const ESM::Position& position)
{
mPlayerPos->setPos (position.pos[0], position.pos[1], position.pos[2], true);
mPlayerPos->setCell (cell);
// TODO orientation
mEnvironment.mMechanicsManager->addActor (mPlayerPos->getPlayer());
mEnvironment.mMechanicsManager->watchActor (mPlayerPos->getPlayer());
}
void World::adjustSky()
{
if (mSky)
{
toggleSky();
// TODO set weather
toggleSky();
}
}
World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir,
const std::string& master, bool newGame, Environment& environment)
: mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0),
mSky (false), mCellChanged (false), mEnvironment (environment)
{
boost::filesystem::path masterPath (dataDir);
masterPath /= master;
std::cout << "Loading ESM " << masterPath.string() << "\n";
// This parses the ESM file and loads a sample cell
mEsm.open (masterPath.file_string());
mStore.load (mEsm);
mPlayerPos = new MWRender::PlayerPos (mScene.getCamera(), mStore.npcs.find ("player"), *this);
// global variables
mGlobalVariables = new Globals (mStore);
if (newGame)
{
// set new game mark
mGlobalVariables->setInt ("chargenstate", 1);
}
mSkyManager =
MWRender::SkyManager::create(renderer.getWindow(), mScene.getCamera());
}
World::~World()
{
for (CellRenderCollection::iterator iter (mActiveCells.begin());
iter!=mActiveCells.end(); ++iter)
delete iter->second;
for (CellRenderCollection::iterator iter (mBufferedCells.begin());
iter!=mBufferedCells.end(); ++iter)
delete iter->second;
delete mPlayerPos;
delete mSkyManager;
delete mGlobalVariables;
}
MWRender::PlayerPos& World::getPlayerPos()
{
return *mPlayerPos;
}
ESMS::ESMStore& World::getStore()
{
return mStore;
}
const World::ScriptList& World::getLocalScripts() const
{
return mLocalScripts;
}
2010-07-03 10:12:13 +00:00
bool World::hasCellChanged() const
{
2010-07-22 10:29:23 +00:00
return mCellChanged;
2010-07-03 10:12:13 +00:00
}
Globals::Data& World::getGlobalVariable (const std::string& name)
{
return (*mGlobalVariables)[name];
}
char World::getGlobalVariableType (const std::string& name) const
{
return mGlobalVariables->getType (name);
}
Ptr World::getPtr (const std::string& name, bool activeOnly)
{
// the player is always in an active cell.
if (name=="player")
{
return mPlayerPos->getPlayer();
}
// active cells
for (CellRenderCollection::iterator iter (mActiveCells.begin());
iter!=mActiveCells.end(); ++iter)
{
Ptr ptr = getPtr (name, *iter->first);
if (!ptr.isEmpty())
return ptr;
}
if (!activeOnly)
{
// TODO: inactive cells
}
throw std::runtime_error ("unknown ID: " + name);
}
Ptr World::getPtrViaHandle (const std::string& handle)
{
// TODO player
for (CellRenderCollection::iterator iter (mActiveCells.begin());
iter!=mActiveCells.end(); ++iter)
{
Ptr ptr = getPtrViaHandle (handle, *iter->first);
if (!ptr.isEmpty())
return ptr;
}
throw std::runtime_error ("unknown Ogre handle: " + handle);
}
void World::enable (Ptr reference)
2010-07-09 14:07:03 +00:00
{
if (!reference.getRefData().isEnabled())
2010-07-09 14:07:03 +00:00
{
reference.getRefData().enable();
if (MWRender::CellRender *render = searchRender (reference.getCell()))
2010-07-09 14:07:03 +00:00
{
render->enable (reference.getRefData().getHandle());
if (mActiveCells.find (reference.getCell())!=mActiveCells.end())
Class::get (reference).enable (reference, mEnvironment);
2010-07-09 14:07:03 +00:00
}
}
}
void World::disable (Ptr reference)
2010-07-09 14:07:03 +00:00
{
2010-08-07 18:29:10 +00:00
if (reference.getRefData().isEnabled())
2010-07-09 14:07:03 +00:00
{
2010-08-07 18:29:10 +00:00
reference.getRefData().disable();
if (MWRender::CellRender *render = searchRender (reference.getCell()))
2010-07-09 14:07:03 +00:00
{
render->disable (reference.getRefData().getHandle());
if (mActiveCells.find (reference.getCell())!=mActiveCells.end())
2010-08-14 09:39:32 +00:00
{
Class::get (reference).disable (reference, mEnvironment);
2010-08-14 09:39:32 +00:00
mEnvironment.mSoundManager->stopSound3D (reference);
}
2010-07-09 14:07:03 +00:00
}
}
2010-07-09 14:07:03 +00:00
}
2010-07-18 16:29:16 +00:00
void World::advanceTime (double hours)
{
hours += mGlobalVariables->getFloat ("gamehour");
2010-07-18 16:29:16 +00:00
setHour (hours);
int days = hours / 24;
if (days>0)
mGlobalVariables->setInt ("dayspassed", days + mGlobalVariables->getInt ("dayspassed"));
2010-07-18 16:29:16 +00:00
}
2010-07-18 16:29:16 +00:00
void World::setHour (double hour)
{
if (hour<0)
hour = 0;
2010-07-18 16:29:16 +00:00
int days = hour / 24;
2010-07-18 16:29:16 +00:00
hour = std::fmod (hour, 24);
2010-07-18 16:29:16 +00:00
mGlobalVariables->setFloat ("gamehour", hour);
mSkyManager->setHour (hour);
2010-07-18 16:29:16 +00:00
if (days>0)
setDay (days + mGlobalVariables->getInt ("day"));
2010-07-18 16:29:16 +00:00
}
2010-07-18 16:29:16 +00:00
void World::setDay (int day)
{
if (day<0)
day = 0;
int month = mGlobalVariables->getInt ("month");
while (true)
2010-07-18 16:29:16 +00:00
{
int days = getDaysPerMonth (month);
if (day<days)
break;
if (month<11)
{
++month;
}
else
{
month = 0;
mGlobalVariables->setInt ("year", mGlobalVariables->getInt ("year")+1);
}
day -= days;
}
mGlobalVariables->setInt ("day", day);
mGlobalVariables->setInt ("month", month);
mSkyManager->setDate (day, month);
}
void World::setMonth (int month)
{
if (month<0)
month = 0;
int years = month / 12;
month = month % 12;
int days = getDaysPerMonth (month);
if (mGlobalVariables->getInt ("day")>=days)
mGlobalVariables->setInt ("day", days-1);
mGlobalVariables->setInt ("month", month);
if (years>0)
mGlobalVariables->setInt ("year", years+mGlobalVariables->getInt ("year"));
mSkyManager->setDate (mGlobalVariables->getInt ("day"), month);
}
void World::toggleSky()
{
if (mSky)
{
mSky = false;
mSkyManager->disable();
}
else
{
mSky = true;
// TODO check for extorior or interior with sky.
mSkyManager->setHour (mGlobalVariables->getFloat ("gamehour"));
mSkyManager->setDate (mGlobalVariables->getInt ("day"),
mGlobalVariables->getInt ("month"));
mSkyManager->enable();
}
}
int World::getMasserPhase() const
{
return mSkyManager->getMasserPhase();
}
int World::getSecundaPhase() const
{
return mSkyManager->getSecundaPhase();
}
void World::setMoonColour (bool red)
{
mSkyManager->setMoonColour (red);
}
float World::getTimeScaleFactor() const
{
return mGlobalVariables->getInt ("timescale");
}
2010-07-22 10:29:23 +00:00
void World::changeCell (const std::string& cellName, const ESM::Position& position)
{
// remove active
CellRenderCollection::iterator active = mActiveCells.begin();
2010-07-22 10:29:23 +00:00
if (active!=mActiveCells.end())
{
2010-08-21 09:43:07 +00:00
unloadCell (active);
2010-07-22 10:29:23 +00:00
}
2010-08-22 18:55:22 +00:00
// Load cell.
mInteriors[cellName].loadInt (cellName, mStore, mEsm);
Ptr::CellStore *cell = &mInteriors[cellName];
2010-07-22 10:29:23 +00:00
2010-08-22 18:55:22 +00:00
loadCell (cell, new MWRender::InteriorCellRender (*cell, mEnvironment, mScene));
2010-08-22 18:55:22 +00:00
// adjust player
playerCellChange (cell, position);
// Sky system
2010-08-22 18:55:22 +00:00
adjustSky();
2010-07-22 10:29:23 +00:00
mCellChanged = true;
}
void World::changeCell (int X, int Y, const ESM::Position& position)
{
// remove active
CellRenderCollection::iterator active = mActiveCells.begin();
if (active!=mActiveCells.end())
{
2010-08-21 09:43:07 +00:00
unloadCell (active);
}
2010-08-22 18:55:22 +00:00
// Load cell.
mExteriors[std::make_pair (X, Y)].loadExt (X, Y, mStore, mEsm);
Ptr::CellStore *cell = &mExteriors[std::make_pair (X, Y)];
2010-08-22 18:55:22 +00:00
loadCell (cell, new MWRender::ExteriorCellRender (*cell, mEnvironment, mScene));
2010-08-22 18:55:22 +00:00
// adjust player
playerCellChange (cell, position);
// Sky system
2010-08-22 18:55:22 +00:00
adjustSky();
mCellChanged = true;
}
void World::changeToExteriorCell (const ESM::Position& position)
{
int x = 0;
int y = 0;
positionToIndex (position.pos[0], position.pos[1], x, y);
changeCell (x, y, position);
}
2010-07-22 10:29:23 +00:00
void World::markCellAsUnchanged()
{
mCellChanged = false;
2010-07-22 10:29:23 +00:00
}
std::string World::getFacedHandle()
{
std::pair<std::string, float> result = mScene.getFacedHandle (*this);
if (result.first.empty() ||
result.second>getStore().gameSettings.find ("iMaxActivateDist")->i)
return "";
return result.first;
}
2010-08-07 18:25:17 +00:00
void World::deleteObject (Ptr ptr)
{
if (ptr.getRefData().getCount()>0)
{
ptr.getRefData().setCount (0);
if (MWRender::CellRender *render = searchRender (ptr.getCell()))
{
render->deleteObject (ptr.getRefData().getHandle());
ptr.getRefData().setHandle ("");
if (mActiveCells.find (ptr.getCell())!=mActiveCells.end())
2010-08-14 09:39:32 +00:00
{
Class::get (ptr).disable (ptr, mEnvironment);
2010-08-14 09:39:32 +00:00
mEnvironment.mSoundManager->stopSound3D (ptr);
}
2010-08-07 18:25:17 +00:00
}
}
}
void World::moveObject (Ptr ptr, float x, float y, float z)
{
ptr.getCellRef().pos.pos[0] = x;
ptr.getCellRef().pos.pos[1] = y;
ptr.getCellRef().pos.pos[2] = z;
if (ptr==mPlayerPos->getPlayer())
{
CellRenderCollection::iterator active = mActiveCells.begin();
if (active!=mActiveCells.end())
{
if (!(active->first->cell->data.flags & ESM::Cell::Interior))
{
// exterior -> adjust loaded cells
int cellX = 0;
int cellY = 0;
positionToIndex (x, y, cellX, cellY);
if (active->first->cell->data.gridX!=cellX || active->first->cell->data.gridY!=cellY)
{
changeCell (cellX, cellY, mPlayerPos->getPlayer().getCellRef().pos);
}
}
}
}
// TODO cell change for non-player ref
}
void World::indexToPosition (int cellX, int cellY, float &x, float &y) const
{
const int cellSize = 8192;
x = cellSize * cellX;
y = cellSize * cellY;
}
void World::positionToIndex (float x, float y, int &cellX, int &cellY) const
{
const int cellSize = 8192;
cellX = static_cast<int> (x/cellSize);
if (x<0)
--cellX;
cellY = static_cast<int> (y/cellSize);
if (y<0)
--cellY;
}
}