by Major Cooke » Sat Dec 13, 2014 6:50 pm
I'm not sure what to do about resolving with the array though. Any ideas?
Here's my code so far. Again, this is... very alien territory for me so it shouldn't be a surprise if I completely screwed the code up entirely.
Currently, it doesn't work. Access violation occurs in the Resolve call.
Spoiler:
thingdef_exp.h
Code: Select all
class FxPick : public FxExpression
{
protected:
FRandom * rng;
TArray<FxExpression*> min;
public:
FxPick(FRandom *, TArray<FxExpression*> mi, const FScriptPosition &pos);
~FxPick();
FxExpression *Resolve(FCompileContext&);
ExpVal EvalExpression(AActor *self);
};
thingdef_expression.cpp
Code: Select all
//==========================================================================
//
//
//
//==========================================================================
FxPick::FxPick(FRandom * r, TArray<FxExpression*> mi, const FScriptPosition &pos)
: FxExpression(pos)
{
int index = 0;
int max = mi.Max();
if (max > 0)
{
for (index = 0; index < max; index++)
min.Push(new FxIntCast(mi[index]));
}
else min.Clear();
rng = r;
ValueType = VAL_Int;
}
//==========================================================================
//
//
//
//==========================================================================
FxPick::~FxPick()
{
min.Clear();
}
//==========================================================================
//
//
//
//==========================================================================
FxExpression *FxPick::Resolve(FCompileContext &ctx)
{
int index = 0;
CHECKRESOLVED();
int max = min.Max();
if (max > 0)
{
for (index = 0; index < max; index++)
{
RESOLVE(min[index], ctx); //<--Access violation occurs here, not sure how to change it.
ABORT(min[index]);
}
}
return this;
};
//==========================================================================
//
//
//
//==========================================================================
ExpVal FxPick::EvalExpression(AActor *self)
{
ExpVal val;
val.Type = VAL_Int;
int max = min.Max();
if (max > 0)
{
int select = (*rng)(max);
val.Int = min[select]->EvalExpression(self).GetInt();
}
else
{
val.Int = (*rng)();
}
return val;
}
thingdef_exp.cpp
Code: Select all
else if (sc.CheckToken(TK_Pick))
{
FRandom *rng;
TArray<FxExpression*> list;
list.Clear();
int index = 0;
if (sc.CheckToken('['))
{
sc.MustGetToken(TK_Identifier);
rng = FRandom::StaticFindRNG(sc.String);
sc.MustGetToken(']');
}
else
{
rng = &pr_exrandom;
}
sc.MustGetToken('(');
while (!(sc.CheckToken(')')))
{
FxExpression *min = ParseExpressionM(sc, cls);
list.Push(min);
if (sc.CheckToken(')'))
break;
else
sc.MustGetToken(',');
}
return new FxPick(rng, list, sc);
}
I'm not sure what to do about resolving with the array though. Any ideas?
Here's my code so far. Again, this is... very alien territory for me so it shouldn't be a surprise if I completely screwed the code up entirely. :?
Currently, it doesn't work. Access violation occurs in the Resolve call.
[spoiler]thingdef_exp.h
[code]class FxPick : public FxExpression
{
protected:
FRandom * rng;
TArray<FxExpression*> min;
public:
FxPick(FRandom *, TArray<FxExpression*> mi, const FScriptPosition &pos);
~FxPick();
FxExpression *Resolve(FCompileContext&);
ExpVal EvalExpression(AActor *self);
};[/code]
thingdef_expression.cpp
[code]//==========================================================================
//
//
//
//==========================================================================
FxPick::FxPick(FRandom * r, TArray<FxExpression*> mi, const FScriptPosition &pos)
: FxExpression(pos)
{
int index = 0;
int max = mi.Max();
if (max > 0)
{
for (index = 0; index < max; index++)
min.Push(new FxIntCast(mi[index]));
}
else min.Clear();
rng = r;
ValueType = VAL_Int;
}
//==========================================================================
//
//
//
//==========================================================================
FxPick::~FxPick()
{
min.Clear();
}
//==========================================================================
//
//
//
//==========================================================================
FxExpression *FxPick::Resolve(FCompileContext &ctx)
{
int index = 0;
CHECKRESOLVED();
int max = min.Max();
if (max > 0)
{
for (index = 0; index < max; index++)
{
RESOLVE(min[index], ctx); //<--Access violation occurs here, not sure how to change it.
ABORT(min[index]);
}
}
return this;
};
//==========================================================================
//
//
//
//==========================================================================
ExpVal FxPick::EvalExpression(AActor *self)
{
ExpVal val;
val.Type = VAL_Int;
int max = min.Max();
if (max > 0)
{
int select = (*rng)(max);
val.Int = min[select]->EvalExpression(self).GetInt();
}
else
{
val.Int = (*rng)();
}
return val;
}[/code]
thingdef_exp.cpp
[code] else if (sc.CheckToken(TK_Pick))
{
FRandom *rng;
TArray<FxExpression*> list;
list.Clear();
int index = 0;
if (sc.CheckToken('['))
{
sc.MustGetToken(TK_Identifier);
rng = FRandom::StaticFindRNG(sc.String);
sc.MustGetToken(']');
}
else
{
rng = &pr_exrandom;
}
sc.MustGetToken('(');
while (!(sc.CheckToken(')')))
{
FxExpression *min = ParseExpressionM(sc, cls);
list.Push(min);
if (sc.CheckToken(')'))
break;
else
sc.MustGetToken(',');
}
return new FxPick(rng, list, sc);
}[/code][/spoiler]