Improve glass
[oweals/minetest.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, weight);
31         writeF1000(os, crackiness);
32         writeF1000(os, crumbliness);
33         writeF1000(os, cuttability);
34 }
35
36 void MaterialProperties::deSerialize(std::istream &is)
37 {
38         int version = readU8(is);
39         if(version != 0)
40                 throw SerializationError("unsupported MaterialProperties version");
41         diggability = (enum Diggability)readU8(is);
42         weight = readF1000(is);
43         crackiness = readF1000(is);
44         crumbliness = readF1000(is);
45         cuttability = readF1000(is);
46 }
47
48 DiggingProperties getDiggingProperties(u16 content, ToolDiggingProperties *tp,
49                 INodeDefManager *nodemgr)
50 {
51         assert(tp);
52         const MaterialProperties &mp = nodemgr->get(content).material;
53         if(mp.diggability == DIGGABLE_NOT)
54                 return DiggingProperties(false, 0, 0);
55         if(mp.diggability == DIGGABLE_CONSTANT)
56                 return DiggingProperties(true, mp.constant_time, 0);
57
58         float time = tp->basetime;
59         time += tp->dt_weight * mp.weight;
60         time += tp->dt_crackiness * mp.crackiness;
61         time += tp->dt_crumbliness * mp.crumbliness;
62         time += tp->dt_cuttability * mp.cuttability;
63         if(time < 0.2)
64                 time = 0.2;
65
66         float durability = tp->basedurability;
67         durability += tp->dd_weight * mp.weight;
68         durability += tp->dd_crackiness * mp.crackiness;
69         durability += tp->dd_crumbliness * mp.crumbliness;
70         durability += tp->dd_cuttability * mp.cuttability;
71         if(durability < 1)
72                 durability = 1;
73
74         float wear = 1.0 / durability;
75         u16 wear_i = wear/65535.;
76         return DiggingProperties(true, time, wear_i);
77 }
78