implemented getdistance with explicit references

actorid
Marc Zinnschlag 15 years ago
parent c37b007be0
commit 69e607e140

@ -110,7 +110,7 @@ namespace SAInterpreter
void Context::stopScript (const std::string& name) {}
float Context::getDistance (const std::string& name) const
float Context::getDistance (const std::string& name, const std::string& id) const
{
return 0;
}

@ -58,7 +58,7 @@ namespace SAInterpreter
virtual void stopScript (const std::string& name);
virtual float getDistance (const std::string& name) const;
virtual float getDistance (const std::string& name, const std::string& id = "") const;
virtual float getSecondsPassed() const;

@ -176,18 +176,18 @@ namespace MWScript
mEnvironment.mGlobalScripts->removeScript (name);
}
float InterpreterContext::getDistance (const std::string& name) const
float InterpreterContext::getDistance (const std::string& name, const std::string& id) const
{
if (mReference.isEmpty())
throw std::runtime_error ("no implicit reference");
// TODO handle exterior cells (when ref and ref2 are located in different cells)
CPtrWithCell ref2 = getReference (id, false);
std::pair<MWWorld::Ptr, MWWorld::World::CellStore *> ref =
mEnvironment.mWorld->getPtr (name, true);
double diff[3];
for (int i=0; i<3; ++i)
diff[i] = ref.first.getCellRef().pos.pos[i] - mReference.getCellRef().pos.pos[i];
diff[i] = ref.first.getCellRef().pos.pos[i] - ref2.first.getCellRef().pos.pos[i];
return std::sqrt (diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2]);
}

@ -70,7 +70,7 @@ namespace MWScript
virtual void stopScript (const std::string& name);
virtual float getDistance (const std::string& name) const;
virtual float getDistance (const std::string& name, const std::string& id = "") const;
virtual bool hasBeenActivated() const;

@ -311,6 +311,19 @@ namespace Compiler
mNextOperand = false;
return true;
}
else if (keyword==Scanner::K_getdistance)
{
mTokenLoc = loc;
parseArguments ("c", scanner);
Generator::getDistance (mCode, mLiterals, mExplicit);
mOperands.push_back ('f');
mExplicit.clear();
mRefOp = false;
mNextOperand = false;
return true;
}
}
return Parser::parseKeyword (keyword, loc, scanner);
@ -360,18 +373,18 @@ namespace Compiler
mNextOperand = false;
return true;
}
}
else if (keyword==Scanner::K_getdistance)
{
mTokenLoc = loc;
parseArguments ("c", scanner);
Generator::getDistance (mCode);
Generator::getDistance (mCode, mLiterals, "");
mOperands.push_back ('f');
mNextOperand = false;
return true;
}
}
else if (keyword==Scanner::K_getsecondspassed)
{
mTokenLoc = loc;

@ -314,6 +314,11 @@ namespace
{
code.push_back (Compiler::Generator::segment5 (56));
}
void opGetDistanceExplicit (Compiler::Generator::CodeContainer& code)
{
code.push_back (Compiler::Generator::segment5 (57));
}
}
namespace Compiler
@ -706,9 +711,18 @@ namespace Compiler
opStopScript (code);
}
void getDistance (CodeContainer& code)
void getDistance (CodeContainer& code, Literals& literals, const std::string id)
{
opGetDistance (code);
if (id.empty())
{
opGetDistance (code);
}
else
{
int index = literals.addString (id);
opPushInt (code, index);
opGetDistanceExplicit (code);
}
}
void getSecondsPassed (CodeContainer& code)

@ -107,7 +107,7 @@ namespace Compiler
void stopScript (CodeContainer& code);
void getDistance (CodeContainer& code);
void getDistance (CodeContainer& code, Literals& literals, const std::string id);
void getSecondsPassed (CodeContainer& code);

@ -53,7 +53,8 @@ namespace Interpreter
virtual void stopScript (const std::string& name) = 0;
virtual float getDistance (const std::string& name) const = 0;
virtual float getDistance (const std::string& name, const std::string& id = "") const
= 0;
virtual float getSecondsPassed() const = 0;

@ -115,6 +115,8 @@ op 53: push 1, if implicit reference is disabled, 0 else
op 54: explicit reference = stack[0]; pop; enable explicit reference
op 55: explicit reference = stack[0]; pop; disable explicit reference
op 56: explicit reference = stack[0]; pop; push 1, if explicit reference is disabled, 0 else
opcodes 57-33554431 unused
op 57: explicit reference = stack[0]; pop;
replace stack[0] with distance between explicit reference and a reference of ID stack[0]
opcodes 58-33554431 unused
opcodes 33554432-67108863 reserved for extensions

@ -103,6 +103,7 @@ namespace Interpreter
// spacial
interpreter.installSegment5 (49, new OpGetDistance);
interpreter.installSegment5 (57, new OpGetDistanceExplicit);
}
}

@ -19,6 +19,24 @@ namespace Interpreter
runtime[0] = *reinterpret_cast<Type_Data *> (&distance);
}
};
class OpGetDistanceExplicit : public Opcode0
{
public:
virtual void execute (Runtime& runtime)
{
int index = runtime[0];
runtime.pop();
std::string id = runtime.getStringLiteral (index);
std::string name = runtime.getStringLiteral (runtime[0]);
float distance = runtime.getContext().getDistance (name, id);
runtime[0] = *reinterpret_cast<Type_Data *> (&distance);
}
};
}
#endif

Loading…
Cancel
Save