#include "world.hpp" #include #include #include "components/bsa/bsa_archive.hpp" #include "apps/openmw/mwrender/sky.hpp" #include "apps/openmw/mwrender/interior.hpp" #include "ptr.hpp" namespace { template void listCellScripts (const ESMS::ESMStore& store, ESMS::CellRefList& cellRefList, MWWorld::World::ScriptList& scriptList, MWWorld::Ptr::CellStore *cell) { for (typename ESMS::CellRefList::List::iterator iter ( cellRefList.list.begin()); iter!=cellRefList.list.end(); ++iter) { if (!iter->base->script.empty()) { if (const ESM::Script *script = store.scripts.find (iter->base->script)) { iter->mData.setLocals (*script); scriptList.push_back ( std::make_pair (iter->base->script, MWWorld::Ptr (&*iter, cell))); } } } } } namespace MWWorld { void World::insertInteriorScripts (ESMS::CellStore& 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 *ref = cell.activators.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.potions.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.appas.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.armors.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.books.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.clothes.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.containers.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.creatures.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.doors.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.ingreds.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.creatureLists.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.itemLists.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.lights.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.lockpicks.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.miscItems.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.npcs.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.probes.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.repairs.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.statics.find (name)) return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.weapons.find (name)) return Ptr (ref, &cell); return Ptr(); } MWRender::CellRender *World::searchRender (Ptr::CellStore *store) { CellRenderCollection::iterator iter = mActiveCells.find (store); if (iter!=mActiveCells.end()) { return iter->second; } else { iter = mBufferedCells.find (store); if (iter!=mBufferedCells.end()) return iter->second; } return 0; } World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, const std::string& master, const std::string& startCell, bool newGame) : mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0), mSky (false) { 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); mInteriors[startCell].loadInt (startCell, mStore, mEsm); mCurrentCell = &mInteriors[startCell]; insertInteriorScripts (mInteriors[startCell]); mPlayerPos = new MWRender::PlayerPos (mScene.getCamera(), mStore.npcs.find ("player")); // global variables mGlobalVariables = new Globals (mStore); if (newGame) { // set new game mark mGlobalVariables->setInt ("chargenstate", 1); } // This connects the cell data with the rendering scene. mActiveCells.insert (std::make_pair (&mInteriors[startCell], new MWRender::InteriorCellRender (mInteriors[startCell], mScene))); // Load the cell and insert it into the renderer for (CellRenderCollection::iterator iter (mActiveCells.begin()); iter!=mActiveCells.end(); ++iter) iter->second->show(); mSkyManager = MWRender::SkyManager::create(renderer.getWindow(), mScene.getCamera()); toggleSky(); } 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; } bool World::hasCellChanged() const { // Cell change not implemented yet. return false; } 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 Ptr (mPlayerPos->getPlayer(), mCurrentCell); } // 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); } void World::enable (Ptr reference) { if (!reference.getRefData().isEnabled()) { reference.getRefData().enable(); if (MWRender::CellRender *render = searchRender (reference.getCell())) { render->enable (reference.getRefData().getHandle()); } } } void World::disable (Ptr reference) { if (!reference.getRefData().isEnabled()) { reference.getRefData().enable(); if (MWRender::CellRender *render = searchRender (reference.getCell())) { render->disable (reference.getRefData().getHandle()); } } } void World::advanceTime (double hours) { hours += mGlobalVariables->getFloat ("gamehour"); setHour (hours); } void World::setHour (double hour) { if (hour<0) hour = 0; int days = hour / 24; hour = std::fmod (hour, 24); mGlobalVariables->setFloat ("gamehour", hour); mSkyManager->setHour (hour); if (days>0) { setDay (days + mGlobalVariables->getInt ("year")); days += mGlobalVariables->getInt ("dayspassed"); mGlobalVariables->setInt ("dayspassed", days); } } void World::setDay (int day) { if (day<0) day = 0; int year = day / 365; day = day % 365; mGlobalVariables->setInt ("day", day); mSkyManager->setDay (day); if (year>0) { year += mGlobalVariables->getInt ("year"); mGlobalVariables->setInt ("year", year); } } 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->setDay (mGlobalVariables->getInt ("day")); 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"); } }