Skip to content

Commit

Permalink
Tool.cpp/.h, lua_api/l_util.cpp: Tidy up code and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog authored and paramat committed Jan 3, 2018
1 parent d7c1f6c commit 345e104
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
8 changes: 2 additions & 6 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -158,18 +158,14 @@ int ModApiUtil::l_write_json(lua_State *L)
return 1;
}

// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
// get_dig_params(groups, tool_capabilities)
int ModApiUtil::l_get_dig_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ItemGroupList groups;
read_groups(L, 1, groups);
ToolCapabilities tp = read_tool_capabilities(L, 2);
if(lua_isnoneornil(L, 3))
push_dig_params(L, getDigParams(groups, &tp));
else
push_dig_params(L, getDigParams(groups, &tp,
luaL_checknumber(L, 3)));
push_dig_params(L, getDigParams(groups, &tp));
return 1;
}

Expand Down
63 changes: 24 additions & 39 deletions src/tool.cpp
Expand Up @@ -170,16 +170,13 @@ void ToolCapabilities::deserializeJson(std::istream &is)
}

DigParams getDigParams(const ItemGroupList &groups,
const ToolCapabilities *tp, float time_from_last_punch)
const ToolCapabilities *tp)
{
//infostream<<"getDigParams"<<std::endl;
/* Check group dig_immediate */
switch(itemgroup_get(groups, "dig_immediate")){
// Group dig_immediate has fixed time and no wear
switch (itemgroup_get(groups, "dig_immediate")) {
case 2:
//infostream<<"dig_immediate=2"<<std::endl;
return DigParams(true, 0.5, 0, "dig_immediate");
case 3:
//infostream<<"dig_immediate=3"<<std::endl;
return DigParams(true, 0, 0, "dig_immediate");
default:
break;
Expand All @@ -192,49 +189,37 @@ DigParams getDigParams(const ItemGroupList &groups,
std::string result_main_group;

int level = itemgroup_get(groups, "level");
//infostream<<"level="<<level<<std::endl;
for (const auto &groupcap : tp->groupcaps) {
const std::string &name = groupcap.first;
//infostream<<"group="<<name<<std::endl;
const ToolGroupCap &cap = groupcap.second;
int rating = itemgroup_get(groups, name);

int leveldiff = cap.maxlevel - level;
if (leveldiff < 0)
continue;

const std::string &groupname = groupcap.first;
float time = 0;
int rating = itemgroup_get(groups, groupname);
bool time_exists = cap.getTime(rating, &time);
int leveldiff = cap.maxlevel - level;
time /= MYMAX(1, leveldiff);
if(!result_diggable || time < result_time){
if(cap.maxlevel >= level && time_exists){
result_diggable = true;
result_time = time;
if(cap.uses != 0)
result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
else
result_wear = 0;
result_main_group = name;
}
if (!time_exists)
continue;

if (leveldiff > 1)
time /= leveldiff;
if (!result_diggable || time < result_time) {
result_time = time;
result_diggable = true;
if (cap.uses != 0)
result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
else
result_wear = 0;
result_main_group = groupname;
}
}
//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;
u16 wear_i = U16_MAX * result_wear;
return DigParams(result_diggable, result_time, wear_i, result_main_group);
}

DigParams getDigParams(const ItemGroupList &groups,
const ToolCapabilities *tp)
{
return getDigParams(groups, tp, 1000000);
}

HitParams getHitParams(const ItemGroupList &armor_groups,
const ToolCapabilities *tp, float time_from_last_punch)
{
Expand Down
3 changes: 0 additions & 3 deletions src/tool.h
Expand Up @@ -95,9 +95,6 @@ struct DigParams
{}
};

DigParams getDigParams(const ItemGroupList &groups,
const ToolCapabilities *tp, float time_from_last_punch);

DigParams getDigParams(const ItemGroupList &groups,
const ToolCapabilities *tp);

Expand Down

0 comments on commit 345e104

Please sign in to comment.