Moving NPC in IBAndroid

Discuss any ideas and suggestions you may have on how to improve the Android version of the IceBlink Engine.

Moving NPC in IBAndroid

Postby youngneil1 » Sat Oct 04, 2014 11:50 pm

First baby steps with (pseudo-)java script, based almost 99% on Jer's normal PathFinder.java:

WorldPathFinder.java:

Code: Select all
package com.iceblinkengine.ibb.ibcore;


import java.util.ArrayList;
import java.util.List;

public class PathFinder
{
    public int[][] grid;

    //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
    public int[][] values = new int[mod.currentArea.MapSizeX][mod.currentArea.MapSizeY];

    public List<Coordinate> pathNodes = new ArrayList<Coordinate>();
    public boolean foundEnd = false;
    public Module mod;
   
   public PathFinder(Module m)
   {
      mod = m;
   }
      
    public Coordinate findNewPoint(Creature crt, Coordinate end)
    {
       foundEnd = false;
       Coordinate newPoint = new Coordinate(-1,-1);
        //set start value to 0
   
   //yn1: changed crt.combatLocX and crt.combatLocY to just LocationX and just LocationY; I think these don't exist so far as public ints for creatures
        values[crt.LocationX][crt.LocationY] = 0;

        //yn1: changed to area's creature list like mod.currentArea.CreatureList (just guessing)               
        for (Creature cr:mod.currentArea.CreatureList)
      {
           if (cr != crt)

                //yn1: changed crt.combatLocX and crt.combatLocY to just LocationX and just LocationY
      //yn1: creatures will not step on squares of other cretaures, gives clearer picture and provides a clear resolution when party step on such square (one creature = one conversation or encounter call)
           grid[cr.LocationX][cr.LocationY] = 1;
      }
      
      //yn1: design choice whether creatures will step on the party square; imho they should be able to do so. When they step on it, they can call their enocunter or conversation;
                //The same way the party can step on a creature occupied squre; that's important in case the party has to get through choke points cluttered with NPC
                //All in all, I commented out the lines assigning a non-passable state to teh current party location 
      //for (Player p:mod.playerList)
      //{
         //if ((!p.charStatus.equals("Dead")) && (p.hp > 0))
         //{
            
               
            //grid[p.combatLocX][p.combatLocY] = 1;
         //}
      //}
               
                //////////////////////////////////////
                //we need a check for: 1) props with collision; should be assigned a value of 1 at their corresponing LocationX and LocationY (or do those chnage the walable state itself?)
                //////////////////////////////////////

                //yn1: changed crt.combatLocX and crt.combatLocY to just LocationX and just LocationY 
      grid[crt.LocationX][crt.LocationY] = 2;
      grid[end.X][end.Y] = 3;
        buildPath();
       
        if (!foundEnd)
        {
            //do not build path for now so return (-1,-1), later add code for picking a spot to move
        }
        else
        {
            pathNodes.add(new Coordinate(end.X, end.Y));
            for (int i = 0; i < values[end.X][end.Y]; i++)
            {
                pathNodes.add(getLowestNeighbor(pathNodes.get(pathNodes.size() - 1)));
            }
            //build list of path points
       //yn1: Uncertain why this is using -2?
            newPoint = pathNodes.get(pathNodes.size() - 2);
        }
        return newPoint;
    }
    public void resetGrid()
    {
       //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
   grid = new int[mod.currentArea.MapSizeX][mod.currentArea.MapSizeY];
        //create the grid with 1s and 0s
   //yn1: I guessed it's x coordiante here, better double check
       for (int col = 0; col < mod.currentArea.MapSizeX;col++)
       {
          //yn1: yn1: I guessed it's y coordiante here, better double check
      for (int row = 0; row < mod.currentArea.MapSizeY; row++)
          {
             if (isWalkable(col,row))
             {
                grid[row][col] = 0;
             }
             else
             {
                grid[row][col] = 1;
             }
          }
       }
       
        //assign 9 to every value
   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        for (int x = 0; x < mod.currentArea.MapSizeX; x++)
        {
            //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
       for (int y = 0; y < mod.currentArea.MapSizeY; y++)
            {
                values[x][y] = 99;
            }
        }
    }
    public void buildPath()
    {
        //iterate through all values for next number and evaluate neighbors
        int next = 0;           
        for (int cnt = 0; cnt < 99; cnt++)
        {
            //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
       for (int x = 0; x < mod.currentArea.MapSizeX; x++)
            {
                //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
      for (int y = 0; y < 7; y++)
                {
                    if (values[x][y] == next)
                    {
                        //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
         if ((x + 1 < mod.currentArea.MapSizeX) && (evaluateValue(x + 1, y, next)))
                        {
                            foundEnd = true;
                            return;
                        }
                        if ((x - 1 >= 0) && (evaluateValue(x - 1, y, next)))
                        {
                            foundEnd = true;
                            return;
                        }
         //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
                        if ((y + 1 < mod.currentArea.MapSizeY) && (evaluateValue(x, y + 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
                        if ((y - 1 >= 0) && (evaluateValue(x, y - 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
         //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
                        if ((x + 1 < mod.currentArea.MapSizeX) && (y + 1 < mod.currentArea.MapSizeY) && (evaluateValue(x + 1, y + 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
                        if ((x - 1 >= 0) && (y - 1 >= 0) && (evaluateValue(x - 1, y - 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
         //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
                        if ((x - 1 >= 0) && (y + 1 < mod.currentArea.MapSizeY) && (evaluateValue(x - 1, y + 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
         //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
                        if ((x + 1 < mod.currentArea.MapSizeX) && (y - 1 >= 0) && (evaluateValue(x + 1, y - 1, next)))
                        {
                            foundEnd = true;
                            return;
                        }
                    }
                }
            }
            next++;
        }
    }
    public boolean evaluateValue(int x, int y, int next)
    {
        //evaluate each surrounding node and replace if greater than next number + 1
        //check for end           
        if (grid[x][y] == 3)
        {
            values[x][y] = next + 1;
            return true; //found end
        }
        //check if open and replace if lower
        if (grid[x][y] == 0)
        {
            if (values[x][y] > next + 1)
            {
                values[x][y] = next + 1;
            }
        }
        return false; //didn't find end
    }           
    public Coordinate getLowestNeighbor(Coordinate p)
    {
       Coordinate lowest = new Coordinate();
        int val = 1000;

   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        if ((p.X + 1 < mod.currentArea.MapSizeX) && (values[p.X + 1][p.Y] < val))
        {
            val = values[p.X + 1][p.Y];
            lowest = new Coordinate(p.X + 1, p.Y);
        }
        if ((p.X - 1 >= 0) && (values[p.X - 1][p.Y] < val))
        {
            val = values[p.X - 1][p.Y];
            lowest = new Coordinate(p.X - 1, p.Y);
        }

   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        if ((p.Y + 1 < mod.currentArea.MapSizeY) && (values[p.X][p.Y + 1] < val))
        {
            val = values[p.X][p.Y + 1];
            lowest = new Coordinate(p.X, p.Y + 1);
        }
        if ((p.Y - 1 >= 0) && (values[p.X][p.Y - 1] < val))
        {
            val = values[p.X][p.Y - 1];
            lowest = new Coordinate(p.X, p.Y - 1);
        }

   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        if ((p.X + 1 < mod.currentArea.MapSizeX) && (p.Y + 1 < mod.currentArea.MapSizeY) && (values[p.X + 1][p.Y + 1] < val))
        {
            val = values[p.X + 1][p.Y + 1];
            lowest = new Coordinate(p.X + 1, p.Y + 1);
        }
        if ((p.X - 1 >= 0) && (p.Y - 1 >= 0) && (values[p.X - 1][p.Y - 1] < val))
        {
            val = values[p.X - 1][p.Y - 1];
            lowest = new Coordinate(p.X - 1, p.Y - 1);
        }

   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        if ((p.X - 1 >= 0) && (p.Y + 1 < mod.currentArea.MapSizeY) && (values[p.X - 1][p.Y + 1] < val))
        {
            val = values[p.X - 1][p.Y + 1];
            lowest = new Coordinate(p.X - 1, p.Y + 1);
        }
   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
        if ((p.X + 1 < mod.currentArea.MapSizeX) && (p.Y - 1 >= 0) && (values[p.X + 1][p.Y - 1] < val))
        {
            val = values[p.X + 1][p.Y - 1];
            lowest = new Coordinate(p.X + 1, p.Y - 1);
        }
        return lowest;
    }
    public boolean isWalkable(int col, int row)
   {
       
   //yn1: replacing 7 against mod.currentArea.MapSizeX and mod.currentArea.MapSizeY in line below
   //also this is  not about encopunter tiles, but area tiles; hope y is right choice below; do w ehave areaTiles?
   if (mod.currentArea.areaTiles.get(col * mod.currentArea.MapSizeY + row).Walkable == true)
        {
            return true;
        }           
        return false;
       
      /*char ch = mod.currentEncounter.encounterMapLayout[col][row];
      if ((ch == 'b') || (ch == 't') || (ch == 'r') || (ch == 'w'))
      {
         return false;
      }      
      return true;*/         
   }
}
User avatar
youngneil1
Backer
Backer
 
Posts: 4659
Joined: Sat Dec 08, 2012 7:51 am

Return to Engine Ideas and Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest