diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-02-12 20:18:58 -0800 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-02-12 21:09:59 -0800 |
commit | b17b9021ecf9b16c265d0a6b60faa761b34eae35 (patch) | |
tree | e8192de5b2458864f0f5ce5edd0e1ccf5605c644 /src/map/magic-stmt.cpp | |
parent | 80e36aa669274637bcd5956fbf4020dba1d4739c (diff) | |
download | tmwa-b17b9021ecf9b16c265d0a6b60faa761b34eae35.tar.gz tmwa-b17b9021ecf9b16c265d0a6b60faa761b34eae35.tar.bz2 tmwa-b17b9021ecf9b16c265d0a6b60faa761b34eae35.tar.xz tmwa-b17b9021ecf9b16c265d0a6b60faa761b34eae35.zip |
Replace mt_rand with <random>
Also add some utility methods and classes.
Diffstat (limited to 'src/map/magic-stmt.cpp')
-rw-r--r-- | src/map/magic-stmt.cpp | 30 |
1 files changed, 6 insertions, 24 deletions
diff --git a/src/map/magic-stmt.cpp b/src/map/magic-stmt.cpp index fdb95fc..c729cfd 100644 --- a/src/map/magic-stmt.cpp +++ b/src/map/magic-stmt.cpp @@ -1,7 +1,7 @@ #include <cassert> #include "../common/cxxstdio.hpp" -#include "../common/mt_rand.hpp" +#include "../common/random2.hpp" #include "../common/timer.hpp" #include "magic-expr.hpp" @@ -1235,36 +1235,18 @@ effect_t *run_foreach(invocation_t *invocation, effect_t *foreach, cont_activation_record_t *ar = add_stack_entry(invocation, CONT_STACK::FOREACH, return_location); int entities_allocd = 64; - int *entities_collect; int *entities; - int *shuffle_board; int entities_nr = 0; - int i; if (!ar) return return_location; - CREATE(entities_collect, int, entities_allocd); - + CREATE(entities, int, entities_allocd); find_entities_in_area(area.v.v_area, &entities_allocd, &entities_nr, - &entities_collect, filter); - - /* Now shuffle */ - CREATE(shuffle_board, int, entities_nr); - CREATE(entities, int, entities_nr); - for (i = 0; i < entities_nr; i++) - shuffle_board[i] = i; - - for (i = entities_nr - 1; i >= 0; i--) - { - int random_index = MRAND(i + 1); - entities[i] = entities_collect[shuffle_board[random_index]]; - shuffle_board[random_index] = shuffle_board[i]; // thus, we are guaranteed only to use unused indices - } - - free(entities_collect); - free(shuffle_board); - /* Done shuffling */ + &entities, filter); + RECREATE(entities, int, entities_nr); + // iterator_pair will go away when this gets properly containerized. + random_::shuffle(iterator_pair(entities, entities + entities_nr)); ar->c.c_foreach.id = id; ar->c.c_foreach.body = body; |