Remove function cache from C4DefScriptHost

rope
Günther Brammer 2011-10-15 01:59:15 +02:00
parent bb3861d4f1
commit a01bb2cf9c
6 changed files with 26 additions and 41 deletions

View File

@ -1561,12 +1561,7 @@ void C4Command::Transfer()
// Call target transfer script
if (!::Game.iTick5)
{
C4AulScriptFunc *f;
bool fHandled = (f = Target->Def->Script.SFn_ControlTransfer) != NULL;
if (fHandled) fHandled = f->Exec(Target,&C4AulParSet(C4VObj(cObj), Tx, C4VInt(Ty))).getBool();
if (!fHandled)
if (!Target->Call(PSF_ControlTransfer, &C4AulParSet(C4VObj(cObj), Tx, C4VInt(Ty))).getBool())
// Transfer not handled by target: done
{ Finish(true); return; }
}

View File

@ -562,21 +562,16 @@ void C4Def::Draw(C4Facet &cgo, bool fSelected, DWORD iColor, C4Object *pObj, int
int32_t C4Def::GetValue(C4Object *pInBase, int32_t iBuyPlayer)
{
// CalcDefValue defined?
C4AulFunc *pCalcValueFn = Script.GetSFunc(PSF_CalcDefValue, AA_PROTECTED);
int32_t iValue;
if (pCalcValueFn)
// then call it!
iValue = pCalcValueFn->Exec(NULL, &C4AulParSet(C4VObj(pInBase), C4VInt(iBuyPlayer))).getInt();
else
// otherwise, use default value
iValue = Value;
C4Value r = Call(PSF_CalcDefValue, &C4AulParSet(C4VObj(pInBase), C4VInt(iBuyPlayer)));
int32_t iValue = Value;
if (r != C4VNull)
iValue = r.getInt();
// do any adjustments based on where the item is bought
if (pInBase)
{
C4AulFunc *pFn;
if ((pFn = pInBase->Def->Script.GetSFunc(PSF_CalcBuyValue, AA_PROTECTED)))
iValue = pFn->Exec(pInBase, &C4AulParSet(C4VPropList(this), C4VInt(iValue))).getInt();
r = pInBase->Call(PSF_CalcBuyValue, &C4AulParSet(C4VPropList(this), C4VInt(iValue)));
if (r != C4VNull)
iValue = r.getInt();
}
return iValue;
}

View File

@ -1808,28 +1808,26 @@ void C4Object::SetName(const char * NewName)
int32_t C4Object::GetValue(C4Object *pInBase, int32_t iForPlayer)
{
C4Value r = Call(PSF_CalcValue, &C4AulParSet(C4VObj(pInBase), C4VInt(iForPlayer)));
int32_t iValue;
// value by script?
if (C4AulScriptFunc *f = Def->Script.SFn_CalcValue)
iValue = f->Exec(this, &C4AulParSet(C4VObj(pInBase), C4VInt(iForPlayer))).getInt();
if (r != C4VNull)
iValue = r.getInt();
else
{
// get value of def
// Caution: Do not pass pInBase here, because the def base value is to be queried
// - and not the value if you had to buy the object in this particular base
iValue=Def->GetValue(NULL, iForPlayer);
iValue = Def->GetValue(NULL, iForPlayer);
}
// Con percentage
iValue=iValue*Con/FullCon;
// value adjustments buy base function
iValue = iValue * Con / FullCon;
// do any adjustments based on where the item is bought
if (pInBase)
{
C4AulFunc *pFn;
if ((pFn = pInBase->Def->Script.GetSFunc(PSF_CalcSellValue, AA_PROTECTED)))
iValue = pFn->Exec(pInBase, &C4AulParSet(C4VObj(this), C4VInt(iValue))).getInt();
r = pInBase->Call(PSF_CalcSellValue, &C4AulParSet(C4VObj(this), C4VInt(iValue)));
if (r != C4VNull)
iValue = r.getInt();
}
// Return value
return iValue;
}

View File

@ -117,7 +117,6 @@ bool C4ValueToMatrix(const C4ValueArray& array, StdMeshMatrix* matrix);
#define PSF_UpdateTransferZone "~UpdateTransferZone"
#define PSF_CalcValue "~CalcValue" // C4Object *pInBase, int iForPlayer
#define PSF_CalcDefValue "~CalcDefValue" // C4Object *pInBase, int iForPlayer
#define PSF_SellTo "~SellTo" // int iByPlr
#define PSF_InputCallback "InputCallback" // const char *szText
#define PSF_MenuQueryCancel "~MenuQueryCancel" // int iSelection
#define PSF_IsFulfilled "~IsFulfilled" // int for_plr

View File

@ -160,13 +160,15 @@ bool C4DefScriptHost::Load(C4Group & g, const char * f, const char * l, C4LangSt
return r;
}
void C4DefScriptHost::Clear()
{
if (Def) Def->TimerCall = 0;
C4ScriptHost::Clear();
}
void C4DefScriptHost::AfterLink()
{
C4AulScript::AfterLink();
// Search cached functions
SFn_CalcValue = GetSFunc(PSF_CalcValue , AA_PROTECTED);
SFn_SellTo = GetSFunc(PSF_SellTo , AA_PROTECTED);
SFn_ControlTransfer = GetSFunc(PSF_ControlTransfer, AA_PROTECTED);
C4ScriptHost::AfterLink();
if (Def && Def->STimerCall[0])
{
Def->TimerCall = Def->GetFunc(Def->STimerCall);

View File

@ -70,9 +70,9 @@ protected:
class C4DefScriptHost : public C4ScriptHost
{
public:
C4DefScriptHost(C4Def * Def) : C4ScriptHost(), Def(Def) { SFn_CalcValue = SFn_SellTo = SFn_ControlTransfer = NULL; }
C4DefScriptHost(C4Def * Def) : C4ScriptHost(), Def(Def) { }
C4Value Call(const char *szFunction, C4Object *pObj=0, C4AulParSet *pPars=0, bool fPrivateCall=false, bool fPassError=false);
void Clear() { SFn_CalcValue = SFn_SellTo = SFn_ControlTransfer = NULL; C4ScriptHost::Clear(); }
void Clear();
bool Delete() { return false; } // do NOT delete this - it's just a class member!
virtual bool Load(C4Group &, const char *, const char *, C4LangStringTable *);
@ -80,10 +80,6 @@ public:
protected:
C4Def *Def; // owning def file
void AfterLink(); // get common funcs
public:
C4AulScriptFunc *SFn_CalcValue; // get object value
C4AulScriptFunc *SFn_SellTo; // player par(0) sold the object
C4AulScriptFunc *SFn_ControlTransfer; // object par(0) tries to get to par(1)/par(2)
};