bool look_for_object, const v3s16 &camera_offset);
void handlePointingAtNothing(const ItemStack &playerItem);
void handlePointingAtNode(const PointedThing &pointed,
- const ItemDefinition &playeritem_def, const ItemStack &playeritem,
- const ToolCapabilities &playeritem_toolcap, f32 dtime);
+ const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime);
void handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
const v3f &player_position, bool show_debug);
void handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
- const ToolCapabilities &playeritem_toolcap, f32 dtime);
+ const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime);
void updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
const CameraOrientation &cam);
void updateProfilerGraphs(ProfilerGraph *graph);
void updateChat(f32 dtime, const v2u32 &screensize);
- bool nodePlacementPrediction(const ItemDefinition &playeritem_def,
- const ItemStack &playeritem, const v3s16 &nodepos, const v3s16 &neighbourpos);
+ bool nodePlacementPrediction(const ItemDefinition &selected_def,
+ const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos);
static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];
InputHandler *input = nullptr;
Camera *camera = nullptr;
Clouds *clouds = nullptr; // Free using ->Drop()
Sky *sky = nullptr; // Free using ->Drop()
- Inventory *local_inventory = nullptr;
Hud *hud = nullptr;
Minimap *mapper = nullptr;
delete server; // deleted first to stop all server threads
delete hud;
- delete local_inventory;
delete camera;
delete quicktune;
delete eventmgr;
scsf->setSky(sky);
skybox = NULL; // This is used/set later on in the main run loop
- local_inventory = new Inventory(itemdef_manager);
-
- if (!(sky && local_inventory)) {
- *error_message = "Memory allocation error (sky or local inventory)";
+ if (!sky) {
+ *error_message = "Memory allocation error sky";
errorstream << *error_message << std::endl;
return false;
}
player->hurt_tilt_timer = 0;
player->hurt_tilt_strength = 0;
- hud = new Hud(guienv, client, player, local_inventory);
+ hud = new Hud(guienv, client, player, &player->inventory);
if (!hud) {
*error_message = "Memory error: could not create HUD";
/* Item selection using mouse wheel
*/
- *new_playeritem = client->getPlayerItem();
+ *new_playeritem = player->getWieldIndex();
s32 wheel = input->getMouseWheel();
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE - 1,
a->count = single_item ? 1 : 0;
a->from_inv.setCurrentPlayer();
a->from_list = "main";
- a->from_i = client->getPlayerItem();
+ a->from_i = client->getEnv().getLocalPlayer()->getWieldIndex();
client->inventoryAction(a);
}
*/
ItemStack playeritem;
{
- InventoryList *mlist = local_inventory->getList("main");
-
- if (mlist && client->getPlayerItem() < mlist->getSize())
- playeritem = mlist->getItem(client->getPlayerItem());
+ ItemStack selected, hand;
+ playeritem = player->getWieldedItem(&selected, &hand);
}
- if (playeritem.getDefinition(itemdef_manager).name.empty()) { // override the hand
- InventoryList *hlist = local_inventory->getList("hand");
- if (hlist)
- playeritem = hlist->getItem(0);
- }
-
-
ToolCapabilities playeritem_toolcap =
playeritem.getToolCapabilities(itemdef_manager);
{
LocalPlayer *player = client->getEnv().getLocalPlayer();
- ItemStack playeritem;
- {
- InventoryList *mlist = local_inventory->getList("main");
-
- if (mlist && client->getPlayerItem() < mlist->getSize())
- playeritem = mlist->getItem(client->getPlayerItem());
- }
-
- const ItemDefinition &playeritem_def =
- playeritem.getDefinition(itemdef_manager);
- InventoryList *hlist = local_inventory->getList("hand");
- const ItemDefinition &hand_def =
- hlist ? hlist->getItem(0).getDefinition(itemdef_manager) : itemdef_manager->get("");
-
v3f player_position = player->getPosition();
v3f player_eye_position = player->getEyePosition();
v3f camera_position = camera->getPosition();
Calculate what block is the crosshair pointing to
*/
- f32 d = playeritem_def.range; // max. distance
- f32 d_hand = hand_def.range;
+ ItemStack selected_item, hand_item;
+ const ItemStack &tool_item = player->getWieldedItem(&selected_item, &hand_item);
- if (d < 0 && d_hand >= 0)
- d = d_hand;
- else if (d < 0)
- d = 4.0;
+ const ItemDefinition &selected_def = selected_item.getDefinition(itemdef_manager);
+ f32 d = BS * getToolRange(selected_def, hand_item.getDefinition(itemdef_manager));
core::line3d<f32> shootline;
#endif
PointedThing pointed = updatePointedThing(shootline,
- playeritem_def.liquids_pointable,
+ selected_def.liquids_pointable,
!runData.ldown_for_dig,
camera_offset);
else
runData.repeat_rightclick_timer = 0;
- if (playeritem_def.usable && input->getLeftState()) {
+
+ if (selected_def.usable && input->getLeftState()) {
if (input->getLeftClicked() && (!client->moddingEnabled()
- || !client->getScript()->on_item_use(playeritem, pointed)))
+ || !client->getScript()->on_item_use(selected_item, pointed)))
client->interact(4, pointed);
} else if (pointed.type == POINTEDTHING_NODE) {
- ToolCapabilities playeritem_toolcap =
- playeritem.getToolCapabilities(itemdef_manager);
- if (playeritem.name.empty()) {
- const ToolCapabilities *handToolcap = hlist
- ? &hlist->getItem(0).getToolCapabilities(itemdef_manager)
- : itemdef_manager->get("").tool_capabilities;
-
- if (handToolcap != nullptr)
- playeritem_toolcap = *handToolcap;
- }
- handlePointingAtNode(pointed, playeritem_def, playeritem,
- playeritem_toolcap, dtime);
+ handlePointingAtNode(pointed, selected_item, hand_item, dtime);
} else if (pointed.type == POINTEDTHING_OBJECT) {
- handlePointingAtObject(pointed, playeritem, player_position, show_debug);
+ handlePointingAtObject(pointed, tool_item, player_position, show_debug);
} else if (input->getLeftState()) {
// When button is held down in air, show continuous animation
runData.left_punch = true;
} else if (input->getRightClicked()) {
- handlePointingAtNothing(playeritem);
+ handlePointingAtNothing(selected_item);
}
runData.pointed_old = pointed;
void Game::handlePointingAtNode(const PointedThing &pointed,
- const ItemDefinition &playeritem_def, const ItemStack &playeritem,
- const ToolCapabilities &playeritem_toolcap, f32 dtime)
+ const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime)
{
v3s16 nodepos = pointed.node_undersurface;
v3s16 neighbourpos = pointed.node_abovesurface;
if (runData.nodig_delay_timer <= 0.0 && input->getLeftState()
&& !runData.digging_blocked
&& client->checkPrivilege("interact")) {
- handleDigging(pointed, nodepos, playeritem_toolcap, dtime);
+ handleDigging(pointed, nodepos, selected_item, hand_item, dtime);
}
// This should be done after digging handling
// If the wielded item has node placement prediction,
// make that happen
- bool placed = nodePlacementPrediction(playeritem_def, playeritem, nodepos,
+ auto &def = selected_item.getDefinition(itemdef_manager);
+ bool placed = nodePlacementPrediction(def, selected_item, nodepos,
neighbourpos);
if (placed) {
client->interact(3, pointed);
// Read the sound
soundmaker->m_player_rightpunch_sound =
- playeritem_def.sound_place;
+ def.sound_place;
if (client->moddingEnabled())
- client->getScript()->on_placenode(pointed, playeritem_def);
+ client->getScript()->on_placenode(pointed, def);
} else {
soundmaker->m_player_rightpunch_sound =
SimpleSoundSpec();
- if (playeritem_def.node_placement_prediction.empty() ||
+ if (def.node_placement_prediction.empty() ||
nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
client->interact(3, pointed); // Report to server
} else {
soundmaker->m_player_rightpunch_sound =
- playeritem_def.sound_place_failed;
+ def.sound_place_failed;
}
}
}
}
}
-bool Game::nodePlacementPrediction(const ItemDefinition &playeritem_def,
- const ItemStack &playeritem, const v3s16 &nodepos, const v3s16 &neighbourpos)
+bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
+ const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos)
{
- std::string prediction = playeritem_def.node_placement_prediction;
+ std::string prediction = selected_def.node_placement_prediction;
const NodeDefManager *nodedef = client->ndef();
ClientMap &map = client->getEnv().getClientMap();
MapNode node;
if (!prediction.empty() && !(nodedef->get(node).rightclickable &&
!isKeyDown(KeyType::SNEAK))) {
verbosestream << "Node placement prediction for "
- << playeritem_def.name << " is "
+ << selected_item.name << " is "
<< prediction << std::endl;
v3s16 p = neighbourpos;
if (!found) {
errorstream << "Node placement prediction failed for "
- << playeritem_def.name << " (places "
+ << selected_item.name << " (places "
<< prediction
<< ") - Name not known" << std::endl;
return false;
if ((predicted_f.param_type_2 == CPT2_COLOR
|| predicted_f.param_type_2 == CPT2_COLORED_FACEDIR
|| predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
- const std::string &indexstr = playeritem.metadata.getString(
+ const std::string &indexstr = selected_item.metadata.getString(
"palette_index", 0);
if (!indexstr.empty()) {
s32 index = mystoi(indexstr);
}
} catch (InvalidPositionException &e) {
errorstream << "Node placement prediction failed for "
- << playeritem_def.name << " (places "
+ << selected_item.name << " (places "
<< prediction
<< ") - Position not loaded" << std::endl;
}
return false;
}
-void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
- const v3f &player_position, bool show_debug)
+void Game::handlePointingAtObject(const PointedThing &pointed,
+ const ItemStack &tool_item, const v3f &player_position, bool show_debug)
{
std::wstring infotext = unescape_translate(
utf8_to_wide(runData.selected_object->infoText()));
// Report direct punch
v3f objpos = runData.selected_object->getPosition();
v3f dir = (objpos - player_position).normalize();
- ItemStack item = playeritem;
- if (playeritem.name.empty()) {
- InventoryList *hlist = local_inventory->getList("hand");
- if (hlist) {
- item = hlist->getItem(0);
- }
- }
bool disable_send = runData.selected_object->directReportPunch(
- dir, &item, runData.time_from_last_punch);
+ dir, &tool_item, runData.time_from_last_punch);
runData.time_from_last_punch = 0;
if (!disable_send)
void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
- const ToolCapabilities &playeritem_toolcap, f32 dtime)
+ const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime)
{
+ // See also: serverpackethandle.cpp, action == 2
LocalPlayer *player = client->getEnv().getLocalPlayer();
ClientMap &map = client->getEnv().getClientMap();
MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
// cheat detection.
// Get digging parameters
DigParams params = getDigParams(nodedef_manager->get(n).groups,
- &playeritem_toolcap);
+ &selected_item.getToolCapabilities(itemdef_manager));
// If can't dig, try hand
if (!params.diggable) {
- InventoryList *hlist = local_inventory->getList("hand");
- const ToolCapabilities *tp = hlist
- ? &hlist->getItem(0).getToolCapabilities(itemdef_manager)
- : itemdef_manager->get("").tool_capabilities;
-
- if (tp)
- params = getDigParams(nodedef_manager->get(n).groups, tp);
+ params = getDigParams(nodedef_manager->get(n).groups,
+ &hand_item.getToolCapabilities(itemdef_manager));
}
if (!params.diggable) {
Inventory
*/
- if (client->getPlayerItem() != runData.new_playeritem)
- client->selectPlayerItem(runData.new_playeritem);
+ if (player->getWieldIndex() != runData.new_playeritem)
+ client->setPlayerItem(runData.new_playeritem);
// Update local inventory if it has changed
if (client->getLocalInventoryUpdated()) {
//infostream<<"Updating local inventory"<<std::endl;
- client->getLocalInventory(*local_inventory);
runData.update_wielded_item_trigger = true;
}
if (runData.update_wielded_item_trigger) {
// Update wielded tool
- InventoryList *mlist = local_inventory->getList("main");
-
- if (mlist && (client->getPlayerItem() < mlist->getSize())) {
- ItemStack item = mlist->getItem(client->getPlayerItem());
- if (item.getDefinition(itemdef_manager).name.empty()) { // override the hand
- InventoryList *hlist = local_inventory->getList("hand");
- if (hlist)
- item = hlist->getItem(0);
- }
- camera->wield(item);
- }
+ ItemStack selected_item, hand_item;
+ ItemStack &tool_item = player->getWieldedItem(&selected_item, &hand_item);
+ camera->wield(tool_item);
runData.update_wielded_item_trigger = false;
}
*pkt >> item;
- playersao->setWieldIndex(item);
+ playersao->getPlayer()->setWieldIndex(item);
}
void Server::handleCommand_Respawn(NetworkPacket* pkt)
bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std::string &what)
{
- PlayerSAO *playersao = player->getPlayerSAO();
- const InventoryList *hlist = playersao->getInventory()->getList("hand");
- const ItemDefinition &playeritem_def =
- playersao->getWieldedItem().getDefinition(m_itemdef);
- const ItemDefinition &hand_def =
- hlist ? hlist->getItem(0).getDefinition(m_itemdef) : m_itemdef->get("");
-
- float max_d = BS * playeritem_def.range;
- float max_d_hand = BS * hand_def.range;
-
- if (max_d < 0 && max_d_hand >= 0)
- max_d = max_d_hand;
- else if (max_d < 0)
- max_d = BS * 4.0f;
+ ItemStack selected_item, hand_item;
+ player->getWieldedItem(&selected_item, &hand_item);
+ f32 max_d = BS * getToolRange(selected_item.getDefinition(m_itemdef),
+ hand_item.getDefinition(m_itemdef));
// Cube diagonal * 1.5 for maximal supported node extents:
// sqrt(3) * 1.5 ≅ 2.6
<< "d=" << d <<", max_d=" << max_d
<< ". ignoring." << std::endl;
// Call callbacks
- m_script->on_cheat(playersao, "interacted_too_far");
+ m_script->on_cheat(player->getPlayerSAO(), "interacted_too_far");
return false;
}
return true;
v3f player_pos = playersao->getLastGoodPosition();
// Update wielded item
- playersao->setWieldIndex(item_i);
+ playersao->getPlayer()->setWieldIndex(item_i);
// Get pointed to node (undefined if not POINTEDTYPE_NODE)
v3s16 p_under = pointed.node_undersurface;
if (pointed_object->isGone())
return;
- ItemStack punchitem = playersao->getWieldedItemOrHand();
+ ItemStack punchitem = playersao->getWieldedItem();
ToolCapabilities toolcap =
punchitem.getToolCapabilities(m_itemdef);
v3f dir = (pointed_object->getBasePosition() -
// Call callbacks
m_script->on_cheat(playersao, "finished_unknown_dig");
}
+
// Get player's wielded item
- ItemStack playeritem = playersao->getWieldedItemOrHand();
- ToolCapabilities playeritem_toolcap =
- playeritem.getToolCapabilities(m_itemdef);
+ // See also: Game::handleDigging
+ ItemStack selected_item, hand_item;
+ playersao->getPlayer()->getWieldedItem(&selected_item, &hand_item);
+
// Get diggability and expected digging time
DigParams params = getDigParams(m_nodedef->get(n).groups,
- &playeritem_toolcap);
+ &selected_item.getToolCapabilities(m_itemdef));
// If can't dig, try hand
if (!params.diggable) {
- InventoryList *hlist = playersao->getInventory()->getList("hand");
- const ToolCapabilities *tp = hlist
- ? &hlist->getItem(0).getToolCapabilities(m_itemdef)
- : m_itemdef->get("").tool_capabilities;
-
- if (tp)
- params = getDigParams(m_nodedef->get(n).groups, tp);
+ params = getDigParams(m_nodedef->get(n).groups,
+ &hand_item.getToolCapabilities(m_itemdef));
}
// If can't dig, ignore dig
if (!params.diggable) {