Skip to content

Commit

Permalink
fix (?) vm logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Student Main committed Nov 25, 2018
1 parent 990aab0 commit 7096737
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import AsyncTask from "./async/asynctask";
import Character from "./character";
import KSVM from "./ksvm";
import ObjectMapper from './objectmapper';
import Runtime from "./runtime";
import TJSVM from "./tjsvm";
import YZBgImg from "./ui/bgimg";
import YZCG from "./ui/cg";
import YZSound from "./ui/sound";
import YZText from "./ui/text";
import AsyncTask from "./async/asynctask";
import YZVideo from "./ui/video";
import FilePath from './utils/filepath';
import KSParser from "./utils/ksparser";
import TJSON from "./utils/tjson";
import Preloader from "./async/preload";
import YZCG from "./ui/cg";
import YZVideo from "./ui/video";

async function LoadVMData() {
//Unicode 万国码
Expand All @@ -37,8 +36,22 @@ async function LoadVMData() {
return;
}

TJSVM.addObject('f');
TJSVM.addObject('sf');
TJSVM.addObject('f', {
sak_flag: 0,
rur_flag: 0,
san_flag: 0,
aoi_flag: 0,
mah_flag: 0,
yuk_flag: 0,
});
TJSVM.addObject('sf', {
sakuya_clear: false,
ruri_clear: false,
sana_clear: false,
aoi_clear: false,
mahiro_clear: false,
yukari_clear: false,
});
TJSVM.addObject('kag');

$(document).ready(async () => {
Expand All @@ -50,8 +63,9 @@ $(document).ready(async () => {
YZVideo.Init();
AsyncTask.Init();
await LoadVMData();
KSVM.RunFrom('start');
$(document).click(() => KSVM.Next());
KSVM.RunFrom('start');
KSVM.Next();
let preloadps = [];
// TODO: let vm cache module load other script
let scripts = Object.keys(FilePath.ls('scenario'));
Expand Down
34 changes: 15 additions & 19 deletions src/ksvm.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import Runtime from "./runtime";
import AsyncTask from "./async/asynctask";
import TJSVM from './tjsvm';

// when to hang up vm
let VM_STEP = 0;
let VM_SCENE = 1;
let VM_SELECT = 2;
let VM_NEVER = 3;
// vm display mode
let VM_NORMAL = 0; // standard mode
let VM_AUTO = 1; // wait for voice end
let VM_QUICK = 2; // fixed interval
let VM_NONE = 3; // no output
import Runtime from "./runtime";

const VMMode = {
Step: 0, // stop per step
Expand All @@ -23,7 +11,7 @@ const VMMode = {

export default class KSVM {
static Init() {
this.mode = VMMode.Step;
this.mode = VMMode.Text;
this.hang = false;
// scripts = {name: script}
this.scripts = {};
Expand Down Expand Up @@ -83,13 +71,13 @@ export default class KSVM {
* @param {String} script script name
*/
static LocateTag(tag, script) {
script = script.match(/(.+?)\.([^.]*$|$)/i)[1];
if (script) script = script.split('.')[0];
// No tag, return first line of script
if (tag === undefined) {
return { script: script, line: 0 };
}
const tags = this.tags[tag.substr(1)];
if (script === undefined) return tags[0];
if (script === undefined) return (tags || [])[0];
else {
for (const t of tags) {
if (t.script == script) return t;
Expand Down Expand Up @@ -120,7 +108,7 @@ export default class KSVM {
if (this.runlock) return;
this.runlock = true;
while (!this.hang) {
if (this.HitBreakPoint(this.currentpos)) debugger;

if (this.CurrentCmd() === undefined) {
// too far
this.hang = true;
Expand All @@ -137,8 +125,9 @@ export default class KSVM {
// Okay, comand return a new position, lets use it
if (next !== undefined) {
if (this.mode == VMMode.Step) this.hang = true;
let nextpos = this.LocateTag(next[0], next[1]);
if (nextpos === undefined) debugger;
this.currentpos = this.LocateTag(next[0], next[1]);
if (this.currentpos === undefined) debugger;
}
break;
case "text":
Expand All @@ -152,6 +141,13 @@ export default class KSVM {
}
break;
}
if (this.HitBreakPoint(this.currentpos)) {
this.hang = true;
this.runlock = true;
debugger;
return;
}
if(VMMode.Step) this.hang = true;
this.currentpos.line++;
}
this.runlock = false;
Expand All @@ -161,13 +157,13 @@ export default class KSVM {
static async RunFrom(tag) {
this.currentpos = this.tags[tag][0];
this.runlock = false;
await this.Run();
}

// VM Control Functions
// .
static async Next() {
this.hang = false;
this.mode = VMMode.Text;
AsyncTask.Cancel();
await this.Run();
}
Expand Down
45 changes: 24 additions & 21 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// runtime libs
import Character from './character';
import ObjectMapper from './objectmapper';
import TJSVM from './tjsvm';
import YZBgImg from './ui/bgimg';
import YZCG from './ui/cg';
import YZSound from './ui/sound';
import YZText from './ui/text';
import TJSVM from './tjsvm';
import YZCG from './ui/cg';
import YZVideo from './ui/video';

export default class Runtime {
Expand Down Expand Up @@ -43,13 +43,14 @@ export default class Runtime {
// TODO: mselect is Tenshin Ranman only command?
// add map select option
static MapSelectAdd(cmd) {
this.MapSelectData.push([
cmd.param.name,
cmd.param.target,
cmd.param.cond,
cmd.param.storage,
cmd.param.place,
]);
let p = cmd.param;
this.MapSelectData.push({
name: p.name,
target: p.target,
cond: p.cond,
storage: p.storage,
place: p.place
});
}

// raise a map select
Expand All @@ -58,23 +59,25 @@ export default class Runtime {
var n = 0;
for (const d of this.MapSelectData) {
s += n;
s += d[0];
s += d.name;
s += '\n';
n++;
}
var r = prompt(s, 0);
var ro = this.MapSelectData[r];
this.MapSelectData = [];
return [ro[1], ro[3]];
if (!ro.target && !ro.storage) return undefined;
return [ro.target, ro.storage];
}

static SelectAdd(cmd) {
this.SelectData.push([
cmd.param.text,
cmd.param.target,
cmd.param.exp,
cmd.param.storage,
]);
let p = cmd.param;
this.SelectData.push({
text: p.text,
target: p.target,
exp: p.exp,
storage: p.storage
});
}

// raise a normal select
Expand All @@ -83,16 +86,16 @@ export default class Runtime {
var n = 0;
for (const d of this.SelectData) {
s += n;
s += d[0];
s += d.text;
s += '\n';
n++;
}
var r = prompt(s, 0);
var ro = this.SelectData[r];
if (ro[2] !== undefined)
TJSVM.eval(ro[2]);
if (ro.exp) TJSVM.eval(ro.exp);
this.SelectData = [];
return [ro[1], ro[3]];
if (!ro.target && !ro.storage) return undefined;
return [ro.target, ro.storage];
}

static AddTrans(cmd) {
Expand Down

0 comments on commit 7096737

Please sign in to comment.