1
0

Added cItemGrid to represent an XY grid of items; converted chests to use cItemGrid.

http://forum.mc-server.org/showthread.php?tid=831

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1380 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2013-04-10 19:52:03 +00:00
parent c603180470
commit cc26f16463
15 changed files with 1321 additions and 115 deletions

View File

@ -456,6 +456,14 @@
RelativePath="..\source\Item.h"
>
</File>
<File
RelativePath="..\source\ItemGrid.cpp"
>
</File>
<File
RelativePath="..\source\ItemGrid.h"
>
</File>
<File
RelativePath="..\source\LeakFinder.cpp"
>

View File

@ -38,6 +38,7 @@ $cfile "Server.h"
$cfile "World.h"
$cfile "Inventory.h"
$cfile "Item.h"
$cfile "ItemGrid.h"
$cfile "ChestEntity.h"
$cfile "WebAdmin.h"
$cfile "WebPlugin.h"

View File

@ -1,6 +1,6 @@
/*
** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 04/06/13 23:17:20.
** Generated automatically by tolua++-1.0.92 on 04/10/13 21:35:56.
*/
#ifndef __cplusplus
@ -37,6 +37,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S);
#include "World.h"
#include "Inventory.h"
#include "Item.h"
#include "ItemGrid.h"
#include "ChestEntity.h"
#include "WebAdmin.h"
#include "WebPlugin.h"
@ -142,6 +143,7 @@ static void tolua_reg_types (lua_State* tolua_S)
tolua_usertype(tolua_S,"cCraftingRecipe");
tolua_usertype(tolua_S,"cPlugin_NewLua");
tolua_usertype(tolua_S,"cStringMap");
tolua_usertype(tolua_S,"cItemGrid");
tolua_usertype(tolua_S,"cBlockArea");
tolua_usertype(tolua_S,"cInventory");
tolua_usertype(tolua_S,"cRoot");
@ -162,30 +164,30 @@ static void tolua_reg_types (lua_State* tolua_S)
tolua_usertype(tolua_S,"Vector3i");
tolua_usertype(tolua_S,"cCraftingRecipes");
tolua_usertype(tolua_S,"Lua__cPlayer");
tolua_usertype(tolua_S,"cPawn");
tolua_usertype(tolua_S,"cStairs");
tolua_usertype(tolua_S,"cChestEntity");
tolua_usertype(tolua_S,"cWebAdmin");
tolua_usertype(tolua_S,"cGroupManager");
tolua_usertype(tolua_S,"cBlockEntity");
tolua_usertype(tolua_S,"Lua__cPickup");
tolua_usertype(tolua_S,"Lua__cEntity");
tolua_usertype(tolua_S,"cPluginManager");
tolua_usertype(tolua_S,"cWebPlugin");
tolua_usertype(tolua_S,"cPluginManager");
tolua_usertype(tolua_S,"HTTPRequest");
tolua_usertype(tolua_S,"cLadder");
tolua_usertype(tolua_S,"HTTPFormData");
tolua_usertype(tolua_S,"cLadder");
tolua_usertype(tolua_S,"cWorld");
tolua_usertype(tolua_S,"cEntity");
tolua_usertype(tolua_S,"cIniFile");
tolua_usertype(tolua_S,"cServer");
tolua_usertype(tolua_S,"cPlugin");
tolua_usertype(tolua_S,"AStringVector");
tolua_usertype(tolua_S,"cVine");
tolua_usertype(tolua_S,"cPlayer");
tolua_usertype(tolua_S,"cTorch");
tolua_usertype(tolua_S,"cBlockEntityWindowOwner");
tolua_usertype(tolua_S,"cPlugin");
tolua_usertype(tolua_S,"cServer");
tolua_usertype(tolua_S,"Lua__cChestEntity");
tolua_usertype(tolua_S,"cWorld");
tolua_usertype(tolua_S,"cStairs");
tolua_usertype(tolua_S,"cPawn");
tolua_usertype(tolua_S,"Lua__cEntity");
tolua_usertype(tolua_S,"Vector3d");
}
@ -13158,6 +13160,40 @@ static int tolua_AllToLua_cItem_HasDuration00(lua_State* tolua_S)
}
#endif //#ifndef TOLUA_DISABLE
/* method: IsStackableWith of class cItem */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItem_IsStackableWith00
static int tolua_AllToLua_cItem_IsStackableWith00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItem",0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"const cItem",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItem* self = (cItem*) tolua_tousertype(tolua_S,1,0);
const cItem* a_OtherStack = ((const cItem*) tolua_tousertype(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'IsStackableWith'", NULL);
#endif
{
bool tolua_ret = (bool) self->IsStackableWith(*a_OtherStack);
tolua_pushboolean(tolua_S,(bool)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'IsStackableWith'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: IsEnchantable of class cItem */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItem_IsEnchantable00
static int tolua_AllToLua_cItem_IsEnchantable00(lua_State* tolua_S)
@ -13542,6 +13578,664 @@ tolua_lerror:
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetWidth of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetWidth00
static int tolua_AllToLua_cItemGrid_GetWidth00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetWidth'", NULL);
#endif
{
int tolua_ret = (int) self->GetWidth();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetWidth'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetHeight of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetHeight00
static int tolua_AllToLua_cItemGrid_GetHeight00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetHeight'", NULL);
#endif
{
int tolua_ret = (int) self->GetHeight();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetHeight'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetNumSlots of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetNumSlots00
static int tolua_AllToLua_cItemGrid_GetNumSlots00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetNumSlots'", NULL);
#endif
{
int tolua_ret = (int) self->GetNumSlots();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetNumSlots'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetSlotNum of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetSlotNum00
static int tolua_AllToLua_cItemGrid_GetSlotNum00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_X = ((int) tolua_tonumber(tolua_S,2,0));
int a_Y = ((int) tolua_tonumber(tolua_S,3,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetSlotNum'", NULL);
#endif
{
int tolua_ret = (int) self->GetSlotNum(a_X,a_Y);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetSlotNum'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetItem00
static int tolua_AllToLua_cItemGrid_GetItem00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_X = ((int) tolua_tonumber(tolua_S,2,0));
int a_Y = ((int) tolua_tonumber(tolua_S,3,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetItem'", NULL);
#endif
{
const cItem& tolua_ret = (const cItem&) self->GetItem(a_X,a_Y);
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"const cItem");
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetItem'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetItem01
static int tolua_AllToLua_cItemGrid_GetItem01(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_X = ((int) tolua_tonumber(tolua_S,2,0));
int a_Y = ((int) tolua_tonumber(tolua_S,3,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetItem'", NULL);
#endif
{
cItem& tolua_ret = (cItem&) self->GetItem(a_X,a_Y);
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"cItem");
}
}
return 1;
tolua_lerror:
return tolua_AllToLua_cItemGrid_GetItem00(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetItem02
static int tolua_AllToLua_cItemGrid_GetItem02(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_SlotNum = ((int) tolua_tonumber(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetItem'", NULL);
#endif
{
const cItem& tolua_ret = (const cItem&) self->GetItem(a_SlotNum);
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"const cItem");
}
}
return 1;
tolua_lerror:
return tolua_AllToLua_cItemGrid_GetItem01(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetItem03
static int tolua_AllToLua_cItemGrid_GetItem03(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_SlotNum = ((int) tolua_tonumber(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetItem'", NULL);
#endif
{
cItem& tolua_ret = (cItem&) self->GetItem(a_SlotNum);
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"cItem");
}
}
return 1;
tolua_lerror:
return tolua_AllToLua_cItemGrid_GetItem02(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: SetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_SetItem00
static int tolua_AllToLua_cItemGrid_SetItem00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,4,&tolua_err) || !tolua_isusertype(tolua_S,4,"const cItem",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,5,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_X = ((int) tolua_tonumber(tolua_S,2,0));
int a_Y = ((int) tolua_tonumber(tolua_S,3,0));
const cItem* a_Item = ((const cItem*) tolua_tousertype(tolua_S,4,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetItem'", NULL);
#endif
{
self->SetItem(a_X,a_Y,*a_Item);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'SetItem'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: SetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_SetItem01
static int tolua_AllToLua_cItemGrid_SetItem01(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnumber(tolua_S,4,0,&tolua_err) ||
!tolua_isnumber(tolua_S,5,0,&tolua_err) ||
!tolua_isnumber(tolua_S,6,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,7,&tolua_err)
)
goto tolua_lerror;
else
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_X = ((int) tolua_tonumber(tolua_S,2,0));
int a_Y = ((int) tolua_tonumber(tolua_S,3,0));
short a_ItemType = ((short) tolua_tonumber(tolua_S,4,0));
char a_ItemCount = ((char) tolua_tonumber(tolua_S,5,0));
short a_ItemDamage = ((short) tolua_tonumber(tolua_S,6,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetItem'", NULL);
#endif
{
self->SetItem(a_X,a_Y,a_ItemType,a_ItemCount,a_ItemDamage);
}
}
return 0;
tolua_lerror:
return tolua_AllToLua_cItemGrid_SetItem00(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: SetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_SetItem02
static int tolua_AllToLua_cItemGrid_SetItem02(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,3,&tolua_err) || !tolua_isusertype(tolua_S,3,"const cItem",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_SlotNum = ((int) tolua_tonumber(tolua_S,2,0));
const cItem* a_Item = ((const cItem*) tolua_tousertype(tolua_S,3,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetItem'", NULL);
#endif
{
self->SetItem(a_SlotNum,*a_Item);
}
}
return 0;
tolua_lerror:
return tolua_AllToLua_cItemGrid_SetItem01(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: SetItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_SetItem03
static int tolua_AllToLua_cItemGrid_SetItem03(lua_State* tolua_S)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnumber(tolua_S,4,0,&tolua_err) ||
!tolua_isnumber(tolua_S,5,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,6,&tolua_err)
)
goto tolua_lerror;
else
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_SlotNum = ((int) tolua_tonumber(tolua_S,2,0));
short a_ItemType = ((short) tolua_tonumber(tolua_S,3,0));
char a_ItemCount = ((char) tolua_tonumber(tolua_S,4,0));
short a_ItemDamage = ((short) tolua_tonumber(tolua_S,5,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'SetItem'", NULL);
#endif
{
self->SetItem(a_SlotNum,a_ItemType,a_ItemCount,a_ItemDamage);
}
}
return 0;
tolua_lerror:
return tolua_AllToLua_cItemGrid_SetItem02(tolua_S);
}
#endif //#ifndef TOLUA_DISABLE
/* method: Clear of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_Clear00
static int tolua_AllToLua_cItemGrid_Clear00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Clear'", NULL);
#endif
{
self->Clear();
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'Clear'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: HowManyCanFit of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_HowManyCanFit00
static int tolua_AllToLua_cItemGrid_HowManyCanFit00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"const cItem",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
const cItem* a_ItemStack = ((const cItem*) tolua_tousertype(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'HowManyCanFit'", NULL);
#endif
{
int tolua_ret = (int) self->HowManyCanFit(*a_ItemStack);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'HowManyCanFit'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: AddItem of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_AddItem00
static int tolua_AllToLua_cItemGrid_AddItem00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"cItem",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
cItem* a_ItemStack = ((cItem*) tolua_tousertype(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItem'", NULL);
#endif
{
bool tolua_ret = (bool) self->AddItem(*a_ItemStack);
tolua_pushboolean(tolua_S,(bool)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'AddItem'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: AddItems of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_AddItems00
static int tolua_AllToLua_cItemGrid_AddItems00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cItemGrid",0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"cItems",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cItemGrid* self = (cItemGrid*) tolua_tousertype(tolua_S,1,0);
cItems* a_ItemStackList = ((cItems*) tolua_tousertype(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'AddItems'", NULL);
#endif
{
bool tolua_ret = (bool) self->AddItems(*a_ItemStackList);
tolua_pushboolean(tolua_S,(bool)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'AddItems'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetFirstEmptySlot of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetFirstEmptySlot00
static int tolua_AllToLua_cItemGrid_GetFirstEmptySlot00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetFirstEmptySlot'", NULL);
#endif
{
int tolua_ret = (int) self->GetFirstEmptySlot();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetFirstEmptySlot'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetLastEmptySlot of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetLastEmptySlot00
static int tolua_AllToLua_cItemGrid_GetLastEmptySlot00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetLastEmptySlot'", NULL);
#endif
{
int tolua_ret = (int) self->GetLastEmptySlot();
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetLastEmptySlot'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetNextEmptySlot of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetNextEmptySlot00
static int tolua_AllToLua_cItemGrid_GetNextEmptySlot00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
int a_StartFrom = ((int) tolua_tonumber(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetNextEmptySlot'", NULL);
#endif
{
int tolua_ret = (int) self->GetNextEmptySlot(a_StartFrom);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetNextEmptySlot'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: CopyToItems of class cItemGrid */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_CopyToItems00
static int tolua_AllToLua_cItemGrid_CopyToItems00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"const cItemGrid",0,&tolua_err) ||
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !tolua_isusertype(tolua_S,2,"cItems",0,&tolua_err)) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const cItemGrid* self = (const cItemGrid*) tolua_tousertype(tolua_S,1,0);
cItems* a_Items = ((cItems*) tolua_tousertype(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'CopyToItems'", NULL);
#endif
{
self->CopyToItems(*a_Items);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'CopyToItems'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: new of class cChestEntity */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cChestEntity_new00
static int tolua_AllToLua_cChestEntity_new00(lua_State* tolua_S)
@ -13632,8 +14326,8 @@ static int tolua_AllToLua_cChestEntity_GetSlot00(lua_State* tolua_S)
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetSlot'", NULL);
#endif
{
const cItem* tolua_ret = (const cItem*) self->GetSlot(a_Slot);
tolua_pushusertype(tolua_S,(void*)tolua_ret,"const cItem");
const cItem& tolua_ret = (const cItem&) self->GetSlot(a_Slot);
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"const cItem");
}
}
return 1;
@ -13713,6 +14407,38 @@ static int tolua_AllToLua_cChestEntity_UsedBy00(lua_State* tolua_S)
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetContents of class cChestEntity */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cChestEntity_GetContents00
static int tolua_AllToLua_cChestEntity_GetContents00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cChestEntity",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cChestEntity* self = (cChestEntity*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetContents'", NULL);
#endif
{
cItemGrid& tolua_ret = (cItemGrid&) self->GetContents();
tolua_pushusertype(tolua_S,(void*)&tolua_ret,"cItemGrid");
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetContents'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* get function: __cBlockEntityWindowOwner__ of class cChestEntity */
#ifndef TOLUA_DISABLE_tolua_get_cChestEntity___cBlockEntityWindowOwner__
static int tolua_get_cChestEntity___cBlockEntityWindowOwner__(lua_State* tolua_S)
@ -24046,6 +24772,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_function(tolua_S,"GetMaxDuration",tolua_AllToLua_cItem_GetMaxDuration00);
tolua_function(tolua_S,"DamageItem",tolua_AllToLua_cItem_DamageItem00);
tolua_function(tolua_S,"HasDuration",tolua_AllToLua_cItem_HasDuration00);
tolua_function(tolua_S,"IsStackableWith",tolua_AllToLua_cItem_IsStackableWith00);
tolua_function(tolua_S,"IsEnchantable",tolua_AllToLua_cItem_IsEnchantable00);
tolua_variable(tolua_S,"m_ItemType",tolua_get_cItem_m_ItemType,tolua_set_cItem_m_ItemType);
tolua_variable(tolua_S,"m_ItemCount",tolua_get_cItem_m_ItemCount,tolua_set_cItem_m_ItemCount);
@ -24062,6 +24789,29 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_function(tolua_S,"Add",tolua_AllToLua_cItems_Add01);
tolua_function(tolua_S,"Set",tolua_AllToLua_cItems_Set01);
tolua_endmodule(tolua_S);
tolua_cclass(tolua_S,"cItemGrid","cItemGrid","",NULL);
tolua_beginmodule(tolua_S,"cItemGrid");
tolua_function(tolua_S,"GetWidth",tolua_AllToLua_cItemGrid_GetWidth00);
tolua_function(tolua_S,"GetHeight",tolua_AllToLua_cItemGrid_GetHeight00);
tolua_function(tolua_S,"GetNumSlots",tolua_AllToLua_cItemGrid_GetNumSlots00);
tolua_function(tolua_S,"GetSlotNum",tolua_AllToLua_cItemGrid_GetSlotNum00);
tolua_function(tolua_S,"GetItem",tolua_AllToLua_cItemGrid_GetItem00);
tolua_function(tolua_S,"GetItem",tolua_AllToLua_cItemGrid_GetItem01);
tolua_function(tolua_S,"GetItem",tolua_AllToLua_cItemGrid_GetItem02);
tolua_function(tolua_S,"GetItem",tolua_AllToLua_cItemGrid_GetItem03);
tolua_function(tolua_S,"SetItem",tolua_AllToLua_cItemGrid_SetItem00);
tolua_function(tolua_S,"SetItem",tolua_AllToLua_cItemGrid_SetItem01);
tolua_function(tolua_S,"SetItem",tolua_AllToLua_cItemGrid_SetItem02);
tolua_function(tolua_S,"SetItem",tolua_AllToLua_cItemGrid_SetItem03);
tolua_function(tolua_S,"Clear",tolua_AllToLua_cItemGrid_Clear00);
tolua_function(tolua_S,"HowManyCanFit",tolua_AllToLua_cItemGrid_HowManyCanFit00);
tolua_function(tolua_S,"AddItem",tolua_AllToLua_cItemGrid_AddItem00);
tolua_function(tolua_S,"AddItems",tolua_AllToLua_cItemGrid_AddItems00);
tolua_function(tolua_S,"GetFirstEmptySlot",tolua_AllToLua_cItemGrid_GetFirstEmptySlot00);
tolua_function(tolua_S,"GetLastEmptySlot",tolua_AllToLua_cItemGrid_GetLastEmptySlot00);
tolua_function(tolua_S,"GetNextEmptySlot",tolua_AllToLua_cItemGrid_GetNextEmptySlot00);
tolua_function(tolua_S,"CopyToItems",tolua_AllToLua_cItemGrid_CopyToItems00);
tolua_endmodule(tolua_S);
#ifdef __cplusplus
tolua_cclass(tolua_S,"cChestEntity","cChestEntity","cBlockEntity",tolua_collect_cChestEntity);
#else
@ -24074,6 +24824,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_function(tolua_S,"GetSlot",tolua_AllToLua_cChestEntity_GetSlot00);
tolua_function(tolua_S,"SetSlot",tolua_AllToLua_cChestEntity_SetSlot00);
tolua_function(tolua_S,"UsedBy",tolua_AllToLua_cChestEntity_UsedBy00);
tolua_function(tolua_S,"GetContents",tolua_AllToLua_cChestEntity_GetContents00);
tolua_variable(tolua_S,"__cBlockEntityWindowOwner__",tolua_get_cChestEntity___cBlockEntityWindowOwner__,NULL);
tolua_endmodule(tolua_S);
#ifdef __cplusplus

View File

@ -1,6 +1,6 @@
/*
** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 04/06/13 23:17:21.
** Generated automatically by tolua++-1.0.92 on 04/10/13 21:35:56.
*/
/* Exported function */

View File

@ -24,7 +24,8 @@ class cRoot;
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ)
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ),
m_Contents(c_ChestWidth, c_ChestHeight)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
}
@ -34,7 +35,8 @@ cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World)
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World),
m_Contents(c_ChestWidth, c_ChestHeight)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
}
@ -60,14 +62,8 @@ void cChestEntity::Destroy(void)
{
// Drop items
cItems Pickups;
for (int i = 0; i < c_ChestHeight * c_ChestWidth; ++i)
{
if (!m_Content[i].IsEmpty())
{
Pickups.push_back(m_Content[i]);
m_Content[i].Empty();
}
}
m_Contents.CopyToItems(Pickups);
m_Contents.Clear();
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
}
@ -75,67 +71,6 @@ void cChestEntity::Destroy(void)
const cItem * cChestEntity::GetSlot(int a_Slot) const
{
if ((a_Slot > -1) && (a_Slot < c_ChestHeight * c_ChestWidth))
{
return &m_Content[a_Slot];
}
return NULL;
}
void cChestEntity::SetSlot(int a_Slot, const cItem & a_Item)
{
if ((a_Slot > -1) && (a_Slot < c_ChestHeight * c_ChestWidth))
{
m_Content[a_Slot] = a_Item;
}
}
void cChestEntity::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed)
{
// Calculate the total weight:
int TotalProbab = 1;
for (int i = 0; i < a_CountLootProbabs; i++)
{
TotalProbab += a_LootProbabs[i].m_Weight;
}
// Pick the loot items:
cNoise Noise(a_Seed);
for (int i = 0; i < a_NumSlots; i++)
{
int Rnd = (Noise.IntNoise1DInt(i) / 7);
int LootRnd = Rnd % TotalProbab;
Rnd >>= 8;
cItem CurrentLoot = cItem(E_ITEM_BOOK, 1, 0); // TODO: enchantment
for (int j = 0; j < a_CountLootProbabs; j++)
{
LootRnd -= a_LootProbabs[i].m_Weight;
if (LootRnd < 0)
{
CurrentLoot = a_LootProbabs[i].m_Item;
CurrentLoot.m_ItemCount = a_LootProbabs[i].m_MinAmount + (Rnd % (a_LootProbabs[i].m_MaxAmount - a_LootProbabs[i].m_MinAmount));
Rnd >>= 8;
break;
}
} // for j - a_LootProbabs[]
SetSlot(Rnd % ARRAYCOUNT(m_Content), CurrentLoot);
} // for i - NumSlots
}
bool cChestEntity::LoadFromJson(const Json::Value & a_Value)
{
m_PosX = a_Value.get("x", 0).asInt();
@ -164,16 +99,11 @@ void cChestEntity::SaveToJson(Json::Value & a_Value)
a_Value["y"] = m_PosY;
a_Value["z"] = m_PosZ;
unsigned int NumSlots = c_ChestHeight*c_ChestWidth;
Json::Value AllSlots;
for (unsigned int i = 0; i < NumSlots; i++)
for (int i = m_Contents.GetNumSlots(); i >= 0; i--)
{
Json::Value Slot;
const cItem * Item = GetSlot(i);
if (Item != NULL)
{
Item->GetJson(Slot);
}
m_Contents.GetItem(i).GetJson(Slot);
AllSlots.append(Slot);
}
a_Value["Slots"] = AllSlots;

View File

@ -3,6 +3,7 @@
#include "BlockEntity.h"
#include "UI/WindowOwner.h"
#include "ItemGrid.h"
@ -15,8 +16,6 @@ namespace Json
class cClientHandle;
class cServer;
class cItem;
class cLootProbab;
class cNBTData;
@ -41,14 +40,11 @@ public:
static const char * GetClassStatic() { return "cChestEntity"; }
const cItem * GetSlot(int a_Slot) const; // tolua_export
void SetSlot(int a_Slot, const cItem & a_Item ); // tolua_export
// tolua_begin
const cItem & GetSlot(int a_Slot) const { return m_Contents.GetItem(a_Slot); }
void SetSlot(int a_Slot, const cItem & a_Item ) { m_Contents.SetItem(a_Slot, a_Item); }
// tolua_end
/** Generates random loot from the specified loot probability table, with a chance of enchanted books added.
A total of a_NumSlots are taken by the loot
*/
void GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed);
bool LoadFromJson( const Json::Value& a_Value );
// cBlockEntity overrides:
@ -59,14 +55,16 @@ public:
/// Opens a new chest window for this chests. Scans for neighbors to open a double chest window, if appropriate.
void OpenNewWindow(void);
cItem * GetContents(void) { return m_Content; }
const cItemGrid & GetContents(void) const { return m_Contents; }
cItemGrid & GetContents(void) { return m_Contents; } // tolua_export
static const int c_ChestWidth = 9;
static const int c_ChestHeight = 3;
private:
cItem m_Content[c_ChestWidth * c_ChestHeight]; // TODO: replace this by a generic ItemGridHolder
cItemGrid m_Contents;
} ; // tolua_export

View File

@ -798,7 +798,7 @@ void cMineShaftCorridor::PlaceChest(cChunkDesc & a_ChunkDesc)
cNoise Noise(a_ChunkDesc.GetChunkX() ^ a_ChunkDesc.GetChunkZ());
int NumSlots = 3 + ((Noise.IntNoise3DInt(x, m_BoundingBox.p1.y, z) / 11) % 4);
int Seed = Noise.IntNoise2DInt(x, z);
ChestEntity->GenerateRandomLootWithBooks(LootProbab, ARRAYCOUNT(LootProbab), NumSlots, Seed);
ChestEntity->GetContents().GenerateRandomLootWithBooks(LootProbab, ARRAYCOUNT(LootProbab), NumSlots, Seed);
a_ChunkDesc.AddBlockEntity(ChestEntity);
}
}

View File

@ -8,6 +8,21 @@
bool cItem::IsStackableWith(const cItem & a_OtherStack)
{
if (a_OtherStack.m_ItemType != m_ItemType)
{
return false;
}
// TODO: match enchantments etc.
return true;
}
void cItem::GetJson( Json::Value & a_OutValue ) const
{
a_OutValue["ID"] = m_ItemType;

View File

@ -106,6 +106,9 @@ public:
}
inline bool HasDuration() { return GetMaxDuration() > 0; }
/// Returns true if this itemstack can stack with the specified stack (types match, enchantments etc.) ItemCounts are ignored!
bool IsStackableWith(const cItem & a_OtherStack);
// tolua_end
void GetJson( Json::Value & a_OutValue ) const;

349
source/ItemGrid.cpp Normal file
View File

@ -0,0 +1,349 @@
// ItemGrid.cpp
// Implements the cItemGrid class representing a storage for items in a XY grid (chests, dispensers, inventory etc.)
#include "Globals.h"
#include "ItemGrid.h"
#include "Items/ItemHandler.h"
#include "Noise.h"
cItemGrid::cItemGrid(int a_Width, int a_Height) :
m_Width(a_Width),
m_Height(a_Height),
m_NumSlots(a_Width * a_Height),
m_Items(new cItem[a_Width * a_Height])
{
}
cItemGrid::~cItemGrid()
{
delete[] m_Items;
}
int cItemGrid::GetSlotNum(int a_X, int a_Y) const
{
if (
(a_X < 0) || (a_X >= m_Width) ||
(a_Y < 0) || (a_Y >= m_Height)
)
{
LOGWARNING("%s: coords out of range: (%d, %d) in grid of size (%d, %d)",
__FUNCTION__, a_X, a_Y, m_Width, m_Height
);
return -1;
}
return a_X + m_Width * a_Y;
}
void cItemGrid::GetSlotCoords(int a_SlotNum, int & a_X, int & a_Y) const
{
if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots))
{
LOGWARNING("%s: SlotNum out of range: %d in grid of range %d",
__FUNCTION__, a_SlotNum, m_NumSlots
);
a_X = -1;
a_Y = -1;
return;
}
a_X = a_SlotNum % m_Width;
a_Y = a_SlotNum / m_Width;
}
const cItem & cItemGrid::GetItem(int a_X, int a_Y) const
{
return GetItem(GetSlotNum(a_X, a_Y));
}
cItem & cItemGrid::GetItem(int a_X, int a_Y)
{
return GetItem(GetSlotNum(a_X, a_Y));
}
const cItem & cItemGrid::GetItem(int a_SlotNum) const
{
if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots))
{
LOGWARNING("%s: Invalid slot number, %d out of %d slots",
__FUNCTION__, a_SlotNum, m_NumSlots
);
return m_Items[0];
}
return m_Items[a_SlotNum];
}
cItem & cItemGrid::GetItem(int a_SlotNum)
{
if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots))
{
LOGWARNING("%s: Invalid slot number, %d out of %d slots",
__FUNCTION__, a_SlotNum, m_NumSlots
);
return m_Items[0];
}
return m_Items[a_SlotNum];
}
void cItemGrid::SetItem(int a_X, int a_Y, const cItem & a_Item)
{
SetItem(GetSlotNum(a_X, a_Y), a_Item);
}
void cItemGrid::SetItem(int a_X, int a_Y, short a_ItemType, char a_ItemCount, short a_ItemDamage)
{
SetItem(GetSlotNum(a_X, a_Y), cItem(a_ItemType, a_ItemCount, a_ItemDamage));
}
void cItemGrid::SetItem(int a_SlotNum, const cItem & a_Item)
{
if ((a_SlotNum < 0) || (a_SlotNum >= m_NumSlots))
{
LOGWARNING("%s: Invalid slot number %d out of %d slots",
__FUNCTION__, a_SlotNum, m_NumSlots
);
return;
}
m_Items[a_SlotNum] = a_Item;
}
void cItemGrid::SetItem(int a_SlotNum, short a_ItemType, char a_ItemCount, short a_ItemDamage)
{
SetItem(a_SlotNum, cItem(a_ItemType, a_ItemCount, a_ItemDamage));
}
void cItemGrid::Clear(void)
{
for (int i = 0; i < m_NumSlots; i++)
{
m_Items[i].Empty();
}
}
int cItemGrid::HowManyCanFit(const cItem & a_ItemStack)
{
char NumLeft = a_ItemStack.m_ItemCount;
int MaxStack = ItemHandler(a_ItemStack.m_ItemType)->GetMaxStackSize();
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (m_Items[i].IsEmpty())
{
NumLeft -= MaxStack;
}
else if (m_Items[i].IsStackableWith(a_ItemStack))
{
NumLeft -= MaxStack - m_Items[i].m_ItemCount;
}
if (NumLeft <= 0)
{
// All items fit
return a_ItemStack.m_ItemCount;
}
} // for i - m_Items[]
return a_ItemStack.m_ItemCount - NumLeft;
}
bool cItemGrid::AddItem(cItem & a_ItemStack)
{
int NumLeft = a_ItemStack.m_ItemCount;
int MaxStack = ItemHandler(a_ItemStack.m_ItemType)->GetMaxStackSize();
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (m_Items[i].IsEmpty())
{
m_Items[i] = a_ItemStack;
m_Items[i].m_ItemCount = std::min(MaxStack, NumLeft);
NumLeft -= m_Items[i].m_ItemCount;
}
else if (m_Items[i].IsStackableWith(a_ItemStack))
{
int PrevCount = m_Items[i].m_ItemCount;
m_Items[i].m_ItemCount = std::min(MaxStack, PrevCount + NumLeft);
NumLeft -= m_Items[i].m_ItemCount - PrevCount;
}
if (NumLeft <= 0)
{
// All items fit
return true;
}
} // for i - m_Items[]
return (a_ItemStack.m_ItemCount > NumLeft);
}
bool cItemGrid::AddItems(cItems & a_ItemStackList)
{
bool res;
for (cItems::iterator itr = a_ItemStackList.begin(); itr != a_ItemStackList.end();)
{
res = AddItem(*itr) | res;
if (itr->IsEmpty())
{
itr = a_ItemStackList.erase(itr);
}
else
{
++itr;
}
}
return res;
}
int cItemGrid::GetFirstEmptySlot(void) const
{
return GetNextEmptySlot(-1);
}
int cItemGrid::GetLastEmptySlot(void) const
{
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (m_Items[i].IsEmpty())
{
return i;
}
}
return -1;
}
int cItemGrid::GetNextEmptySlot(int a_StartFrom) const
{
for (int i = a_StartFrom + 1; i < m_NumSlots; i++)
{
if (m_Items[i].IsEmpty())
{
return i;
}
}
return -1;
}
void cItemGrid::CopyToItems(cItems & a_Items) const
{
for (int i = 0; i < m_NumSlots; i++)
{
if (!m_Items[i].IsEmpty())
{
a_Items.push_back(m_Items[i]);
}
} // for i - m_Items[]
}
void cItemGrid::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed)
{
// Calculate the total weight:
int TotalProbab = 1;
for (int i = 0; i < a_CountLootProbabs; i++)
{
TotalProbab += a_LootProbabs[i].m_Weight;
}
// Pick the loot items:
cNoise Noise(a_Seed);
for (int i = 0; i < a_NumSlots; i++)
{
int Rnd = (Noise.IntNoise1DInt(i) / 7);
int LootRnd = Rnd % TotalProbab;
Rnd >>= 8;
cItem CurrentLoot = cItem(E_ITEM_BOOK, 1, 0); // TODO: enchantment
for (int j = 0; j < a_CountLootProbabs; j++)
{
LootRnd -= a_LootProbabs[i].m_Weight;
if (LootRnd < 0)
{
CurrentLoot = a_LootProbabs[i].m_Item;
CurrentLoot.m_ItemCount = a_LootProbabs[i].m_MinAmount + (Rnd % (a_LootProbabs[i].m_MaxAmount - a_LootProbabs[i].m_MinAmount));
Rnd >>= 8;
break;
}
} // for j - a_LootProbabs[]
SetItem(Rnd % m_NumSlots, CurrentLoot);
} // for i - NumSlots
}

94
source/ItemGrid.h Normal file
View File

@ -0,0 +1,94 @@
// ItemGrid.h
// Declares the cItemGrid class representing a storage for items in a XY grid (chests, dispensers, inventory etc.)
#pragma once
#include "Item.h"
// tolua_begin
class cItemGrid
{
public:
// tolua_end
cItemGrid(int a_Width, int a_Height);
~cItemGrid();
// tolua_begin
int GetWidth (void) const { return m_Width; }
int GetHeight (void) const { return m_Height; }
int GetNumSlots(void) const { return m_NumSlots; }
/// Converts XY coords into slot number; returns -1 on invalid coords
int GetSlotNum(int a_X, int a_Y) const;
// tolua_end
/// Converts slot number into XY coords; sets coords to -1 on invalid slot number. Exported in ManualBindings.cpp
void GetSlotCoords(int a_SlotNum, int & a_X, int & a_Y) const;
// tolua_begin
// Retrieve items by coords or slot number; Logs warning and returns the first item on invalid coords / slotnum
const cItem & GetItem(int a_X, int a_Y) const;
cItem & GetItem(int a_X, int a_Y);
const cItem & GetItem(int a_SlotNum) const;
cItem & GetItem(int a_SlotNum);
// Set item by coords or slot number; Logs warning and doesn't set on invalid coords / slotnum
void SetItem(int a_X, int a_Y, const cItem & a_Item);
void SetItem(int a_X, int a_Y, short a_ItemType, char a_ItemCount, short a_ItemDamage);
void SetItem(int a_SlotNum, const cItem & a_Item);
void SetItem(int a_SlotNum, short a_ItemType, char a_ItemCount, short a_ItemDamage);
/// Sets all items as empty
void Clear(void);
/// Returns number of items out of a_ItemStack that can fit in the storage
int HowManyCanFit(const cItem & a_ItemStack);
/// Adds as many items out of a_ItemStack as can fit; the rest is left in a_ItemStack; returns true if any items fit.
bool AddItem(cItem & a_ItemStack);
/// Same as AddItem, but works on an entire list of item stacks
bool AddItems(cItems & a_ItemStackList);
/// Returns the index of the first empty slot; -1 if all full
int GetFirstEmptySlot(void) const;
/// Returns the index of the last empty slot; -1 if all full
int GetLastEmptySlot(void) const;
/// Returns the index of the first empty slot following a_StartFrom (a_StartFrom is not checked)
int GetNextEmptySlot(int a_StartFrom) const;
/// Copies the contents into a cItems object
void CopyToItems(cItems & a_Items) const;
// tolua_end
/** Generates random loot from the specified loot probability table, with a chance of enchanted books added.
A total of a_NumSlots are taken by the loot.
Cannot export to Lua due to raw array a_LootProbabs.
*/
void GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed);
// tolua_begin
protected:
int m_Width;
int m_Height;
int m_NumSlots; // m_Width * m_Height, for easier validity checking in the access functions
cItem * m_Items; // x + m_Width * y
} ;
// tolua_end

View File

@ -968,6 +968,10 @@ static int copy_lua_values(lua_State * a_Source, lua_State * a_Destination, int
return 1;
}
static int tolua_cPlugin_Call(lua_State* tolua_S)
{
cPlugin_NewLua * self = (cPlugin_NewLua *) tolua_tousertype(tolua_S, 1, 0);
@ -1095,6 +1099,40 @@ static int tolua_get_HTTPRequest_FormData(lua_State* tolua_S)
static int Lua_ItemGrid_GetSlotCoords(lua_State * L)
{
tolua_Error tolua_err;
if (
!tolua_isusertype(L, 1, "const cItemGrid", 0, &tolua_err) ||
!tolua_isnumber (L, 2, 0, &tolua_err) ||
!tolua_isnoobj (L, 3, &tolua_err)
)
{
goto tolua_lerror;
}
const cItemGrid * self = (const cItemGrid *)tolua_tousertype(L, 1, 0);
int SlotNum = (int)tolua_tonumber(L, 2, 0);
if (self == NULL)
{
tolua_error(L, "invalid 'self' in function 'cItemGrid:GetSlotCoords'", NULL);
return 0;
}
int X, Y;
self->GetSlotCoords(SlotNum, X, Y);
tolua_pushnumber(L, (lua_Number)X);
tolua_pushnumber(L, (lua_Number)Y);
return 2;
tolua_lerror:
tolua_error(L, "#ferror in function 'FindKey'.", &tolua_err);
return 0;
}
void ManualBindings::Bind( lua_State* tolua_S )
{
tolua_beginmodule(tolua_S,NULL);
@ -1158,6 +1196,10 @@ void ManualBindings::Bind( lua_State* tolua_S )
tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cItemGrid");
tolua_function(tolua_S, "GetSlotCoords", Lua_ItemGrid_GetSlotCoords);
tolua_endmodule(tolua_S);
tolua_function(tolua_S, "md5", tolua_md5);
tolua_endmodule(tolua_S);

View File

@ -224,7 +224,7 @@ cSlotAreaChest::cSlotAreaChest(cChestEntity * a_Chest, cWindow & a_ParentWindow)
const cItem * cSlotAreaChest::GetSlot(int a_SlotNum, cPlayer & a_Player)
{
// a_SlotNum ranges from 0 to 26, use that to index the chest entity's inventory directly:
return m_Chest->GetSlot(a_SlotNum);
return &(m_Chest->GetSlot(a_SlotNum));
}
@ -259,11 +259,11 @@ const cItem * cSlotAreaDoubleChest::GetSlot(int a_SlotNum, cPlayer & a_Player)
// a_SlotNum ranges from 0 to 53, use that to index the correct chest's inventory:
if (a_SlotNum < 27)
{
return m_TopChest->GetSlot(a_SlotNum);
return &(m_TopChest->GetSlot(a_SlotNum));
}
else
{
return m_BottomChest->GetSlot(a_SlotNum - 27);
return &(m_BottomChest->GetSlot(a_SlotNum - 27));
}
}

View File

@ -11,7 +11,7 @@
#include "../SignEntity.h"
#include "../NoteEntity.h"
#include "../JukeboxEntity.h"
#include "../Item.h"
#include "../ItemGrid.h"
#include "../StringCompression.h"
#include "../Entity.h"
#include "../OSSupport/MakeDir.h"
@ -74,6 +74,24 @@ void cNBTChunkSerializer::AddItem(const cItem & a_Item, int a_Slot, const AStrin
void cNBTChunkSerializer::AddItemGrid(const cItemGrid & a_Grid, int a_BeginSlotNum)
{
int NumSlots = a_Grid.GetNumSlots();
for (int i = 0; i < NumSlots; i++)
{
const cItem & Item = a_Grid.GetItem(i);
if (Item.IsEmpty())
{
continue;
}
AddItem(Item, i + a_BeginSlotNum);
} // for i - chest slots[]
}
void cNBTChunkSerializer::AddBasicTileEntity(cBlockEntity * a_Entity, const char * a_EntityTypeID)
{
m_Writer.AddInt ("x", a_Entity->GetPosX());
@ -91,15 +109,7 @@ void cNBTChunkSerializer::AddChestEntity(cChestEntity * a_Entity)
m_Writer.BeginCompound("");
AddBasicTileEntity(a_Entity, "Chest");
m_Writer.BeginList("Items", TAG_Compound);
for (int i = 0; i < cChestEntity::c_ChestHeight * cChestEntity::c_ChestWidth; i++)
{
const cItem * Item = a_Entity->GetSlot(i);
if ((Item == NULL) || Item->IsEmpty())
{
continue;
}
AddItem(*Item, i);
} // for i - chest slots[]
AddItemGrid(a_Entity->GetContents());
m_Writer.EndList();
m_Writer.EndCompound();
}

View File

@ -31,6 +31,8 @@ class cMinecartWithChest;
class cMinecartWithFurnace;
class cMonster;
class cPickup;
class cItemGrid;
@ -71,6 +73,9 @@ protected:
/// Writes an item into the writer, if slot >= 0, adds the Slot tag. The compound is named as requested.
void AddItem(const cItem & a_Item, int a_Slot, const AString & a_CompoundName = "");
/// Writes an item grid into the writer; begins the stored slot numbers with a_BeginSlotNum
void AddItemGrid(const cItemGrid & a_Grid, int a_BeginSlotNum = 0);
// Block entities:
void AddBasicTileEntity(cBlockEntity * a_Entity, const char * a_EntityTypeID);
void AddChestEntity(cChestEntity * a_Entity);