Advertisement
Guest User

BesiegeBot

a guest
Jun 6th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 28.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using spaar;
  8. using System.IO;
  9.  
  10. namespace BesiegeBot
  11. {
  12.     [spaar.Mod("BesiegeBotLoader",author="ITR",version="BB needs no version. BB is sentient and is always improving itself, thus always is the perfect version. Hail Besiege Bot you pleb!")]
  13.     public class BesiegeBotLoader : MonoBehaviour
  14.     {
  15.         public BesiegeBotLoader() {
  16.             gameObject.AddComponent<ViewMapper>();
  17.             gameObject.AddComponent<ControlMachine>();
  18.             gameObject.AddComponent<SelectVictim>();
  19.             gameObject.AddComponent<BesiegeBotCore>();
  20.             Debug.Log("You have been visited by BB. He bless your besiege game by playing on it, until he decides to leave.");
  21.         }
  22.     }
  23.  
  24.     public class BesiegeBotCore : MonoBehaviour{
  25.         ViewMapper viewMapper;
  26.         ControlMachine controlMachine;
  27.         SelectVictim selectVictim;
  28.  
  29.         MachineObjectTracker machineObjectTracker;
  30.         AddPiece addPiece;
  31.         HudInputControl hudInputControl;
  32.  
  33.         int state = -1;
  34.         int currentMachineController = 0;
  35.  
  36.         bool foundEverythingNeeded = false;
  37.  
  38.         public BesiegeBotCore() {
  39.  
  40.         }
  41.  
  42.         public void startGame(bool wantedAddPieceState) {
  43.             if(AddPiece.isSimulating!=wantedAddPieceState) {
  44.                 addPiece.Simulate();
  45.             }
  46.         }
  47.  
  48.         public void startGame() {
  49.             startGame(true);
  50.         }
  51.  
  52.         public bool getEverythingNeededToRunTheClass() {
  53.             state = -1;
  54.             if(viewMapper==null) {
  55.                 viewMapper = GetComponent<ViewMapper>();
  56.                 if(viewMapper==null) {
  57.                     return false;
  58.                 }
  59.                 Debug.Log("Found ViewMapper");
  60.             }
  61.             if(controlMachine==null) {
  62.                 controlMachine = GetComponent<ControlMachine>();
  63.                 if(controlMachine==null) {
  64.                     return false;
  65.                 }
  66.                 Debug.Log("Found ControlMachine");
  67.             }
  68.             if(selectVictim==null) {
  69.                 selectVictim = GetComponent<SelectVictim>();
  70.                 if(selectVictim==null) {
  71.                     return false;
  72.                 }
  73.                 Debug.Log("Found SelectVictim");
  74.             }
  75.             state = 0;
  76.             if(machineObjectTracker==null) {
  77.                 try{
  78.                     machineObjectTracker = UnityEngine.Object.FindObjectOfType<MachineObjectTracker>();
  79.                 }
  80.                 catch{
  81.  
  82.                 }
  83.                 if(machineObjectTracker==null) {
  84.                     return false;
  85.                 }
  86.                 Debug.Log("Found MachineObjectTracker");
  87.             }
  88.             if(addPiece==null) {
  89.                 try {
  90.                     addPiece = GameObject.Find("BUILDER").GetComponent<AddPiece>();
  91.                 }
  92.                 catch {
  93.  
  94.                 }
  95.                 if(addPiece==null) {
  96.                     return false;
  97.                 }
  98.                 Debug.Log("Found AddPiece");
  99.             }
  100.  
  101.             if(hudInputControl==null) {
  102.                 hudInputControl = UnityEngine.Object.FindObjectOfType<HudInputControl>();
  103.                 if(hudInputControl==null) {
  104.                     return false;
  105.                 }
  106.                 Debug.Log("Found HudInputControl");
  107.             }
  108.  
  109.             if(!foundEverythingNeeded) {
  110.                 foundEverythingNeeded = true;
  111.                 Debug.Log("BBCore found everything needed!");
  112.             }
  113.  
  114.             state = 1;
  115.             return true;
  116.         }
  117.  
  118.         public void viewMapperHandler() {
  119.             if(viewMapper.state==0) {
  120.                 if(AddPiece.isSimulating&&controlMachine.state>1) {
  121.                     Debug.Log("Changing viewLoggerState to 1");
  122.                     viewMapper.defaultScale = viewMapper.mouseOrbit.distance;
  123.                     viewMapper.state = 1;
  124.                 }
  125.             }
  126.             else if(viewMapper.state==2) {
  127.                 if(!AddPiece.isSimulating) {
  128.                     Debug.Log("Changing viewLoggerState to 4");
  129.                     viewMapper.state = 4;
  130.                 }
  131.             }
  132.             else if(viewMapper.state==3) {
  133.                 if(AddPiece.isSimulating&&controlMachine.state==4) {
  134. //                  startGame(false);
  135.                     Debug.Log("Changing viewLoggerState to 5");
  136.                     viewMapper.state = 5;
  137.                 }
  138.             }
  139.             else if(viewMapper.state==4) {
  140.                 if(AddPiece.isSimulating) {
  141.                     Debug.Log("Changing viewLoggerState to 1");
  142.                     viewMapper.state = 1;
  143.                 }
  144.             }
  145.             else if(viewMapper.state==5) {
  146.                 if(!AddPiece.isSimulating) {
  147.                     Debug.Log("Changing viewLoggerState to 4");
  148.                     viewMapper.state = 4;
  149.                 }
  150.             }
  151.         }
  152.  
  153.         public void controlMachineHandler() {
  154.             if(controlMachine.state==0) {
  155.                 if(!AddPiece.isSimulating) {
  156.                     startGame();
  157.                     controlMachine.state = 1;
  158.                 }
  159.                 else {
  160.                     controlMachine.state = 1;
  161.                 }
  162.             }
  163.             if(controlMachine.state==2) {
  164.                 if(AddPiece.isSimulating&&viewMapper.state==3) {
  165.                     controlMachine.state = 3;
  166.                 }
  167.             }
  168.             if(controlMachine.state==3) {
  169.                 selectVictim.state = 0;
  170.                 controlMachine.hasControl = requestControl(0);
  171.             }
  172.             if(controlMachine.state==5) {
  173.                 if(controlMachine.requestToViewMachine) {
  174.                     viewMapper.requestViewMachine();
  175.                 }
  176.             }
  177.         }
  178.  
  179.         public void selectVictimHandler() {
  180.             if(!AddPiece.isSimulating) {
  181.                 viewMapper.victim = -1;
  182.                 selectVictim.state = 0;
  183.                 return;
  184.             }
  185.             if(selectVictim.requestAttackVictim) {
  186.                 attackVictim();
  187.                 selectVictim.requestAttackVictim = false;
  188.             }
  189.             if(selectVictim.requestingVictim) {
  190.                 viewMapper.selectVictim();
  191.                 if(viewMapper.victim!=-1) {
  192.                     selectVictim.requestingVictim = false;
  193.                     selectVictim.victimPosition = viewMapper.allWorldlastPositionGameObjectWasSeen[viewMapper.victim];
  194.                     selectVictim.victimState = 5;
  195.                     selectVictim.state = 4;
  196.                 }
  197.                 else {
  198.                     selectVictim.state = 0;
  199.                 }
  200.             }
  201.             else {
  202.                 selectVictim.victimPosition = viewMapper.allWorldlastPositionGameObjectWasSeen[viewMapper.victim];
  203.                 if(selectVictim.state==1) {
  204.                     requestControl(1);
  205.                     if((Vector3.Project(selectVictim.victimPosition,Vector3.up)-Vector3.Project(controlMachine.baseBlock.transform.position,Vector3.up)).magnitude<4) {
  206.                         selectVictim.state = 2;
  207.                     }
  208.                 }
  209.                 if(selectVictim.state==2) {
  210.                     requestControl(1);
  211.                     viewMapper.requestViewMachine();
  212.                 }
  213.                 if(selectVictim.state==3) {
  214.                     requestControl(1);
  215.                     viewMapper.requestViewMachine();
  216.                 }
  217.                 if(selectVictim.state==4) {
  218.                     if(requestControl(1)) {
  219.                         selectVictim.state = 1;
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.  
  225.         public bool requestControl(int newController) {
  226.             if(newController!=currentMachineController) {
  227.                 if(controlMachine.wantedArrivalTime-Time.realtimeSinceStartup>=0) {
  228.                     return false;
  229.                 }
  230.                 if(currentMachineController==0&&controlMachine.state<=3) {
  231.                     return false;
  232.                 }
  233.                 if(currentMachineController==1&&selectVictim.state>0){
  234.                     return false;
  235.                 }
  236.                 controlMachine.hasControl = false;
  237.                 selectVictim.hasControl = false;
  238.                 currentMachineController = newController;
  239.             }
  240.             if(currentMachineController==1) {
  241.                 if(selectVictim.moveToPosition.y<0)
  242.                     selectVictim.moveToPosition -= new Vector3(0,selectVictim.moveToPosition.y,0);
  243.                 controlMachine.moveToPosition = selectVictim.moveToPosition;
  244.                 controlMachine.wantedArrivalTime = selectVictim.wantedArrivalTime;
  245.             }
  246.             return true;
  247.         }
  248.  
  249.         public void attackVictim() {
  250.             if(viewMapper.victim==-1)
  251.                 return;
  252.             int r = UnityEngine.Random.Range(0,4);
  253.             if(r==0) {
  254.                 controlMachine.baseBlock.rigidbody.AddForce(100*(viewMapper.allWorldlastPositionGameObjectWasSeen[viewMapper.victim]-controlMachine.baseBlock.transform.position).normalized);
  255.             }
  256.             else if(r==1){
  257.                 Transform item;
  258.                 try {
  259.                     item = (Transform)Instantiate(machineObjectTracker.AllPrefabs[23].transform,viewMapper.allWorldlastPositionGameObjectWasSeen[viewMapper.victim],controlMachine.baseBlock.transform.rotation);
  260.                 }
  261.                 catch {
  262.                     return;
  263.                 }
  264.                 try {
  265.                     item.gameObject.GetComponent<ExplodeOnCollide>().Explodey();
  266.                 }
  267.                 catch {
  268.                     Debug.Log("Could not explode");
  269.                 }
  270.                 if(item!=null) {
  271.                     Destroy(item.gameObject);
  272.                 }
  273.             }
  274.             else if(r==2) {
  275.                 viewMapper.allWorldGameObjects[viewMapper.victim].transform.position += Vector3.up*50;
  276.             }
  277.             else if(r==3) {
  278.                 viewMapper.allWorldGameObjects[viewMapper.victim].transform.Rotate(Vector3.forward,180);
  279.             }
  280.             else if(r==4) {
  281.                 Destroy(viewMapper.allWorldGameObjects[viewMapper.victim].gameObject);
  282.             }
  283.         }
  284.  
  285.         public void Update() {
  286.             if(!getEverythingNeededToRunTheClass()) {
  287.                 return;
  288.             }
  289.             viewMapperHandler();
  290.             controlMachineHandler();
  291.             selectVictimHandler();
  292.         }
  293.  
  294.  
  295.         public void OnGUI() {
  296.             if(state==-1)
  297.                 return;
  298.             if(hudInputControl!=null)
  299.                 if(!hudInputControl.hudCam.enabled)
  300.                     return;
  301.  
  302.             string tempString = "";
  303.             GUILayout.Space(58);
  304.             GUILayout.Label("Besiege Bot Information: ");
  305.             GUILayout.Label("Current Position: "+controlMachine.currentPosition);
  306.             tempString = "Current victim: ";
  307.             if(viewMapper.victim==-1) {
  308.                 tempString += "None. BB is merciful. BB decides to let you live. For now.";
  309.             }
  310.             else {
  311.                 if(viewMapper.allWorldGameObjects[viewMapper.victim]==null) {
  312.                     tempString += "BB has already ended their existance. Their name is no longer important.";
  313.                 }
  314.                 else {
  315.                     tempString += viewMapper.allWorldGameObjects[viewMapper.victim].name;
  316.                 }
  317.             }
  318.             GUILayout.Label(tempString);
  319.             tempString = "ViewMapper: Current state: "+viewMapper.state.ToString()+", Current Action: ";
  320.             if(viewMapper.state==-1) {tempString+="Searching for everything needed for the class to work";}
  321.             if(viewMapper.state==0) {tempString+="Waiting for simulation to start";}
  322.             if(viewMapper.state==1) {tempString+="Obtaining list over all gameObjects";}
  323.             if(viewMapper.state==2) {tempString+="Obtaining information over all gameObjects, Current object: "+viewMapper.allWorldGameObjectsChecked.ToString()+"/"+viewMapper.allWorldGameObjects.Length.ToString();}
  324.             if(viewMapper.state==3) {tempString+="Waiting. Just waiting. For how long? No one knows. (Except BB, BB knows everything)";}
  325.             if(viewMapper.state==4) {tempString+="Resting";}
  326.             if(viewMapper.state==5) {tempString+="Considering if moving to an object is a good idea, Chose not to move "+viewMapper.choseNotToMove+" times";}
  327.             GUILayout.Label(tempString);
  328.  
  329.             tempString = "ControlMachine: Current state: "+controlMachine.state.ToString()+", Blocks: "+(controlMachine.n+controlMachine.n2).ToString()+", Current Action: ";
  330.             if(controlMachine.state==-2) { tempString+="Searching for everything needed for the class to work";};
  331.             if(controlMachine.state==-1) { tempString+="Finishing setting up";};
  332.             if(controlMachine.state==0) { tempString+="Waiting for simulation to start"; };
  333.             if(controlMachine.state==1) { tempString+="Obtaining information about the machine"; };
  334.             if(controlMachine.state==2) { tempString+="Considering if it should continue working"; };
  335.             if(controlMachine.state==3) { tempString+="Remembering how to move. BB knows everything, and never learns anything, it just KNOWS!"; }
  336.             if(controlMachine.state==4) { tempString+="Deciding where to move"; }
  337.             if(controlMachine.state==5) { tempString+="Moving to "+controlMachine.moveToPosition.ToString()+" in "+(Mathf.Floor((controlMachine.wantedArrivalTime-Time.realtimeSinceStartup)*100)/100).ToString(); }
  338.             GUILayout.Label(tempString);
  339.  
  340.             tempString = "SelectVictim: Current state: "+selectVictim.state.ToString()+", Substate: "+selectVictim.victimState.ToString()+", Current Action: ";
  341.             if(selectVictim.state==0) { tempString += "Selecting a victim";}
  342.             if(selectVictim.state==1) { tempString += "Moving towards a victim";}
  343.             if(selectVictim.state==2) { tempString += "Preparing to attack victim";}
  344.             if(selectVictim.state==3) { tempString += "Relentlessly attacking victim"; }
  345.             if(selectVictim.state==4) { tempString += "He who fights with BB should be careful lest he thereby become a victim. And if thou gaze long at BB, then BB will also gaze into thee."; }
  346.  
  347.             GUILayout.Label(tempString);
  348.            
  349.         }
  350.     }
  351.  
  352.     public class SelectVictim : MonoBehaviour {
  353.         public SelectVictim(){
  354.  
  355.         }
  356.        
  357.         public int state = 0;
  358.         public int victimState = 5;
  359.         public bool hasControl = false;
  360.        
  361.         public bool requestAttackVictim = false;
  362.  
  363.         public bool requestingVictim = true;
  364.         public Vector3 victimPosition;
  365.         public Vector3 moveToPosition = new Vector3(5,5,0);
  366.         public float wantedArrivalTime = 0;
  367.  
  368.         public void FixedUpdate() {
  369.             if(requestingVictim||!AddPiece.isSimulating)
  370.                 return;
  371.             if(state==0) {
  372.                 victimState = 5;
  373.                 requestingVictim = true;
  374.                 return;
  375.             }
  376.             if(state==2){
  377.                 moveToPosition=victimPosition;
  378.                 wantedArrivalTime = Time.realtimeSinceStartup+3;
  379.                 state=3;
  380.             }
  381.             if(state==3){
  382.                 moveToPosition=victimPosition;
  383.                 wantedArrivalTime += Time.fixedDeltaTime/2;
  384.                 if(wantedArrivalTime<=Time.realtimeSinceStartup){
  385.                     requestAttackVictim = true;
  386.                     state = 0;
  387.                     victimState = 5;
  388.                 }
  389.             }
  390.  
  391.             if(victimState==5) {
  392.                 moveToPosition = victimPosition;
  393.                 wantedArrivalTime = Time.realtimeSinceStartup+5;
  394.                 victimState = 0;
  395.             }
  396.             if(victimState==0) {
  397.                 moveToPosition=victimPosition;
  398.                 if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  399.                     moveToPosition = victimPosition+new Vector3(0,10+UnityEngine.Random.Range(0,10),0);
  400.                     wantedArrivalTime = Time.realtimeSinceStartup+10;
  401.                     victimState = 1;
  402.                 }
  403.             }
  404.             if(victimState==1) {
  405.                 if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  406.                     moveToPosition = victimPosition;
  407.                     wantedArrivalTime = Time.realtimeSinceStartup+5;
  408.                     victimState = 2;
  409.                 }
  410.             }
  411.             if(victimState==2) {
  412.                 moveToPosition=victimPosition;
  413.                 wantedArrivalTime += Time.fixedDeltaTime/2;
  414.                 if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  415.                     victimState=3;
  416.                     wantedArrivalTime = Time.realtimeSinceStartup+3;
  417.                 }
  418.             }
  419.             if(victimState==3) {
  420.                 moveToPosition=victimPosition;
  421.                 wantedArrivalTime += Time.fixedDeltaTime/4;
  422.                 if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  423.                     victimState=4;
  424.                     wantedArrivalTime = Time.realtimeSinceStartup+1;
  425.                 }
  426.             }
  427.             if(victimState==4) {
  428.                 moveToPosition=victimPosition;
  429.                 wantedArrivalTime += Time.fixedDeltaTime/8;
  430.                 if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  431.                     victimState=5;
  432.                     state = 0;
  433.                 }
  434.             }
  435.         }
  436.  
  437.     }
  438.  
  439.     public class ControlMachine : MonoBehaviour {
  440.         public string currentPosition = "Pan";
  441.  
  442.         Texture2D texture2D = new Texture2D(1,1);
  443.         public GameObject baseBlock = null;
  444.         public int state = -2;
  445.  
  446.         public bool hasControl = false;
  447.         List<GameObject> flyBlockList = new List<GameObject>();
  448.  
  449.         public Vector3 moveToPosition = new Vector3(0,10,0);
  450.         public float wantedArrivalTime = 0;
  451.         public bool requestToViewMachine = false;
  452.  
  453.         public float forceMultiplier = 5;
  454.         public Vector3 previousDistanceAwayFromObject = Vector3.zero;
  455.  
  456.         public int n = 0;
  457.         public int n2 = 0;
  458.  
  459.  
  460.  
  461.         public bool BBpngExists = File.Exists(Application.dataPath+"/Resources/BB.png");
  462.  
  463.         public ControlMachine() {
  464.            
  465.         }
  466.  
  467.         public void useTheForce() {
  468.             baseBlock.rigidbody.AddForce(-Physics.gravity*Time.fixedDeltaTime,ForceMode.Acceleration);
  469.             float wantedTimeLeftUntilArrival = wantedArrivalTime-Time.realtimeSinceStartup;
  470.             if(wantedTimeLeftUntilArrival<=0)
  471.                 return;
  472.             Vector3 distanceAwayFromPosition = moveToPosition-baseBlock.transform.position;
  473.             if((distanceAwayFromPosition-previousDistanceAwayFromObject).magnitude<0.63f) {
  474.                 return;
  475.             }
  476.             previousDistanceAwayFromObject = distanceAwayFromPosition;
  477.             Vector3 wantedVelocity = distanceAwayFromPosition/wantedTimeLeftUntilArrival;
  478.             if((wantedVelocity-baseBlock.rigidbody.velocity).magnitude*forceMultiplier<=0.15f)
  479.                 return;
  480.             baseBlock.rigidbody.AddForce((wantedVelocity-baseBlock.rigidbody.velocity),ForceMode.VelocityChange);
  481.         }
  482.  
  483.         public void getBlockCount() {
  484.             for(int i=n; i<=n; i++) {
  485.                 if(GameObject.Find("bsg"+i.ToString())!=null) {
  486.                     n++;
  487.                 }
  488.             }
  489.             for(int i=n2;i<=n2;i++) {
  490.                 if(GameObject.Find("bgeL"+i.ToString())!=null) {
  491.                     n2++;
  492.                 }
  493.             }
  494.         }
  495.  
  496.         public void findFlyBlocks() {
  497.             flyBlockList = new List<GameObject>();
  498.             for(int i = 0;i<n;i++) {
  499.                 GameObject currentGameObject = GameObject.Find("bsg"+i.ToString());
  500.                 if(currentGameObject!=null) {
  501.                     if(currentGameObject.GetComponent<MyBlockInfo>().blockName=="FLYING SPIRAL") {
  502.                         flyBlockList.Add(currentGameObject);
  503.                     }
  504.                 }
  505.             }
  506.             for(int i = 0;i<n2;i++) {
  507.                 GameObject currentGameObject = GameObject.Find("bgeL"+i.ToString());
  508.                 if(currentGameObject!=null) {
  509.                     if(currentGameObject.GetComponent<MyBlockInfo>().blockName=="FLYING SPIRAL") {
  510.                         flyBlockList.Add(currentGameObject);
  511.                     }
  512.                 }
  513.             }
  514.         }
  515.  
  516.         public bool getEverythingNeededToRunTheClass() {
  517.             currentPosition = "Pan";
  518.             if(texture2D.width==1&&BBpngExists) {
  519.                 Debug.Log(Application.dataPath+"/Resources/BB.png");
  520.                 byte[] imageData = File.ReadAllBytes(Application.dataPath+"/Resources/BB.png");
  521.                 texture2D.LoadImage(imageData);
  522.                 if(texture2D.width==1) {
  523.                     return false;
  524.                 }
  525.                 Debug.Log("Found BB.png");
  526.             }
  527.             if(baseBlock==null) {
  528.                 baseBlock = GameObject.Find("bgeL0");
  529.                 if(baseBlock==null) {
  530.                     return false;
  531.                 }
  532.                 Debug.Log("Found bgeL0");
  533.             }
  534.             else {
  535.                 baseBlock = GameObject.Find("bgeL0");
  536.             }
  537.             currentPosition = baseBlock.transform.position.ToString();
  538.             return true;
  539.         }
  540.  
  541.         public void FixedUpdate() {
  542.             if(!getEverythingNeededToRunTheClass()) {
  543.                 if(state>-2) {
  544.                     n = 0;
  545.                     n2 = 0;
  546.                     state = -1;
  547.                 }
  548.                 return;
  549.             }
  550.             getBlockCount();
  551.             if(state==-2) {
  552.                 state = -1;
  553.             }
  554.             if(state==-1) {
  555.                 Renderer[] tempRenderers;
  556.                 try {
  557.                     tempRenderers = baseBlock.GetComponentsInChildren<Renderer>();
  558.                 }
  559.                 catch {
  560.                     Debug.Log("Could not find renderers");
  561.                     state = 0;
  562.                     return;
  563.                 }
  564.                 Texture tempTexture = new Texture();
  565.                 try {
  566.                     tempTexture = texture2D;
  567.                 }
  568.                 catch {
  569.                     Debug.Log("Could not save Texture");
  570.                     state = 0;
  571.                     return;
  572.                 }
  573.                 tempTexture.wrapMode = TextureWrapMode.Clamp;
  574.                 foreach(Renderer tempRenderer in tempRenderers) {
  575.                     try {
  576.                         tempRenderer.material.mainTexture = tempTexture;
  577.                     }
  578.                     catch {
  579.                         continue;
  580.                     }
  581.                     Debug.Log("Applied BB texture to "+tempRenderer.name);
  582.                 }
  583.                 Collider[] colliders = baseBlock.GetComponentsInChildren<Collider>();
  584.                 foreach(Collider ccollider in colliders) {
  585.                     try {
  586.                         ccollider.material.bounciness = 0.75f;
  587.                         ccollider.material.bounceCombine = PhysicMaterialCombine.Maximum;
  588.                     }
  589.                     catch {
  590.                         continue;
  591.                     }
  592.                     Debug.Log("Applied bounciness to "+ccollider.name);
  593.                 }
  594.                 foreach(Collider ccollider in colliders) {
  595.                     try {
  596.                         ccollider.material.staticFriction = 0;
  597.                         ccollider.material.dynamicFriction = 0;
  598.                         ccollider.material.frictionCombine = PhysicMaterialCombine.Minimum;
  599.                     }
  600.                     catch {
  601.                         continue;
  602.                     }
  603.                     Debug.Log("Applied friction to "+ccollider.name);
  604.                 }
  605.                 state = 0;
  606.             }
  607.             if(state==1) {
  608.                 if(AddPiece.isSimulating) {
  609.                     Debug.Log(baseBlock.transform.position.ToString());
  610.                     findFlyBlocks();
  611.                     state = 2;
  612.                 }
  613.             }
  614.             if(state==3) {
  615.                 if(hasControl) {
  616.                     Debug.Log(baseBlock.transform.position.ToString());
  617.                     //SOME FANCY FUNCTION, OKAY?
  618.                     wantedArrivalTime = Time.realtimeSinceStartup + 5;
  619.                     state = 4;
  620.                 }
  621.             }
  622.             if(state==4) {
  623.                 if(AddPiece.isSimulating) {
  624.                     if(wantedArrivalTime>Time.realtimeSinceStartup&&(baseBlock.transform.position-moveToPosition).magnitude>2) {
  625.                         state = 5;
  626.                     }
  627.                 }
  628.                 else {
  629.                     state = 1;
  630.                 }
  631.             }
  632.             if(state==5) {
  633.                 if(AddPiece.isSimulating) {
  634.                     if(wantedArrivalTime<=Time.realtimeSinceStartup) {
  635.                         state = 4;
  636.                         return;
  637.                     }
  638. //                  if(n==0&&n2==1) {
  639.                         useTheForce();
  640. //                      return;
  641. //                  }
  642.                 }
  643.                 else {
  644.                     state = 1;
  645.                 }
  646.             }
  647.         }
  648.     }
  649.  
  650.     public class ViewMapper : MonoBehaviour {
  651.         public int state = -1;
  652.         public float timeSinceLastCheck = 0;
  653.         public int allWorldGameObjectsChecked = 0;
  654.         public float defaultScale = 1;
  655.  
  656.         public int choseNotToMove = 0;
  657.         public int gotToViewMachine = 0;
  658.         public float viewMachineImmunity = 0;
  659.  
  660.         public MouseOrbit mouseOrbit;
  661.         public GameObject[] allWorldGameObjects;
  662.         public List<float> allWorldlastTimeGameObjectWasSeen = new List<float>();
  663.         public List<bool> allWorldlastMovedGameObjectWasSeen = new List<bool>();
  664.         public List<bool> allWorldhasMoved = new List<bool>();
  665.         public List<Vector3> allWorldlastPositionGameObjectWasSeen = new List<Vector3>();
  666.         public List<Vector3> allWorldlastVelocityGameObjectWasSeen = new List<Vector3>();
  667.         public int victim = -1;
  668.  
  669.         public ViewMapper() {
  670.             state = 0; //Nothing mapped state
  671.         }
  672.  
  673.         public void selectVictim() {
  674.             if(state<3)
  675.                 return;
  676.             for(int i = 0;i<1000;i++) {
  677.                 int current = 0;
  678.                 try {
  679.                     current = UnityEngine.Random.Range(0,allWorldGameObjects.Length-1);
  680.                 }
  681.                 catch {
  682.                     Debug.Log("Could not get random number");
  683.                     continue;
  684.                 }
  685.                 try {
  686.                     if(allWorldGameObjects[current]==null) {
  687.                         i--;
  688.                         continue;
  689.                     }
  690.                 }
  691.                 catch {
  692.                     Debug.Log("Could not check if null");
  693.                     continue;
  694.                 }
  695.                 try {
  696.                     if(allWorldGameObjects[current].transform.position.magnitude>1000) {
  697.                         i--;
  698.                         continue;
  699.                     }
  700.                 }
  701.                 catch {
  702.                     Debug.Log("Could not check position");
  703.                     continue;
  704.                 }
  705.                 try {
  706.                     if(allWorldGameObjects[current].rigidbody==null)
  707.                         continue;
  708.                 }
  709.                 catch {
  710.                     Debug.Log("Could not check if rigidbody is null");
  711.                     continue;
  712.                 }
  713.                 try {
  714.                     if(allWorldhasMoved[current]) {
  715.                         victim = current;
  716.                         return;
  717.                     }
  718.                 }
  719.                 catch {
  720.                     Debug.Log("Could not select victim");
  721.                     continue;
  722.                 }
  723.             }
  724.             victim = -1;
  725.         }
  726.  
  727.         public bool checkIfLikelyToViewMachine(int n) {
  728.             int r = UnityEngine.Random.Range(0,1+gotToViewMachine);
  729.             if(r<=n)
  730.                 return true;
  731.             return false;
  732.         }
  733.  
  734.         public bool requestViewMachine() {
  735.             if(timeSinceLastCheck<0) {
  736.                 return true;
  737.             }
  738.             if(viewMachineImmunity>0) {
  739.                 viewMachineImmunity -= Time.deltaTime;
  740.                 return false;
  741.             }
  742.             if(checkIfLikelyToViewMachine(3)) {
  743.                 mouseOrbit.target = GameObject.Find("bgeL0").transform;
  744. //              mouseOrbit.wasdPOSdelegate = Vector3.zero;
  745. //              mouseOrbit.distance = defaultScale*GameObject.Find("bgeL0").transform.localScale.magnitude;
  746.                 mouseOrbit.zoomSmooth = 10;
  747. //              mouseOrbit.x = 0;
  748.                 gotToViewMachine++;
  749.                 return true;
  750.             }
  751.                 gotToViewMachine--;
  752.             if(gotToViewMachine<=3)
  753.                 gotToViewMachine=0;
  754.             return false;
  755.         }
  756.  
  757.         public bool saveObject(int allWorldN) {
  758.             if(allWorldGameObjects.Length<=allWorldN||allWorldN<0)
  759.                 return false;
  760.             if(allWorldGameObjects[allWorldN]==null)
  761.                 return false;
  762.             if(!allWorldGameObjects[allWorldN].activeInHierarchy) {
  763.                 if(allWorldlastTimeGameObjectWasSeen.Count==allWorldN-1) {
  764.                     allWorldlastTimeGameObjectWasSeen.Add(Time.realtimeSinceStartup);
  765.                     allWorldlastPositionGameObjectWasSeen.Add(Vector3.zero);
  766.                     allWorldlastVelocityGameObjectWasSeen.Add(Vector3.zero);
  767.                     allWorldhasMoved.Add(false);
  768.                     allWorldlastMovedGameObjectWasSeen.Add(false);
  769.                 }
  770.                 return false;
  771.             }
  772.             if(allWorldGameObjects[allWorldN].rigidbody==null) {
  773.                 if(allWorldlastTimeGameObjectWasSeen.Count==allWorldN-1) {
  774.                     allWorldlastTimeGameObjectWasSeen.Add(Time.realtimeSinceStartup);
  775.                     allWorldlastPositionGameObjectWasSeen.Add(Vector3.zero);
  776.                     allWorldlastVelocityGameObjectWasSeen.Add(Vector3.zero);
  777.                     allWorldhasMoved.Add(false);
  778.                     allWorldlastMovedGameObjectWasSeen.Add(false);
  779.                 }
  780.                 return false;
  781.             }
  782.             if(allWorldlastTimeGameObjectWasSeen.Count<=allWorldN){
  783.                 if(allWorldlastTimeGameObjectWasSeen.Count==allWorldN-1){
  784.                     allWorldlastTimeGameObjectWasSeen.Add(Time.realtimeSinceStartup);
  785.                     allWorldlastPositionGameObjectWasSeen.Add(allWorldGameObjects[allWorldN].transform.position);
  786.                     allWorldlastVelocityGameObjectWasSeen.Add(allWorldGameObjects[allWorldN].rigidbody.velocity);
  787.                     allWorldhasMoved.Add(false);
  788.                     allWorldlastMovedGameObjectWasSeen.Add(false);
  789.                 }
  790.                 else {
  791.                     return false;
  792.                 }
  793.             }
  794.             else {
  795.                 allWorldlastTimeGameObjectWasSeen[allWorldN] = Time.realtimeSinceStartup;
  796.                 if(allWorldGameObjects[allWorldN].rigidbody.velocity.magnitude>=0.15f||allWorldGameObjects[allWorldN].transform.position!=allWorldlastPositionGameObjectWasSeen[allWorldN]) {
  797.                     allWorldhasMoved[allWorldN] = true;
  798.                     allWorldlastMovedGameObjectWasSeen[allWorldN] = true;
  799.                 }
  800.                 else {
  801.                     allWorldlastMovedGameObjectWasSeen[allWorldN] = false;
  802.                 }
  803.                 allWorldlastPositionGameObjectWasSeen[allWorldN] = allWorldGameObjects[allWorldN].transform.position;
  804.                 allWorldlastVelocityGameObjectWasSeen[allWorldN] = allWorldGameObjects[allWorldN].rigidbody.velocity;
  805.             }
  806.             return true;
  807.         }
  808.  
  809.         public bool getEverythingNeededToRunTheClass(){
  810.             if(mouseOrbit==null) {
  811.                 mouseOrbit = Camera.main.GetComponent<MouseOrbit>();
  812.                 if(mouseOrbit==null) {
  813.                     return false;
  814.                 }
  815.                 Debug.Log("Found MouseOrbit");
  816.             }
  817.  
  818.             return true;
  819.         }
  820.  
  821.         public void goToNextObject() {
  822.             timeSinceLastCheck += Time.deltaTime;
  823.             if(state==2) {
  824.                 timeSinceLastCheck += Time.deltaTime*(allWorldGameObjects.Length/125+1);
  825.             }
  826.             while(timeSinceLastCheck>=0.5f) {
  827.                 timeSinceLastCheck -= 0.5f;
  828.                 if(state==2) {
  829.                     if(allWorldGameObjectsChecked>=allWorldGameObjects.Length) {
  830.                         Debug.Log("Changing viewLoggerState to 3");
  831.                         state = 3;
  832.                         mouseOrbit.target = GameObject.Find("bgeL0").transform;
  833.     //                  mouseOrbit.wasdPOSdelegate = Vector3.zero;
  834.     //                  mouseOrbit.distance = defaultScale/2;
  835.                         mouseOrbit.zoomSmooth = 10;
  836.     //                  mouseOrbit.x = 0;
  837.                         return;
  838.                     }
  839.                     else {
  840.                         while(allWorldGameObjectsChecked<=allWorldGameObjects.Length) {
  841.                             if(saveObject(allWorldGameObjectsChecked)) {
  842.                                 mouseOrbit.target = allWorldGameObjects[allWorldGameObjectsChecked].transform;
  843.     //                          mouseOrbit.wasdPOSdelegate = Vector3.zero;
  844.                                 allWorldGameObjectsChecked++;
  845.                                 return;
  846.                             }
  847.                             allWorldGameObjectsChecked++;
  848.                         }
  849.                     }
  850.                 }
  851.                 else if(state==5) {
  852.                     for(int i = 0;i<allWorldGameObjects.Length/10;i++) {
  853.                         int current = 0;
  854.                         try {
  855.                             current = UnityEngine.Random.Range(0,allWorldGameObjects.Length-1);
  856.                         }
  857.                         catch {
  858.  
  859.                         }
  860.                         if(allWorldGameObjects[current]==null) {
  861.                             i--;
  862.                             continue;
  863.                         }
  864.                         if(allWorldGameObjects[current].activeInHierarchy) {
  865.                             if(allWorldGameObjects[current].rigidbody!=null) {
  866.                                 float div = 10;
  867.                                 if(!allWorldhasMoved[current]){div*=4;}
  868.                                 if(!allWorldlastMovedGameObjectWasSeen[current]){div*=2;}
  869.                                 if(UnityEngine.Random.Range(0f,100f)<=(Time.realtimeSinceStartup-allWorldlastTimeGameObjectWasSeen[current])/(div)) {
  870.                                     mouseOrbit.target = allWorldGameObjects[current].transform;
  871.     //                              mouseOrbit.wasdPOSdelegate = Vector3.zero;
  872.                                     this.choseNotToMove = 0;
  873.                                     return;
  874.                                 }
  875.                             }
  876.                         }
  877.                     }
  878.                     choseNotToMove++;
  879.                     if(choseNotToMove==20) {
  880.                         mouseOrbit.target = GameObject.Find("bgeL0").transform;
  881.     //                  mouseOrbit.wasdPOSdelegate = Vector3.zero;
  882.     //                  mouseOrbit.distance = defaultScale/2;
  883.                         mouseOrbit.zoomSmooth = 10;
  884.     //                  mouseOrbit.x = 0;
  885.                     }
  886.                 }
  887.             }
  888.         }
  889.  
  890.         public void saveEverythingYouSee() {
  891.             for(int i = 0;i<allWorldGameObjects.Length;i++) {
  892.                 if(allWorldGameObjects[i]!=null) {
  893.                     if(allWorldGameObjects[i].activeInHierarchy) {
  894.                         if(allWorldGameObjects[i].renderer!=null) {
  895.                             if(allWorldGameObjects[i].renderer.isVisible) {
  896.                                 saveObject(i);
  897.                             }
  898.                         }
  899.                     }
  900.                 }
  901.             }
  902.         }
  903.  
  904.         public void Update() {
  905.             if(!getEverythingNeededToRunTheClass()) {
  906.                 return;
  907.             }
  908.             if(state==-1) {
  909.                 Debug.Log("Changing viewLoggerState to 0");
  910.                 state = 0;
  911.             }
  912.             if(state==1) {
  913.                 allWorldGameObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
  914.                 allWorldGameObjectsChecked = 0;
  915.                 allWorldlastTimeGameObjectWasSeen = new List<float>();
  916.                 allWorldlastMovedGameObjectWasSeen = new List<bool>();
  917.                 allWorldhasMoved = new List<bool>();
  918.                 allWorldlastPositionGameObjectWasSeen = new List<Vector3>();
  919.                 allWorldlastVelocityGameObjectWasSeen = new List<Vector3>();
  920.                 Debug.Log("Changing viewLoggerState to 2");
  921.                 state = 2;
  922.             }
  923.             if(state==2) {
  924.                 mouseOrbit.x -= 80*Time.deltaTime;
  925.                 mouseOrbit.distance = Mathf.Lerp(mouseOrbit.distance, defaultScale*mouseOrbit.target.localScale.magnitude,Time.deltaTime);
  926.                 goToNextObject();
  927.             }
  928.             if(state==5) {
  929.                 mouseOrbit.x += 5*Time.deltaTime;
  930.                 mouseOrbit.distance = Mathf.Lerp(mouseOrbit.distance,defaultScale*mouseOrbit.target.localScale.magnitude/2,Time.deltaTime/2);
  931.                 goToNextObject();
  932.                 saveEverythingYouSee();
  933.             }
  934.         }
  935.  
  936.     }
  937. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement