mirror of
https://github.com/minetest/minetest.git
synced 2024-12-22 22:22:23 +01:00
parent
f2c66b9ceb
commit
1bccb4e48c
107
src/tool.cpp
107
src/tool.cpp
@ -44,18 +44,25 @@ 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())
|
||||||
if (json["maxlevel"].isInt())
|
return;
|
||||||
maxlevel = json["maxlevel"].asInt();
|
|
||||||
if (json["uses"].isInt())
|
if (json["maxlevel"].isInt())
|
||||||
uses = json["uses"].asInt();
|
maxlevel = json["maxlevel"].asInt();
|
||||||
const Json::Value ×_object = json["times"];
|
|
||||||
if (times_object.isArray()) {
|
if (json["uses"].isInt())
|
||||||
Json::ArrayIndex size = times_object.size();
|
uses = json["uses"].asInt();
|
||||||
for (Json::ArrayIndex i = 0; i < size; ++i)
|
|
||||||
if (times_object[i].isDouble())
|
const Json::Value ×_object = json["times"];
|
||||||
times[i] = times_object[i].asFloat();
|
|
||||||
}
|
if (!times_object.isArray())
|
||||||
|
return;
|
||||||
|
|
||||||
|
Json::ArrayIndex size = times_object.size();
|
||||||
|
|
||||||
|
for (Json::ArrayIndex i = 0; i < size; ++i) {
|
||||||
|
if (times_object[i].isDouble())
|
||||||
|
times[i] = times_object[i].asFloat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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["full_punch_interval"].isDouble())
|
|
||||||
full_punch_interval = root["full_punch_interval"].asFloat();
|
|
||||||
if (root["max_drop_level"].isInt())
|
|
||||||
max_drop_level = root["max_drop_level"].asInt();
|
|
||||||
if (root["punch_attack_uses"].isInt())
|
|
||||||
punch_attack_uses = root["punch_attack_uses"].asInt();
|
|
||||||
|
|
||||||
Json::Value &groupcaps_object = root["groupcaps"];
|
if (!root.isObject())
|
||||||
if (groupcaps_object.isObject()) {
|
return;
|
||||||
Json::ValueIterator gciter;
|
|
||||||
for (gciter = groupcaps_object.begin();
|
|
||||||
gciter != groupcaps_object.end(); ++gciter) {
|
|
||||||
ToolGroupCap groupcap;
|
|
||||||
groupcap.fromJson(*gciter);
|
|
||||||
groupcaps[gciter.key().asString()] = groupcap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Json::Value &damage_groups_object = root["damage_groups"];
|
if (root["full_punch_interval"].isDouble())
|
||||||
if (damage_groups_object.isObject()) {
|
full_punch_interval = root["full_punch_interval"].asFloat();
|
||||||
Json::ValueIterator dgiter;
|
|
||||||
for (dgiter = damage_groups_object.begin();
|
if (root["max_drop_level"].isInt())
|
||||||
dgiter != damage_groups_object.end(); ++dgiter) {
|
max_drop_level = root["max_drop_level"].asInt();
|
||||||
Json::Value &value = *dgiter;
|
|
||||||
if (value.isInt())
|
if (root["punch_attack_uses"].isInt())
|
||||||
damageGroups[dgiter.key().asString()] =
|
punch_attack_uses = root["punch_attack_uses"].asInt();
|
||||||
value.asInt();
|
|
||||||
}
|
deserializeJsonGroupcaps(root["groupcaps"]);
|
||||||
}
|
deserializeJsonDamageGroups(root["damage_groups"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToolCapabilities::deserializeJsonGroupcaps(Json::Value &json)
|
||||||
|
{
|
||||||
|
if (!json.isObject())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
|
||||||
|
ToolGroupCap value;
|
||||||
|
value.fromJson(*iter);
|
||||||
|
groupcaps[iter.key().asString()] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToolCapabilities::deserializeJsonDamageGroups(Json::Value &json)
|
||||||
|
{
|
||||||
|
if (!json.isObject())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (Json::ValueIterator iter = json.begin(); iter != json.end(); ++iter) {
|
||||||
|
Json::Value &value = *iter;
|
||||||
|
if (value.isInt())
|
||||||
|
damageGroups[iter.key().asString()] = value.asInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,9 +384,8 @@ 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