Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Threading;
- using System.Collections;
- using System.Collections.Generic;
- public class AI : MonoBehaviour {
- const byte _TOP = 0x1;
- const byte _BOT = 0x2;
- const byte _LEFT = 0x4;
- const byte _RIGHT = 0x8;
- const byte _STEP = 0x16;
- // list of coordinates in current path
- public List<Vector3> path;// = new ArrayList<Vector3>();
- public float offset = -14.546f;
- public float Distancefrom = 0;
- public Vector3 nextTarget = Vector3.zero;
- public float Speed = 50;
- public GameObject Cube;
- float vertVelocity = 0;
- public bool hastarget = false;
- public int StepLimit = 10;
- public float StepInc = 1;
- GameObject plyr;
- GameObject trgt;
- Vector3 lastseen;
- CharacterController me;
- Animator anim;
- // Use this for initialization
- void Start () {
- //if (!plyr)
- me = GetComponent<CharacterController>();
- anim = GetComponent<Animator>();
- plyr = GameObject.Find("Player");
- if (!plyr)
- Debug.Log("Player not found!");
- trgt = GameObject.Find("AITarget");
- //CreatePath();
- lastseen= transform.position;
- path = new List<Vector3>();
- CreatePath();
- hastarget = false;
- }
- // Update is called once per frame
- bool hadtarget = false;
- void Update () {
- //if (!hastarget)
- hastarget = Check(plyr);
- if (hastarget != hadtarget) {
- if (hastarget) {
- path.Add(plyr.transform.position);
- Debug.Log("Player is in view!");
- } else {
- lastseen = plyr.transform.position;
- Debug.Log("Player was last seen "+lastseen);
- }
- hadtarget = hastarget;
- } else {
- if (hastarget) {
- // ignore pathing and go straight for the player
- //path.Clear();
- //path.Add(new Vector3(plyr.transform.position.x, plyr.transform.position.y-14.546f,plyr.transform.position.z));
- }
- }
- vertVelocity += (Physics.gravity.y/2) * Time.deltaTime;
- if (path.Count > 0) {
- //Vector3 dir = (path[0] - transform.position);
- nextTarget = path[0];
- trgt.transform.position = new Vector3(path[0].x, trgt.transform.position.y, path[0].z);
- Distancefrom = (new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(path[0].x, 0, path[0].z)).magnitude;
- // Have we reached target? Is path blocked?
- if (Distancefrom < 25 || !Check(new Vector3(path[0].x, path[0].y+offset, path[0].z))) {
- path.Remove(path[0]);
- anim.SetFloat("Speed", 0);
- }
- if (path.Count == 0)
- return;
- Vector3 dest = Vector3.MoveTowards(transform.position, new Vector3(path[0].x, path[0].y+offset, path[0].z), Speed*Time.deltaTime)-transform.position;
- vertVelocity += Physics.gravity.y;
- dest.y += vertVelocity;
- transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(new Vector3(path[0].x-transform.position.x, 0, path[0].z-transform.position.z)), 5*Time.deltaTime);
- anim.SetFloat("Speed", dest.magnitude);
- me.Move(dest);
- //gameObject.transform.position = dest;
- } else {
- anim.SetFloat("Speed", 0);
- }
- }
- void LateUpdate() {
- if (hastarget) {
- // Add target location to pathing if he's a distance away
- // and only if he's moved from the location
- if ((new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(plyr.transform.position.x, 0, plyr.transform.position.z)).magnitude > 15) {
- // Not at target
- //Debug.Log(path.Count+" coordinates to path.");
- if (path.Count > 0) {
- if ((plyr.transform.position - path[path.Count-1]).magnitude > 4) {
- // player has moved from path
- //Debug.Log("Player in a new location!");
- path.Clear();
- path.Add(plyr.transform.position);
- } else {
- // update last added position
- //Debug.Log("Updating last path.");
- path[path.Count-1] = plyr.transform.position;
- }
- } else {
- // player has moved
- //Debug.Log("New path added.");
- path.Add(plyr.transform.position);
- }
- }
- else {
- //Debug.Log("Reached Player!");
- //path.Remove(path[0]);
- path.Clear();
- }
- }
- }
- // store map containing pathing to target
- List<List<Vector3>> routes = new List<List<Vector3>>();
- List<Vector3> route = new List<Vector3>();
- Dictionary<Vector3, int> map = new Dictionary<Vector3, int>();
- public bool pathing = false;
- Vector3 ppos = Vector3.zero;
- double cycles = 0;
- void CreatePath() {
- if (hastarget) {
- Debug.Log("Creating table...");
- pathing = true;
- map.Clear();
- ppos = new Vector3(plyr.transform.position.x, plyr.transform.position.y+offset, plyr.transform.position.z);
- // start a pathfinding tree
- //Thread t = new Thread(() => {
- System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
- cycles = 0;
- stopwatch.Start();
- MapCoord(new Vector3(((transform.position.x)), ((transform.position.y))+10, ((transform.position.z))), 1, 0);
- stopwatch.Stop();
- Debug.Log(cycles + " trees cycled in "+(stopwatch.Elapsed)+" seconds! "+map.Count+" steps marked, "+((pathing)? "Player not found?":"Player found!"));
- // did we find our player?
- if (!pathing) {
- routes.Clear(); route.Clear();
- Debug.Log(routes.Count+" possible routes, fastest in "+path.Count+" gridblocks");
- pathing = false;
- }
- foreach (var key in map.Keys) {
- if (map[key] != -1) {
- float step = ((float)map[key]-1) / StepLimit; float R = (step); float G = (1-R);
- //Debug.Log("Step = "+map[key]+",R: "+R+", G: "+G);
- GameObject cube = (GameObject)Instantiate(Cube, key, transform.rotation);// (map[key] | _STEP);
- cube.renderer.material.color = new Color(R, G, 0, 10.0f);
- cube.transform.localScale = new Vector3(StepInc*2/3, 0, StepInc*2/3);
- //Debug.Log(msg);
- } else {
- GameObject cube = (GameObject)Instantiate(Cube, key, transform.rotation);// (map[key] | _STEP);
- cube.renderer.material.color = new Color(0, 0, 0, 10.0f);
- cube.transform.localScale = new Vector3(StepInc, 0, StepInc);
- }
- }
- //});
- //t.Start();
- }
- }
- // tree based scan
- void MapCoord(Vector3 loc, int step, int back) {
- cycles++;
- //int top = 0, bot = 0, left = 0, right = 0;
- // if we have reached player, stop looking
- if ((loc-ppos).magnitude < StepInc) {
- Debug.Log("Player found!");
- //map.Add (coord, (byte)(top | bot<<1 | left<<2 | right<<3 | step<<4));
- //path.Add(loc);
- if (map.ContainsKey(loc)) {
- map[loc] = -1;
- } else {
- map.Add(loc, -1);
- }
- //path.Add(loc);
- pathing = false;
- return;
- }
- // leave a limiter to prevent infinite loops
- if (step >= StepLimit) {
- //Debug.Log("Step limit reached.");
- return;
- }
- // ignore retracing steps
- if (map.ContainsKey(loc)) {
- if ((map[loc]) <= step)
- return;
- map[loc] = step;
- } else {
- map.Add(loc, step);
- }
- // vertical path limiter, stop mapping this tree
- if (Check (loc,new Vector3(loc.x, loc.y+10, loc.z))) {
- Debug.Log("Steep drop D:");
- //return;
- }
- string dbg = "Found:";
- if (back != 1) {
- if (!Check (loc,new Vector3(loc.x, loc.y, loc.z+StepInc))) {
- //top = 1;
- dbg += " Top";
- MapCoord(new Vector3(loc.x, loc.y, loc.z+StepInc), step+1, 2);
- }
- }
- if (back != 2) {
- if (!Check (loc,new Vector3(loc.x, loc.y, loc.z-StepInc))) {
- //bot = 1;
- dbg += " Bot";
- MapCoord(new Vector3(loc.x, loc.y, loc.z-StepInc), step+1, 1);
- }
- }
- if (back != 3) {
- if (!Check (loc,new Vector3(loc.x-StepInc, loc.y, loc.z))) {
- //left = 1;
- dbg += " Left";
- MapCoord(new Vector3(loc.x-StepInc, loc.y, loc.z), step+1, 4);
- }
- }
- if (back != 4) {
- if (!Check (loc,new Vector3(loc.x+StepInc, loc.y, loc.z))) {
- //right = 1;
- dbg += " Right";
- MapCoord(new Vector3(loc.x+StepInc, loc.y, loc.z), step+1, 3);
- }
- }
- //Debug.Log("Step "+step+" "+dbg);
- return;
- }
- // returns true if collision with map is detected
- bool Check(Vector3 begin, Vector3 target) {
- RaycastHit hit;
- if (Physics.Raycast(begin, (target-begin), out hit, (target-begin).magnitude))
- if (hit.collider.gameObject.tag == "Field")
- return true;
- return false;
- }
- // returns false if this object can see target object
- bool Check(GameObject obj) {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, obj.transform.position-transform.position, out hit))
- if (hit.collider.gameObject.name == obj.name)
- return true;
- return false;
- }
- // returns false if this object can move to destination coordinate
- bool Check(Vector3 target) {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, target-transform.position, out hit))
- if (hit.collider.gameObject.tag == "Field")
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment