summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-09-12 18:17:42 +0400
committerAlexander Gavrilov2012-09-12 18:17:42 +0400
commit7c71aeab5f44c7fce106a0efc07c1ea2860e4638 (patch)
treedb2c792ddd19b323c7a8156435f7c3daaea54a98
parent448d7e3633efd5171feaa3c3a0cdc4afbfafe105 (diff)
downloaddfhack-7c71aeab5f44c7fce106a0efc07c1ea2860e4638.tar.gz
dfhack-7c71aeab5f44c7fce106a0efc07c1ea2860e4638.tar.bz2
dfhack-7c71aeab5f44c7fce106a0efc07c1ea2860e4638.tar.xz
Add function for making item projectiles.
-rw-r--r--LUA_API.rst4
-rw-r--r--Lua API.html3
-rw-r--r--library/LuaApi.cpp8
-rw-r--r--library/include/modules/Items.h4
-rw-r--r--library/modules/Items.cpp45
5 files changed, 64 insertions, 0 deletions
diff --git a/LUA_API.rst b/LUA_API.rst
index a5be09a0..1ffdada0 100644
--- a/LUA_API.rst
+++ b/LUA_API.rst
@@ -999,6 +999,10 @@ Items module
Move the item to the unit inventory. Returns *false* if impossible.
+* ``dfhack.items.makeProjectile(item)``
+
+ Turns the item into a projectile, and returns the new object, or *nil* if impossible.
+
Maps module
-----------
diff --git a/Lua API.html b/Lua API.html
index 7886fbc2..168f7dcc 100644
--- a/Lua API.html
+++ b/Lua API.html
@@ -1213,6 +1213,9 @@ Returns <em>false</em> in case of error.</p>
<li><p class="first"><tt class="docutils literal">dfhack.items.moveToInventory(item,unit,use_mode,body_part)</tt></p>
<p>Move the item to the unit inventory. Returns <em>false</em> if impossible.</p>
</li>
+<li><p class="first"><tt class="docutils literal">dfhack.items.makeProjectile(item)</tt></p>
+<p>Turns the item into a projectile, and returns the new object, or <em>nil</em> if impossible.</p>
+</li>
</ul>
</div>
<div class="section" id="maps-module">
diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp
index d39a945d..f69fa7a1 100644
--- a/library/LuaApi.cpp
+++ b/library/LuaApi.cpp
@@ -80,6 +80,7 @@ distribution.
#include "df/region_map_entry.h"
#include "df/flow_info.h"
#include "df/unit_misc_trait.h"
+#include "df/proj_itemst.h"
#include <lua.h>
#include <lauxlib.h>
@@ -885,6 +886,12 @@ static bool items_moveToInventory
return Items::moveToInventory(mc, item, unit, mode, body_part);
}
+static df::proj_itemst *items_makeProjectile(df::item *item)
+{
+ MapExtras::MapCache mc;
+ return Items::makeProjectile(mc, item);
+}
+
static const LuaWrapper::FunctionReg dfhack_items_module[] = {
WRAPM(Items, getGeneralRef),
WRAPM(Items, getSpecificRef),
@@ -896,6 +903,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = {
WRAPN(moveToContainer, items_moveToContainer),
WRAPN(moveToBuilding, items_moveToBuilding),
WRAPN(moveToInventory, items_moveToInventory),
+ WRAPN(makeProjectile, items_makeProjectile),
{ NULL, NULL }
};
diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h
index 4236f068..7493d22f 100644
--- a/library/include/modules/Items.h
+++ b/library/include/modules/Items.h
@@ -44,6 +44,7 @@ distribution.
namespace df
{
struct itemdef;
+ struct proj_itemst;
}
namespace MapExtras {
@@ -155,5 +156,8 @@ DFHACK_EXPORT bool moveToContainer(MapExtras::MapCache &mc, df::item *item, df::
DFHACK_EXPORT bool moveToBuilding(MapExtras::MapCache &mc, df::item *item, df::building_actual *building,int16_t use_mode);
DFHACK_EXPORT bool moveToInventory(MapExtras::MapCache &mc, df::item *item, df::unit *unit,
df::unit_inventory_item::T_mode mode = df::unit_inventory_item::Carried, int body_part = -1);
+
+/// Detaches the items from its current location and turns it into a projectile
+DFHACK_EXPORT df::proj_itemst *makeProjectile(MapExtras::MapCache &mc, df::item *item);
}
}
diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp
index dc64143c..751797f0 100644
--- a/library/modules/Items.cpp
+++ b/library/modules/Items.cpp
@@ -72,8 +72,11 @@ using namespace std;
#include "df/general_ref_contains_itemst.h"
#include "df/general_ref_contained_in_itemst.h"
#include "df/general_ref_building_holderst.h"
+#include "df/general_ref_projectile.h"
#include "df/viewscreen_itemst.h"
#include "df/vermin.h"
+#include "df/proj_itemst.h"
+#include "df/proj_list_link.h"
#include "df/unit_inventory_item.h"
#include "df/body_part_raw.h"
@@ -88,6 +91,7 @@ using namespace df::enums;
using df::global::world;
using df::global::ui;
using df::global::ui_selected_unit;
+using df::global::proj_next_id;
#define ITEMDEF_VECTORS \
ITEM(WEAPON, weapons, itemdef_weaponst) \
@@ -866,3 +870,44 @@ bool DFHack::Items::moveToInventory(
return true;
}
+
+df::proj_itemst *Items::makeProjectile(MapExtras::MapCache &mc, df::item *item)
+{
+ CHECK_NULL_POINTER(item);
+
+ if (!world || !proj_next_id)
+ return NULL;
+
+ auto pos = getPosition(item);
+ if (!pos.isValid())
+ return NULL;
+
+ auto ref = df::allocate<df::general_ref_projectile>();
+ if (!ref)
+ return NULL;
+
+ if (!detachItem(mc, item))
+ {
+ delete ref;
+ return NULL;
+ }
+
+ item->pos = pos;
+ item->flags.bits.in_job = true;
+
+ auto proj = new df::proj_itemst();
+ proj->link = new df::proj_list_link();
+ proj->link->item = proj;
+ proj->id = (*proj_next_id)++;
+
+ proj->origin_pos = proj->target_pos = pos;
+ proj->cur_pos = proj->prev_pos = pos;
+ proj->item = item;
+
+ ref->projectile_id = proj->id;
+ item->itemrefs.push_back(ref);
+
+ linked_list_append(&world->proj_list, proj->link);
+
+ return proj;
+}