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), $block, false, false); } }} 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 squaredfor($x = floor($center->x - $radius); $x <= ceil($center->x + $radius); $x++){ for($y = floor($center->y - $radius); $y <= ceil($center->y + $radius); $y++){ for($z = floor($center->z - $radius); $z <= ceil($center->z + $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, $block, false, false); } } }} 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; }}
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: