Skip to content

Commit

Permalink
[Lua API] Added item definition property 'effective_on'. Fixed #143.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 13, 2020
1 parent 9df06a4 commit 1485c15
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 7 deletions.
13 changes: 13 additions & 0 deletions docs/lua-api-item.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ mod:item {

## Attributes

### `effective_on`

List of groups or block string IDs on which that item is effective when mining.

Example:
```lua
effective_on = {
"group:om_material_dirt",
"group:om_material_sand"
}
```

### `groups`

Groups of the item. They can be used in recipes, and can also filter Lua-defined inventory widgets.
Expand All @@ -30,6 +42,7 @@ Engine groups always start with `om_` prefix. If you create your own groups, ple
Available engine groups:

- `om_fuel`: used in `default:furnace` and `MouseWidgetItem`, the value represents the burn time
- `om_material_dirt/sand/wood/stone`: these groups are not used in the engine, but it's nice to use them if you can

### `harvest_capability`

Expand Down
27 changes: 26 additions & 1 deletion mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ mod:block {
id = "dirt",
name = "Dirt",
tiles = "dirt.png",
groups = {
om_material_dirt = 1,
},
}

mod:block {
Expand All @@ -45,6 +48,9 @@ mod:block {
name = "Grass",
tiles = {"grass_block_top.png", "dirt.png", "grass_block_side.png"},
color_multiplier = {129, 191, 91, 255},
groups = {
om_material_dirt = 1,
},

item_drop = {
id = mod:id()..":dirt",
Expand All @@ -68,6 +74,9 @@ mod:block {
id = "oak_wood",
name = "Oak Wood",
tiles = {"oak_log_top.png", "oak_log.png"},
groups = {
om_material_wood = 1,
}
}

mod:block {
Expand All @@ -86,6 +95,9 @@ mod:block {
id = "sand",
name = "Sand",
tiles = "sand.png",
groups = {
om_material_sand = 1
}
}

mod:block {
Expand Down Expand Up @@ -130,6 +142,7 @@ mod:block {
hardness = 1,

groups = {
om_material_wood = 1,
default_planks = 1
}
}
Expand Down Expand Up @@ -198,6 +211,9 @@ mod:block {
id = "oak_slab",
name = "Oak Wood Slab",
tiles = "oak_planks.png",
groups = {
om_material_wood = 1,
},

draw_type = "boundingbox",
is_opaque = false,
Expand Down Expand Up @@ -238,6 +254,9 @@ mod:block {
id = "soul_sand",
name = "Soul Sand",
tiles = "soul_sand.png",
groups = {
om_material_sand = 1
}
}

mod:block {
Expand All @@ -257,6 +276,7 @@ mod:block {
id = "cactus",
name = "Cactus",
tiles = {"cactus_top.png", "cactus_bottom.png", "cactus_side.png"},
hardness = 0,

draw_type = "cactus",
bounding_box = {1/16, 1/16, 0, 14/16, 14/16, 1};
Expand Down Expand Up @@ -318,7 +338,6 @@ mod:block {
id = "redstone_lamp",
name = "Redstone Lamp",
tiles = "redstone_lamp_off.png",
harvest_requirements = 1,

states = {
{ is_light_source = true, tiles = "redstone_lamp_on.png" }
Expand All @@ -335,6 +354,9 @@ mod:block {
name = "Farmland",
tiles = {"farmland_dry.png", "dirt.png", "dirt.png"},
is_opaque = false,
groups = {
om_material_dirt = 1,
},

states = {
{ alt_tiles = {"farmland_wet.png", "dirt.png", "dirt.png"} },
Expand All @@ -349,6 +371,9 @@ mod:block {
name = "Grass Path",
tiles = {"grass_path_top.png", "dirt.png", "grass_path_side.png"},
is_opaque = false,
groups = {
om_material_dirt = 1,
},

draw_type = "boundingbox",
bounding_box = {0, 0, 0, 1, 1, 15 / 16},
Expand Down
5 changes: 4 additions & 1 deletion mods/default/blocks/door.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ mod:block {
name = "Wooden Door",
tiles = "door_wood_upper.png",
inventory_image = "door_wood.png",
groups = {ci_ignore = 1},
groups = {
ci_ignore = 1,
om_material_wood = 1
},

is_rotatable = true,
is_opaque = false,
Expand Down
3 changes: 3 additions & 0 deletions mods/default/blocks/workbench.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ mod:block {
id = "workbench",
name = "Workbench",
tiles = {"crafting_table_top.png", "crafting_table_top.png", "crafting_table_front.png", "crafting_table_side.png"},
groups = {
om_material_wood = 1
},

hardness = 1,
harvest_requirements = 4,
Expand Down
9 changes: 9 additions & 0 deletions mods/default/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ function register_tool(name, material, mining_speed, harvest_capability)
end
end
elseif name == "shovel" then
tool_def.effective_on = {
"group:om_material_dirt",
"group:om_material:sand"
}

tool_def.on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:grass_path")
end
end
elseif name == "axe" then
tool_def.effective_on = {
"group:om_material_wood"
}
end

mod:item(tool_def)
Expand Down
10 changes: 9 additions & 1 deletion source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ void BlockCursor::update(const Hotbar &hotbar) {
m_currentTool = &currentStack;
}
else {
timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed());
bool isEffective = false;
for (auto &it : currentStack.item().effectiveOn()) {
if (m_currentBlock->block().hasGroup(it)) {
isEffective = true;
break;
}
}

timeToBreak = m_currentBlock->timeToBreak(currentStack.item().harvestCapability(), currentStack.item().miningSpeed(), isEffective);

if (ticks > m_animationStart + timeToBreak * 1000) {
m_world.setBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z, 0);
Expand Down
6 changes: 4 additions & 2 deletions source/common/inventory/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ Item::Item(u32 id, const TilesDef &tiles, const std::string &stringID, const std

void Item::serialize(sf::Packet &packet) const {
packet << m_id << m_tiles << m_stringID << m_label << m_isBlock
<< m_miningSpeed << m_harvestCapability << m_groups << m_canBeActivated;
<< m_miningSpeed << m_harvestCapability << m_groups << m_canBeActivated
<< m_effectiveOn;
}

void Item::deserialize(sf::Packet &packet) {
packet >> m_id >> m_tiles >> m_stringID >> m_label >> m_isBlock
>> m_miningSpeed >> m_harvestCapability >> m_groups >> m_canBeActivated;
>> m_miningSpeed >> m_harvestCapability >> m_groups >> m_canBeActivated
>> m_effectiveOn;
}

// Please update 'docs/lua-api-cpp.md' if you change this
Expand Down
5 changes: 5 additions & 0 deletions source/common/inventory/Item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Item : public gk::ISerializable {

bool canBeActivated() const { return m_canBeActivated; }

const std::vector<std::string> &effectiveOn() const { return m_effectiveOn; }
void addEffectiveBlock(const std::string &blockID) { m_effectiveOn.emplace_back(blockID); }

static void initUsertype(sol::state &lua);

protected:
Expand All @@ -91,6 +94,8 @@ class Item : public gk::ISerializable {
float m_miningSpeed = 1;

std::unordered_map<std::string, u16> m_groups;

std::vector<std::string> m_effectiveOn;
};

#endif // ITEM_HPP_
4 changes: 2 additions & 2 deletions source/common/world/BlockState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class BlockState : public gk::ISerializable {

u16 id() const { return m_id; }

float timeToBreak(u8 harvestCapability, float miningSpeed) const {
if ((harvestRequirements() & harvestCapability) || (harvestCapability == 0 && harvestRequirements() == 0))
float timeToBreak(u8 harvestCapability, float miningSpeed, bool isEffective) const {
if (isEffective || (harvestRequirements() & harvestCapability) || (harvestCapability == 0 && harvestRequirements() == 0))
return 1.5 * m_hardness / miningSpeed;
else
return 5 * m_hardness;
Expand Down
12 changes: 12 additions & 0 deletions source/server/lua/loader/LuaItemLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,17 @@ void LuaItemLoader::loadItem(const sol::table &table) const {
else
gkError() << "For item" << stringID << ": 'groups' should be a table";
}

sol::object effectiveOnObject = table["effective_on"];
if (effectiveOnObject.valid()) {
if (effectiveOnObject.get_type() == sol::type::table) {
sol::table effectiveOnTable = effectiveOnObject.as<sol::table>();
for (auto &effectiveOnObject : effectiveOnTable) {
item.addEffectiveBlock(effectiveOnObject.second.as<std::string>());
}
}
else
gkError() << "For item" << stringID << ": 'effective_on' should be a table";
}
}

0 comments on commit 1485c15

Please sign in to comment.