protected bool __isFulfilled(Conditional cnd) { // Debug.Log(type); if(type==Type.TRUE) return true; string variable=variables; if(type==Type.ISSET) { object var=cnd[variable]; if(var!=null) return true; return false; } if(type==Type.SELF) { Condition var=cnd[variable] as Condition; //Debug.Log(variable+var.ToString()); if(var!=null) return var.isFulfilled(cnd,cnd[_upcond] as Conditional); else return false; } if(type==Type.COMMAND_TYPE) { if(values.Length<1) return false; object var=cnd[variable]; if(var==null||!(var is Operation)) { return false; } Operation op= var as Operation; string cndn=values[0] as string; Operation.Commands cmd=Operation.getTypeFromString(cndn); if(cmd==Operation.Commands.ERROR) return false; return (op.command==cmd); } if(type==Type.COMMAND_ARG) { if(values.Length<2) return false; object var=cnd[variable]; if(var==null||!(var is Operation)) { return false; } Operation op= var as Operation; int nm=(int)values[0]; Condition scond=values[1] as Condition; if(scond==null) return false; IList lsta=op[_args] as IList; if(lsta.Count<=nm) return false; Conditional secval=new Conditional(false); secval["_arg"]=lsta[nm]; return scond.isFulfilled(secval,cnd); } if(type==Type.REFCOMP) //deref? ;) { if(variable.StartsWith(_dr)) variable=cnd[variable.Substring(_drl)] as string; if(values.Length<1) return false; string valstr=values[0] as string; if(valstr==null) return false; if(valstr.StartsWith(_dr)) valstr=cnd[valstr.Substring(_drl)] as string; if(valstr==null) return false; if(variable==null) return false; if(cnd[variable]==cnd[valstr]) return true; return false; } /*if(variables.Length==0) return false; if(variables.Length>1) { string [] deeper=new string[variables.Length-1]; for(int i=1;i<variables.Length;i++) deeper[i-1]=variables[i]; string cvar=variables[0]; Conditional cv=cnd[cvar] as Conditional; if(cv==null) return false; Condition deepcond=new Condition(type,deeper,values); return deepcond.__isFulfilled(cv); } else variable=variables[0];*/ if(type==Type.TAG) //also dereferenced { if(variable.StartsWith(_dr)) variable=cnd[variable.Substring(_drl)] as string; return cnd.hasTag(variable); } if(type==Type.STRING) { if(values.Length<1) return false; string val2=cnd[variable] as string; string cmpr=(values[0] as string); if(cmpr.StartsWith(_dr)) cmpr=cnd[cmpr.Substring(_drl)] as string; // Debug.Log(variable); // Debug.Log(cmpr); // Debug.Log(val2); if(val2==null) return false; return (val2==cmpr); } if(type==Type.COMPOUND_ALL||type==Type.COMPOUND_ANY||type==Type.COMPOUND_COUNT) //compounds! EW { if(values.Length<1) return false; if(values.Length<2&&type==Type.COMPOUND_COUNT) return false; Condition compcond=values[0] as Condition; if(compcond==null) { #if THING Debug.Log(string.Format("Invalid value for compound condition : {0}", values[0])); #endif return false; } object cval=cnd[variable]; if(cval is Conditional) //a list of one? { List<Conditional> vl=new List<Conditional>(); vl.Add (cval as Conditional); cval=vl; } if(!(cval is IList)) return false; IList ccnds=cval as IList; if(ccnds==null) return false; int cnt=0; foreach(object ocn in ccnds) { Conditional cn=ocn as Conditional; if(cn==null) return false; bool res=compcond.isFulfilled(cn,cnd); if(type==Type.COMPOUND_ALL&&!res) return false; if(type==Type.COMPOUND_ANY&&res) return true; if(type==Type.COMPOUND_COUNT&&res) cnt=cnt+1; } if(type==Type.COMPOUND_ALL) return true; if(type==Type.COMPOUND_ANY) return false; if(type==Type.COMPOUND_COUNT) { Condition cnd2=values[1] as Condition; if(cnd2==null) return false; Conditional temp=new Conditional(false); // Debug.Log(string.Format("Count: {0}",cnt)); temp[_count]=cnt; temp[_parent]=cnd; return cnd2.isFulfilled(temp,cnd); } } if(type==Type.MULTI_AND) { bool ok=true; foreach(object obj in values) { Condition tcnd=obj as Condition; if(tcnd==null) { #if THING Debug.Log(string.Format("Invalid value for multi_and condition : {0}", obj)); #endif return false; } if(!tcnd.isFulfilled(cnd,cnd[_upcond] as Conditional)) { ok=false; break; } } return ok; } if(type==Type.MULTI_OR) { bool ok=false; foreach(object obj in values) { Condition tcnd=obj as Condition; if(tcnd==null) { #if THING Debug.Log(string.Format("Invalid value for multi_or condition : {0}", obj)); #endif return false; } if(tcnd.isFulfilled(cnd,cnd[_upcond] as Conditional)) { ok=true; break; } } return ok; } // no good way to compare values, I guess... if(values.Length<1) return false; object val=cnd[variable]; object ncmpval=values[0]; if(values[0] is string)// try loading it as cnd name? { string st=values[0] as string; if(st!=null&&st.StartsWith(_dr)) { string vname=st.Substring(_drl); ncmpval=cnd[vname]; } } int cmp=0; if(val==null) return false; System.Type vtp_stupid=val.GetType(); System.TypeCode vtp=System.Type.GetTypeCode(vtp_stupid); switch(vtp) { case System.TypeCode.Int16: case System.TypeCode.Int32: case System.TypeCode.Int64: { System.Int64 cndval=System.Convert.ToInt64(ncmpval); System.Int64 cmpval=System.Convert.ToInt64(val); cmp=cmpval.CompareTo(cndval); } ; break; default: { System.Double cndval=System.Convert.ToDouble(ncmpval); System.Double cmpval=System.Convert.ToDouble(val); cmp=cmpval.CompareTo(cndval); } ; break; } //Debug.Log(string.Format("{0}: {1} ({2}), {3} {4} in {5} {6}",type,variable,val,values[0],ncmpval,cnd,cnd["slot"])); switch(type) { case Type.GREATER: { return (cmp==1);} case Type.LESS: { return (cmp==-1);} case Type.EQUAL: { return (cmp==0);} case Type.GE: { return (cmp==1||cmp==0);} case Type.LE: { return (cmp==-1||cmp==0);} default: return false; } //return true; //int cmp=val.com }
public void _execute(Conditional stack) { uplink(); stack[_currentCommand]=this; IList efs=stack[_effects] as IList; if(efs==null) { #if THING Debug.Log("Possibly invalid setting of _effects in stack"); #endif } else { stack.setTag(EXECUTE_PREFIX); iterateOverEffects(efs, stack, EXECUTE_PREFIX); stack.removeTag(EXECUTE_PREFIX); if(stack.hasTag(TAG_ABORT)) return; } __pureExecute(stack); if(stack.hasTag(TAG_ABORT)) return; if(efs!=null) { stack.setTag(EXECUTE_POSTFIX); iterateOverEffects(efs, stack, EXECUTE_POSTFIX); stack.removeTag(EXECUTE_POSTFIX); if(stack.hasTag(TAG_ABORT)) return; } }
public Operation executeList(object lst, Conditional stack) //has *new* stack as parameter { if(!(lst is IList)) { #if THING Debug.Log("Invalid command list"); #endif return null; } IList list=lst as IList; foreach(object obj in list) { Operation op=obj as Operation; if(op==null) { #if THING Debug.Log(string.Format("Invalid command: {0}", obj)); #endif return null; } op._execute(stack); if(stack.hasTag(TAG_RETURN)) return stack[_returnValue] as Operation; if(stack.hasTag(TAG_ABORT)) return null; //newstack[_command] } return null; }
void iterateOverEffects(IList efs, Conditional stack, string tag) { if(stack.hasTag(TAG_ABORT)) return; bool didActivate=true; foreach(object o in efs) { Conditional preffect=o as Conditional; if(preffect!=null) preffect.removeTag(TAG_ACTIVATED); } // Debug.Log(efs.Count); while(didActivate) { didActivate=false; int ii=0; while(ii<efs.Count) { object o= efs[ii]; ii++; //Debug.Log("effecting"); Conditional preffect=o as Conditional; Conditional effect=null; if(preffect!=null&&!preffect.hasTag(TAG_ACTIVATED)&&!preffect.hasTag(TAG_STACKED)) { effect=preffect[_effect] as Conditional; // Debug.Log(effect[_condition]); if(effect.hasTag(tag)) { //Debug.Log(tag); Condition cn=effect[_condition] as Condition; if(cn==null) { Debug.Log(string.Format("Invalid effect condition {0}", effect[_condition])); } else { if(cn.isFulfilled(stack,stack[_Game] as Conditional)) //execute effect { preffect.setTag(TAG_ACTIVATED); didActivate=true; Operation ret=executeList(effect[_commands], createStack(stack, effect)); if(ret!=null) { ret.uplink(); ret.__pureExecute(stack); } if(stack.hasTag(TAG_ABORT)||stack.hasTag(TAG_CONTINUE)) return; } } } } else { if(preffect==null) Debug.Log(string.Format("Invalid effect {0}", o)); } } } }
void __pureExecute(Conditional stack) { { if(stack.hasTag(TAG_ABORT)) return; // Conditional target=stack[_target] as Conditional; //IList args=this[_args] as IList; switch(_command) { case Commands.CONDITION_AND: { string nm1=this["arg0"] as string; Condition cnds=stack[nm1] as Condition; if(cnds==null) { #if THING Debug.Log("Cond_and : not condition"); #endif } else { Condition toAdd=deRef(this["arg1"], stack) as Condition; if(toAdd!=null) { Condition wrp=new Condition(Condition.Type.MULTI_AND,"_wrap",cnds,toAdd); stack[nm1]=wrp; } } deRef(this["arg1"], stack); }break; case Commands.CONDITION_OR: { string nm1=this["arg0"] as string; Condition cnds=stack[nm1] as Condition; if(cnds==null) { #if THING Debug.Log("Cond_or : not condition"); #endif } else { Condition toAdd=deRef(this["arg1"], stack) as Condition; if(toAdd!=null) { Condition wrp=new Condition(Condition.Type.MULTI_OR,"_wrap",cnds,toAdd); stack[nm1]=wrp; } } deRef(this["arg1"], stack); }break; case Commands.TAG_SET: { (stack[this["arg0"] as string] as Conditional).setTag(deRef(this["arg1"],stack) as string);} break; case Commands.TAG_REMOVE: { (stack[this["arg0"] as string] as Conditional).removeTag(deRef(this["arg1"],stack) as string);} break; case Commands.TAG_SWITCH: { Conditional ct=(stack[this["arg0"]as string] as Conditional); if(ct.hasTag(this["arg1"] as string)) { ct.removeTag(deRef(this["arg1"],stack) as string); ct.setTag(deRef(this["arg2"],stack) as string); } } break; case Commands.VALUE_SET: { // Conditional ct=(stack[this["arg0"] as string] as Conditional); // Debug.Log(this["arg0"] as string); // Debug.Log((this["arg1"] as string).Substring(_drl)); //Debug.Log(stack["|-"]); if(this["arg1"]is string&&(this["arg1"] as string).StartsWith(_dr)) { stack[this["arg0"] as string]=stack[(this["arg1"] as string).Substring(_drl)]; } else stack[this["arg0"] as string]=this["arg1"];} break; case Commands.INSERT: { Conditional cndi=stack[this["arg0"] as string] as Conditional; IList lst=stack[this["arg1"] as string] as IList; string vl=this["arg2"] as string; if(lst!=null&&cndi!=null&&cndi[vl]!=null) { double ins=System.Convert.ToDouble(cndi[vl]); int ind=0; foreach(object o in lst) { Conditional ccc=o as Conditional; if(ccc!=null&&ccc[vl]!=null) { ccc[vl]=System.Convert.ToDouble(ccc[vl]); double cr=System.Convert.ToDouble(ccc[vl]); if(ins.CompareTo(cr)<0) {break;}; } ind++; } lst.Insert(ind,cndi); // Debug.Log(ind); } else { Debug.Log(string.Format("Invalid list : {0} {1} {2} {3} Var: {4} Val: {5} ",this["arg0"],cndi,this["arg1"],lst,vl,cndi[vl])); } if(this["arg1"]is string&&(this["arg1"] as string).StartsWith(_dr)) { stack[this["arg0"] as string]=stack[(this["arg1"] as string).Substring(_drl)]; } else stack[this["arg0"] as string]=this["arg1"];} break; case Commands.ADD: { object a1=stack[this["arg0"] as string]; object a2=deRef(this["arg1"], stack); stack[this["arg0"] as string]=System.Convert.ChangeType(System.Convert.ToDouble(a1)+System.Convert.ToDouble(a2), a1.GetType());} break; case Commands.SUBTRACT: { // Debug.Log(this["arg0"]); //Debug.Log(stack["castPlayer"]); object a1=stack[this["arg0"] as string]; object a2=deRef(this["arg1"], stack); stack[this["arg0"] as string]=System.Convert.ChangeType(System.Convert.ToDouble(a1)-System.Convert.ToDouble(a2), a1.GetType());} break; case Commands.MULTIPLY: { object a1=stack[this["arg0"] as string]; object a2=deRef(this["arg1"], stack); stack[this["arg0"] as string]=System.Convert.ChangeType(System.Convert.ToDouble(a1)*System.Convert.ToDouble(a2), a1.GetType());} break; case Commands.DIVIDE: { object a1=stack[this["arg0"] as string]; object a2=deRef(this["arg1"], stack); stack[this["arg0"] as string]=System.Convert.ChangeType(System.Convert.ToDouble(a1)/System.Convert.ToDouble(a2), a1.GetType());} break; case Commands.ABORT: { stack.setTag(TAG_ABORT); } break; case Commands.CONTINUE: { stack.setTag(TAG_CONTINUE); } break; case Commands.NEW: { string nm=this["arg0"] as string; if(stack[nm] as Conditional==null) { Conditional cn=new Conditional(); cn[_rootl]=stack[_rootl]; stack[nm]=cn; } } break; case Commands.NEWLIST: { string nm=this["arg0"] as string; if(stack[nm] as IList==null) stack[nm]=new List<Conditional>(); } break; case Commands.RETURN: //argument : returned command/operation { stack.setTag(TAG_RETURN); stack[_returnValue]=this["arg0"] as Operation; } break; case Commands.EXECUTE:// arguments: ...none? XD I guess list name would work. arg[0]-> list name in stack this["arg1"]->list of commands { object oldtarget=stack[_target]; string cname=this["arg0"] as string; Conditional pseudostack=stack[cname] as Conditional; if(pseudostack!=null) { IList lst=deRef(this["arg1"],stack) as IList; if(lst!=null) { object st= pseudostack[_effects]; pseudostack[_effects]=stack[_effects]; executeList(lst, pseudostack); pseudostack[_effects]=st; } else { #if THING Debug.Log(string.Format("Invalid execute argument: {0}", this["arg1"])); #endif } } stack[_target]=oldtarget; } break; case Commands.FOREACH:// arguments: ...none? XD I guess list name would work. arg[0]-> list name in stack this["arg1"]->list of commands { object oldtarget=stack[_target]; string lname=this["arg0"] as string; if(stack[lname]!=null) { IList lst=stack[lname] as IList; if(lst!=null) { foreach(object pcn in lst) { Conditional newtarget=pcn as Conditional; if(newtarget==null) { #if THING Debug.Log("Invalid target in foreach list"); #endif } else { stack[_target]=newtarget; executeList(this["arg1"], stack); if(stack.hasTag(TAG_ABORT)) return; stack.removeTag(TAG_CONTINUE); } } } else { #if THING Debug.Log(string.Format("Invalid foreach argument: {0}", lname)); #endif } } stack[_target]=oldtarget; } break; case Commands.IF:// arguments: var name, cond, list { string vname=this["arg0"] as string; vname=deRef(vname,stack) as string; Conditional vr=stack[vname] as Conditional; Condition appl=this["arg1"] as Condition; if(vr==null||appl==null) { #if THING Debug.Log(string.Format("Invalid variable or condition in the if statement {0} {1} ",this["arg0"],this["arg1"] )); #endif } else { //Debug.Log(TAG_CONTINUE); if(appl.isFulfilled(vr,stack)) executeList(this["arg2"], stack); } } break; case Commands.WHILE:// arguments: ...none? XD I guess list name would work. arg[0]-> list name in stack this["arg1"]->list of commands { // object oldtarget=stack[_target]; Condition lname=this["arg0"] as Condition; if(lname!=null) { while(lname.isFulfilled(stack,stack[_Game] as Conditional)) { executeList(this["arg1"], stack); if(stack.hasTag(TAG_ABORT)) return; stack.removeTag(TAG_CONTINUE); } } //stack[_target]=oldtarget; } break; case Commands.TARGET://will assign _targetList? value in stack arg0 - condition, arg1 - list, equivalent of accumulate for specific list { List<Conditional> targs=new List<Conditional>(); Condition baseCond=this["arg0"] as Condition; if(baseCond==null) { #if THING Debug.Log(string.Format("Invalid target condition : {0}", this["arg0"])); #endif return; } IList vars=stack[this["arg1"] as string] as IList; if(vars==null) { #if THING Debug.Log(string.Format("Invalid target list : {0}", this["arg1"])); #endif return; } foreach(object potCn in vars) { Conditional check=potCn as Conditional; if(baseCond.isFulfilled(check,stack)) targs.Add(check); } stack[_targetList]=targs; } ; break; case Commands.ACCUMULATE://will assign arg2 value in stack arg0 - condition, arg1 - list. appends to list if it exists { List<Conditional> targs=new List<Conditional>(); string listname=this["arg2"] as string; IList oldList=stack[listname] as IList; if(oldList!=null) { foreach(object l in oldList) targs.Add(l as Conditional); } Condition baseCond=this["arg0"] as Condition; if(baseCond==null) { #if THING Debug.Log(string.Format("Invalid target condition : {0}", this["arg0"])); #endif return; } IList vars=stack[this["arg1"] as string] as IList; if(vars==null) { #if THING Debug.Log(string.Format("Invalid target list : {0}", this["arg1"])); #endif return; } foreach(object potCn in vars) { Conditional check=potCn as Conditional; if(baseCond.isFulfilled(check,stack)) targs.Add(check); } stack[listname]=targs; } ; break; case Commands.CLEAR: { string nm=deRef(this["arg0"],stack) as string; if(nm!=null) stack[nm]=null; } ; break; case Commands.POP: //arg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst!=null&&retname!=null) { if(lst.Count>0) { stack[retname]=lst[0]; lst.RemoveAt(0); } } } ; break; case Commands.PUSH: //arg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst==null) { lst=new List<object>(); stack[nm]=lst; } //should be avoided... if(lst!=null&&retname!=null&&stack[retname]!=null) { lst.Insert(0, stack[retname]); } } ; break; case Commands.SHIFT: //arg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst!=null&&retname!=null) { if(lst.Count>0) { stack[retname]=lst[lst.Count-1]; lst.RemoveAt(lst.Count-1); } } } ; break; case Commands.APPEND: //arg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst==null) { lst=new List<object>(); stack[nm]=lst; } //should be avoided... if(lst!=null&&retname!=null&&stack[retname]!=null) { lst.Add(stack[retname]); } } ; break; case Commands.REMOVE: // removes object from listarg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst==null) { lst=new List<object>(); stack[nm]=lst; } //should be avoided... if(lst!=null&&retname!=null&&stack[retname]!=null) { lst.Remove(stack[retname]); } } ; break; case Commands.ANY: //get any (random) in list without deleting arg0 - list name, arg1- stackvar { string nm=deRef(this["arg0"],stack) as string; IList lst=stack[nm] as IList; string retname=this["arg1"] as string; if(lst!=null&&retname!=null) { if(lst.Count>0) { stack[retname]=lst[rngRange(0, lst.Count)]; } } } ; break; case Commands.HOOK: //call hook of name with data { //Monitor.Exit(gameLock); string nm=deRef(this["arg0"],stack) as string; string nmData=this["arg1"] as string; GameManager.startHook(nm, stack[nmData] as Conditional); //Monitor.Enter(gameLock); } ; break; case Commands.CHOICE: //call for player choice, store result in arg2: { //Monitor.Exit(gameLock); string chname=deRef(this["arg0"],stack) as string; string nm=this["arg1"] as string; IList lst=stack[nm] as IList; if(lst==null) { #if THING Debug.Log(string.Format("Invalid choice list : {0}", nm)); #endif return; } string nmRet=this["arg2"] as string; Conditional ret=GameManager.startChoice(chname, lst); //Monitor.Enter(gameLock); stack[nmRet]=ret; } ; break; } ; } }