RandomNode.hx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package arm.logic;
  2. import zui.Nodes;
  3. import arm.logic.LogicNode;
  4. import arm.Translator._tr;
  5. @:keep
  6. class RandomNode extends LogicNode {
  7. public function new(tree: LogicTree) {
  8. super(tree);
  9. }
  10. override function get(from: Int, done: Dynamic->Void) {
  11. inputs[0].get(function(min: Float) {
  12. inputs[1].get(function(max: Float) {
  13. done(min + getFloat() * (max - min));
  14. });
  15. });
  16. }
  17. static var a: Int;
  18. static var b: Int;
  19. static var c: Int;
  20. static var d = setSeed(352124);
  21. public static function setSeed(seed: Int): Int {
  22. d = seed;
  23. a = 0x36aef51a;
  24. b = 0x21d4b3eb;
  25. c = 0xf2517abf;
  26. // Immediately skip a few possibly poor results the easy way
  27. for (i in 0...15) {
  28. getInt();
  29. }
  30. return d;
  31. }
  32. public static function getSeed(): Int {
  33. return d;
  34. }
  35. // Courtesy of https://github.com/Kode/Kha/blob/main/Sources/kha/math/Random.hx
  36. public static function getInt(): Int {
  37. var t = (a + b | 0) + d | 0;
  38. d = d + 1 | 0;
  39. a = b ^ b >>> 9;
  40. b = c + (c << 3) | 0;
  41. c = c << 21 | c >>> 11;
  42. c = c + t | 0;
  43. return t & 0x7fffffff;
  44. }
  45. public static function getFloat(): Float {
  46. return getInt() / 0x7fffffff;
  47. }
  48. public static var def: TNode = {
  49. id: 0,
  50. name: _tr("Random"),
  51. type: "RandomNode",
  52. x: 0,
  53. y: 0,
  54. color: 0xffb34f5a,
  55. inputs: [
  56. {
  57. id: 0,
  58. node_id: 0,
  59. name: _tr("Min"),
  60. type: "VALUE",
  61. color: 0xffa1a1a1,
  62. default_value: 0.0
  63. },
  64. {
  65. id: 0,
  66. node_id: 0,
  67. name: _tr("Max"),
  68. type: "VALUE",
  69. color: 0xffa1a1a1,
  70. default_value: 1.0
  71. }
  72. ],
  73. outputs: [
  74. {
  75. id: 0,
  76. node_id: 0,
  77. name: _tr("Value"),
  78. type: "VALUE",
  79. color: 0xffa1a1a1,
  80. default_value: 0.5
  81. }
  82. ],
  83. buttons: []
  84. };
  85. }