minetest/src/tool.cpp

193 lines
5.5 KiB
C++
Raw Normal View History

2012-02-28 18:45:23 +01:00
/*
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"
2012-03-05 00:30:55 +01:00
#include "inventory.h"
2012-02-28 18:45:23 +01:00
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;
}
}
2012-03-04 13:22:35 +01:00
DigParams getDigParams(const ItemGroupList &groups,
2012-02-28 18:45:23 +01:00
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;
2012-03-23 23:33:58 +01:00
return DigParams(true, 0.5, 0, "dig_immediate");
case 3:
//infostream<<"dig_immediate=3"<<std::endl;
2012-03-23 23:33:58 +01:00
return DigParams(true, 0.0, 0, "dig_immediate");
2012-02-28 18:45:23 +01:00
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;
2012-03-23 23:33:58 +01:00
std::string result_main_group = "";
2012-02-28 18:45:23 +01:00
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;
result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
2012-03-23 23:33:58 +01:00
result_main_group = name;
2012-02-28 18:45:23 +01:00
}
}
}
//infostream<<"result_diggable="<<result_diggable<<std::endl;
//infostream<<"result_time="<<result_time<<std::endl;
//infostream<<"result_wear="<<result_wear<<std::endl;
if(time_from_last_punch < tp->full_punch_interval){
float f = time_from_last_punch / tp->full_punch_interval;
//infostream<<"f="<<f<<std::endl;
result_time /= f;
result_wear /= f;
}
u16 wear_i = 65535.*result_wear;
2012-03-23 23:33:58 +01:00
return DigParams(result_diggable, result_time, wear_i, result_main_group);
2012-02-28 18:45:23 +01:00
}
2012-03-04 13:22:35 +01:00
DigParams getDigParams(const ItemGroupList &groups,
2012-02-28 18:45:23 +01:00
const ToolCapabilities *tp)
{
return getDigParams(groups, tp, 1000000);
}
2012-03-04 13:22:35 +01:00
HitParams getHitParams(const ItemGroupList &groups,
2012-02-28 18:45:23 +01:00
const ToolCapabilities *tp, float time_from_last_punch)
{
DigParams digprop = getDigParams(groups, tp,
time_from_last_punch);
2012-03-04 15:29:04 +01:00
if(time_from_last_punch > tp->full_punch_interval)
time_from_last_punch = tp->full_punch_interval;
2012-03-04 12:40:40 +01:00
// Damage in hp is equivalent to nodes dug in time_from_last_punch
2012-02-28 18:45:23 +01:00
s16 hp = 0;
if(digprop.diggable)
2012-03-04 12:40:40 +01:00
hp = time_from_last_punch / digprop.time;
2012-02-28 18:45:23 +01:00
// Wear is the same as for digging a single node
s16 wear = (float)digprop.wear;
2012-03-23 23:33:58 +01:00
return HitParams(hp, wear, digprop.main_group);
2012-02-28 18:45:23 +01:00
}
2012-03-04 13:22:35 +01:00
HitParams getHitParams(const ItemGroupList &groups,
2012-02-28 18:45:23 +01:00
const ToolCapabilities *tp)
{
return getHitParams(groups, tp, 1000000);
}
2012-03-05 00:30:55 +01:00
PunchDamageResult getPunchDamage(
const ItemGroupList &armor_groups,
const ToolCapabilities *toolcap,
const ItemStack *punchitem,
float time_from_last_punch
){
bool do_hit = true;
{
if(do_hit && punchitem){
if(itemgroup_get(armor_groups, "punch_operable") &&
(toolcap == NULL || punchitem->name == ""))
do_hit = false;
}
if(do_hit){
if(itemgroup_get(armor_groups, "immortal"))
do_hit = false;
}
}
PunchDamageResult result;
if(do_hit)
{
HitParams hitparams = getHitParams(armor_groups, toolcap,
time_from_last_punch);
result.did_punch = true;
result.wear = hitparams.wear;
result.damage = hitparams.hp;
2012-03-23 23:33:58 +01:00
result.main_group = hitparams.main_group;
2012-03-05 00:30:55 +01:00
}
return result;
}