3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "materials.h"
26 void MaterialProperties::serialize(std::ostream &os)
28 writeU8(os, 0); // version
29 writeU8(os, diggability);
30 writeF1000(os, constant_time);
31 writeF1000(os, weight);
32 writeF1000(os, crackiness);
33 writeF1000(os, crumbliness);
34 writeF1000(os, cuttability);
35 writeF1000(os, flammability);
38 void MaterialProperties::deSerialize(std::istream &is)
40 int version = readU8(is);
42 throw SerializationError("unsupported MaterialProperties version");
43 diggability = (enum Diggability)readU8(is);
44 constant_time = readF1000(is);
45 weight = readF1000(is);
46 crackiness = readF1000(is);
47 crumbliness = readF1000(is);
48 cuttability = readF1000(is);
49 flammability = readF1000(is);
52 DiggingProperties getDiggingProperties(u16 content, ToolDiggingProperties *tp,
53 INodeDefManager *nodemgr)
56 const MaterialProperties &mp = nodemgr->get(content).material;
57 if(mp.diggability == DIGGABLE_NOT)
58 return DiggingProperties(false, 0, 0);
59 if(mp.diggability == DIGGABLE_CONSTANT)
60 return DiggingProperties(true, mp.constant_time, 0);
62 float time = tp->basetime;
63 time += tp->dt_weight * mp.weight;
64 time += tp->dt_crackiness * mp.crackiness;
65 time += tp->dt_crumbliness * mp.crumbliness;
66 time += tp->dt_cuttability * mp.cuttability;
70 float durability = tp->basedurability;
71 durability += tp->dd_weight * mp.weight;
72 durability += tp->dd_crackiness * mp.crackiness;
73 durability += tp->dd_crumbliness * mp.crumbliness;
74 durability += tp->dd_cuttability * mp.cuttability;
78 float wear = 1.0 / durability;
79 u16 wear_i = 65535.*wear;
80 return DiggingProperties(true, time, wear_i);