Add function for making item projectiles.

develop
Alexander Gavrilov 2012-09-12 18:17:42 +04:00
parent 448d7e3633
commit 7c71aeab5f
5 changed files with 64 additions and 0 deletions

@ -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
-----------

@ -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">

@ -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 }
};

@ -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);
}
}

@ -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;
}