Document the new Tree Feller algorithm

Also any formatting fixes go here

Also move treeFellerReachedThreshold into Woodcutting, the statics class
This commit is contained in:
riking 2013-10-14 20:38:15 -07:00
parent 2fba223d82
commit 833ca1bef2
3 changed files with 44 additions and 26 deletions

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.skills.woodcutting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@ -26,6 +27,8 @@ public final class Woodcutting {
public static int leafBlowerUnlockLevel = AdvancedConfig.getInstance().getLeafBlowUnlockLevel(); public static int leafBlowerUnlockLevel = AdvancedConfig.getInstance().getLeafBlowUnlockLevel();
public static int treeFellerThreshold = Config.getInstance().getTreeFellerThreshold(); public static int treeFellerThreshold = Config.getInstance().getTreeFellerThreshold();
protected static boolean treeFellerReachedThreshold = false;
protected enum ExperienceGainMethod { protected enum ExperienceGainMethod {
DEFAULT, DEFAULT,
TREE_FELLER, TREE_FELLER,
@ -140,6 +143,29 @@ public final class Woodcutting {
* @param blockState Block being checked * @param blockState Block being checked
* @param treeFellerBlocks List of blocks to be removed * @param treeFellerBlocks List of blocks to be removed
*/ */
/*
* Algorithm: An int[][] of X/Z directions is created on static class
* initialization, representing a cylinder with radius of about 2 - the
* (0,0) center and all (+-2, +-2) corners are omitted.
*
* handleBlock() returns a boolean, which is used for the sole purpose of
* switching between these two behaviors:
*
* (Call blockState "this log" for the below explanation.)
*
* [A] There is another log above this log (TRUNK)
* Only the flat cylinder in the directions array is searched.
* [B] There is not another log above this log (BRANCH AND TOP)
* The cylinder in the directions array is extended up and down by 1
* block in the Y-axis, and the block below this log is checked as
* well. Due to the fact that the directions array will catch all
* blocks on a red mushroom, the special method for it is eliminated.
*
* This algorithm has been shown to achieve a performance of 2-5
* milliseconds on regular trees and 10-15 milliseconds on jungle trees
* once the JIT has optimized the function (use the ability about 4 times
* before taking measurements).
*/
protected static void processTree(BlockState blockState, LinkedHashSet<BlockState> treeFellerBlocks) { protected static void processTree(BlockState blockState, LinkedHashSet<BlockState> treeFellerBlocks) {
List<BlockState> futureCenterBlocks = new ArrayList<BlockState>(); List<BlockState> futureCenterBlocks = new ArrayList<BlockState>();
@ -148,7 +174,7 @@ public final class Woodcutting {
for (int[] dir : directions) { for (int[] dir : directions) {
handleBlock(blockState.getBlock().getRelative(dir[0], 0, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks); handleBlock(blockState.getBlock().getRelative(dir[0], 0, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks);
if (WoodcuttingManager.treeFellerReachedThreshold) { if (treeFellerReachedThreshold) {
return; return;
} }
} }
@ -161,7 +187,7 @@ public final class Woodcutting {
for (int[] dir : directions) { for (int[] dir : directions) {
handleBlock(blockState.getBlock().getRelative(dir[0], y, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks); handleBlock(blockState.getBlock().getRelative(dir[0], y, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks);
if (WoodcuttingManager.treeFellerReachedThreshold) { if (treeFellerReachedThreshold) {
return; return;
} }
} }
@ -170,7 +196,7 @@ public final class Woodcutting {
// Recursive call for each log found // Recursive call for each log found
for (BlockState futureCenterBlock : futureCenterBlocks) { for (BlockState futureCenterBlock : futureCenterBlocks) {
if (WoodcuttingManager.treeFellerReachedThreshold) { if (treeFellerReachedThreshold) {
return; return;
} }
@ -185,7 +211,7 @@ public final class Woodcutting {
* @param inHand tool being used * @param inHand tool being used
* @return True if the tool can sustain the durability loss * @return True if the tool can sustain the durability loss
*/ */
protected static boolean handleDurabilityLoss(LinkedHashSet<BlockState> treeFellerBlocks, ItemStack inHand) { protected static boolean handleDurabilityLoss(Set<BlockState> treeFellerBlocks, ItemStack inHand) {
Material inHandMaterial = inHand.getType(); Material inHandMaterial = inHand.getType();
if (inHandMaterial == Material.AIR) { if (inHandMaterial == Material.AIR) {
@ -212,22 +238,22 @@ public final class Woodcutting {
/** /**
* Handle a block addition to the list of blocks to be removed and to the * Handle a block addition to the list of blocks to be removed and to the
* list of blocks used for future recursive calls of * list of blocks used for future recursive calls of
* 'processRecursively()' * 'processTree()'
* *
* @param blockState Block to be added * @param blockState Block to be added
* @param futureCenterBlocks List of blocks that will be used to call * @param futureCenterBlocks List of blocks that will be used to call
* 'processRecursively()' * 'processTree()'
* @param treeFellerBlocks List of blocks to be removed * @param treeFellerBlocks List of blocks to be removed
* @return true if and only if the given blockState was a Log not already * @return true if and only if the given blockState was a Log not already
* in treeFellerBlocks. * in treeFellerBlocks.
*/ */
private static boolean handleBlock(BlockState blockState, List<BlockState> futureCenterBlocks, LinkedHashSet<BlockState> treeFellerBlocks) { private static boolean handleBlock(BlockState blockState, List<BlockState> futureCenterBlocks, Set<BlockState> treeFellerBlocks) {
if (mcMMO.getPlaceStore().isTrue(blockState) || treeFellerBlocks.contains(blockState)) { if (treeFellerBlocks.contains(blockState) || mcMMO.getPlaceStore().isTrue(blockState)) {
return false; return false;
} }
if (treeFellerBlocks.size() > treeFellerThreshold) { if (treeFellerBlocks.size() > treeFellerThreshold) {
WoodcuttingManager.treeFellerReachedThreshold = true; treeFellerReachedThreshold = true;
} }
// Without this check Tree Feller propagates through leaves until the threshold is hit // Without this check Tree Feller propagates through leaves until the threshold is hit
@ -235,7 +261,8 @@ public final class Woodcutting {
treeFellerBlocks.add(blockState); treeFellerBlocks.add(blockState);
futureCenterBlocks.add(blockState); futureCenterBlocks.add(blockState);
return true; return true;
} else if (BlockUtils.isLeaves(blockState)) { }
else if (BlockUtils.isLeaves(blockState)) {
treeFellerBlocks.add(blockState); treeFellerBlocks.add(blockState);
return false; return false;
} }

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.skills.woodcutting; package com.gmail.nossr50.skills.woodcutting;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -25,8 +26,6 @@ import com.gmail.nossr50.util.skills.CombatUtils;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
public class WoodcuttingManager extends SkillManager { public class WoodcuttingManager extends SkillManager {
protected static boolean treeFellerReachedThreshold = false;
public WoodcuttingManager(McMMOPlayer mcMMOPlayer) { public WoodcuttingManager(McMMOPlayer mcMMOPlayer) {
super(mcMMOPlayer, SkillType.WOODCUTTING); super(mcMMOPlayer, SkillType.WOODCUTTING);
} }
@ -74,11 +73,13 @@ public class WoodcuttingManager extends SkillManager {
Player player = getPlayer(); Player player = getPlayer();
LinkedHashSet<BlockState> treeFellerBlocks = new LinkedHashSet<BlockState>(); LinkedHashSet<BlockState> treeFellerBlocks = new LinkedHashSet<BlockState>();
Woodcutting.treeFellerReachedThreshold = false;
Woodcutting.processTree(blockState, treeFellerBlocks); Woodcutting.processTree(blockState, treeFellerBlocks);
// If the player is trying to break too many blocks // If the player is trying to break too many blocks
if (treeFellerReachedThreshold) { if (Woodcutting.treeFellerReachedThreshold) {
treeFellerReachedThreshold = false; Woodcutting.treeFellerReachedThreshold = false;
player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFellerThreshold")); player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFellerThreshold"));
return; return;
@ -98,7 +99,7 @@ public class WoodcuttingManager extends SkillManager {
} }
dropBlocks(treeFellerBlocks); dropBlocks(treeFellerBlocks);
treeFellerReachedThreshold = false; // Reset the value after we're done with Tree Feller each time. Woodcutting.treeFellerReachedThreshold = false; // Reset the value after we're done with Tree Feller each time.
} }
/** /**
@ -106,7 +107,7 @@ public class WoodcuttingManager extends SkillManager {
* *
* @param treeFellerBlocks List of blocks to be dropped * @param treeFellerBlocks List of blocks to be dropped
*/ */
private void dropBlocks(LinkedHashSet<BlockState> treeFellerBlocks) { private void dropBlocks(Set<BlockState> treeFellerBlocks) {
Player player = getPlayer(); Player player = getPlayer();
int xp = 0; int xp = 0;

View File

@ -199,16 +199,6 @@ public final class BlockUtils {
} }
} }
/**
* Determine if a given block should be affected by Tree Feller
*
* @param blockState The {@link BlockState} of the block to check
* @return true if the block should affected by Tree Feller, false otherwise
*/
public static boolean affectedByTreeFeller(BlockState blockState) {
return isLog(blockState) || isLeaves(blockState);
}
/** /**
* Check if a given block is a log * Check if a given block is a log
* *