PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
BiomeSelector.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world\generator\biome;
25
31
32abstract class BiomeSelector{
33 private Simplex $temperature;
34 private Simplex $rainfall;
35
40 private \SplFixedArray $map;
41
42 public function __construct(Random $random){
43 $this->temperature = new Simplex($random, 2, 1 / 16, 1 / 512);
44 $this->rainfall = new Simplex($random, 2, 1 / 16, 1 / 512);
45 }
46
52 abstract protected function lookup(float $temperature, float $rainfall) : int;
53
54 public function recalculate() : void{
55 $this->map = new \SplFixedArray(64 * 64);
56
57 $biomeRegistry = BiomeRegistry::getInstance();
58 for($i = 0; $i < 64; ++$i){
59 for($j = 0; $j < 64; ++$j){
60 $biome = $biomeRegistry->getBiome($this->lookup($i / 63, $j / 63));
61 if($biome instanceof UnknownBiome){
62 throw new \RuntimeException("Unknown biome returned by selector with ID " . $biome->getId());
63 }
64 $this->map[$i + ($j << 6)] = $biome;
65 }
66 }
67 }
68
69 public function getTemperature(float $x, float $z) : float{
70 return ($this->temperature->noise2D($x, $z, true) + 1) / 2;
71 }
72
73 public function getRainfall(float $x, float $z) : float{
74 return ($this->rainfall->noise2D($x, $z, true) + 1) / 2;
75 }
76
77 public function pickBiome(float $x, float $z) : Biome{
78 $temperature = (int) ($this->getTemperature($x, $z) * 63);
79 $rainfall = (int) ($this->getRainfall($x, $z) * 63);
80
81 return $this->map[$temperature + ($rainfall << 6)];
82 }
83}
lookup(float $temperature, float $rainfall)