Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.02 KB | None | 0 0
  1. package;
  2.  
  3. import haxe.ds.Map;
  4. // import kha.Assets;
  5. // import kha.Framebuffer;
  6. // import kha.Scheduler;
  7. // import kha.System;
  8. import kha.math.Random;
  9.  
  10. interface IRange {
  11.     public var from(get, null):Int;
  12.     public var to(get, null):Int;
  13. }
  14.  
  15. class Range implements IRange {
  16.     @:isVar public var from(get, null):Int;
  17.     @:isVar public var to(get, null):Int;
  18.  
  19.     public function new(from:Int, to:Int) {
  20.         this.from = from;
  21.         this.to = to;
  22.     }
  23.  
  24.     function get_from() {
  25.         return from;
  26.     }
  27.  
  28.     function get_to() {
  29.         return to;
  30.     }
  31. }
  32.  
  33. class ThingByFactor {
  34.     @:isVar public var factorMap(default, null):Map<String, Range> = new Map();
  35.     @:isVar public var factorValueMap(default, null):Map<String, Int> = new Map();
  36.     @:isVar public var reducers(default, null):Map<String, Float> = new Map();
  37.  
  38.     public function new() {}
  39.  
  40.     public function addFactor(indent:String, range:Range, getReducer:Range->Int->ThingByFactor->Float) {
  41.         this.factorMap[indent] = range;
  42.         this.factorValueMap[indent] = Random.getIn(range.from, range.to);
  43.         this.reducers[indent] = getReducer(range, this.factorValueMap[indent], this);
  44.     }
  45.  
  46.     public function pipe(outIndent:String, factorIdent:String, range:Range, weight:Int, ?getReducer:Range->Int->ThingByFactor->Float) {
  47.         if (Math.isNaN(this.factorValueMap[outIndent])) {
  48.             this.factorValueMap[outIndent] = range.to;
  49.         }
  50.  
  51.         var y = this.factorValueMap[outIndent];
  52.         this.factorValueMap[outIndent] = this.factorValueMap[outIndent] - Math.round((this.reducers[factorIdent] * weight) * range.to);
  53.         trace(this.factorValueMap[outIndent] + " = " + y + " - Math.round((" + this.reducers[factorIdent] + "("+this.factorValueMap[factorIdent]+")" + " * " + weight + ") * " + range.to + ")");
  54.         this.reducers[outIndent] = getReducer(range, this.factorValueMap[outIndent], this);
  55.     }
  56.  
  57.     public function getValueByIdent(outIndent:String){
  58.         return this.factorValueMap[outIndent];
  59.     }
  60. }
  61.  
  62. class Plant {
  63.     var secPercent:Int;
  64.     var intHotTime:Bool = true;
  65.  
  66.     public function new(secPercent:Int, intHotTime:Bool = true, seed:Int = 111112) {
  67.         if (secPercent < 0 || secPercent > 100) {
  68.             throw "secPercent must be in range of [0, 100]";
  69.         }
  70.  
  71.         this.secPercent = secPercent;
  72.         this.intHotTime = intHotTime;
  73.     }
  74.  
  75.     public function outputPlantData() {
  76.        
  77.         Random.init(2212222);
  78.  
  79.         var tbf = new ThingByFactor();
  80.  
  81.         //----------------------------------------------------------------------------------------------------
  82.  
  83.         tbf.addFactor("rainGramsPerSecond", new Range(0, 1000), function(range:Range, value:Int, tbf:ThingByFactor) {
  84.             return (value / range.to);
  85.         });
  86.         tbf.addFactor("soilAcidity", new Range(0, 100), function(range:Range, value:Int, tbf:ThingByFactor) {
  87.             return Math.abs((range.to / 2) -value) / (range.to / 2);
  88.         });
  89.         tbf.addFactor("coldTimeInSecs", new Range(31557600, 31557600 * 5), function(range:Range, value:Int, tbf:ThingByFactor) {
  90.             return 1 - (value / range.to);
  91.         });
  92.         tbf.addFactor("hotTimeInSecs", new Range(31557600, 31557600 * 5), function(range:Range, value:Int, tbf:ThingByFactor) {
  93.             return Math.abs((range.to / 2) -value) / (range.to / 2);
  94.         });
  95.         tbf.addFactor("midHotTimeSunRadiation", new Range(0, 1000), function(range:Range, value:Int, tbf:ThingByFactor) {
  96.             return Math.abs((range.to / 2) -value) / (range.to / 2);
  97.         });
  98.         tbf.addFactor("midColdTimeSunRadiation", new Range(0, 1000), function(range:Range, value:Int, tbf:ThingByFactor) {
  99.             return Math.abs((range.to / 2) -value) / (range.to / 2);
  100.         });
  101.  
  102.         //----------------------------------------------------------------------------------------------------
  103.  
  104.         tbf.pipe("plantLength", "rainGramsPerSecond", new Range(0, 1000), 1, function(range:Range, value:Int, tbf:ThingByFactor) {
  105.             return 1.0;
  106.         });
  107.         tbf.pipe("plantLength", "coldTimeInSecs", new Range(0, 1000), 1, function(range:Range, value:Int, tbf:ThingByFactor) {
  108.             return 1.0;
  109.         });
  110.         tbf.pipe("plantLength", "hotTimeInSecs", new Range(0, 1000), 1, function(range:Range, value:Int, tbf:ThingByFactor) {
  111.             return 1.0;
  112.         });
  113.         tbf.pipe("plantLength", "midHotTimeSunRadiation", new Range(0, 1000), 1, function(range:Range, value:Int, tbf:ThingByFactor) {
  114.             return 1.0;
  115.         });
  116.         tbf.pipe("plantLength", "soilAcidity", new Range(0, 1000), 1, function(range:Range, value:Int, tbf:ThingByFactor) {
  117.             return 1.0;
  118.         });
  119.  
  120.         //----------------------------------------------------------------------------------------------------
  121.     }
  122. }
  123.  
  124. class Main {
  125.     /*static function update():Void {}
  126.         static function render(frames:Array<Framebuffer>):Void {
  127.     }*/
  128.     public static function main() {
  129.         var p = new Plant(20, true);
  130.         p.outputPlantData();
  131.  
  132.         /*System.start({title: "Project", width: 1024, height: 768}, function(_) {
  133.             // Just loading everything is ok for small projects
  134.             Assets.loadEverything(function() {
  135.                 // Avoid passing update/render directly,
  136.                 // so replacing them via code injection works
  137.                 Scheduler.addTimeTask(function() {
  138.                     update();
  139.                 }, 0, 1 / 60);
  140.                 System.notifyOnFrames(function(frames) {
  141.                     render(frames);
  142.                 });
  143.             });
  144.         });*/
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement