aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_base.cpp
diff options
context:
space:
mode:
authorTHANOS SIOURDAKIS <siourdakisthanos@gmail.com>2020-03-31 16:40:14 +0000
committersfan5 <sfan5@live.de>2020-04-03 23:15:52 +0200
commit965e2028dea964baa52b8fffa0d8e30323ad8ace (patch)
treeb611e5671e25dd62cd76962e1dc26718ca719eba /src/script/cpp_api/s_base.cpp
parentbe0200b2157f1dc444ec2d07e1784aec1375127a (diff)
downloadminetest-965e2028dea964baa52b8fffa0d8e30323ad8ace.tar.gz
minetest-965e2028dea964baa52b8fffa0d8e30323ad8ace.tar.bz2
minetest-965e2028dea964baa52b8fffa0d8e30323ad8ace.zip
Translated using Weblate (Greek)
Currently translated at 1.7% (22 of 1288 strings)
Diffstat (limited to 'src/script/cpp_api/s_base.cpp')
0 files changed, 0 insertions, 0 deletions
ef='#n134'>134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/*
Minetest-c55
Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>

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.

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.

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.
*/

#include "tool.h"
#include "utility.h"
#include "itemdef.h" // For itemgroup_get()
#include "log.h"
#include "inventory.h"

void ToolCapabilities::serialize(std::ostream &os) const
{
	writeU8(os, 0); // version
	writeF1000(os, full_punch_interval);
	writeS16(os, max_drop_level);
	writeU32(os, groupcaps.size());
	for(std::map<std::string, ToolGroupCap>::const_iterator
			i = groupcaps.begin(); i != groupcaps.end(); i++){
		const std::string *name = &i->first;
		const ToolGroupCap *cap = &i->second;
		os<<serializeString(*name);
		writeF1000(os, cap->maxwear);
		writeF1000(os, cap->maxlevel);
		writeU32(os, cap->times.size());
		for(std::map<int, float>::const_iterator
				i = cap->times.begin(); i != cap->times.end(); i++){
			writeS16(os, i->first);
			writeF1000(os, i->second);
		}
	}
}

void ToolCapabilities::deSerialize(std::istream &is)
{
	int version = readU8(is);
	if(version != 0) throw SerializationError(
			"unsupported ToolCapabilities version");
	full_punch_interval = readF1000(is);
	max_drop_level = readS16(is);
	groupcaps.clear();
	u32 groupcaps_size = readU32(is);
	for(u32 i=0; i<groupcaps_size; i++){
		std::string name = deSerializeString(is);
		ToolGroupCap cap;
		cap.maxwear = readF1000(is);
		cap.maxlevel = readF1000(is);
		u32 times_size = readU32(is);
		for(u32 i=0; i<times_size; i++){
			int level = readS16(is);
			float time = readF1000(is);
			cap.times[level] = time;
		}
		groupcaps[name] = cap;
	}
}

DigParams getDigParams(const ItemGroupList &groups,
		const ToolCapabilities *tp, float time_from_last_punch)
{
	//infostream<<"getDigParams"<<std::endl;
	/* Check group dig_immediate */
	switch(itemgroup_get(groups, "dig_immediate")){
	case 2:
		//infostream<<"dig_immediate=2"<<std::endl;
		return DigParams(true, 0.5, 0);
	case 3:
		//infostream<<"dig_immediate=3"<<std::endl;
		return DigParams(true, 0.0, 0);
	default:
		break;
	}
	
	// Values to be returned (with a bit of conversion)
	bool result_diggable = false;
	float result_time = 0.0;
	float result_wear = 0.0;

	int level = itemgroup_get(groups, "level");
	//infostream<<"level="<<level<<std::endl;
	for(std::map<std::string, ToolGroupCap>::const_iterator
			i = tp->groupcaps.begin(); i != tp->groupcaps.end(); i++){
		const std::string &name = i->first;
		//infostream<<"group="<<name<<std::endl;
		const ToolGroupCap &cap = i->second;
		int rating = itemgroup_get(groups, name);
		float time = 0;
		bool time_exists = cap.getTime(rating, &time);
		if(!result_diggable || time < result_time){
			if(cap.maxlevel > level && time_exists){
				result_diggable = true;
				result_time = time;
				int leveldiff = cap.maxlevel - level;