2011-11-15 18:58:36 +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.
|
|
|
|
*/
|
|
|
|
|
2010-12-25 00:54:39 +01:00
|
|
|
#include "materials.h"
|
2011-06-17 21:20:15 +02:00
|
|
|
#include "mapnode.h"
|
2011-11-14 20:41:30 +01:00
|
|
|
#include "nodedef.h"
|
|
|
|
#include "tooldef.h"
|
2011-11-15 18:58:36 +01:00
|
|
|
#include "utility.h"
|
|
|
|
|
|
|
|
void MaterialProperties::serialize(std::ostream &os)
|
|
|
|
{
|
|
|
|
writeU8(os, 0); // version
|
|
|
|
writeU8(os, diggability);
|
2011-11-15 21:04:16 +01:00
|
|
|
writeF1000(os, constant_time);
|
2011-11-15 18:58:36 +01:00
|
|
|
writeF1000(os, weight);
|
|
|
|
writeF1000(os, crackiness);
|
|
|
|
writeF1000(os, crumbliness);
|
|
|
|
writeF1000(os, cuttability);
|
2011-11-16 01:22:41 +01:00
|
|
|
writeF1000(os, flammability);
|
2011-11-15 18:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialProperties::deSerialize(std::istream &is)
|
|
|
|
{
|
|
|
|
int version = readU8(is);
|
|
|
|
if(version != 0)
|
|
|
|
throw SerializationError("unsupported MaterialProperties version");
|
|
|
|
diggability = (enum Diggability)readU8(is);
|
2011-11-15 21:04:16 +01:00
|
|
|
constant_time = readF1000(is);
|
2011-11-15 18:58:36 +01:00
|
|
|
weight = readF1000(is);
|
|
|
|
crackiness = readF1000(is);
|
|
|
|
crumbliness = readF1000(is);
|
|
|
|
cuttability = readF1000(is);
|
2011-11-16 01:22:41 +01:00
|
|
|
flammability = readF1000(is);
|
2011-11-15 18:58:36 +01:00
|
|
|
}
|
2010-12-25 00:54:39 +01:00
|
|
|
|
2011-11-14 20:41:30 +01:00
|
|
|
DiggingProperties getDiggingProperties(u16 content, ToolDiggingProperties *tp,
|
|
|
|
INodeDefManager *nodemgr)
|
2010-12-25 00:54:39 +01:00
|
|
|
{
|
2011-11-13 23:19:48 +01:00
|
|
|
assert(tp);
|
2011-11-14 20:41:30 +01:00
|
|
|
const MaterialProperties &mp = nodemgr->get(content).material;
|
2011-11-13 14:45:38 +01:00
|
|
|
if(mp.diggability == DIGGABLE_NOT)
|
|
|
|
return DiggingProperties(false, 0, 0);
|
|
|
|
if(mp.diggability == DIGGABLE_CONSTANT)
|
|
|
|
return DiggingProperties(true, mp.constant_time, 0);
|
|
|
|
|
2011-11-13 23:19:48 +01:00
|
|
|
float time = tp->basetime;
|
|
|
|
time += tp->dt_weight * mp.weight;
|
|
|
|
time += tp->dt_crackiness * mp.crackiness;
|
|
|
|
time += tp->dt_crumbliness * mp.crumbliness;
|
|
|
|
time += tp->dt_cuttability * mp.cuttability;
|
2011-11-13 14:45:38 +01:00
|
|
|
if(time < 0.2)
|
|
|
|
time = 0.2;
|
|
|
|
|
2011-11-13 23:19:48 +01:00
|
|
|
float durability = tp->basedurability;
|
|
|
|
durability += tp->dd_weight * mp.weight;
|
|
|
|
durability += tp->dd_crackiness * mp.crackiness;
|
|
|
|
durability += tp->dd_crumbliness * mp.crumbliness;
|
|
|
|
durability += tp->dd_cuttability * mp.cuttability;
|
2011-11-13 14:45:38 +01:00
|
|
|
if(durability < 1)
|
|
|
|
durability = 1;
|
|
|
|
|
|
|
|
float wear = 1.0 / durability;
|
|
|
|
u16 wear_i = wear/65535.;
|
|
|
|
return DiggingProperties(true, time, wear_i);
|
2010-12-25 00:54:39 +01:00
|
|
|
}
|
|
|
|
|