From 3ff5d38a5c637929c41ad0681c10317928b64a4c Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov
Date: Tue, 30 Oct 2012 12:40:26 +0400
Subject: [PATCH] Add a few utility functions to the main library.
---
Lua API.html | 6 ++++++
Lua API.rst | 8 ++++++++
library/LuaApi.cpp | 2 ++
library/MiscUtils.cpp | 5 +++++
library/include/MiscUtils.h | 2 ++
library/include/modules/Items.h | 5 +++++
library/modules/Items.cpp | 14 ++++++++++++++
plugins/siege-engine.cpp | 5 -----
8 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/Lua API.html b/Lua API.html
index be52ea985..a52347104 100644
--- a/Lua API.html
+++ b/Lua API.html
@@ -1239,6 +1239,12 @@ Returns false in case of error.
dfhack.items.getContainedItems(item)
Returns a list of items contained in this one.
+dfhack.items.getHolderBuilding(item)
+Returns the holder building or nil.
+
+dfhack.items.getHolderUnit(item)
+Returns the holder unit or nil.
+
dfhack.items.moveToGround(item,pos)
Move the item to the ground at position. Returns false if impossible.
diff --git a/Lua API.rst b/Lua API.rst
index 8dad3663b..bd712d301 100644
--- a/Lua API.rst
+++ b/Lua API.rst
@@ -1019,6 +1019,14 @@ Items module
Returns a list of items contained in this one.
+* ``dfhack.items.getHolderBuilding(item)``
+
+ Returns the holder building or *nil*.
+
+* ``dfhack.items.getHolderUnit(item)``
+
+ Returns the holder unit or *nil*.
+
* ``dfhack.items.moveToGround(item,pos)``
Move the item to the ground at position. Returns *false* if impossible.
diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp
index 6ca1188d9..e7424ad50 100644
--- a/library/LuaApi.cpp
+++ b/library/LuaApi.cpp
@@ -942,6 +942,8 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = {
WRAPM(Items, getOwner),
WRAPM(Items, setOwner),
WRAPM(Items, getContainer),
+ WRAPM(Items, getHolderBuilding),
+ WRAPM(Items, getHolderUnit),
WRAPM(Items, getDescription),
WRAPM(Items, isCasteMaterial),
WRAPM(Items, getSubtypeCount),
diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp
index a05e7eba5..53c403026 100644
--- a/library/MiscUtils.cpp
+++ b/library/MiscUtils.cpp
@@ -148,6 +148,11 @@ bool prefix_matches(const std::string &prefix, const std::string &key, std::stri
return false;
}
+int random_int(int max)
+{
+ return int(int64_t(rand())*max/(int64_t(RAND_MAX)+1));
+}
+
#ifdef LINUX_BUILD // Linux
uint64_t GetTimeMs64()
{
diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h
index 61dd69297..f5b4e8ded 100644
--- a/library/include/MiscUtils.h
+++ b/library/include/MiscUtils.h
@@ -331,6 +331,8 @@ inline T clip_range(T a, T1 minv, T2 maxv) {
return a;
}
+DFHACK_EXPORT int random_int(int max);
+
/**
* Returns the amount of milliseconds elapsed since the UNIX epoch.
* Works on both windows and linux.
diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h
index 217a0f28a..34ca98162 100644
--- a/library/include/modules/Items.h
+++ b/library/include/modules/Items.h
@@ -151,6 +151,11 @@ DFHACK_EXPORT df::item *getContainer(df::item *item);
/// which items does it contain?
DFHACK_EXPORT void getContainedItems(df::item *item, /*output*/ std::vector *items);
+/// which building holds it?
+DFHACK_EXPORT df::building *getHolderBuilding(df::item *item);
+/// which unit holds it?
+DFHACK_EXPORT df::unit *getHolderUnit(df::item *item);
+
/// Returns the true position of the item.
DFHACK_EXPORT df::coord getPosition(df::item *item);
diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp
index cf99d9426..877f8abe0 100644
--- a/library/modules/Items.cpp
+++ b/library/modules/Items.cpp
@@ -592,6 +592,20 @@ void Items::getContainedItems(df::item *item, std::vector *items)
}
}
+df::building *Items::getHolderBuilding(df::item * item)
+{
+ auto ref = getGeneralRef(item, general_ref_type::BUILDING_HOLDER);
+
+ return ref ? ref->getBuilding() : NULL;
+}
+
+df::unit *Items::getHolderUnit(df::item * item)
+{
+ auto ref = getGeneralRef(item, general_ref_type::UNIT_HOLDER);
+
+ return ref ? ref->getUnit() : NULL;
+}
+
df::coord Items::getPosition(df::item *item)
{
CHECK_NULL_POINTER(item);
diff --git a/plugins/siege-engine.cpp b/plugins/siege-engine.cpp
index 5c7b8833a..4b2060e99 100644
--- a/plugins/siege-engine.cpp
+++ b/plugins/siege-engine.cpp
@@ -132,11 +132,6 @@ static void orient_engine(df::building_siegeenginest *bld, df::coord target)
df::building_siegeenginest::Up;
}
-static int random_int(int val)
-{
- return int(int64_t(rand())*val/RAND_MAX);
-}
-
static int point_distance(df::coord speed)
{
return std::max(abs(speed.x), std::max(abs(speed.y), abs(speed.z)));