📄 cscript.java
字号:
m_recursionLevel++;
short sNo = toNumber(1); pop(1);
m_curScript = sNo;
m_curIns = 0;
if(m_script[m_curScript] == null)
LoadScript(m_curScript, true);
}
// -------------------------------------------------------------------
// 消息部分
// -------------------------------------------------------------------
//运行指定脚本 sNo:脚本号
public static void runScript(int sNo) {
b_running = true;
m_curScript = (short) sNo;
m_curIns = 0;
}
public static void run() {
try {
while (b_running && m_curIns < m_script[m_curScript].length) {
runCommand();
}
}catch(Exception e) {
e.printStackTrace();
}
}
// -------------------------------------------------------------------
// 控制部分
// -------------------------------------------------------------------
public static void stop() {
b_running = false;
}
public static void turnOn() {
b_running = true;
}
private static void runCommand() {
try {
if (m_curIns < 0 || m_curIns >= m_script[m_curScript].length) {
return;
}
//确保所运行的指令都在内存中
if(m_script[m_curScript] == null)
LoadScript(m_curScript, true);
//获取参数
int insID = m_script[m_curScript][m_curIns][0];
// System.out.println("Command: " + insID); //调试用语句
// --------------------------------------------------------------------
// 系统指令
// --------------------------------------------------------------------
int right, left, con, type, value;
int id, count;
short[] t, tl, tr;
switch(insID) {
case VM_PUSH:
m_stackData[m_sDataIndex++] = getTeminal(1);
break;
case VM_POPU:
right = m_stackExp[--m_sExpIndex];
ASSERT(right == TRUE || right == FALSE, "CScript.runCommand() -> POPU Unknown right type");
left = m_stackExp[--m_sExpIndex];
ASSERT(left == TRUE || left == FALSE, "CScript.runCommand() -> POPU Unknown left type");
t = getTeminal(1);
con = popuCondition(left, t, right);
ASSERT(con == TRUE || con == FALSE, "CScript.runCommand() -> POPU Unknown con Type");
m_stackExp[m_sExpIndex++] = (byte) con;
break;
case VM_IF:
con = m_stackExp[--m_sExpIndex];
if(con == FALSE) { //跳转
t = getTeminal(1);
type = t[0]; value = t[1];
ASSERT(type == T_INTEGER, "CScript.runCommand() -> IF Teminal Type should be T_INTEGER");
m_curIns = (short) value;
return;
}
break;
case VM_JUMP:
t = getTeminal(1);
type = t[0]; value = t[1];
ASSERT(type == T_INTEGER, "CScript.runCommand() -> JUMP Teminal Type should be T_INTEGER");
m_curIns = (short) value;
return;
case VM_PCALL:
t = getTeminal(1);
id = t[1]; //函数ID
t = getTeminal(2);
count = t[1]; //参数个数
if(id == KEYWORD_CALL_ID) {
ASSERT(count == 1, "CScript.runCommand() -> Call Command Parameter Count Error!");
callCommand();
return;
}
else if(id == KEYWORD_LOADSCRIPT_ID) {
ASSERT(count == 1, "CScript.runCommand() -> LoadScript Command Parameter Count Error!");
short sNo = toNumber(1); pop(1);
LoadScript(sNo, true);
}
else if(id == KEYWORD_UNLOADSCRIPT_ID) {
ASSERT(count == 1, "CScript.runCommand() -> LoadScript Command Parameter Count Error!");
short sNo = toNumber(1); pop(1);
UnLoadScript(sNo);
}
else
Game.function(id, count);
break;
case VM_CALP:
tl = getTeminal(1);
t = getTeminal(2);
tr = getTeminal(3);
con = calpCondition(tl, t, tr);
ASSERT(con == TRUE || con == FALSE, "CScript.runCommand() -> CALP Unknown con Type");
m_stackExp[m_sExpIndex++] = (byte) con;
break;
case VM_RETURN:
m_recursionLevel--;
if(m_recursionLevel >= 0) {
m_curScript = m_recursion[m_recursionLevel][0];
m_curIns = m_recursion[m_recursionLevel][1];
m_curIns++; //返回下一条指令
}
else {
m_recursionLevel = 0;
m_curIns = (short) m_script[m_curScript].length;
stop();
}
return;
case VM_STOP:
m_recursionLevel = 0;
m_curIns = (short) m_script[m_curScript].length;
stop();
return;
}
nextCommand();
}
catch(Exception e) {
e.printStackTrace();
}
}
private static byte popuCondition(int left, short[] op, int right) {
int type, value;
type = op[0];
ASSERT(type == T_RSYMBOL, "CScript.popuCondition() -> Teminal Type should be T_RSYMBOL");
value = op[1];
switch(value) {
case O_OR:
if(left == TRUE || right == TRUE) return TRUE;
else return FALSE;
case O_AND:
if(left == TRUE && right == TRUE) return TRUE;
else return FALSE;
default:
ASSERT(false, "CScript.popuCondition() -> Unknown Relation Type");
break;
}
return -1;
}
private static byte calpCondition(short[] tl, short[] op, short[] tr) {
int left, right;
int type, value;
type = tl[0]; value = tl[1];
if (type == T_IDENTIFER) {
left = m_variable[value];
}
else {
left = value;
}
type = tr[0]; value = tr[1];
if (type == T_IDENTIFER) {
right = m_variable[value];
}
else {
right = value;
}
type = op[0]; value = op[1];
switch (value) {
case O_EQ: //==
if (left == right) return TRUE;
else return FALSE;
case O_NE: //!=
if (left != right) return TRUE;
else return FALSE;
case O_LT: //<
if (left < right) return TRUE;
else return FALSE;
case O_GT: //>
if (left > right) return TRUE;
else return FALSE;
case O_LE: //<=
if (left <= right)return TRUE;
else return FALSE;
case O_GE: //>=
if (left >= right)return TRUE;
else return FALSE;
default:
ASSERT(false, "CScript.calpCondition() -> Unknown Operation op type");
break;
}
return -1;
}
private static void nextCommand() {
m_curIns++;
}
//获得指令中的终结符
private static short[] getTeminal(int pos) {
int index = m_script[m_curScript][m_curIns][pos];
return m_tData[m_curScript][index];
}
private static void ASSERT(boolean value, String msg) {
if(!value) {
System.out.println("ERROR: " + msg);
System.out.println("==========================================================");
System.exit(1);
}
}
public static void pop(int n) {
m_sDataIndex -= n;
}
public static void pushNumber(int num) {
short[] t = new short[2];
t[0] = T_INTEGER;
t[1] = (short) num;
m_stackData[m_sDataIndex++] = t;
}
public static void pushBoolean(boolean b) {
}
public static String toString(int index) {
try {
short[] t = m_stackData[m_sDataIndex - index];
int type, size;
type = t[0];
size = t[1];
ASSERT(type == T_STRING, "CScript.toString(" + index + ")");
m_strbuf.delete(0, m_strbuf.length());
for (int i = 0; i < size; i++)
m_strbuf.append( (char) t[i + 2]);
return m_strbuf.toString();
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
public static short toNumber(int index) {
try {
short[] t = m_stackData[m_sDataIndex - index];
int type = t[0];
ASSERT(type == T_INTEGER || type == T_SCRIPT, "CScript.toNumber(" + index + ")");
return t[1];
}
catch(Exception e) {
e.printStackTrace();
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -