예제 #1
0
        public void UpdatePathing()
        {
            ////// new temp stuff
            MovingEntity body = GetComponent <MovingEntity>();

            //pathingTarget = LVector3.FromUnityVector3(FindObjectOfType<BlocksPlayer>().transform.position);

            /*
             * // find position on ground below player and path to that instead
             * int goDownAmount = 10; // don't get into infinite loop, give up eventually
             * while (pathingTarget.BlockV == BlockValue.Air && goDownAmount > 0)
             * {
             *  pathingTarget = pathingTarget - new LVector3(0, 1, 0);
             *  goDownAmount--;
             * }
             * pathingTarget += new LVector3(0, 2, 0);
             */
            //////

            pathingTarget = LVector3.Invalid;

            if (thingDoing != null)
            {
                if (thingDoing.target != null)
                {
                    if (thingDoing.target.entity != null)
                    {
                        pathingTarget        = LVector3.FromUnityVector3(thingDoing.target.entity.transform.position);
                        iNeedToPathfindAgain = true;
                    }
                    else if (thingDoing.target.block != LVector3.Invalid)
                    {
                        pathingTarget = thingDoing.target.block;
                        //Debug.Log("going to pathing target with block in thing doing of " + pathingTarget);
                    }
                }
            }

            if (curPath == null)
            {
                iNeedToPathfindAgain = true;
            }

            if (pathingTarget == LVector3.Invalid)
            {
                body.desiredMove = Vector3.zero;
            }
            if (PhysicsUtils.millis() - lastPathfind > 1000.0 / pathfindsPerSecond && frameUpdatedLast != Time.frameCount && iNeedToPathfindAgain) // offset so everyone isn't aligned on the same frame
            {
                LVector3 myPos = LVector3.FromUnityVector3(transform.position);
                LVector3 foundThing;

                /*
                 * if (Search(out foundThing))
                 * {
                 *  // found it, is it closer?
                 *  if (pathingTarget == LVector3.Invalid || LVector3.CityBlockDistance(foundThing, myPos) < LVector3.CityBlockDistance(pathingTarget, myPos))
                 *  {
                 *      // if so, go to it instead
                 *      ThingDoing newThingDoing = new ThingDoing(thingDoing.typeOfThing, new ThingDoingTarget(foundThing));
                 *      // copy over valid blocks for future reference
                 *      if (thingDoing != null && thingDoing.target != null)
                 *      {
                 *          newThingDoing.target.validBlocks = thingDoing.target.validBlocks;
                 *      }
                 *      thingDoing = newThingDoing;
                 *      pathingTarget = foundThing;
                 *  }
                 *  //Debug.Log("found thing when looking");
                 * }
                 * else
                 * {
                 *  // did not find
                 *  //Debug.Log("did not find thing when looking");
                 *  //Debug.Log("did not find thing in " + steps + " steps");
                 * }
                 */
                frameUpdatedLast = Time.frameCount;
                //Debug.Log("updating pathing");
                RaycastResults blockStandingOn;

                bool lookingForBlocks = false;
                if (thingDoing != null && thingDoing.target != null && thingDoing.target.entity == null && thingDoing.target.block == LVector3.Invalid && thingDoing.target.validBlocks != null)
                {
                    lookingForBlocks = true;
                }

                if (pathingTarget != LVector3.Invalid || lookingForBlocks)
                {
                    if (PhysicsUtils.RayCastAlsoHitWater(body.transform.position, -Vector3.up, 20.0f, out blockStandingOn))
                    {
                        // if we are using shift and standing over an empty block, but our feet are on a neighboring block, use that neighboring block for pathfinding instead
                        if (blockStandingOn != LVector3.Invalid)
                        {
                            myPos = blockStandingOn.hitBlock + new LVector3(0, blocksHeight, 0);
                        }
                        //LVector3 playerPos = LVector3.FromUnityVector3(pathingTarget.transform.position);
                        bool iShouldJump;
                        if (thingDoing.typeOfThing == TypeOfThingDoing.RunningAway)
                        {
                            PhysicsUtils.PathfindAway(blocksHeight, ref curPath, out iShouldJump, myPos, pathingTarget, 100);
                        }
                        else
                        {
                            //// new stuff
                            bool             pathingSuccess = false;
                            PathingRegionPos resPath        = null;
                            if (lookingForBlocks)
                            {
                                resPath = PathingChunk.PathindToResource(World.mainWorld, myPos, GetComponent <MovingEntity>().reachRange, thingDoing.target.validBlocks, new MobilityCriteria(1, 1, blocksHeight, 1), out pathingSuccess, out blockWeCanSeeOnceWeGetThere, verbose: world.blocksWorld.verbosePathing);
                                if (resPath != null)
                                {
                                    pathingTarget = new LVector3(resPath.wx, resPath.wy, resPath.wz);
                                    thingDoing    = new ThingDoing(thingDoing.typeOfThing, new ThingDoingTarget(pathingTarget, thingDoing.target.validBlocks));
                                }
                            }
                            else
                            {
                                resPath = BlocksPathing.Pathfind(World.mainWorld, myPos, pathingTarget, 1, 1, blocksHeight, 1, out pathingSuccess, verbose: false);
                            }
                            iShouldJump = false;
                            if (resPath != null)
                            {
                                curPath = (new PathingResult(resPath)).GetPathNode();

                                iNeedToPathfindAgain = false;

                                //Debug.Log("got path with next pos " + curPath.pos + " also, my pos is " + myPos + " and pathing target is " + pathingTarget);
                                if (curPath.nextNode != null && curPath.nextNode.pos.y > curPath.pos.y)
                                {
                                    iShouldJump = true;
                                }

                                if (curPath.nextNode != null)
                                {
                                    //curPath = curPath.nextNode;
                                }
                            }
                            else
                            {
                                curPath = null;
                                iNeedToPathfindAgain = true;
                            }


                            /////
                            ///

                            // old thing:
                            //PhysicsUtils.Pathfind(blocksHeight, ref curPath, out iShouldJump, myPos, pathingTarget+new LVector3(0,1,0), 100);
                        }
                        if (curPath != null)
                        {
                            if (curPath.nextNode == null)
                            {
                                //Debug.Log("curPath = " + curPath.pos + " myPos = " + myPos + " next null");
                            }
                            else
                            {
                                //Debug.Log("curPath = " + curPath.pos + " myPos = " + myPos + " nextPath = " + curPath.nextNode.pos);
                            }
                        }
                        if (iShouldJump)
                        {
                            body.jumping = true;
                        }
                    }
                    else
                    {
                        //Debug.Log("falling far, cannot pathfind");
                    }
                }
                lastPathfind = PhysicsUtils.millis();
            }


            if (pathingTarget == LVector3.Invalid)
            {
                //Debug.Log("has invalid pathing target?");
                iNeedToPathfindAgain = true;
            }
            body.desiredMove = Vector3.zero;
            Vector3 targetPos = transform.position;

            if (curPath != null && pathingTarget != LVector3.Invalid)
            {
                /* // dani commented this out recently
                 * LVector3 myPos = LVector3.FromUnityVector3(transform.position);
                 * RaycastResults blockStandingOn;
                 * if (PhysicsUtils.RayCastAlsoHitWater(body.transform.position, -Vector3.up, 20.0f, out blockStandingOn))
                 * {
                 *  // if we are using shift and standing over an empty block, but our feet are on a neighboring block, use that neighboring block for pathfinding instead
                 * myPos = blockStandingOn.hitBlock + new LVector3(0, blocksHeight, 0);
                 * }
                 * LVector3 myPosBeforeJump = myPos - new LVector3(0, 1, 0);
                 * LVector3 myPosBeforeFall = myPos + new LVector3(0, 1, 0);
                 */



                //if (curPath.prevNode != null && (myPos == curPath.prevNode.pos || myPosBeforeJump == curPath.prevNode.pos))
                //{

                //}
                //else

                /*
                 * PathNode closest = curPath;
                 * float closestDist = LVector3.EuclideanDistance(closest.pos, myPos);
                 * PathNode curTmp = closest;
                 * while(curTmp.prevNode != null)
                 * {
                 *  curTmp = curTmp.prevNode;
                 *  float tmpDist = LVector3.EuclideanDistance(curTmp.pos, myPos);
                 *  if (tmpDist < closestDist)
                 *  {
                 *      closest = curTmp;
                 *      closestDist = tmpDist;
                 *  }
                 * }
                 * curTmp = curPath;
                 * while (curTmp.nextNode != null)
                 * {
                 *  curTmp = curTmp.nextNode;
                 *  float tmpDist = LVector3.EuclideanDistance(curTmp.pos, myPos);
                 *  if (tmpDist < closestDist)
                 *  {
                 *      closest = curTmp;
                 *      closestDist = tmpDist;
                 *  }
                 * }
                 *
                 * if (closest.pos == myPos)
                 * {
                 *  if (closest.nextNode == null)
                 *  {
                 *      Debug.Log("reached end of path");
                 *  }
                 *  else
                 *  {
                 *      curPath = closest.nextNode;
                 *  }
                 * }
                 * else
                 * {
                 *  curPath = closest;
                 * }*/


                /* // dani commented this out recently
                 * if (curPath.nextNode != null && (myPos == curPath.nextNode.pos || myPosBeforeJump == curPath.nextNode.pos || myPosBeforeFall == curPath.nextNode.pos))
                 * {
                 *  curPath = curPath.nextNode;
                 * }
                 * else if (myPos == curPath.pos || myPosBeforeJump == curPath.pos || myPosBeforeFall == curPath.pos)
                 * {
                 *  if (curPath.nextNode != null)
                 *  {
                 *      curPath = curPath.nextNode;
                 *  }
                 * }
                 *
                 *
                 * LVector3 targetBlock = curPath.pos;
                 * if (targetBlock.y == LVector3.FromUnityVector3(transform.position).y)
                 * {
                 *  //body.usingShift = true;
                 *  body.usingShift = false;
                 * }
                 * else
                 * {
                 *  body.usingShift = false;
                 * }
                 * if (targetBlock.y > myPos.y)
                 * {
                 *  body.jumping = true;
                 * }
                 * else
                 * {
                 *  body.jumping = false;
                 * }
                 * targetPos = targetBlock.BlockCentertoUnityVector3();
                 * body.SetAbsoluteDesiredMove((targetPos - transform.position).normalized);
                 */


                Vector3 dirToMove;
                bool    pushedOffPath;
                curPath = curPath.GetCurNode(transform.position, out body.jumping, out dirToMove, out pushedOffPath);
                body.SetAbsoluteDesiredMove(dirToMove);

                if (pushedOffPath)
                {
                    //Debug.Log("pushed off path");
                    //iNeedToPathfindAgain = true;
                }

                if (curPath.nextNode != null)
                {
                    //Debug.Log("got cur path " + curPath.pos + " with jumping " + body.jumping + " and dir to move " + dirToMove + " and next " + curPath.nextNode.pos + " and pushed off path " + pushedOffPath);
                }
                else
                {
                    //Debug.Log("got cur path " + curPath.pos + " with jumping " + body.jumping + " and dir to move " + dirToMove + " and no next and pushed off path " + pushedOffPath);
                }


                BlockValue blockWeAreGoingTo = blockWeCanSeeOnceWeGetThere.BlockV;

                if (!DesiredBlock(blockWeAreGoingTo) && thingDoing != null && thingDoing.target != null && thingDoing.target.entity == null)
                {
                    Debug.Log("rip someone took my block , my block is now " + World.BlockToString(blockWeAreGoingTo));
                    curPath       = null;
                    pathingTarget = LVector3.Invalid;
                }
                else
                {
                    float myDist = LVector3.CityBlockDistance(LVector3.FromUnityVector3(transform.position), pathingTarget);
                    // found it, eat it
                    if (myDist <= 2)
                    {
                        //Debug.Log("got to desired block");
                        if (thingDoing != null && thingDoing.typeOfThing == TypeOfThingDoing.GettingFood)
                        {
                            OnReachFoodBlock(blockWeCanSeeOnceWeGetThere);
                        }
                        else if (thingDoing != null && thingDoing.typeOfThing == TypeOfThingDoing.Gathering)
                        {
                            OnReachBlockToGather(blockWeCanSeeOnceWeGetThere);
                        }

                        /*
                         * DidEatObject(pathingTarget, 1.0f);
                         * world[pathingTarget] = (int)Example.Flower;
                         */
                        //this.thingDoing = new ThingDoing(TypeOfThingDoing.GettingFood, null);
                        if (thingDoing != null && thingDoing.target != null)
                        {
                            this.thingDoing = new ThingDoing(thingDoing.typeOfThing, new ThingDoingTarget(thingDoing.target.validBlocks));
                        }
                        else if (thingDoing != null)
                        {
                            this.thingDoing = new ThingDoing(thingDoing.typeOfThing, null);
                        }
                        curPath              = null;
                        pathingTarget        = LVector3.Invalid;
                        body.jumping         = false;
                        iNeedToPathfindAgain = true;
                    }
                    // still pathing to it
                    else
                    {
                    }
                }
            }
        }