/Assets/Scripts/Genetics/GeneticsMain.cs

https://bitbucket.org/clomax/evosim · C# · 121 lines · 79 code · 22 blank · 20 comment · 9 complexity · 0879d5c0dd7f169c69cee86a2b4f354f MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. /*
  4. * Author: Craig Lomax
  5. * Date: 06.09.2011
  6. * URL: clomax.me.uk
  7. * email: craig@clomax.me.uk
  8. *
  9. */
  10. public class GeneticsMain : MonoBehaviour {
  11. private Ether eth;
  12. private Settings settings;
  13. private Spawner spw;
  14. public static GameObject container;
  15. public static GeneticsMain instance;
  16. Chromosome chromosome;
  17. int starting_creatures;
  18. float creature_spread;
  19. Vector3 max_root_scale;
  20. Vector3 min_root_scale;
  21. void Start () {
  22. spw = Spawner.getInstance();
  23. settings = Settings.getInstance();
  24. eth = Ether.getInstance();
  25. max_root_scale = new Vector3();
  26. max_root_scale.x = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["x"].ToString() );
  27. max_root_scale.y = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["y"].ToString() );
  28. max_root_scale.z = float.Parse( settings.contents["creature"]["root"]["max_root_scale"]["z"].ToString() );
  29. min_root_scale = new Vector3();
  30. min_root_scale.x = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["x"].ToString() );
  31. min_root_scale.y = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["y"].ToString() );
  32. min_root_scale.z = float.Parse( settings.contents["creature"]["root"]["min_root_scale"]["z"].ToString() );
  33. starting_creatures = (int) settings.contents["ether"]["starting_creatures"];
  34. creature_spread = float.Parse( settings.contents["ether"]["creature_spread"].ToString() );
  35. double init_energy = (double) settings.contents["creature"]["init_energy"];
  36. int branch_limit = (int) settings.contents["creature"]["branch_limit"];
  37. int recursion_limit = (int) settings.contents["creature"]["recursion_limit"];
  38. /*
  39. * For each new creature, generate random genes and spawn the bugger
  40. */
  41. for (int i=0; i<starting_creatures; i++) {
  42. chromosome = new Chromosome();
  43. // random colours
  44. Color col = new Color( (float)Random.Range(0.0F,1.0F),
  45. (float)Random.Range(0.0F,1.0F),
  46. (float)Random.Range(0.0F,1.0F)
  47. );
  48. chromosome.setColour(col.r, col.g, col.b);
  49. // random root scale
  50. chromosome.setRootScale((float) Random.Range(min_root_scale.x,max_root_scale.x),
  51. (float) Random.Range(min_root_scale.y,max_root_scale.y),
  52. (float) Random.Range(min_root_scale.z,max_root_scale.z)
  53. );
  54. // random initial limbs
  55. int branches = Random.Range (1,branch_limit);
  56. chromosome.setBranches(branches);
  57. for (int j=0; j<branches; j++) {
  58. /* Randomly select point on root's surface */
  59. Vector3 tmp = new Vector3(0F,0F,0F);
  60. // set all axes to random float between -.5 and .5
  61. for (int k=0; k<3; k++) {
  62. tmp[k] = Random.Range(-0.5F, 0.5F);
  63. }
  64. // randomly select between x,y,z
  65. int axis = Random.Range(0,3);
  66. // randomly set that axis to -.5 or .5
  67. int rnd = Random.Range(0,1);
  68. if (rnd == 0) tmp[axis] = -0.5F;
  69. else tmp[axis] = 0.5F;
  70. // set anchor point to new vector
  71. Vector3 point = tmp;
  72. //Debug.Log(point);
  73. Vector3 scale = new Vector3 (2F,2F,5F);
  74. int recurrances = Random.Range(0,recursion_limit);
  75. chromosome.addLimb(col, point, scale, recurrances);
  76. int recursion = Random.Range(0,recursion_limit);
  77. for (int k=0; k<recursion; k++) {
  78. // create new limb
  79. point = Vector3.zero; // Position is not a factor in child limbs, set it to zero
  80. scale = new Vector3 (2F,2F,5F);
  81. recurrances -= 1;
  82. chromosome.addLimb(col, point, scale, recurrances);
  83. }
  84. }
  85. if (eth.enoughEnergy(init_energy)) {
  86. spw.spawn(Utility.RandomFlatVec(-creature_spread,10,creature_spread), Utility.RandomRotVec(), init_energy, chromosome);
  87. eth.subtractEnergy(init_energy);
  88. }
  89. }
  90. }
  91. public static GeneticsMain getInstance () {
  92. if(!instance) {
  93. container = new GameObject();
  94. container.name = "GeneticsMain";
  95. instance = container.AddComponent<GeneticsMain>();
  96. }
  97. return instance;
  98. }
  99. }