diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 80c7fcabf..41859e983 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -23,10 +23,9 @@ distribution. */ #pragma once -#ifndef CL_MOD_CREATURES -#define CL_MOD_CREATURES + /* - * Creatures + * Units */ #include "Export.h" #include "modules/Items.h" @@ -65,21 +64,17 @@ static const int MAX_COLORS = 15; /** - * The Units module - allows reading all non-vermin creatures and their properties - * \ingroup grp_modules - * \ingroup grp_units + * The Units module - allows reading all non-vermin units and their properties */ -DFHACK_EXPORT bool isValid(); - /* Read Functions */ -// Read creatures in a box, starting with index. Returns -1 if no more creatures -// found. Call repeatedly do get all creatures in a specified box (uses tile coords) -DFHACK_EXPORT int32_t getNumCreatures(); -DFHACK_EXPORT df::unit * getCreature(const int32_t index); -DFHACK_EXPORT int32_t getCreatureInBox(const int32_t index, df::unit ** furball, - const uint16_t x1, const uint16_t y1,const uint16_t z1, - const uint16_t x2, const uint16_t y2,const uint16_t z2); +// Read units in a box, starting with index. Returns -1 if no more units +// found. Call repeatedly do get all units in a specified box (uses tile coords) +DFHACK_EXPORT int32_t getNumUnits(); +DFHACK_EXPORT df::unit *getUnit(const int32_t index); +DFHACK_EXPORT bool getUnitsInBox(std::vector &units, + int16_t x1, int16_t y1, int16_t z1, + int16_t x2, int16_t y2, int16_t z2); DFHACK_EXPORT int32_t findIndexById(int32_t id); @@ -186,4 +181,3 @@ DFHACK_EXPORT df::activity_event *getMainSocialEvent(df::unit *unit); } } -#endif diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index b8170148c..ffec94910 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -83,54 +83,43 @@ using df::global::world; using df::global::ui; using df::global::gamemode; -bool Units::isValid() -{ - return (world->units.all.size() > 0); -} - -int32_t Units::getNumCreatures() +int32_t Units::getNumUnits() { return world->units.all.size(); } -df::unit * Units::getCreature (const int32_t index) +df::unit *Units::getUnit (const int32_t index) { - if (!isValid()) return NULL; - - // read pointer from vector at position - if(size_t(index) > world->units.all.size()) - return 0; - return world->units.all[index]; + return vector_get(world->units.all, index); } // returns index of creature actually read or -1 if no creature can be found -int32_t Units::getCreatureInBox (int32_t index, df::unit ** furball, - const uint16_t x1, const uint16_t y1, const uint16_t z1, - const uint16_t x2, const uint16_t y2, const uint16_t z2) +bool Units::getUnitsInBox (std::vector &units, + int16_t x1, int16_t y1, int16_t z1, + int16_t x2, int16_t y2, int16_t z2) { - if (!isValid()) - return -1; + if (!world) + return false; - size_t size = world->units.all.size(); - while (size_t(index) < size) + if (x1 > x2) swap(x1, x2); + if (y1 > y2) swap(y1, y2); + if (z1 > z2) swap(z1, z2); + + units.clear(); + for (df::unit *u : world->units.all) { - // read pointer from vector at position - df::unit * temp = world->units.all[index]; - if (temp->pos.x >= x1 && temp->pos.x < x2) + if (u->pos.x >= x1 && u->pos.x <= x2) { - if (temp->pos.y >= y1 && temp->pos.y < y2) + if (u->pos.y >= y1 && u->pos.y <= y2) { - if (temp->pos.z >= z1 && temp->pos.z < z2) + if (u->pos.z >= z1 && u->pos.z <= z2) { - *furball = temp; - return index; + units.push_back(u); } } } - index++; } - *furball = NULL; - return -1; + return true; } int32_t Units::findIndexById(int32_t creature_id)