Advertisement
_DarkLex_

Untitled

Aug 4th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. public abstract class Crops : Block
  2.     {
  3.         private static readonly ILog Log = LogManager.GetLogger(typeof (Crops));
  4.  
  5.         protected byte MaxGrowth { get; set; } = 7;
  6.  
  7.         protected Crops(byte id) : base(id)
  8.         {
  9.             IsSolid = false;
  10.             IsTransparent = true;
  11.         }
  12.  
  13.         public override bool Interact(Level level, Player player, BlockCoordinates blockCoordinates, BlockFace face, Vector3 faceCoord)
  14.         {
  15.             var itemInHand = player.Inventory.GetItemInHand();
  16.             if (Metadata < MaxGrowth && itemInHand is ItemDye && itemInHand.Metadata == 15)
  17.             {
  18.                 Metadata += (byte) new Random().Next(2, 6);
  19.                 if (Metadata > MaxGrowth) Metadata = MaxGrowth;
  20.  
  21.                 level.SetBlock(this);
  22.  
  23.                 return true;
  24.             }
  25.  
  26.             return false;
  27.         }
  28.  
  29.         public override void OnTick(Level level, bool isRandom)
  30.         {
  31.             if (!isRandom) return;
  32.  
  33.             if (Metadata < MaxGrowth && CalculateGrowthChance(level, this))
  34.             {
  35.                 Metadata++;
  36.                 level.SetBlock(this);
  37.             }
  38.         }
  39.  
  40.         private static bool CalculateGrowthChance(Level level, Block target)
  41.         {
  42.             //1 / (floor(25 / points) + 1)
  43.             double points = 0;
  44.  
  45.             // The farmland block the crop is planted in gives 2 points if dry or 4 if hydrated.
  46.             Block under = level.GetBlock(target.Coordinates + BlockCoordinates.Down);
  47.             points += under.Metadata == 0 ? 2 : 4;
  48.  
  49.             Block west = level.GetBlock(target.Coordinates + BlockCoordinates.West);
  50.             Block east = level.GetBlock(target.Coordinates + BlockCoordinates.East);
  51.             Block south = level.GetBlock(target.Coordinates + BlockCoordinates.North);
  52.             Block north = level.GetBlock(target.Coordinates + BlockCoordinates.South);
  53.             Block southWest = level.GetBlock(target.Coordinates + BlockCoordinates.West + BlockCoordinates.South);
  54.             Block southEast = level.GetBlock(target.Coordinates + BlockCoordinates.East + BlockCoordinates.South);
  55.             Block northWest = level.GetBlock(target.Coordinates + BlockCoordinates.North + BlockCoordinates.North);
  56.             Block northEast = level.GetBlock(target.Coordinates + BlockCoordinates.South + BlockCoordinates.North);
  57.  
  58.             // For each of the 8 blocks around the block in which the crop is planted, dry farmland gives 0.25 points, and hydrated farmland gives 0.75
  59.             points += west is Farmland ? west.Metadata == 0 ? 0.25 : 0.75 : 0;
  60.             points += east is Farmland ? east.Metadata == 0 ? 0.25 : 0.75 : 0;
  61.             points += south is Farmland ? south.Metadata == 0 ? 0.25 : 0.75 : 0;
  62.             points += north is Farmland ? north.Metadata == 0 ? 0.25 : 0.75 : 0;
  63.             points += northWest is Farmland ? northWest.Metadata == 0 ? 0.25 : 0.75 : 0;
  64.             points += northEast is Farmland ? northEast.Metadata == 0 ? 0.25 : 0.75 : 0;
  65.             points += southWest is Farmland ? southWest.Metadata == 0 ? 0.25 : 0.75 : 0;
  66.             points += southEast is Farmland ? southEast.Metadata == 0 ? 0.25 : 0.75 : 0;
  67.  
  68.             // If any plants of the same type are growing in the eight surrounding blocks, the point total is cut in half unless the crops are arranged in rows.
  69.             // TODO: Check rows .. muuhhhuu
  70.             bool cutHalf = false;
  71.             cutHalf |= west.GetType() == target.GetType();
  72.             cutHalf |= east.GetType() == target.GetType();
  73.             cutHalf |= south.GetType() == target.GetType();
  74.             cutHalf |= north.GetType() == target.GetType();
  75.             cutHalf |= northEast.GetType() == target.GetType();
  76.             cutHalf |= northWest.GetType() == target.GetType();
  77.             cutHalf |= southEast.GetType() == target.GetType();
  78.             cutHalf |= southWest.GetType() == target.GetType();
  79.             points /= cutHalf ? 2 : 1;
  80.  
  81.             //1 / (floor(25 / points) + 1)
  82.  
  83.             double chance = 1/(Math.Floor(25/points) + 1);
  84.  
  85.             var calculateGrowthChance = level.Random.NextDouble() <= chance;
  86.             //Log.Debug($"Calculated growth chance. Will grow={calculateGrowthChance} on a chance score of {chance}");
  87.             return calculateGrowthChance;
  88.         }
  89.  
  90.         protected override bool CanPlace(Level world, BlockCoordinates blockCoordinates, BlockCoordinates targetCoordinates, BlockFace face)
  91.         {
  92.             if (base.CanPlace(world, blockCoordinates, face))
  93.             {
  94.                 Block under = world.GetBlock(Coordinates + BlockCoordinates.Down);
  95.                 return under is Farmland;
  96.             }
  97.  
  98.             return false;
  99.         }
  100.  
  101.         public override void BlockUpdate(Level level, BlockCoordinates blockCoordinates)
  102.         {
  103.             if (Coordinates + BlockCoordinates.Down == blockCoordinates)
  104.             {
  105.                 level.BreakBlock(this);
  106.             }
  107.         }
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement