Oh wow, finally!t's working now

This commit is contained in:
Andrea Cavalli 2018-04-21 22:05:15 +02:00
parent a591a39d87
commit ca08cef307
5 changed files with 59 additions and 40 deletions

View File

@ -94,9 +94,9 @@ public class Main {
}
if (arg.contains("verbose") || arg.contains("debug")) {
StaticVars.outputLevel = Utils.OUTPUTLEVEL_DEBUG_VERBOSE;
if (arg.contains("uncached")) {
Utils.debugCache = true;
}
}
if (arg.contains("uncached")) {
Utils.debugCache = true;
}
if (arg.contains("ms-dos")) {
Utils.headlessOverride = true;

View File

@ -63,6 +63,8 @@ public class CPUEngine implements GraphicEngine {
public boolean wasResized() {
if (INSTANCE.wasResized) {
r.size = new int[] { INSTANCE.getWidth(), INSTANCE.getHeight() };
if (r.size[0] <= 0) r.size[0] = 1;
if (r.size[1] <= 0) r.size[1] = 1;
CPURenderer.canvas2d = new int[r.size[0] * r.size[1]];
g = new BufferedImage(r.size[0], r.size[1], BufferedImage.TYPE_INT_ARGB);
INSTANCE.wasResized = false;

View File

@ -59,6 +59,8 @@ public class CPURenderer implements Renderer {
y0 += StaticVars.screenPos[1];
double incrementX = Math.abs((double)(x1-x0)/(double)(s1-s0));
double incrementY = Math.abs((double)(y1-y0)/(double)(t1-t0));
boolean flippedX = (x1-x0)/(s1-s0) < 0;
boolean flippedY = (y1-y0)/(t1-t0) < 0;
int oldColor;
int newColor;
final int onex = s0 <= s1 ? 1 : -1;
@ -121,8 +123,11 @@ public class CPURenderer implements Renderer {
int[] newColors = new int[(1+expX*2)*(1+expY*2)];
for (int expXi = -expX; expXi <= expX; expXi++) {
for (int expYi = -expY; expYi <= expY; expYi++) {
int skinIndex = (int) (s0 + (texx+expXi) + (t0 + (texy+expYi)) * currentSkin.skinSize[0]);
newColors[(expXi+expX)+(expYi+expY)*(1+expY*2)] = getSkinColorAt(currentSkin.skinData, skinIndex);
int skinIndex = (int) (s0 + (texx*(flippedX ? -1d : 1d)+(flippedX ? -(s0 - s1)-1 : 0)+expXi) + (t0 + (texy*(flippedY ? -1d : 1d)+(flippedY ? -(t0 - t1)-1 : 0)+expYi)) * currentSkin.skinSize[0]);
int idx = (expXi+expX)+(expYi+expY)*(1+expY*2);
if (idx >= 0 && idx < newColors.length) {
newColors[idx] = getSkinColorAt(currentSkin.skinData, skinIndex);
}
}
}
newColor = joinColors(newColors);
@ -249,7 +254,9 @@ public class CPURenderer implements Renderer {
final int sizeW = size[0];
for (int px = x0; px < x1; px++) {
for (int py = y0; py < y1; py++) {
canvas2d[(px) + (py) * sizeW] = color;
int idx = (px) + (py) * sizeW;
if (px < sizeW && idx >= 0 && idx < canvas2d.length)
canvas2d[idx] = color;
}
}
}
@ -284,10 +291,13 @@ public class CPURenderer implements Renderer {
final int bit = dx + dy * currentFont.charW;
currentInt = (int) (Math.floor(bit) / (CPUFont.intBits));
currentIntBitPosition = bit - (currentInt * CPUFont.intBits);
bitData = (currentFont.chars32[charIndex * currentFont.charIntCount + currentInt] >> currentIntBitPosition) & 1;
screenPos = ix + cpos + dx + (iy + dy) * screenSize[0];
if (bitData == 1 & screenLength > screenPos & screenPos >= 0) {
screen[screenPos] = color;
int charIdx = charIndex * currentFont.charIntCount + currentInt;
if (charIdx >= 0 && charIdx < currentFont.chars32.length) {
bitData = (currentFont.chars32[charIdx] >> currentIntBitPosition) & 1;
screenPos = ix + cpos + dx + (iy + dy) * screenSize[0];
if (bitData == 1 & screenLength > screenPos & screenPos >= 0) {
screen[screenPos] = color;
}
}
}
}

View File

@ -17,7 +17,7 @@ public class MathSolver {
private final Function initialFunction;
private AtomicInteger stepState = new AtomicInteger(0);
private int currentStepStateN = 0;
private int stepStateRepetitions = 0;
private int consecutiveNullSteps = 0;
private enum StepState {
_1_CALCULATION,
@ -39,32 +39,36 @@ public class MathSolver {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", "Solving all steps. Input: " + initialFunction.toString());
currFnc.add(initialFunction);
long stepNumber = 0;
int stepBefore = 0, stepAfter = 0;
int initStepState = 0, endStepState = 0;
AtomicInteger stepState = new AtomicInteger(0);
do {
final String stepName = "Step " + stepNumber;
for (int i = stepBefore; i <= stepAfter; i++) {
lastFnc = lastFunctions[i] = currFnc;
for (int i = initStepState; i < endStepState; i++) {
lastFunctions[i] = currFnc;
}
stepBefore = stepState.get();
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Starting step " + stepStates[stepBefore] + ". Input: " + currFnc);
lastFnc = currFnc;
initStepState = stepState.get();
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Starting step " + stepStates[initStepState] + ". Input: " + currFnc);
ObjectArrayList<Function> stepResult = solveStep(lastFnc, stepState);
if (stepResult == null) {
currFnc = lastFnc;
} else {
if (stepResult != null) {
for (Function result : stepResult) {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, result.toString());
}
currFnc = stepResult;
steps.add(currFnc);
}
stepAfter = stepState.get();
endStepState = stepState.get();
stepNumber++;
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Step result: " + stepResult);
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Step result details: Consecutive steps that did nothing: " + consecutiveNullSteps + ", this step did " + currentStepStateN + " simplifications.");
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Next step state: " + stepStates[stepAfter]);
} while(consecutiveNullSteps < stepStates.length &&!checkEquals(currFnc, lastFunctions[stepAfter]));
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Step result details: Consecutive steps that did nothing: " + consecutiveNullSteps + ", this step did " + stepStateRepetitions + " simplifications.");
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", stepName, "Next step state: " + stepStates[endStepState]);
} while(consecutiveNullSteps < stepStates.length && !checkEquals(currFnc, lastFunctions[endStepState]));
if (consecutiveNullSteps >= stepStates.length) {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", "Loop ended because " + consecutiveNullSteps + " >= " + stepStates.length);
} else {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", "Loop ended because " + currFnc + " is equals to " + lastFunctions[endStepState]);
}
return steps;
}
@ -124,27 +128,27 @@ public class MathSolver {
if (results == null) {
stepState.incrementAndGet();
consecutiveNullSteps++;
currentStepStateN = 0;
stepStateRepetitions = 0;
} else {
consecutiveNullSteps = 0;
currentStepStateN++;
stepStateRepetitions++;
return results;
}
break;
}
case _2_EXPANSION: {
if (results == null) {
if (currentStepStateN == 0) {
if (stepStateRepetitions == 0) {
stepState.addAndGet(2);
consecutiveNullSteps += 2;
} else {
stepState.incrementAndGet();
consecutiveNullSteps++;
}
currentStepStateN = 0;
stepStateRepetitions = 0;
} else {
consecutiveNullSteps = 0;
currentStepStateN++;
stepStateRepetitions++;
return results;
}
break;
@ -153,10 +157,10 @@ public class MathSolver {
if (results == null) {
stepState.incrementAndGet();
consecutiveNullSteps++;
currentStepStateN = 0;
stepStateRepetitions = 0;
} else {
consecutiveNullSteps = 0;
currentStepStateN++;
stepStateRepetitions++;
return results;
}
break;
@ -165,11 +169,11 @@ public class MathSolver {
if (results == null) {
stepState.set(1);
consecutiveNullSteps++;
currentStepStateN = 0;
stepStateRepetitions = 0;
} else {
stepState.set(0);
consecutiveNullSteps = 0;
currentStepStateN++;
stepStateRepetitions++;
return results;
}
break;
@ -185,14 +189,16 @@ public class MathSolver {
ObjectArrayList<Rule> rules = initialFunction.getMathContext().getAcceptableRules(currentAcceptedRules);
ObjectArrayList<Function> results = null;
Rule appliedRule = null;
for (Function fnc : fncs) {
for (Rule rule : rules) {
List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) {
if (results == null) results = new ObjectArrayList<Function>();
results.addAll(ruleResults);
appliedRule = rule;
break;
out: {
for (Function fnc : fncs) {
for (Rule rule : rules) {
List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) {
if (results == null) results = new ObjectArrayList<Function>();
results.addAll(ruleResults);
appliedRule = rule;
break out; //TODO: prima era un break normale, controllare se bisogna uscire da tutti e due i for oppure soltanto dall'ultimo.
}
}
}
}

View File

@ -48,6 +48,7 @@ public class SumRule implements Rule {
*/
@Override
public ObjectArrayList<Function> execute(Function f) {
System.out.println("SUM ON " + f.toString());
if (f instanceof Sum) {
ObjectArrayList<Function> result = new ObjectArrayList<>();
Function variable1 = ((FunctionOperator)f).getParameter1();