- Pick can now hold unlimited numbers to choose from.

- I.e. pick(1,4,12,16)
This commit is contained in:
MajorCooke 2014-12-14 04:45:39 -06:00
parent 785f72d6eb
commit 3f3aab42f1
3 changed files with 36 additions and 33 deletions

View file

@ -374,6 +374,9 @@ static FxExpression *ParseExpression0 (FScanner &sc, const PClass *cls)
else if (sc.CheckToken(TK_Pick)) else if (sc.CheckToken(TK_Pick))
{ {
FRandom *rng; FRandom *rng;
TArray<FxExpression*> list;
list.Clear();
int index = 0;
if (sc.CheckToken('[')) if (sc.CheckToken('['))
{ {
@ -387,12 +390,17 @@ static FxExpression *ParseExpression0 (FScanner &sc, const PClass *cls)
} }
sc.MustGetToken('('); sc.MustGetToken('(');
while (!(sc.CheckToken(')')))
{
FxExpression *min = ParseExpressionM(sc, cls); FxExpression *min = ParseExpressionM(sc, cls);
list.Push(min);
if (sc.CheckToken(')'))
break;
else
sc.MustGetToken(','); sc.MustGetToken(',');
FxExpression *max = ParseExpressionM(sc, cls); }
sc.MustGetToken(')');
return new FxPick(rng, min, max, sc); return new FxPick(rng, list, sc);
} }
else if (sc.CheckToken(TK_FRandom)) else if (sc.CheckToken(TK_FRandom))
{ {

View file

@ -563,11 +563,11 @@ class FxPick : public FxExpression
{ {
protected: protected:
FRandom * rng; FRandom * rng;
FxExpression *min, *max; TArray<FxExpression*> min;
public: public:
FxPick(FRandom *, FxExpression *mi, FxExpression *ma, const FScriptPosition &pos); FxPick(FRandom *, TArray<FxExpression*> mi, const FScriptPosition &pos);
~FxPick(); ~FxPick();
FxExpression *Resolve(FCompileContext&); FxExpression *Resolve(FCompileContext&);

View file

@ -1696,15 +1696,17 @@ ExpVal FxRandom::EvalExpression (AActor *self)
// //
// //
//========================================================================== //==========================================================================
FxPick::FxPick(FRandom * r, FxExpression *mi, FxExpression *ma, const FScriptPosition &pos) FxPick::FxPick(FRandom * r, TArray<FxExpression*> mi, const FScriptPosition &pos)
: FxExpression(pos) : FxExpression(pos)
{ {
if (mi != NULL && ma != NULL) int index = 0;
int max = mi.Size();
if (max > 0)
{ {
min = new FxIntCast(mi); for (index = 0; index < max; index++)
max = new FxIntCast(ma); min.Push(new FxIntCast(mi[index]));
} }
else min = max = NULL; else min.Clear();
rng = r; rng = r;
ValueType = VAL_Int; ValueType = VAL_Int;
} }
@ -1717,8 +1719,7 @@ FxPick::FxPick(FRandom * r, FxExpression *mi, FxExpression *ma, const FScriptPos
FxPick::~FxPick() FxPick::~FxPick()
{ {
SAFE_DELETE(min); min.Clear();
SAFE_DELETE(max);
} }
//========================================================================== //==========================================================================
@ -1729,12 +1730,17 @@ FxPick::~FxPick()
FxExpression *FxPick::Resolve(FCompileContext &ctx) FxExpression *FxPick::Resolve(FCompileContext &ctx)
{ {
int index = 0;
CHECKRESOLVED(); CHECKRESOLVED();
if (min && max) int max = min.Size();
if (max > 0)
{ {
RESOLVE(min, ctx); for (index = 0; index < max; index++)
RESOLVE(max, ctx); {
ABORT(min && max); RESOLVE(min[index], ctx);
ABORT(min[index]);
}
} }
return this; return this;
}; };
@ -1750,22 +1756,11 @@ ExpVal FxPick::EvalExpression(AActor *self)
{ {
ExpVal val; ExpVal val;
val.Type = VAL_Int; val.Type = VAL_Int;
int max = min.Size();
if (min != NULL && max != NULL) if (max > 0)
{ {
int minval = min->EvalExpression(self).GetInt(); int select = (*rng)(max);
int maxval = max->EvalExpression(self).GetInt(); val.Int = min[select]->EvalExpression(self).GetInt();
if (maxval < minval)
{
swapvalues(maxval, minval);
}
val.Int = (*rng)(2); //rng->operator()(2); //(maxval - minval + 1) + minval;
if (val.Int > 0)
val.Int = maxval;
else
val.Int = minval;
} }
else else
{ {