]> git.lizzy.rs Git - dragonfireclient.git/blob - src/materials.cpp
Fix craftitem aliases
[dragonfireclient.git] / src / materials.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "materials.h"
21 #include "mapnode.h"
22 #include "nodedef.h"
23 #include "tooldef.h"
24 #include "utility.h"
25
26 void MaterialProperties::serialize(std::ostream &os)
27 {
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);
36 }
37
38 void MaterialProperties::deSerialize(std::istream &is)
39 {
40         int version = readU8(is);
41         if(version != 0)
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);
50 }
51
52 DiggingProperties getDiggingProperties(const MaterialProperties *mp,
53                 const ToolDiggingProperties *tp, float time_from_last_punch)
54 {
55         if(mp->diggability == DIGGABLE_NOT)
56                 return DiggingProperties(false, 0, 0);
57         if(mp->diggability == DIGGABLE_CONSTANT)
58                 return DiggingProperties(true, mp->constant_time, 0);
59
60         float time = tp->basetime;
61         time += tp->dt_weight * mp->weight;
62         time += tp->dt_crackiness * mp->crackiness;
63         time += tp->dt_crumbliness * mp->crumbliness;
64         time += tp->dt_cuttability * mp->cuttability;
65         if(time < 0.2)
66                 time = 0.2;
67
68         float durability = tp->basedurability;
69         durability += tp->dd_weight * mp->weight;
70         durability += tp->dd_crackiness * mp->crackiness;
71         durability += tp->dd_crumbliness * mp->crumbliness;
72         durability += tp->dd_cuttability * mp->cuttability;
73         if(durability < 1)
74                 durability = 1;
75         
76         if(time_from_last_punch < tp->full_punch_interval){
77                 float f = time_from_last_punch / tp->full_punch_interval;
78                 time /= f;
79                 durability /= f;
80         }
81
82         float wear = 1.0 / durability;
83         u16 wear_i = 65535.*wear;
84         return DiggingProperties(true, time, wear_i);
85 }
86
87 DiggingProperties getDiggingProperties(const MaterialProperties *mp,
88                 const ToolDiggingProperties *tp)
89 {
90         return getDiggingProperties(mp, tp, 1000000);
91 }
92
93 DiggingProperties getDiggingProperties(u16 content,
94                 const ToolDiggingProperties *tp, INodeDefManager *nodemgr)
95 {
96         const MaterialProperties &mp = nodemgr->get(content).material;
97         return getDiggingProperties(&mp, tp);
98 }
99
100 HittingProperties getHittingProperties(const MaterialProperties *mp,
101                 const ToolDiggingProperties *tp, float time_from_last_punch)
102 {
103         DiggingProperties digprop = getDiggingProperties(mp, tp,
104                         time_from_last_punch);
105         
106         // If digging time would be 1 second, 2 hearts go in 1 second.
107         s16 hp = 2.0 * 2.0 / digprop.time + 0.5;
108         // Wear is the same as for digging a single node
109         s16 wear = (float)digprop.wear + 0.5;
110
111         return HittingProperties(hp, wear);
112 }
113