Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Useful Frequently Used Algorithms

Comments in 'Resources' started by PEMapModder, Oct 26, 2014.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Remember, this list is not finished. If you have suggestions, improvements, corrections, questions or requests, post them below. If you want to talk to me with instant messaging, find me on IRC channel #pmplugins.

    Creating a cuboid
    Parameters:

    • $xa: x coordinate to start from
    • $ya: y coordinate to start from
    • $za: z coordinate to start from
    • $xb: x coordinate to end at
    • $yb: y coordinate to end at
    • $zb: z coordinate to end at
    • $block: the Block object to placed
    • $level: the Level to be placed in
    PHP:
    for($x $xa$x <= $xb$x++){
      for(
    $y $ya$y <= $yb$y++){
        for(
    $z $za$z <= $zb$z++){
          
    $level->setBlock(new Vector3($x$y$z), $blockfalsefalse);
        }
      }
    }
    The nearby blocks are not updated for performance issues. Nevertheless, you can update the surrounding block. I am not showing you how here since I'm lazy, but maybe I'll add later.

    Creating a sphere (similar theory for circles, but I won't add them here since the principle is the same)
    Parameters:

    • $center: a Position object that represents the center of the sphere
    • $radius: the radius of the sphere
    • $block: the Block object to fill the sphere with
    PHP:
    $r2 $radius ** 2// radius squared
    for($x floor($center->$radius); $x <= ceil($center->$radius); $x++){
      for(
    $y floor($center->$radius); $y <= ceil($center->$radius); $y++){
        for(
    $z floor($center->$radius); $z <= ceil($center->$radius); $z++){
          
    // this is the cube that exactly includes the sphere
          
    $v3 = new Vector3($x$y$z);
          if(
    $v3->distanceSquared($center) <= $radius){ // I used distance squared because this is faster, without having to square root every time; and if I call from $v3 instead of $center, it only has to check coordinates not the level name, also making it faster
            // if distance between $v3 and the center is less than or equal to radius
            
    $center->getLevel()->setBlock($v3$blockfalsefalse);
          }
        }
      }
    }
    Same with the surrounding blocks. Creating an algorithm for hollow circles requires certain technique and thinking, so I'm going to make sure I'm correct before posting.

    Also, the algorithm for generating spheres can be developed from the same algorithm as that to generate circle, and the algorithm for generating circles can be derived from this one.

    Picking a random elementfrom a list of elements, each element with a weight (the relative chance to be picked)
    This algorithm is especially hard when it is decimal, where you can't simply use mt_rand(0, $sumOfWeight) to pick, or to spread and map them in an array.

    In this algorithm, the chance of selecting the first element is slightly biased higher (to a negligible chance of 1 / mt_getrandmax())

    Parameters:
    • $items: the list of elements to select from. The array should be in this format:
    Code:
    [
      [$weight0, $item0],
      [$weight1, $item1],
      ...
    ]
    
    Result: $selected
    PHP:
    $sum 0;
    foreach(
    $items as $item){
      
    $sum += $item[0]; // add the item weight to the sum
    }
    $ratio mt_getrandmax() / $sum// multiply this with the weight to compare to a random figure
    $current 0// starting from 0
    $random mt_rand(); // a random inclusively from 0 to mt_getrandmax()
    foreach($items as $item){
      
    $current += $ratio $item[0];
      if(
    $current >= $random){ // deciding whether it is <= or < is a hard thin . But if we use <, there might never be an answer if somehow if $random equals the max
        
    $selected $item[1];
        break;
      }
    }
    Last edited: Oct 28, 2014
    luca28pet, TigerHix, iksaku and 3 others like this.
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Great idea!
    LDX likes this.
  3. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,434
    Plugins:
    14
    Looks good, but could you please un-bold the text?
    Last edited: Oct 26, 2014
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    I cleaned it up a little.
    PEMapModder and LDX like this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    A deadly mistake in the third algorithm: <= should be >=. I fixed it, now it is really "slightly biased to the first one" instead of always ;)

    The story behind this change:
    Last edited: Oct 27, 2014
    LDX likes this.

Share This Page

Advertisement