Exemplo n.º 1
0
		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;
			}

			
		}
Exemplo n.º 2
0
		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;

			}
			;
      }
		}