only spawn entity if it can walk to a player

Discussion in 'Plugin Development' started by ToastHelmi, Apr 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ToastHelmi

    Hey i want to spawn an entity in a certian radius but only if it can walk to the player who is in the center of this circle
    also i like to prever the locations which habe the same/closest y-coordinats like the player

    at the moment i spawn the entitys in a circle but every time on the surface of the map

    hop you can help me
     
  2. Offline

    StaticJava

    Can I see the current code so I can just edit it?
     
  3. Offline

    CrazyGuy3000

    Would help to see the current code.
     
  4. Offline

    ToastHelmi

    CrazyGuy3000 StaticJava

    well sure but i dont think that ther well be left after edeting :D

    SpawnStuff
    Code:java
    1. public class SpawnPolice {
    2.  
    3.  
    4.  
    5. private static Entity spawnPolice(PoliceType typ , Location loc){
    6. try {
    7. Entity e = typ.getTypeClass().getConstructor(World.class).newInstance(((World)((CraftWorld)loc.getWorld()).getHandle()));
    8. e.setLocation(loc.getX(), loc.getY(), loc.getBlockZ(), loc.getPitch(), loc.getYaw());
    9. ((CraftWorld)loc.getWorld()).getHandle().addEntity(e, SpawnReason.NATURAL);
    10. return e;
    11. System.out.println("Unable to spawn "+ typ.getTypeClass().getSimpleName()+ " because");
    12. e.printStackTrace();
    13. }
    14. return null;
    15.  
    16. }
    17. public static void spawnPolice(Player p){
    18. System.out.println("SPAWN");
    19. List<Location> locs = LocationUtils.getCircle(p.getLocation(), 15);
    20. Location loc;
    21. Random r = new Random(p.getPlayerTime());
    22.  
    23. for(int i = 0; i<5; i++){
    24. spawnPolice(PoliceType.POLICEOFFICER,LocationUtils.getTopLocation(locs.get(r.nextInt(locs.size()-1))));
    25. }
    26.  
    27. }
    28.  
    29. }


    LocatinStuff
    Code:java
    1. public static Location getTopLocation(Location loc){
    2. return loc.getWorld().getHighestBlockAt(loc).getLocation().add(0, 0, 1);
    3. }
    4.  
    5. public static List<Location> getCircle(Location centerLoc, int radius) {
    6. List<Location> circle = new ArrayList<Location>();
    7. World world = centerLoc.getWorld();
    8. int x = 0;
    9. int z = radius;
    10. int error = 0;
    11. int d = 2 - 2 * radius;
    12. while (z >= 0) {
    13. circle.add(new Location(world, centerLoc.getBlockX() + x, centerLoc
    14. .getY(), centerLoc.getBlockZ() + z));
    15. circle.add(new Location(world, centerLoc.getBlockX() - x, centerLoc
    16. .getY(), centerLoc.getBlockZ() + z));
    17. circle.add(new Location(world, centerLoc.getBlockX() - x, centerLoc
    18. .getY(), centerLoc.getBlockZ() - z));
    19. circle.add(new Location(world, centerLoc.getBlockX() + x, centerLoc
    20. .getY(), centerLoc.getBlockZ() - z));
    21. error = 2 * (d + z) - 1;
    22. if ((d < 0) && (error <= 0)) {
    23. x++;
    24. d += 2 * x + 1;
    25. } else {
    26. error = 2 * (d - x) - 1;
    27. if ((d > 0) && (error > 0)) {
    28. z--;
    29. d += 1 - 2 * z;
    30. } else {
    31. x++;
    32. d += 2 * (x - z);
    33. z--;
    34. }
    35. }
    36. }
    37. return circle;
    38. }
     
  5. Offline

    BillyGalbreath

    Just iterate through a BlockIterator using a vector between the future entity and player. If obstructed, do not spawn entity.
     
  6. Offline

    ToastHelmi

    but this would also work when ther is only a 1 block high gab in a wall wouldnt it?
     
  7. Offline

    BillyGalbreath

    Thats all depends on what checks you do while iterating.
     
  8. Offline

    ToastHelmi

    and how would i optain my BlockInterrator given on to Locations


    Than i alos have the problem if i can spawn an entity on the location itself (hight)
     
  9. Offline

    BillyGalbreath

  10. Offline

    ToastHelmi

    so this should be it
    BillyGalbreath
    Code:java
    1. public static boolean cannEntityWalnkTo(Location start, Location dest){
    2. BlockIterator Blocks = new BlockIterator(start.getWorld(), start.toVector(),dest.toVector().subtract(start.toVector()), 1, (int) Math.floor(start.distanceSquared(dest)));
    3. while(Blocks.hasNext()) {
    4. Block b = Blocks.next();
    5. if((b.getType() != Material.AIR || b.getLocation().add(0, 1, 0).getBlock().getType() != Material.AIR) && !b.getType() == Material.WOODEN_DOOR)
    6. return false;
    7.  
    8. }
    9. return true;
    10. }


    EDIT: Thats not working!
     
  11. Offline

    BillyGalbreath

    Its a little more advanced then that example you just pasted. And by little, I mean a lot. You have a lot of things to factor in. Elevation (0-1 block heights are acceptable, 2+ is too high), obstacles (some can be walked through, like tall grass, so you have to compensate for these transparent block types), distance (if too far away the entity will never target the player in the first place), etc etc. There are many things to take into consideration here, including the possibility for walking around any obstacles (which will put your loop off the BlockIterator for deeper tests). Remember, the BlockIterator is a straight line in 3D space, so if you detect an obstacle at a certain point you will then have to do surrounding checks of that obstacle to see if the entity can go around it.
     
  12. Offline

    ToastHelmi

    well will try it with all walkable materials but i have no idea how to check aournd obstackles ore to the elevation...
     
  13. Offline

    Zethariel

    I think you should look into hooking up into the Entity pathfinding system. Mojang did an update on that with recent updates, making it a bit more efficient.
     
  14. Offline

    ToastHelmi

    I already had a look at it because i was creating my own entityselector but with the blurry methodnames and field names its harf for me to understand these algorithms
     
  15. Offline

    Zethariel

    ToastHelmi It's better to use what is already there than create a new method, that does the same and only costs you precious processing time. Advanced functions require advanced code diving :U
     
  16. Offline

    ToastHelmi

    Zethariel
    but what would be a siutable pathfinder?
     
  17. Offline

    Zethariel

    ToastHelmi Each entity has a pathfinding algorithm. Find it, learn how to use it/extract the path from it, apply to your case. I never used it, so I can't help much.
     
  18. Offline

    ToastHelmi

    I have not even an idea what mehtod in the nms classes is the paththing
     
Thread Status:
Not open for further replies.

Share This Page