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.
22 #include "itemdef.h" // For itemgroup_get()
25 void ToolCapabilities::serialize(std::ostream &os) const
27 writeU8(os, 0); // version
28 writeF1000(os, full_punch_interval);
29 writeS16(os, max_drop_level);
30 writeU32(os, groupcaps.size());
31 for(std::map<std::string, ToolGroupCap>::const_iterator
32 i = groupcaps.begin(); i != groupcaps.end(); i++){
33 const std::string *name = &i->first;
34 const ToolGroupCap *cap = &i->second;
35 os<<serializeString(*name);
36 writeF1000(os, cap->maxwear);
37 writeF1000(os, cap->maxlevel);
38 writeU32(os, cap->times.size());
39 for(std::map<int, float>::const_iterator
40 i = cap->times.begin(); i != cap->times.end(); i++){
41 writeS16(os, i->first);
42 writeF1000(os, i->second);
47 void ToolCapabilities::deSerialize(std::istream &is)
49 int version = readU8(is);
50 if(version != 0) throw SerializationError(
51 "unsupported ToolCapabilities version");
52 full_punch_interval = readF1000(is);
53 max_drop_level = readS16(is);
55 u32 groupcaps_size = readU32(is);
56 for(u32 i=0; i<groupcaps_size; i++){
57 std::string name = deSerializeString(is);
59 cap.maxwear = readF1000(is);
60 cap.maxlevel = readF1000(is);
61 u32 times_size = readU32(is);
62 for(u32 i=0; i<times_size; i++){
63 int level = readS16(is);
64 float time = readF1000(is);
65 cap.times[level] = time;
67 groupcaps[name] = cap;
71 DigParams getDigParams(const std::map<std::string, int> &groups,
72 const ToolCapabilities *tp, float time_from_last_punch)
74 //infostream<<"getDigParams"<<std::endl;
75 /* Check group dig_immediate */
76 switch(itemgroup_get(groups, "dig_immediate")){
78 //infostream<<"dig_immediate=1"<<std::endl;
79 return DigParams(true, 0.0, 0);
81 //infostream<<"dig_immediate=2"<<std::endl;
82 return DigParams(true, 1.0, 0);
87 // Values to be returned (with a bit of conversion)
88 bool result_diggable = false;
89 float result_time = 0.0;
90 float result_wear = 0.0;
92 int level = itemgroup_get(groups, "level");
93 //infostream<<"level="<<level<<std::endl;
94 for(std::map<std::string, ToolGroupCap>::const_iterator
95 i = tp->groupcaps.begin(); i != tp->groupcaps.end(); i++){
96 const std::string &name = i->first;
97 //infostream<<"group="<<name<<std::endl;
98 const ToolGroupCap &cap = i->second;
99 int rating = itemgroup_get(groups, name);
101 bool time_exists = cap.getTime(rating, &time);
102 if(!result_diggable || time < result_time){
103 if(cap.maxlevel > level && time_exists){
104 result_diggable = true;
106 int leveldiff = cap.maxlevel - level;
107 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
111 //infostream<<"result_diggable="<<result_diggable<<std::endl;
112 //infostream<<"result_time="<<result_time<<std::endl;
113 //infostream<<"result_wear="<<result_wear<<std::endl;
115 if(time_from_last_punch < tp->full_punch_interval){
116 float f = time_from_last_punch / tp->full_punch_interval;
117 //infostream<<"f="<<f<<std::endl;
122 u16 wear_i = 65535.*result_wear;
123 return DigParams(result_diggable, result_time, wear_i);
126 DigParams getDigParams(const std::map<std::string, int> &groups,
127 const ToolCapabilities *tp)
129 return getDigParams(groups, tp, 1000000);
132 HitParams getHitParams(const std::map<std::string, int> &groups,
133 const ToolCapabilities *tp, float time_from_last_punch)
135 DigParams digprop = getDigParams(groups, tp,
136 time_from_last_punch);
138 // If digging time would be 1 second, 8 half-hearts go in 1 second.
141 hp = 8.0 / digprop.time;
142 // Wear is the same as for digging a single node
143 s16 wear = (float)digprop.wear;
145 return HitParams(hp, wear);
148 HitParams getHitParams(const std::map<std::string, int> &groups,
149 const ToolCapabilities *tp)
151 return getHitParams(groups, tp, 1000000);