Skip to content

Commit

Permalink
v0.2.0 neural paint with mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
benrayfield committed Nov 21, 2017
1 parent ecbad78 commit 7f50d47
Show file tree
Hide file tree
Showing 21 changed files with 891 additions and 16 deletions.
3 changes: 0 additions & 3 deletions META-INF/MANIFEST.MF

This file was deleted.

3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: timelessCellularAutomata.crbm.Start

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleUnaryOperator;

public class MathUtil{
Expand Down Expand Up @@ -73,8 +74,17 @@ public static boolean weightedCoinFlip(double chance){
/** Consumes an average of 2 random bits (so its practical to use SecureRandom which is slow)
by consuming random bits until get the first 1 then going directly to that digit
in the chance as a binary fraction and returning it as the weighted random bit observe.
TODO I wrote that code somewhere, copy it here so its more practical more often to use SecureRandom.
*/
public static boolean weightedCoinFlip(double chance, BooleanSupplier rand){
if(chance < 0 || 1 < chance) throw new ArithmeticException("chance="+chance);
while(rand.getAsBoolean()){
if(.5 <= chance) chance -= .5;
chance *= 2;
}
return .5 <= chance;
}

/** same as weightedCoinFlip(double,BooleanSupplier) */
public static boolean weightedCoinFlip(double chance, Random rand){
if(chance < 0 || 1 < chance) throw new ArithmeticException("chance="+chance);
while(rand.nextBoolean()){
Expand Down
29 changes: 29 additions & 0 deletions src/humanaicore/common/Parallel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** Ben F Rayfield offers HumanAiCore opensource GNU LGPL */
package humanaicore.common;
import java.util.concurrent.ForkJoinPool;

public class Parallel{
private Parallel(){}

public static final ForkJoinPool cpus;
static{
int howManyCpus = Runtime.getRuntime().availableProcessors();
/*ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory(){
public ForkJoinWorkerThread newThread(ForkJoinPool pool){
ForkJoinWorkerThread t = new ForkJoinWorkerThread(pool){
};
}
};
UncaughtExceptionHandler handler = new UncaughtExceptionHandler(){
public void uncaughtException(Thread t, Throwable e){
throw new Err(e);
}
};
boolean asyncMode = false;
cpus = new ForkJoinPool(howManyCpus, factory, handler, asyncMode);
*/
cpus = new ForkJoinPool(howManyCpus);
}

}
15 changes: 15 additions & 0 deletions humanaicore/common/Rand.java → src/humanaicore/common/Rand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@
package humanaicore.common;
import java.security.SecureRandom;
import java.util.Random;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;

public class Rand{
private Rand(){}

public static final Random weakRand;

public static final SecureRandom strongRand;

public static final BooleanSupplier strongRandBits;

/** 0 (inclusive) to 1 (exclusive) */
public static final DoubleSupplier strongRandInclExclFractions;

/** ave 0 stdDev 1 */
public static final DoubleSupplier strongRandBell;

static{
strongRand = new SecureRandom();
//TODO set seed as bigger byte array, more hashcodes to fill it maybe
strongRand.setSeed(3+System.nanoTime()*49999+System.currentTimeMillis()*new Object().hashCode());
weakRand = new Random(strongRand.nextLong());
strongRandBits = ()->strongRand.nextBoolean();
strongRandInclExclFractions = ()->strongRand.nextDouble();
strongRandBell = ()->strongRand.nextGaussian();
}

}
46 changes: 46 additions & 0 deletions src/humanaicore/common/SVar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package humanaicore.common;
//import static log.Lg.*;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

/** Synchronized Var */
public class SVar<T> implements Supplier<T>, Consumer<T>{

protected T value;

protected final Set<Consumer<SVar<T>>> listeners = new HashSet();

public SVar(T firstValue){
value = firstValue;
//lgErr("Constructor SVar.value="+this.value);
}

public synchronized void startListening(Consumer<SVar<T>> listener){
listeners.add(listener);
}

public synchronized void stopListening(Consumer<SVar<T>> listener){
listeners.remove(listener);
}

public synchronized void stopAllListeners(){
listeners.clear();
}

/** Unsynchronized for speed, allowed cuz a pointer wont be split by thread-error */
public T get(){
//lgErr("Get SVar.value="+this.value);
return value;
}

public synchronized void accept(T value){
this.value = value;
//lgErr("Set SVar.value="+this.value);
for(Consumer<SVar<T>> listener : listeners){
listener.accept(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public static void moveToScreenCenter(Window w){
w.setLocation((screen.width-w.getWidth())/2, (screen.height-w.getHeight())/2);
}

/** Unlike new Color(r,g,b).getRGB(), 1-epsilon rounds to brightest.
/** Unlike new Color(r,g,b).getRGB(), 1-epsilon rounds to brightest,
and they are spread evenly over all 256 possible values per color dim byte.
Truncates red, green, and blue into range 0 to 1 if needed.
*
*/
public static int color(float red, float green, float blue){
return 0xff000000 |
(MathUtil.holdInRange(0, (int)(red*0x100), 0xff) << 16) |
Expand All @@ -28,7 +29,7 @@ public static int color(float alpha, float red, float green, float blue){
(MathUtil.holdInRange(0, (int)(red*0x100), 0xff) << 16) |
(MathUtil.holdInRange(0, (int)(green*0x100), 0xff) << 8) |
MathUtil.holdInRange(0, (int)(blue*0x100), 0xff);
}*/
}

public static Window getWindow(Component c){
while(!(c instanceof Window)) c = c.getParent();
Expand Down
74 changes: 74 additions & 0 deletions src/humanaicore/common/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/** Ben F Rayfield offers HumanAiCore opensource GNU LGPL */
package humanaicore.common;

import java.text.DecimalFormat;

public class Time{
private Time(){}

public static final long startMillis;

public static final long startNano;

public static final int secondsPerDay = 24*60*60;

protected static final DecimalFormat secondsFormat = new DecimalFormat("0000000000.0000000");

protected static final DecimalFormat stardateFormat = new DecimalFormat("00000.000000000000");

static{
startMillis = System.currentTimeMillis();
startNano = System.nanoTime();
}

/** Seconds since year 1970
with relative nanosecond precision (System.nanoTime)
and absolute few milliseconds precision (System.currentTimeMillis).
<br><br>
Practically, at least in normal computers in year 2011, this has about microsecond precision
because you can only run it a few million times per second.
TODO test it again on newer computers.
*/
public static double time(){
//TODO optimize by caching the 2 start numbers into 1 double */
long nanoDiff = System.nanoTime()-startNano;
return .001*startMillis + 1e-9*nanoDiff;
}

public static String timeStr(){
return timeStr(time());
}

public static String timeStr(double time){
return secondsFormat.format(time);
}

public static String stardateStr(){
return stardateStr(time());
}

/** number of 24 hour blocks since year 1970 */
public static String stardateStr(double time){
return stardateFormat.format(time/secondsPerDay);
}

/** Uses Thread.sleep(milliseconds,nanoseconds) for extra accuracy,
but don't count on Java running threads often enough to use the extra accuracy on all computers.
*/
public static void sleep(double seconds) throws InterruptedException{
if(seconds <= 0) return;
double millis = seconds*1e3;
long millisL = (long)millis;
millis -= millisL;
double nanos = millis*1e6;
int nanosI = (int)Math.round(nanos);
Thread.sleep(millisL, nanosI);
}

public static void sleepNoThrow(double seconds){
try{
sleep(seconds);
}catch(InterruptedException e){}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

/** Interactive pixel grid that stretches to panel size, normally low resolution
because of the slowness of an IntBinaryOperator function call per magnified pixel.
Remembers which (TODO) keys and mouse buttons are up and down and mouse position
Remembers which keys and mouse buttons are up and down and mouse position
and which square the mouse is over. Displays colored squares
as IntBinaryOperator of (y,x) to colorARGB.
Example: BiFunction which displays the vars in Ainodes each in an NxN smaller square.
*/
public class StretchVideo extends JPanel implements MouseListener, MouseMotionListener{

Expand Down
76 changes: 76 additions & 0 deletions src/log/Lg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package log;


/** Use the following line at the top of each source code file:
import static commonfuncs.CommonFuncs.*;
<br><br>
This is more like functional programming than object oriented.
*/
public class Lg{
private Lg(){}

public static void lgNobr(String s){
System.out.print(s);
}

public static void lg(Object o){
lg(o.toString()); //could have ""+o but thats slower
}

public static void lg(String line){
System.out.println(line);
}

public static void lgErr(Object o){
lgErr(o.toString()); //could have ""+o but thats slower
}

public static void lgErr(String line){
System.err.println(line);
}

public static void lgToUser(Object o){
lgToUser(o.toString()); //could have ""+o but thats slower
}

public static void lgToUser(String line){
System.err.println(line);
}

/** Since JSelfModify doesnt have a User system yet, just room for expansion,
this function uses JSelfModify.rootUser as is normally done.
WARNING: This kind of thing will need to be redesigned securely when we start having usernames.
*
public static Object jsmGet(String path){
try{
return JSelfModify.root.get(JSelfModify.rootUser, path);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/** See WARNING in jsmGet *
public static void jsmPut(String path, Object value){
try{
JSelfModify.root.put(JSelfModify.rootUser, path, value);
}catch(Exception e){
throw new RuntimeException(e);
}
}
/** See WARNING in jsmGet *
public static boolean jsmExist(String path){
try{
return JSelfModify.root.exist(JSelfModify.rootUser, path);
}catch(Exception e){
throw new RuntimeException(e);
}
}*/

/** returns a debug string summarizing a pointer into an acyc as an object *
public static String str(int pointerIntoAcyc){
return XobUtil.describeGlobal(pointerIntoAcyc);
}*/


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** Ben F Rayfield offers this software opensource GNU GPL 2+ */
package timelessCellularAutomata;
package timelessCellularAutomata.cboltz;

import humanaicore.common.MathUtil;
import humanaicore.common.Rand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/** Ben F Rayfield offers this software opensource GNU GPL 2+ */
package timelessCellularAutomata;

package timelessCellularAutomata.cboltz;
import javax.swing.JFrame;

import humanaicore.common.Rand;
import humanaicore.common.ScreenUtil;
import timelessCellularAutomata.ui.GamePanel;
import timelessCellularAutomata.cboltz.ui.GamePanel;

public class Start{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** Ben F Rayfield offers this software opensource GNU GPL 2+ */
package timelessCellularAutomata.ui;
package timelessCellularAutomata.cboltz.ui;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import humanaicore.common.Rand;
import humanaicore.ui.StretchVideo;
import timelessCellularAutomata.BoltzCellAutomata;
import timelessCellularAutomata.cboltz.BoltzCellAutomata;

public class GamePanel extends StretchVideo{

Expand Down
Loading

0 comments on commit 7f50d47

Please sign in to comment.