Advertisement
Guest User

Untitled

a guest
Dec 5th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. /*
  2. * Copyright 2012 Benjamin Glatzel <benjamin.glatzel@me.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.terasology.world.generator.tree;
  17.  
  18. import java.util.HashMap;
  19. import java.util.Stack;
  20.  
  21. import javax.vecmath.AxisAngle4f;
  22. import javax.vecmath.Matrix4f;
  23. import javax.vecmath.Vector3f;
  24.  
  25. import org.terasology.utilities.FastRandom;
  26. import org.terasology.world.WorldView;
  27. import org.terasology.world.block.Block;
  28. import org.terasology.world.block.management.BlockManager;
  29.  
  30. /**
  31. * Allows the generation of complex trees based on L-Systems.
  32. *
  33. * @author Benjamin Glatzel <benjamin.glatzel@me.com>
  34. */
  35. public class TreeGeneratorLSystem extends TreeGenerator {
  36.  
  37. public final int MAX_ANGLE_OFFSET = 5;
  38.  
  39. /* SETTINGS */
  40. private int iterations;
  41. private double angleInDegree;
  42. private Block air;
  43. private Block leafType;
  44. private Block barkType;
  45.  
  46. /* RULES */
  47. private final String initialAxiom;
  48. private final HashMap<String, String> ruleSet;
  49. private final HashMap<String, Double> probabilities;
  50.  
  51. /**
  52. * Init. a new L-System based tree generator.
  53. *
  54. * @param initialAxiom The initial axiom to use
  55. * @param ruleSet The rule set to use
  56. * @param probabilities The probability array
  57. * @param iterations The amount of iterations to execute
  58. * @param angle The angle
  59. */
  60. public TreeGeneratorLSystem(String initialAxiom, HashMap<String, String> ruleSet, HashMap<String, Double> probabilities, int iterations, int angle) {
  61. angleInDegree = angle;
  62. this.iterations = iterations;
  63. air = BlockManager.getInstance().getAir();
  64. leafType = BlockManager.getInstance().getBlock("engine:GreenLeaf");
  65. barkType = BlockManager.getInstance().getBlock("engine:OakTrunk");
  66.  
  67. this.initialAxiom = initialAxiom;
  68. this.ruleSet = ruleSet;
  69. this.probabilities = probabilities;
  70. }
  71.  
  72. @Override
  73. public void generate(WorldView view, FastRandom rand, int posX, int posY, int posZ) {
  74.  
  75. String axiom = initialAxiom;
  76.  
  77. Stack<Vector3f> _stackPosition = new Stack<Vector3f>();
  78. Stack<Matrix4f> _stackOrientation = new Stack<Matrix4f>();
  79.  
  80. for (int i = 0; i < iterations; i++) {
  81.  
  82. String temp = "";
  83.  
  84. for (int j = 0; j < axiom.length(); j++) {
  85. String c = String.valueOf(axiom.charAt(j));
  86.  
  87. double rValue = (rand.randomDouble() + 1.0) / 2.0;
  88.  
  89. if (ruleSet.containsKey(c) && probabilities.get(c) > (1.0 - rValue))
  90. temp += ruleSet.get(c);
  91. else
  92. temp += c;
  93. }
  94.  
  95. axiom = temp;
  96. }
  97.  
  98. Vector3f position = new Vector3f(0, 0, 0);
  99.  
  100. Matrix4f rotation = new Matrix4f();
  101. rotation.setIdentity();
  102. rotation.setRotation(new AxisAngle4f(new Vector3f(0, 0, 1), (float) Math.PI / 2.0f));
  103.  
  104. int angleOffset = rand.randomInt() % MAX_ANGLE_OFFSET;
  105.  
  106. for (int i = 0; i < axiom.length(); i++) {
  107. char c = axiom.charAt(i);
  108.  
  109. Matrix4f tempRotation = new Matrix4f();
  110. tempRotation.setIdentity();
  111.  
  112. switch (c) {
  113. case 'G':
  114. case 'F':
  115. // Tree trunk
  116. view.setBlock(posX + (int) position.x + 1, posY + (int) position.y, posZ + (int) position.z, barkType, view.getBlock(posX + (int) position.x + 1, posY + (int) position.y, posZ + (int) position.z));
  117. view.setBlock(posX + (int) position.x - 1, posY + (int) position.y, posZ + (int) position.z, barkType, view.getBlock(posX + (int) position.x - 1, posY + (int) position.y, posZ + (int) position.z));
  118. view.setBlock(posX + (int) position.x, posY + (int) position.y, posZ + (int) position.z + 1, barkType, view.getBlock(posX + (int) position.x, posY + (int) position.y, posZ + (int) position.z + 1));
  119. view.setBlock(posX + (int) position.x, posY + (int) position.y, posZ + (int) position.z - 1, barkType, view.getBlock(posX + (int) position.x, posY + (int) position.y, posZ + (int) position.z - 1));
  120.  
  121. // Generate leaves
  122. if (_stackOrientation.size() > 1) {
  123. int size = 1;
  124.  
  125. for (int x = -size; x <= size; x++) {
  126. for (int y = -size; y <= size; y++) {
  127. for (int z = -size; z <= size; z++) {
  128. if (Math.abs(x) == size && Math.abs(y) == size && Math.abs(z) == size)
  129. continue;
  130.  
  131. view.setBlock(posX + (int) position.x + x + 1, posY + (int) position.y + y, posZ + z + (int) position.z, leafType, air);
  132. view.setBlock(posX + (int) position.x + x - 1, posY + (int) position.y + y, posZ + z + (int) position.z, leafType, air);
  133. view.setBlock(posX + (int) position.x + x, posY + (int) position.y + y, posZ + z + (int) position.z + 1, leafType, air);
  134. view.setBlock(posX + (int) position.x + x, posY + (int) position.y + y, posZ + z + (int) position.z - 1, leafType, air);
  135. }
  136. }
  137. }
  138. }
  139.  
  140. Vector3f dir = new Vector3f(1, 0, 0);
  141. rotation.transform(dir);
  142.  
  143. position.add(dir);
  144. break;
  145. case '[':
  146. _stackOrientation.push(new Matrix4f(rotation));
  147. _stackPosition.push(new Vector3f(position));
  148. break;
  149. case ']':
  150. rotation = _stackOrientation.pop();
  151. position = _stackPosition.pop();
  152. break;
  153. case '+':
  154. tempRotation.setIdentity();
  155. tempRotation.setRotation(new AxisAngle4f(new Vector3f(0, 0, 1), (float) Math.toRadians(angleInDegree + angleOffset)));
  156. rotation.mul(tempRotation);
  157. break;
  158. case '-':
  159. tempRotation.setIdentity();
  160. tempRotation.setRotation(new AxisAngle4f(new Vector3f(0, 0, -1), (float) Math.toRadians(angleInDegree + angleOffset)));
  161. rotation.mul(tempRotation);
  162. break;
  163. case '&':
  164. tempRotation.setIdentity();
  165. tempRotation.setRotation(new AxisAngle4f(new Vector3f(0, 1, 0), (float) Math.toRadians(angleInDegree + angleOffset)));
  166. rotation.mul(tempRotation);
  167. break;
  168. case '^':
  169. tempRotation.setIdentity();
  170. tempRotation.setRotation(new AxisAngle4f(new Vector3f(0, -1, 0), (float) Math.toRadians(angleInDegree + angleOffset)));
  171. rotation.mul(tempRotation);
  172. break;
  173. case '*':
  174. tempRotation.setIdentity();
  175. tempRotation.setRotation(new AxisAngle4f(new Vector3f(1, 0, 0), (float) Math.toRadians(angleInDegree)));
  176. rotation.mul(tempRotation);
  177. break;
  178. case '/':
  179. tempRotation.setIdentity();
  180. tempRotation.setRotation(new AxisAngle4f(new Vector3f(-1, 0, 0), (float) Math.toRadians(angleInDegree)));
  181. rotation.mul(tempRotation);
  182. break;
  183. }
  184. }
  185. }
  186.  
  187. public TreeGenerator setLeafType(Block b) {
  188. leafType = b;
  189. return this;
  190. }
  191.  
  192.  
  193. public TreeGenerator setBarkType(Block b) {
  194. barkType = b;
  195. return this;
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement