Thread: Spawn NPC randomly within a region

Results 1 to 8 of 8
  1. #1 Spawn NPC randomly within a region 
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Hi, not sure if this is already released but I came up with this to spawn NPC's in a random area!

    Code:
    public void spawnRandom(Client c, int id, int x, int x2, int y, int y2, int height, int walk, int hp, int maxHit, int attack, int defence, boolean attackPlayer, boolean headIcon) {
        NpcHandler.getNpcHandler().spawnNpc(c, id, ThreadLocalRandom.current().nextInt(x, x2 + 1), ThreadLocalRandom.current().nextInt(y, y2 + 1), height, walk, hp, maxHit, attack, defence, attackPlayer, headIcon);
    }
    I got the code from the existing NpcHandler#spawnNpc(...) method that comes with most PI bases.

    A cool thing you can do with this is spawn a ton of npc's in a region in random locations!
    Code:
    //This loop spawns 100 NPC's randomly with a specified bound
    for (int i = 0; i < 100; i++) {
        NpcHandler.getNpcHandler().spawnNpc(c, npcId, ThreadLocalRandom.current().nextInt(3357, 3372 + 1), ThreadLocalRandom.current().nextInt(9629, 9649 + 1), 0, 1, 20, 5, 10, 10, true, false);
    }
    He's how the params work:
    x = bottomLeftX of the Area
    x2 = bottomRigthX of the Area
    y = topRightY of the Area
    y2 = topLeftY of the Area



    Enjoy
    Last edited by Kiissmyswagb; 12-24-2015 at 12:36 AM. Reason: Added prevew image
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    H_E_N_T_A_I_H_E_A_V_E_N_
    Tatsumi's Avatar
    Join Date
    May 2013
    Posts
    1,779
    Thanks given
    537
    Thanks received
    269
    Rep Power
    337
    Thanks.will come in handy
    Attached image
    Reply With Quote  
     

  4. #3  
    Banned Spawn NPC randomly within a region Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Could be done much more nicely if you had some sort of "Location" class and you utilized consumers instead of tons of parameters

    Code:
    public List<Npc> spawnRandom(int npcId, Location location, int amt, Consumer<Npc> action) {
        List<Npc> npcs = new ArrayList<>(amt);
    
        while(amt-- > 0) {
            Npc npc = new Npc(npcId, location.getRandomPosition());
            
            World.getNpcs().add(npc);
            npcs.add(npc);
    
            action.apply(npc);
        }
        return npcs;
    }
    
    public List<Npc> spawnRandom(int npcId, Location location, int amt) {
        return spawnRandom(npcId, location, amt, it -> {});
    }
    Code:
    Location location = new Location(x, y, x2, y2);
    
    spawnRandom(1, location, 100, it -> it.attack(player));
    Reply With Quote  
     

  5. Thankful users:


  6. #4  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    @I LOVE THOTS

    Why are you using a List?
    If I had a location class, I'd use it. If PI was designed for Java 8 I'd use j8 features too.
    Reply With Quote  
     

  7. #5  
    Super Donator


    Join Date
    Feb 2011
    Age
    27
    Posts
    1,126
    Thanks given
    180
    Thanks received
    178
    Rep Power
    243
    Quote Originally Posted by I LOVE THOTS View Post
    Could be done much more nicely if you had some sort of "Location" class and you utilized consumers instead of tons of parameters

    Code:
    public List<Npc> spawnRandom(int npcId, Location location, int amt, Consumer<Npc> action) {
        List<Npc> npcs = new ArrayList<>(amt);
    
        while(amt-- > 0) {
            Npc npc = new Npc(npcId, location.getRandomPosition());
            
            World.getNpcs().add(npc);
            npcs.add(npc);
    
            action.apply(npc);
        }
        return npcs;
    }
    
    public List<Npc> spawnRandom(int npcId, Location location, int amt) {
        return spawnRandom(npcId, location, amt, it -> {});
    }
    Code:
    Location location = new Location(x, y, x2, y2);
    
    spawnRandom(1, location, 100, it -> it.attack(player));
    The power of lambda!!!

    Quote Originally Posted by Kiissmyswagb View Post
    @I LOVE THOTS

    Why are you using a List?
    If I had a location class, I'd use it. If PI was designed for Java 8 I'd use j8 features too.
    Well PI wasent desiged for a good stability in the long run lmao.. Why do you think he and Gabbe made a complete new system but having the same style as a PI in RUSE and released ? Well to make people stop using PI and getting over it.
    Reply With Quote  
     

  8. #6  
    need java lessons
    Eclipse's Avatar
    Join Date
    Aug 2012
    Posts
    4,436
    Thanks given
    686
    Thanks received
    898
    Rep Power
    490
    Quote Originally Posted by I LOVE THOTS View Post
    Could be done much more nicely if you had some sort of "Location" class and you utilized consumers instead of tons of parameters

    Code:
    public List<Npc> spawnRandom(int npcId, Location location, int amt, Consumer<Npc> action) {
        List<Npc> npcs = new ArrayList<>(amt);
    
        while(amt-- > 0) {
            Npc npc = new Npc(npcId, location.getRandomPosition());
            
            World.getNpcs().add(npc);
            npcs.add(npc);
    
            action.apply(npc);
        }
        return npcs;
    }
    
    public List<Npc> spawnRandom(int npcId, Location location, int amt) {
        return spawnRandom(npcId, location, amt, it -> {});
    }
    Code:
    Location location = new Location(x, y, x2, y2);
    
    spawnRandom(1, location, 100, it -> it.attack(player));
    you love these hoes

    Quote Originally Posted by jerryrocks317 View Post
    i am 14 and have my own laptop im on almost 24/7 currently creating rsps lol so please get off my thread lol
    Reply With Quote  
     

  9. #7  
    Registered Member
    Join Date
    Mar 2018
    Posts
    19
    Thanks given
    2
    Thanks received
    1
    Rep Power
    11
    where do i put this ? lol
    Reply With Quote  
     

  10. #8  
    Donator


    Join Date
    Aug 2012
    Posts
    2,462
    Thanks given
    312
    Thanks received
    459
    Rep Power
    457
    I made something like this earlier with clipping and also added a last location recognition to create a better spaced out spawn rather then all possible cluster fuck.

    Code:
    Position toSpawn;
    Position lastSpawn = null;
    for (int i = 0; i < 100; i++) {
         toSpawn = area.getRandomClippedPosition();
         if (lastSpawn != null) {
    	if (toSpawn.isWithinDistance(lastSpawn)) {
    	      toSpawn = area.getRandomClippedPosition();
    	}
         }
         NPC npc = new NPC(26, toSpawn);
         World.getAddNPCQueue().add(npc);
         lastSpawn = toSpawn;
    }
    Attached image

    Attached image
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Basic Npc Random Drops
    By Bitlord44 in forum Tutorials
    Replies: 7
    Last Post: 03-09-2009, 06:55 AM
  2. Permeantly spawning npc's through game?
    By `Leet. in forum Requests
    Replies: 3
    Last Post: 10-19-2008, 03:45 AM
  3. Adding objects/Spawns/Npc's Automatically
    By Pride in forum Tutorials
    Replies: 30
    Last Post: 05-31-2008, 11:01 AM
  4. Saving spawned NPC.
    By -saintt in forum Tutorials
    Replies: 5
    Last Post: 04-19-2008, 09:20 PM
  5. How Do I spawn NPCs so they stay there?
    By Owner Alex in forum Tutorials
    Replies: 6
    Last Post: 09-20-2007, 03:25 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •