Skip to content

Commit

Permalink
Add color and palette properties to items, and special metadata keys
Browse files Browse the repository at this point in the history
  • Loading branch information
juhdanad authored and sofar committed Apr 9, 2017
1 parent 5c52b3c commit 0e31c47
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/itemdef.cpp
Expand Up @@ -82,6 +82,8 @@ ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
sound_place = def.sound_place;
sound_place_failed = def.sound_place_failed;
range = def.range;
palette_image = def.palette_image;
color = def.color;
return *this;
}

Expand All @@ -104,6 +106,8 @@ void ItemDefinition::reset()
description = "";
inventory_image = "";
wield_image = "";
palette_image = "";
color = video::SColor(0xFFFFFFFF);
wield_scale = v3f(1.0, 1.0, 1.0);
stack_max = 99;
usable = false;
Expand Down Expand Up @@ -153,6 +157,8 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
writeF1000(os, range);
os << serializeString(sound_place_failed.name);
writeF1000(os, sound_place_failed.gain);
os << serializeString(palette_image);
writeU32(os, color.color);
}

void ItemDefinition::deSerialize(std::istream &is)
Expand Down Expand Up @@ -209,6 +215,8 @@ void ItemDefinition::deSerialize(std::istream &is)
try {
sound_place_failed.name = deSerializeString(is);
sound_place_failed.gain = readF1000(is);
palette_image = deSerializeString(is);
color.set(readU32(is));
} catch(SerializationError &e) {};
}

Expand All @@ -225,10 +233,12 @@ class CItemDefManager: public IWritableItemDefManager
{
video::ITexture *inventory_texture;
scene::IMesh *wield_mesh;
Palette *palette;

ClientCached():
inventory_texture(NULL),
wield_mesh(NULL)
wield_mesh(NULL),
palette(NULL)
{}
};
#endif
Expand Down Expand Up @@ -337,6 +347,7 @@ class CItemDefManager: public IWritableItemDefManager

scene::IMesh *mesh = getItemMesh(client, item);
cc->wield_mesh = mesh;
cc->palette = tsrc->getPalette(def.palette_image);

// Put in cache
m_clientcached.set(name, cc);
Expand Down Expand Up @@ -398,6 +409,34 @@ class CItemDefManager: public IWritableItemDefManager
return NULL;
return cc->wield_mesh;
}

// Get item palette
virtual Palette* getPalette(const std::string &name,
Client *client) const
{
ClientCached *cc = getClientCached(name, client);
if(!cc)
return NULL;
return cc->palette;
}

virtual video::SColor getItemstackColor(const ItemStack &stack,
Client *client) const
{
// Look for direct color definition
const std::string &colorstring = stack.metadata.getString("color", 0);
video::SColor directcolor;
if ((colorstring != "")
&& parseColorString(colorstring, directcolor, true))
return directcolor;
// See if there is a palette
Palette *palette = getPalette(stack.name, client);
const std::string &index = stack.metadata.getString("paletteindex", 0);
if ((palette != NULL) && (index != ""))
return (*palette)[mystoi(index, 0, 255)];
// Fallback color
return get(stack.name).color;
}
#endif
void clear()
{
Expand Down
13 changes: 13 additions & 0 deletions src/itemdef.h
Expand Up @@ -30,6 +30,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class IGameDef;
class Client;
struct ToolCapabilities;
#ifndef SERVER
#include "client/tile.h"
struct ItemStack;
#endif

/*
Base item definition
Expand Down Expand Up @@ -57,6 +61,8 @@ struct ItemDefinition
*/
std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
std::string palette_image; // If specified, the item will be colorized based on this
video::SColor color; // The fallback color of the node.
v3f wield_scale;

/*
Expand Down Expand Up @@ -112,6 +118,13 @@ class IItemDefManager
// Get item wield mesh
virtual scene::IMesh* getWieldMesh(const std::string &name,
Client *client) const=0;
// Get item palette
virtual Palette* getPalette(const std::string &name,
Client *client) const = 0;
// Returns the base color of an item stack: the color of all
// tiles that do not define their own color.
virtual video::SColor getItemstackColor(const ItemStack &stack,
Client *client) const = 0;
#endif

virtual void serialize(std::ostream &os, u16 protocol_version)=0;
Expand Down
6 changes: 6 additions & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -58,6 +58,12 @@ ItemDefinition read_item_definition(lua_State* L,int index,
getstringfield(L, index, "description", def.description);
getstringfield(L, index, "inventory_image", def.inventory_image);
getstringfield(L, index, "wield_image", def.wield_image);
getstringfield(L, index, "palette", def.palette_image);

// Read item color.
lua_getfield(L, index, "color");
read_color(L, -1, &def.color);
lua_pop(L, 1);

lua_getfield(L, index, "wield_scale");
if(lua_istable(L, -1)){
Expand Down

0 comments on commit 0e31c47

Please sign in to comment.