From 3f3aab42f1f1f20a522411c163800a78472b24b4 Mon Sep 17 00:00:00 2001 From: MajorCooke Date: Sun, 14 Dec 2014 04:45:39 -0600 Subject: [PATCH] - Pick can now hold unlimited numbers to choose from. - I.e. pick(1,4,12,16) --- src/thingdef/thingdef_exp.cpp | 18 ++++++++--- src/thingdef/thingdef_exp.h | 4 +-- src/thingdef/thingdef_expression.cpp | 47 +++++++++++++--------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/thingdef/thingdef_exp.cpp b/src/thingdef/thingdef_exp.cpp index fd0936485..daa7f1a9c 100644 --- a/src/thingdef/thingdef_exp.cpp +++ b/src/thingdef/thingdef_exp.cpp @@ -374,6 +374,9 @@ static FxExpression *ParseExpression0 (FScanner &sc, const PClass *cls) else if (sc.CheckToken(TK_Pick)) { FRandom *rng; + TArray list; + list.Clear(); + int index = 0; if (sc.CheckToken('[')) { @@ -387,12 +390,17 @@ static FxExpression *ParseExpression0 (FScanner &sc, const PClass *cls) } sc.MustGetToken('('); - FxExpression *min = ParseExpressionM(sc, cls); - sc.MustGetToken(','); - FxExpression *max = ParseExpressionM(sc, cls); - sc.MustGetToken(')'); + while (!(sc.CheckToken(')'))) + { + FxExpression *min = ParseExpressionM(sc, cls); + list.Push(min); + if (sc.CheckToken(')')) + break; + else + sc.MustGetToken(','); + } - return new FxPick(rng, min, max, sc); + return new FxPick(rng, list, sc); } else if (sc.CheckToken(TK_FRandom)) { diff --git a/src/thingdef/thingdef_exp.h b/src/thingdef/thingdef_exp.h index 03e465c3a..b53b8796b 100644 --- a/src/thingdef/thingdef_exp.h +++ b/src/thingdef/thingdef_exp.h @@ -563,11 +563,11 @@ class FxPick : public FxExpression { protected: FRandom * rng; - FxExpression *min, *max; + TArray min; public: - FxPick(FRandom *, FxExpression *mi, FxExpression *ma, const FScriptPosition &pos); + FxPick(FRandom *, TArray mi, const FScriptPosition &pos); ~FxPick(); FxExpression *Resolve(FCompileContext&); diff --git a/src/thingdef/thingdef_expression.cpp b/src/thingdef/thingdef_expression.cpp index dc2cb2209..dd8e0e4c9 100644 --- a/src/thingdef/thingdef_expression.cpp +++ b/src/thingdef/thingdef_expression.cpp @@ -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 mi, const FScriptPosition &pos) : FxExpression(pos) { - if (mi != NULL && ma != NULL) + int index = 0; + int max = mi.Size(); + if (max > 0) { - min = new FxIntCast(mi); - max = new FxIntCast(ma); + for (index = 0; index < max; index++) + min.Push(new FxIntCast(mi[index])); } - else min = max = NULL; + else min.Clear(); rng = r; ValueType = VAL_Int; } @@ -1717,8 +1719,7 @@ FxPick::FxPick(FRandom * r, FxExpression *mi, FxExpression *ma, const FScriptPos FxPick::~FxPick() { - SAFE_DELETE(min); - SAFE_DELETE(max); + min.Clear(); } //========================================================================== @@ -1729,12 +1730,17 @@ FxPick::~FxPick() FxExpression *FxPick::Resolve(FCompileContext &ctx) { + int index = 0; CHECKRESOLVED(); - if (min && max) + int max = min.Size(); + if (max > 0) { - RESOLVE(min, ctx); - RESOLVE(max, ctx); - ABORT(min && max); + for (index = 0; index < max; index++) + { + RESOLVE(min[index], ctx); + ABORT(min[index]); + } + } return this; }; @@ -1750,22 +1756,11 @@ ExpVal FxPick::EvalExpression(AActor *self) { ExpVal val; val.Type = VAL_Int; - - if (min != NULL && max != NULL) + int max = min.Size(); + if (max > 0) { - int minval = min->EvalExpression(self).GetInt(); - int maxval = max->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; + int select = (*rng)(max); + val.Int = min[select]->EvalExpression(self).GetInt(); } else {