1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-10 15:39:02 +00:00
OpenMW/apps/openmw/mwscript/cellextensions.cpp

208 lines
7.7 KiB
C++
Raw Normal View History

2010-07-03 10:12:13 +00:00
#include "cellextensions.hpp"
#include <components/esm_store/store.hpp>
2010-07-03 10:12:13 +00:00
#include <components/compiler/extensions.hpp>
#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;
pos.rot[0] = pos.rot[1] = pos.rot[2] = 0;
2010-09-11 09:55:28 +00:00
pos.pos[2] = 0;
if (const ESM::Cell *exterior = MWBase::Environment::get().getWorld()->getExterior (cell))
2010-09-11 09:55:28 +00:00
{
MWBase::Environment::get().getWorld()->indexToPosition (exterior->data.gridX, exterior->data.gridY,
pos.pos[0], pos.pos[1], true);
MWBase::Environment::get().getWorld()->changeToExteriorCell (pos);
2010-09-11 09:55:28 +00:00
}
else
{
pos.pos[0] = pos.pos[1] = 0;
MWBase::Environment::get().getWorld()->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::Environment::get().getWorld()->indexToPosition (x, y, pos.pos[0], pos.pos[1], true);
pos.pos[2] = 0;
pos.rot[0] = pos.rot[1] = pos.rot[2] = 0;
MWBase::Environment::get().getWorld()->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()->getPlayer().getPlayer().getCell()->cell->data.flags &
2011-01-17 09:18:12 +00:00
ESM::Cell::Interior;
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()->getPlayer().getPlayer().getCell()->cell;
2011-04-04 13:10:37 +00:00
std::string current = cell->name;
if (!(cell->data.flags & ESM::Cell::Interior) && current.empty())
{
const ESM::Region *region =
MWBase::Environment::get().getWorld()->getStore().regions.find (cell->region);
2011-04-04 13:10:37 +00:00
current = region->name;
}
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::Ptr::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell();
runtime.push (cell->mWaterLevel);
}
};
class OpSetWaterLevel : public Interpreter::Opcode0
{
public:
virtual void execute (Interpreter::Runtime& runtime)
{
Interpreter::Type_Float level = runtime[0].mFloat;
MWWorld::Ptr::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell();
if (!(cell->cell->data.flags & ESM::Cell::Interior))
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::Ptr::CellStore *cell = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell();
if (!(cell->cell->data.flags & ESM::Cell::Interior))
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
const int opcodeCellChanged = 0x2000000;
2010-07-22 10:29:23 +00:00
const int opcodeCOC = 0x2000026;
2010-09-10 11:01:37 +00:00
const int opcodeCOE = 0x200008e;
2011-01-17 09:18:12 +00:00
const int opcodeGetInterior = 0x2000131;
2011-04-04 13:10:37 +00:00
const int opcodeGetPCCell = 0x2000136;
const int opcodeGetWaterLevel = 0x2000141;
const int opcodeSetWaterLevel = 0x2000142;
const int opcodeModWaterLevel = 0x2000143;
2010-07-03 10:12:13 +00:00
void registerExtensions (Compiler::Extensions& extensions)
{
extensions.registerFunction ("cellchanged", 'l', "", opcodeCellChanged);
2010-07-22 10:29:23 +00:00
extensions.registerInstruction ("coc", "S", opcodeCOC);
extensions.registerInstruction ("centeroncell", "S", opcodeCOC);
extensions.registerInstruction ("coe", "ll", opcodeCOE);
extensions.registerInstruction ("centeronexterior", "ll", opcodeCOE);
extensions.registerInstruction ("setwaterlevel", "f", opcodeSetWaterLevel);
extensions.registerInstruction ("modwaterlevel", "f", opcodeModWaterLevel);
2011-01-17 09:18:12 +00:00
extensions.registerFunction ("getinterior", 'l', "", opcodeGetInterior);
2011-04-04 13:10:37 +00:00
extensions.registerFunction ("getpccell", 'l', "c", opcodeGetPCCell);
extensions.registerFunction ("getwaterlevel", 'f', "", opcodeGetWaterLevel);
2010-07-03 10:12:13 +00:00
}
2010-07-03 10:12:13 +00:00
void installOpcodes (Interpreter::Interpreter& interpreter)
{
interpreter.installSegment5 (opcodeCellChanged, new OpCellChanged);
2010-07-22 10:29:23 +00:00
interpreter.installSegment5 (opcodeCOC, new OpCOC);
interpreter.installSegment5 (opcodeCOE, new OpCOE);
2011-01-17 09:18:12 +00:00
interpreter.installSegment5 (opcodeGetInterior, new OpGetInterior);
2011-04-04 13:10:37 +00:00
interpreter.installSegment5 (opcodeGetPCCell, new OpGetPCCell);
interpreter.installSegment5 (opcodeGetWaterLevel, new OpGetWaterLevel);
interpreter.installSegment5 (opcodeSetWaterLevel, new OpSetWaterLevel);
interpreter.installSegment5 (opcodeModWaterLevel, new OpModWaterLevel);
2010-07-03 10:12:13 +00:00
}
}
}