mirror of
https://github.com/minetest/minetest.git
synced 2024-12-22 22:22:23 +01:00
parent
f2c66b9ceb
commit
1bccb4e48c
73
src/tool.cpp
73
src/tool.cpp
@ -44,20 +44,27 @@ void ToolGroupCap::toJson(Json::Value &object) const
|
|||||||
|
|
||||||
void ToolGroupCap::fromJson(const Json::Value &json)
|
void ToolGroupCap::fromJson(const Json::Value &json)
|
||||||
{
|
{
|
||||||
if (json.isObject()) {
|
if (!json.isObject())
|
||||||
|
return;
|
||||||
|
|
||||||
if (json["maxlevel"].isInt())
|
if (json["maxlevel"].isInt())
|
||||||
maxlevel = json["maxlevel"].asInt();
|
maxlevel = json["maxlevel"].asInt();
|
||||||
|
|
||||||
if (json["uses"].isInt())
|
if (json["uses"].isInt())
|
||||||
uses = json["uses"].asInt();
|
uses = json["uses"].asInt();
|
||||||
|
|
||||||
const Json::Value ×_object = json["times"];
|
const Json::Value ×_object = json["times"];
|
||||||
if (times_object.isArray()) {
|
|
||||||
|
if (!times_object.isArray())
|
||||||
|
return;
|
||||||
|
|
||||||
Json::ArrayIndex size = times_object.size();
|
Json::ArrayIndex size = times_object.size();
|
||||||
for (Json::ArrayIndex i = 0; i < size; ++i)
|
|
||||||
|
for (Json::ArrayIndex i = 0; i < size; ++i) {
|
||||||
if (times_object[i].isDouble())
|
if (times_object[i].isDouble())
|
||||||
times[i] = times_object[i].asFloat();
|
times[i] = times_object[i].asFloat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
|
void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
|
||||||
{
|
{
|
||||||
@ -65,9 +72,11 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
|
|||||||
writeU8(os, 5);
|
writeU8(os, 5);
|
||||||
else
|
else
|
||||||
writeU8(os, 4); // proto == 37
|
writeU8(os, 4); // proto == 37
|
||||||
|
|
||||||
writeF32(os, full_punch_interval);
|
writeF32(os, full_punch_interval);
|
||||||
writeS16(os, max_drop_level);
|
writeS16(os, max_drop_level);
|
||||||
writeU32(os, groupcaps.size());
|
writeU32(os, groupcaps.size());
|
||||||
|
|
||||||
for (const auto &groupcap : groupcaps) {
|
for (const auto &groupcap : groupcaps) {
|
||||||
const std::string *name = &groupcap.first;
|
const std::string *name = &groupcap.first;
|
||||||
const ToolGroupCap *cap = &groupcap.second;
|
const ToolGroupCap *cap = &groupcap.second;
|
||||||
@ -102,6 +111,7 @@ void ToolCapabilities::deSerialize(std::istream &is)
|
|||||||
max_drop_level = readS16(is);
|
max_drop_level = readS16(is);
|
||||||
groupcaps.clear();
|
groupcaps.clear();
|
||||||
u32 groupcaps_size = readU32(is);
|
u32 groupcaps_size = readU32(is);
|
||||||
|
|
||||||
for (u32 i = 0; i < groupcaps_size; i++) {
|
for (u32 i = 0; i < groupcaps_size; i++) {
|
||||||
std::string name = deSerializeString16(is);
|
std::string name = deSerializeString16(is);
|
||||||
ToolGroupCap cap;
|
ToolGroupCap cap;
|
||||||
@ -135,15 +145,19 @@ void ToolCapabilities::serializeJson(std::ostream &os) const
|
|||||||
root["punch_attack_uses"] = punch_attack_uses;
|
root["punch_attack_uses"] = punch_attack_uses;
|
||||||
|
|
||||||
Json::Value groupcaps_object;
|
Json::Value groupcaps_object;
|
||||||
|
|
||||||
for (const auto &groupcap : groupcaps) {
|
for (const auto &groupcap : groupcaps) {
|
||||||
groupcap.second.toJson(groupcaps_object[groupcap.first]);
|
groupcap.second.toJson(groupcaps_object[groupcap.first]);
|
||||||
}
|
}
|
||||||
|
|
||||||
root["groupcaps"] = std::move(groupcaps_object);
|
root["groupcaps"] = std::move(groupcaps_object);
|
||||||
|
|
||||||
Json::Value damage_groups_object;
|
Json::Value damage_groups_object;
|
||||||
|
|
||||||
for (const auto &damagegroup : damageGroups) {
|
for (const auto &damagegroup : damageGroups) {
|
||||||
damage_groups_object[damagegroup.first] = damagegroup.second;
|
damage_groups_object[damagegroup.first] = damagegroup.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
root["damage_groups"] = std::move(damage_groups_object);
|
root["damage_groups"] = std::move(damage_groups_object);
|
||||||
|
|
||||||
fastWriteJson(root, os);
|
fastWriteJson(root, os);
|
||||||
@ -153,36 +167,44 @@ void ToolCapabilities::deserializeJson(std::istream &is)
|
|||||||
{
|
{
|
||||||
Json::Value root;
|
Json::Value root;
|
||||||
is >> root;
|
is >> root;
|
||||||
if (root.isObject()) {
|
|
||||||
|
if (!root.isObject())
|
||||||
|
return;
|
||||||
|
|
||||||
if (root["full_punch_interval"].isDouble())
|
if (root["full_punch_interval"].isDouble())
|
||||||
full_punch_interval = root["full_punch_interval"].asFloat();
|
full_punch_interval = root["full_punch_interval"].asFloat();
|
||||||
|
|
||||||
if (root["max_drop_level"].isInt())
|
if (root["max_drop_level"].isInt())
|
||||||
max_drop_level = root["max_drop_level"].asInt();
|
max_drop_level = root["max_drop_level"].asInt();
|
||||||
|
|
||||||
if (root["punch_attack_uses"].isInt())
|
if (root["punch_attack_uses"].isInt())
|
||||||
punch_attack_uses = root["punch_attack_uses"].asInt();
|
punch_attack_uses = root["punch_attack_uses"].asInt();
|
||||||
|
|
||||||
Json::Value &groupcaps_object = root["groupcaps"];
|
deserializeJsonGroupcaps(root["groupcaps"]);
|
||||||
if (groupcaps_object.isObject()) {
|
deserializeJsonDamageGroups(root["damage_groups"]);
|
||||||
Json::ValueIterator gciter;
|
}
|
||||||
for (gciter = groupcaps_object.begin();
|
|
||||||
gciter != groupcaps_object.end(); ++gciter) {
|
void ToolCapabilities::deserializeJsonGroupcaps(Json::Value &json)
|
||||||
ToolGroupCap groupcap;
|
{
|
||||||
groupcap.fromJson(*gciter);
|
if (!json.isObject())
|
||||||
groupcaps[gciter.key().asString()] = groupcap;
|
return;
|
||||||
|
|
||||||
|
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
|
||||||
|
ToolGroupCap value;
|
||||||
|
value.fromJson(*iter);
|
||||||
|
groupcaps[iter.key().asString()] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value &damage_groups_object = root["damage_groups"];
|
void ToolCapabilities::deserializeJsonDamageGroups(Json::Value &json)
|
||||||
if (damage_groups_object.isObject()) {
|
{
|
||||||
Json::ValueIterator dgiter;
|
if (!json.isObject())
|
||||||
for (dgiter = damage_groups_object.begin();
|
return;
|
||||||
dgiter != damage_groups_object.end(); ++dgiter) {
|
|
||||||
Json::Value &value = *dgiter;
|
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
|
||||||
|
Json::Value &value = *iter;
|
||||||
if (value.isInt())
|
if (value.isInt())
|
||||||
damageGroups[dgiter.key().asString()] =
|
damageGroups[iter.key().asString()] = value.asInt();
|
||||||
value.asInt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,10 +384,9 @@ u32 calculateResultWear(const u32 uses, const u16 initial_wear)
|
|||||||
player.
|
player.
|
||||||
*/
|
*/
|
||||||
u16 wear_extra_at = blocks_normal * wear_normal;
|
u16 wear_extra_at = blocks_normal * wear_normal;
|
||||||
if (initial_wear >= wear_extra_at) {
|
if (initial_wear >= wear_extra_at)
|
||||||
wear_extra = 1;
|
wear_extra = 1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
result_wear = wear_normal + wear_extra;
|
result_wear = wear_normal + wear_extra;
|
||||||
return result_wear;
|
return result_wear;
|
||||||
}
|
}
|
||||||
@ -410,6 +431,7 @@ DigParams getDigParams(const ItemGroupList &groups,
|
|||||||
|
|
||||||
if (leveldiff > 1)
|
if (leveldiff > 1)
|
||||||
time /= leveldiff;
|
time /= leveldiff;
|
||||||
|
|
||||||
if (!result_diggable || time < result_time) {
|
if (!result_diggable || time < result_time) {
|
||||||
result_time = time;
|
result_time = time;
|
||||||
result_diggable = true;
|
result_diggable = true;
|
||||||
@ -509,4 +531,3 @@ f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item,
|
|||||||
|
|
||||||
return max_d;
|
return max_d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,6 +83,10 @@ struct ToolCapabilities
|
|||||||
void deSerialize(std::istream &is);
|
void deSerialize(std::istream &is);
|
||||||
void serializeJson(std::ostream &os) const;
|
void serializeJson(std::ostream &os) const;
|
||||||
void deserializeJson(std::istream &is);
|
void deserializeJson(std::istream &is);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void deserializeJsonGroupcaps(Json::Value &json);
|
||||||
|
void deserializeJsonDamageGroups(Json::Value &json);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WearBarParams
|
struct WearBarParams
|
||||||
|
Loading…
Reference in New Issue
Block a user