X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmaterials.cpp;h=3f88f9d455ab0f3b4b5c6d5a65b7e88fa83eaade;hb=08a10b8a6a77eb729d609979ee822134d5d7a645;hp=8c23056f241a2cb6d518defbd981659e719ef9f0;hpb=63fe487d933f00123bb97d699c98e0c5248a8060;p=dragonfireclient.git diff --git a/src/materials.cpp b/src/materials.cpp index 8c23056f2..3f88f9d45 100644 --- a/src/materials.cpp +++ b/src/materials.cpp @@ -1,116 +1,113 @@ -#include "materials.h" +/* +Minetest-c55 +Copyright (C) 2011 celeron55, Perttu Ahola -#define MATERIAL_PROPERTIES_COUNT 256 +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. -// These correspond to the CONTENT_* constants -MaterialProperties g_material_properties[MATERIAL_PROPERTIES_COUNT]; +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. -bool g_material_properties_initialized = false; +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ -void setStoneLikeDiggingProperties(u8 material, float toughness) -{ - g_material_properties[material].setDiggingProperties("", - DiggingProperties(true, 15.0*toughness, 0)); - - g_material_properties[material].setDiggingProperties("WPick", - DiggingProperties(true, 1.3*toughness, 65535./30.*toughness)); - g_material_properties[material].setDiggingProperties("STPick", - DiggingProperties(true, 0.75*toughness, 65535./100.*toughness)); - g_material_properties[material].setDiggingProperties("SteelPick", - DiggingProperties(true, 0.50*toughness, 65535./333.*toughness)); - - /*g_material_properties[material].setDiggingProperties("MesePick", - DiggingProperties(true, 0.0*toughness, 65535./20.*toughness));*/ -} +#include "materials.h" +#include "mapnode.h" +#include "nodedef.h" +#include "tooldef.h" +#include "utility.h" -void setDirtLikeDiggingProperties(u8 material, float toughness) +void MaterialProperties::serialize(std::ostream &os) { - g_material_properties[material].setDiggingProperties("", - DiggingProperties(true, 0.75*toughness, 0)); - - g_material_properties[material].setDiggingProperties("WShovel", - DiggingProperties(true, 0.4*toughness, 65535./50.*toughness)); - g_material_properties[material].setDiggingProperties("STShovel", - DiggingProperties(true, 0.2*toughness, 65535./150.*toughness)); - g_material_properties[material].setDiggingProperties("SteelShovel", - DiggingProperties(true, 0.15*toughness, 65535./400.*toughness)); + writeU8(os, 0); // version + writeU8(os, diggability); + writeF1000(os, constant_time); + writeF1000(os, weight); + writeF1000(os, crackiness); + writeF1000(os, crumbliness); + writeF1000(os, cuttability); + writeF1000(os, flammability); } -void setWoodLikeDiggingProperties(u8 material, float toughness) +void MaterialProperties::deSerialize(std::istream &is) { - g_material_properties[material].setDiggingProperties("", - DiggingProperties(true, 3.0*toughness, 0)); - - g_material_properties[material].setDiggingProperties("WAxe", - DiggingProperties(true, 1.5*toughness, 65535./30.*toughness)); - g_material_properties[material].setDiggingProperties("STAxe", - DiggingProperties(true, 0.75*toughness, 65535./100.*toughness)); - g_material_properties[material].setDiggingProperties("SteelAxe", - DiggingProperties(true, 0.5*toughness, 65535./333.*toughness)); + int version = readU8(is); + if(version != 0) + throw SerializationError("unsupported MaterialProperties version"); + diggability = (enum Diggability)readU8(is); + constant_time = readF1000(is); + weight = readF1000(is); + crackiness = readF1000(is); + crumbliness = readF1000(is); + cuttability = readF1000(is); + flammability = readF1000(is); } -void initializeMaterialProperties() +DiggingProperties getDiggingProperties(const MaterialProperties *mp, + const ToolDiggingProperties *tp, float time_from_last_punch) { - /* - Now, the g_material_properties array is already initialized - by the constructors to such that no digging is possible. - - Add some digging properties to them. - */ - - setStoneLikeDiggingProperties(CONTENT_STONE, 1.0); - setStoneLikeDiggingProperties(CONTENT_SANDSTONE, 1.0); - setStoneLikeDiggingProperties(CONTENT_BRICK, 3.0); - setStoneLikeDiggingProperties(CONTENT_MESE, 0.5); - setStoneLikeDiggingProperties(CONTENT_COALSTONE, 1.5); - setStoneLikeDiggingProperties(CONTENT_FURNACE, 3.0); - setStoneLikeDiggingProperties(CONTENT_COBBLE, 1.0); - setStoneLikeDiggingProperties(CONTENT_STEEL, 5.0); - - setDirtLikeDiggingProperties(CONTENT_MUD, 1.0); - setDirtLikeDiggingProperties(CONTENT_GRASS, 1.0); - setDirtLikeDiggingProperties(CONTENT_GRASS_FOOTSTEPS, 1.0); - setDirtLikeDiggingProperties(CONTENT_SAND, 1.0); - setDirtLikeDiggingProperties(CONTENT_CLAY, 1.0); - - setWoodLikeDiggingProperties(CONTENT_TREE, 1.0); - setWoodLikeDiggingProperties(CONTENT_LEAVES, 0.15); - setWoodLikeDiggingProperties(CONTENT_CACTUS, 0.75); - setWoodLikeDiggingProperties(CONTENT_GLASS, 0.15); - setWoodLikeDiggingProperties(CONTENT_FENCE, 0.75); - setWoodLikeDiggingProperties(CONTENT_WOOD, 0.75); - setWoodLikeDiggingProperties(CONTENT_CHEST, 1.0); - - g_material_properties[CONTENT_SIGN_WALL].setDiggingProperties("", - DiggingProperties(true, 0.5, 0)); - g_material_properties[CONTENT_TORCH].setDiggingProperties("", - DiggingProperties(true, 0.0, 0)); + if(mp->diggability == DIGGABLE_NOT) + return DiggingProperties(false, 0, 0); + if(mp->diggability == DIGGABLE_CONSTANT) + return DiggingProperties(true, mp->constant_time, 0); + + float time = tp->basetime; + time += tp->dt_weight * mp->weight; + time += tp->dt_crackiness * mp->crackiness; + time += tp->dt_crumbliness * mp->crumbliness; + time += tp->dt_cuttability * mp->cuttability; + if(time < 0.2) + time = 0.2; + + float durability = tp->basedurability; + durability += tp->dd_weight * mp->weight; + durability += tp->dd_crackiness * mp->crackiness; + durability += tp->dd_crumbliness * mp->crumbliness; + durability += tp->dd_cuttability * mp->cuttability; + if(durability < 1) + durability = 1; - /* - Add MesePick to everything - */ - for(u16 i=0; ifull_punch_interval){ + float f = time_from_last_punch / tp->full_punch_interval; + time /= f; + durability /= f; } - g_material_properties_initialized = true; + float wear = 1.0 / durability; + u16 wear_i = 65535.*wear; + return DiggingProperties(true, time, wear_i); } -MaterialProperties * getMaterialProperties(u8 material) +DiggingProperties getDiggingProperties(const MaterialProperties *mp, + const ToolDiggingProperties *tp) { - assert(g_material_properties_initialized); - return &g_material_properties[material]; + return getDiggingProperties(mp, tp, 1000000); } -DiggingProperties getDiggingProperties(u8 material, const std::string &tool) +DiggingProperties getDiggingProperties(u16 content, + const ToolDiggingProperties *tp, INodeDefManager *nodemgr) { - MaterialProperties *mprop = getMaterialProperties(material); - if(mprop == NULL) - // Not diggable - return DiggingProperties(); + const MaterialProperties &mp = nodemgr->get(content).material; + return getDiggingProperties(&mp, tp); +} + +HittingProperties getHittingProperties(const MaterialProperties *mp, + const ToolDiggingProperties *tp, float time_from_last_punch) +{ + DiggingProperties digprop = getDiggingProperties(mp, tp, + time_from_last_punch); - return mprop->getDiggingProperties(tool); + // If digging time would be 1 second, 2 hearts go in 1 second. + s16 hp = 2.0 * 2.0 / digprop.time + 0.5; + // Wear is the same as for digging a single node + s16 wear = (float)digprop.wear + 0.5; + + return HittingProperties(hp, wear); }