1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 09:19:58 +00:00
openmw-tes3mp/apps/openmw/mwscript/cellextensions.cpp

191 lines
6.8 KiB
C++
Raw Normal View History

2010-07-03 10:12:13 +00:00
#include "cellextensions.hpp"
2012-10-01 15:17:04 +00:00
#include "../mwworld/esmstore.hpp"
2010-07-03 10:12:13 +00:00
#include <components/compiler/extensions.hpp>
#include <components/compiler/opcodes.hpp>
2010-07-03 10:12:13 +00:00
#include <components/interpreter/interpreter.hpp>
#include <components/interpreter/runtime.hpp>
#include <components/interpreter/opcodes.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2011-01-17 09:18:12 +00:00
#include "../mwworld/player.hpp"
2010-07-03 10:12:13 +00:00
#include "interpretercontext.hpp"
namespace MWScript
{
namespace Cell
{
class OpCellChanged : public Interpreter::Opcode0
{
public:
2010-07-03 10:12:13 +00:00
virtual void execute (Interpreter::Runtime& runtime)
{
runtime.push (MWBase::Environment::get().getWorld()->hasCellChanged() ? 1 : 0);
}
2010-07-03 10:12:13 +00:00
};
2010-07-22 10:29:23 +00:00
class OpCOC : public Interpreter::Opcode0
{
public:
2010-07-22 10:29:23 +00:00
virtual void execute (Interpreter::Runtime& runtime)
{
std::string cell = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
2010-07-22 10:29:23 +00:00
ESM::Position pos;
MWBase::World *world = MWBase::Environment::get().getWorld();
2010-09-11 09:55:28 +00:00
world->getPlayer().setTeleported(true);
if (world->findExteriorPosition(cell, pos))
{
world->changeToExteriorCell(pos);
2010-09-11 09:55:28 +00:00
}
else
{
// Change to interior even if findInteriorPosition()
// yields false. In this case position will be zero-point.
world->findInteriorPosition(cell, pos);
world->changeToInteriorCell(cell, pos);
2010-09-11 09:55:28 +00:00
}
}
};
class OpCOE : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
Interpreter::Type_Integer x = runtime[0].mInteger;
runtime.pop();
Interpreter::Type_Integer y = runtime[0].mInteger;
runtime.pop();
ESM::Position pos;
MWBase::World *world = MWBase::Environment::get().getWorld();
world->getPlayer().setTeleported(true);
world->indexToPosition (x, y, pos.pos[0], pos.pos[1], true);
pos.pos[2] = 0;
pos.rot[0] = pos.rot[1] = pos.rot[2] = 0;
world->changeToExteriorCell (pos);
}
2010-07-22 10:29:23 +00:00
};
2011-01-17 09:18:12 +00:00
class OpGetInterior : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
bool interior =
!MWBase::Environment::get().getWorld()->getPlayerPtr().getCell()->getCell()->isExterior();
2011-01-17 09:18:12 +00:00
runtime.push (interior ? 1 : 0);
}
};
2011-04-04 13:10:37 +00:00
class OpGetPCCell : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
std::string name = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop();
const ESM::Cell *cell = MWBase::Environment::get().getWorld()->getPlayerPtr().getCell()->getCell();
2011-04-04 13:10:37 +00:00
2012-09-17 07:37:50 +00:00
std::string current = cell->mName;
2011-04-04 13:10:37 +00:00
if (!(cell->mData.mFlags & ESM::Cell::Interior) && current.empty()
&& !cell->mRegion.empty())
2011-04-04 13:10:37 +00:00
{
const ESM::Region *region =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Region>().find (cell->mRegion);
2011-04-04 13:10:37 +00:00
2012-09-17 07:37:50 +00:00
current = region->mName;
2011-04-04 13:10:37 +00:00
}
2014-01-22 13:45:36 +00:00
Misc::StringUtils::toLower(current);
2011-04-04 13:10:37 +00:00
bool match = current.length()>=name.length() &&
current.substr (0, name.length())==name;
runtime.push (match ? 1 : 0);
}
};
class OpGetWaterLevel : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayerPtr().getCell();
if (cell->getCell()->hasWater())
runtime.push (cell->mWaterLevel);
else
runtime.push (-std::numeric_limits<float>().max());
}
};
class OpSetWaterLevel : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
Interpreter::Type_Float level = runtime[0].mFloat;
MWWorld::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayerPtr().getCell();
if (cell->getCell()->isExterior())
throw std::runtime_error("Can't set water level in exterior cell");
cell->mWaterLevel = level;
MWBase::Environment::get().getWorld()->setWaterHeight(cell->mWaterLevel);
}
};
class OpModWaterLevel : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
Interpreter::Type_Float level = runtime[0].mFloat;
MWWorld::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayerPtr().getCell();
if (cell->getCell()->isExterior())
throw std::runtime_error("Can't set water level in exterior cell");
cell->mWaterLevel +=level;
MWBase::Environment::get().getWorld()->setWaterHeight(cell->mWaterLevel);
}
};
2010-07-03 10:12:13 +00:00
void installOpcodes (Interpreter::Interpreter& interpreter)
{
interpreter.installSegment5 (Compiler::Cell::opcodeCellChanged, new OpCellChanged);
interpreter.installSegment5 (Compiler::Cell::opcodeCOC, new OpCOC);
interpreter.installSegment5 (Compiler::Cell::opcodeCOE, new OpCOE);
interpreter.installSegment5 (Compiler::Cell::opcodeGetInterior, new OpGetInterior);
interpreter.installSegment5 (Compiler::Cell::opcodeGetPCCell, new OpGetPCCell);
interpreter.installSegment5 (Compiler::Cell::opcodeGetWaterLevel, new OpGetWaterLevel);
interpreter.installSegment5 (Compiler::Cell::opcodeSetWaterLevel, new OpSetWaterLevel);
interpreter.installSegment5 (Compiler::Cell::opcodeModWaterLevel, new OpModWaterLevel);
2010-07-03 10:12:13 +00:00
}
}
}