Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

get rid of duplicated item creation code

  • Loading branch information...
commit 15f37c2dddd401e914c2d38ce46edc8c14733283 1 parent 24ef876
@fizzet fizzet authored
View
2  gemrb/core/GameScript/GSUtils.h
@@ -88,7 +88,7 @@ void PolymorphCopyCore(Actor *src, Actor *tar, bool base);
void CreateCreatureCore(Scriptable* Sender, Action* parameters, int flags);
int MoveItemCore(Scriptable *Sender, Scriptable *target, const char *resref, int flags, int setflag, int count = 0);
void MoveToObjectCore(Scriptable *Sender, Action *parameters, ieDword flags, bool untilsee);
-bool CreateItemCore(CREItem *item, const char *resref, int a, int b, int c);
+GEM_EXPORT bool CreateItemCore(CREItem *item, const char *resref, int a, int b, int c);
void AttackCore(Scriptable *Sender, Scriptable *target, int flags);
void InitScriptTables();
void HandleBitMod(ieDword &value1, ieDword value2, int opcode);
View
19 gemrb/core/Inventory.cpp
@@ -30,6 +30,7 @@
#include "DisplayMessage.h"
#include "Game.h"
#include "GameData.h"
+#include "GameScript/GSUtils.h"
#include "Interface.h"
#include "Item.h"
#include "Map.h"
@@ -1429,13 +1430,7 @@ int Inventory::FindCandidateSlot(int slottype, size_t first_slot, const char *re
void Inventory::AddSlotItemRes(const ieResRef ItemResRef, int SlotID, int Charge0, int Charge1, int Charge2)
{
CREItem *TmpItem = new CREItem();
- strnlwrcpy(TmpItem->ItemResRef, ItemResRef, 8);
- TmpItem->Expired=0;
- TmpItem->Usages[0]=(ieWord) Charge0;
- TmpItem->Usages[1]=(ieWord) Charge1;
- TmpItem->Usages[2]=(ieWord) Charge2;
- TmpItem->Flags=0;
- if (core->ResolveRandomItem(TmpItem)) {
+ if (CreateItemCore(TmpItem, ItemResRef, Charge0, Charge1, Charge2)) {
int ret = AddSlotItem( TmpItem, SlotID );
if (ret != ASI_SUCCESS) {
// put the remainder on the ground
@@ -1448,23 +1443,17 @@ void Inventory::AddSlotItemRes(const ieResRef ItemResRef, int SlotID, int Charge
delete TmpItem;
}
}
+ CalculateWeight();
} else {
delete TmpItem;
}
- CalculateWeight();
}
void Inventory::SetSlotItemRes(const ieResRef ItemResRef, int SlotID, int Charge0, int Charge1, int Charge2)
{
if(ItemResRef[0]) {
CREItem *TmpItem = new CREItem();
- strnlwrcpy(TmpItem->ItemResRef, ItemResRef, 8);
- TmpItem->Expired=0;
- TmpItem->Usages[0]=(ieWord) Charge0;
- TmpItem->Usages[1]=(ieWord) Charge1;
- TmpItem->Usages[2]=(ieWord) Charge2;
- TmpItem->Flags=0;
- if (core->ResolveRandomItem(TmpItem)) {
+ if (CreateItemCore(TmpItem, ItemResRef, Charge0, Charge1, Charge2)) {
SetSlotItem( TmpItem, SlotID );
} else {
delete TmpItem;
View
25 gemrb/plugins/GUIScript/GUIScript.cpp
@@ -361,25 +361,6 @@ static int SetCreatureStat(Actor *actor, unsigned int StatID, int StatValue, boo
return 1;
}
-/* create an item entry
- TODO: this code snippet exists in many copies, maybe consolidate */
-static CREItem *CreateCreItem(const char *ItemResRef, int Charge0, int Charge1, int Charge2)
-{
- CREItem *TmpItem = new CREItem();
- strnlwrcpy(TmpItem->ItemResRef, ItemResRef, 8);
- TmpItem->Expired=0;
- TmpItem->Usages[0]=(ieWord) Charge0;
- TmpItem->Usages[1]=(ieWord) Charge1;
- TmpItem->Usages[2]=(ieWord) Charge2;
- TmpItem->Flags=0;
- if (core->ResolveRandomItem(TmpItem)) {
- return TmpItem;
- }
- /* item couldn't be resolved */
- delete TmpItem;
- return NULL;
-}
-
PyDoc_STRVAR( GemRB_SetInfoTextColor__doc,
"SetInfoTextColor(red, green, blue, [alpha])\n\n"
"Sets the color of Floating Messages in GameControl." );
@@ -7972,8 +7953,10 @@ static PyObject* GemRB_CreateItem(PyObject * /*self*/, PyObject* args)
// Create item on ground
Map *map = actor->GetCurrentArea();
if (map) {
- CREItem *item = CreateCreItem(ItemResRef, Charge0, Charge1, Charge2 );
- if (item) {
+ CREItem *item = new CREItem();
+ if (!CreateItemCore(item, ItemResRef, Charge0, Charge1, Charge2)) {
+ delete item;
+ } else {
map->AddItemToLocation(actor->Pos, item);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.