More Units module cleanup, modernize getUnitsInBox

develop
lethosor 2017-06-10 20:32:36 -04:00
parent dff94fc97e
commit 778ffb0971
2 changed files with 29 additions and 46 deletions

@ -23,10 +23,9 @@ distribution.
*/ */
#pragma once #pragma once
#ifndef CL_MOD_CREATURES
#define CL_MOD_CREATURES
/* /*
* Creatures * Units
*/ */
#include "Export.h" #include "Export.h"
#include "modules/Items.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 * The Units module - allows reading all non-vermin units and their properties
* \ingroup grp_modules
* \ingroup grp_units
*/ */
DFHACK_EXPORT bool isValid();
/* Read Functions */ /* Read Functions */
// Read creatures in a box, starting with index. Returns -1 if no more creatures // Read units in a box, starting with index. Returns -1 if no more units
// found. Call repeatedly do get all creatures in a specified box (uses tile coords) // found. Call repeatedly do get all units in a specified box (uses tile coords)
DFHACK_EXPORT int32_t getNumCreatures(); DFHACK_EXPORT int32_t getNumUnits();
DFHACK_EXPORT df::unit * getCreature(const int32_t index); DFHACK_EXPORT df::unit *getUnit(const int32_t index);
DFHACK_EXPORT int32_t getCreatureInBox(const int32_t index, df::unit ** furball, DFHACK_EXPORT bool getUnitsInBox(std::vector<df::unit*> &units,
const uint16_t x1, const uint16_t y1,const uint16_t z1, int16_t x1, int16_t y1, int16_t z1,
const uint16_t x2, const uint16_t y2,const uint16_t z2); int16_t x2, int16_t y2, int16_t z2);
DFHACK_EXPORT int32_t findIndexById(int32_t id); DFHACK_EXPORT int32_t findIndexById(int32_t id);
@ -186,4 +181,3 @@ DFHACK_EXPORT df::activity_event *getMainSocialEvent(df::unit *unit);
} }
} }
#endif

@ -83,54 +83,43 @@ using df::global::world;
using df::global::ui; using df::global::ui;
using df::global::gamemode; using df::global::gamemode;
bool Units::isValid() int32_t Units::getNumUnits()
{
return (world->units.all.size() > 0);
}
int32_t Units::getNumCreatures()
{ {
return world->units.all.size(); 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; return vector_get(world->units.all, index);
// read pointer from vector at position
if(size_t(index) > world->units.all.size())
return 0;
return world->units.all[index];
} }
// returns index of creature actually read or -1 if no creature can be found // returns index of creature actually read or -1 if no creature can be found
int32_t Units::getCreatureInBox (int32_t index, df::unit ** furball, bool Units::getUnitsInBox (std::vector<df::unit*> &units,
const uint16_t x1, const uint16_t y1, const uint16_t z1, int16_t x1, int16_t y1, int16_t z1,
const uint16_t x2, const uint16_t y2, const uint16_t z2) int16_t x2, int16_t y2, int16_t z2)
{ {
if (!isValid()) if (!world)
return -1; return false;
size_t size = world->units.all.size(); if (x1 > x2) swap(x1, x2);
while (size_t(index) < size) 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 if (u->pos.x >= x1 && u->pos.x <= x2)
df::unit * temp = world->units.all[index];
if (temp->pos.x >= x1 && temp->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; units.push_back(u);
return index;
} }
} }
} }
index++;
} }
*furball = NULL; return true;
return -1;
} }
int32_t Units::findIndexById(int32_t creature_id) int32_t Units::findIndexById(int32_t creature_id)