mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-16 10:48:00 +00:00
made InputSyncronizedBase.stop() indifferent to the thread that it is ran from.
Introducing game thread concept - that might be used (or might be not) to limit threads devoted to AI thinking with one.
This commit is contained in:
@@ -35,8 +35,8 @@ public class FThreads {
|
|||||||
|
|
||||||
private FThreads() { } // no instances supposed
|
private FThreads() { } // no instances supposed
|
||||||
|
|
||||||
private final static ExecutorService cachedPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
|
private final static ExecutorService gameThreadPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
|
||||||
private static ExecutorService getCachedPool() { return cachedPool; }
|
private static ExecutorService getGameThreadPool() { return gameThreadPool; }
|
||||||
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
|
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
|
||||||
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
|
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
|
||||||
|
|
||||||
@@ -111,8 +111,8 @@ public class FThreads {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void invokeInNewThread(Runnable toRun) {
|
public static void invokeInGameThread(Runnable toRun) {
|
||||||
getCachedPool().execute(toRun);
|
getGameThreadPool().execute(toRun);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -172,4 +172,8 @@ public class FThreads {
|
|||||||
return debugGetStackTraceItem(depth, false);
|
return debugGetStackTraceItem(depth, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isGameThread() {
|
||||||
|
return Thread.currentThread().getName().startsWith("Game");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,11 +186,7 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
|
|||||||
public void run() {
|
public void run() {
|
||||||
HumanPlay.playSpellAbility(chosen.getActivatingPlayer(), chosen);
|
HumanPlay.playSpellAbility(chosen.getActivatingPlayer(), chosen);
|
||||||
onManaAbilityPlayed(chosen);
|
onManaAbilityPlayed(chosen);
|
||||||
if( isAlredyPaid() ) {
|
onStateChanged();
|
||||||
done();
|
|
||||||
stopNonEdt();
|
|
||||||
} else
|
|
||||||
FThreads.invokeInEdtLater(new Runnable() { @Override public void run(){ updateMessage(); }});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
game.getInputQueue().invokeGameAction(proc);
|
game.getInputQueue().invokeGameAction(proc);
|
||||||
@@ -256,12 +252,15 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
|
|||||||
public void showMessage() {
|
public void showMessage() {
|
||||||
if ( isFinished() ) return;
|
if ( isFinished() ) return;
|
||||||
ButtonUtil.enableOnlyCancel();
|
ButtonUtil.enableOnlyCancel();
|
||||||
|
onStateChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onStateChanged() {
|
||||||
if( isAlredyPaid() ) {
|
if( isAlredyPaid() ) {
|
||||||
done();
|
done();
|
||||||
stop();
|
stop();
|
||||||
} else
|
} else
|
||||||
updateMessage();
|
FThreads.invokeInEdtNowOrLater(new Runnable() { @Override public void run(){ updateMessage(); }});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onManaAbilityPaid() {} // some inputs overload it
|
protected void onManaAbilityPaid() {} // some inputs overload it
|
||||||
|
|||||||
@@ -81,13 +81,7 @@ public class InputPayManaExecuteCommands extends InputPayManaBase {
|
|||||||
if (player.canPayLife(this.phyLifeToLose + 2) && manaCost.payPhyrexian()) {
|
if (player.canPayLife(this.phyLifeToLose + 2) && manaCost.payPhyrexian()) {
|
||||||
this.phyLifeToLose += 2;
|
this.phyLifeToLose += 2;
|
||||||
}
|
}
|
||||||
|
onStateChanged();
|
||||||
if (this.manaCost.isPaid()) {
|
|
||||||
this.done();
|
|
||||||
this.stop();
|
|
||||||
} else {
|
|
||||||
this.showMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -200,11 +200,10 @@ public class InputQueue extends MyObservable implements java.io.Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void invokeGameAction(final Runnable proc) {
|
public void invokeGameAction(final Runnable proc) {
|
||||||
if(FThreads.isEDT()) {
|
if( FThreads.isGameThread() ) {
|
||||||
FThreads.invokeInNewThread(proc);
|
|
||||||
} else { // this branch is experimental
|
|
||||||
proc.run();
|
proc.run();
|
||||||
}
|
} else
|
||||||
|
FThreads.invokeInGameThread(proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // InputControl
|
} // InputControl
|
||||||
|
|||||||
@@ -28,9 +28,11 @@ public abstract class InputSyncronizedBase extends InputBase implements InputSyn
|
|||||||
|
|
||||||
|
|
||||||
protected final void stop() {
|
protected final void stop() {
|
||||||
|
// ensure input won't accept any user actions.
|
||||||
|
FThreads.invokeInEdtNowOrLater(new Runnable() { @Override public void run() { setFinished(); } });
|
||||||
|
|
||||||
setFinished();
|
// this will update input proxy, so there might be anything happening in the thread
|
||||||
FThreads.invokeInNewThread(new Runnable() {
|
getQueue().invokeGameAction( new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// this will update input proxy, so there might be anything happening in the thread
|
// this will update input proxy, so there might be anything happening in the thread
|
||||||
@@ -40,18 +42,6 @@ public abstract class InputSyncronizedBase extends InputBase implements InputSyn
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This version does not need to be ran from EDT.
|
|
||||||
protected final void stopNonEdt() {
|
|
||||||
FThreads.assertExecutedByEdt(false);
|
|
||||||
|
|
||||||
// ensure no clicks from EDT will be accepted
|
|
||||||
FThreads.invokeInEdtLater(new Runnable() { @Override public void run() { setFinished(); } });
|
|
||||||
|
|
||||||
// this will update input proxy, so there might be anything happening in the thread
|
|
||||||
getQueue().removeInput(InputSyncronizedBase.this);
|
|
||||||
cdlDone.countDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void selectButtonCancel() {
|
public final void selectButtonCancel() {
|
||||||
if( isFinished() ) return;
|
if( isFinished() ) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user