Castle Paradox Forum Index Castle Paradox

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Gamelist   Review List   Song List   All Journals   Site Stats   Search Gamelist   IRC Chat Room

Working with many NPCs

 
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP!
View previous topic :: View next topic  
Author Message
Baconlabs
PURPLE IS MANLY




Joined: 15 Mar 2009
Posts: 335
Location: Tennessee

PostPosted: Thu Jun 25, 2009 8:54 pm    Post subject: Working with many NPCs Reply with quote

I've got a very specific idea. In the desert, you've got ordinary cacti (map tiles). Then you've got some living, attacking cacti (NPCs).
What I want to do is have the live cactus shuffle toward the player when he gets close, triggering a battle. This is pretty easy to figure out by itself; just assign an invisible Step-On NPC in a certain perimeter around the NPC cactus, suspend the player, move the cactus, etc. etc.

The problem I'm having is that there are about 10 of these instances on the map, and I want to trigger each one separately (with different NPCs if need be), but using just one script.

I'm sure there's a way, probably with Script Arguments, but I have no idea how to use those. Can anyone help me with this?
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Fri Jun 26, 2009 7:41 am    Post subject: Reply with quote

What if the script searches the screen and picks the cactus closest to the hero? You could do that with a script like this:

Code:

include,plotscr.hsd

define constant(5, cactus npc id)

plotscript, find nearest cactus, begin
  variable(x, y)
  variable(start x, start y)
  variable(dist x, dist y)
  variable(best x, best y)
  variable(ref)
  variable(found)
 
  # first get the top left corner tile position
  start x := camera pixel x / 20
  start y := camera pixel y / 20

  # no cactus found yet...
  found := false
 
  # then loop through the tiles on the screen
  for(y, start y, start y + 10) do(
    for(x, start x, start x + 16) do(
      ref := NPC at spot(x, y)
      if(ref) then(
        if(get NPC ID(ref) == cactus npc id) then(
          # Found a cactus!
         
          # now get its distance to the hero
          dist x := npc x(ref) -- hero x(me)
          dist y := npc y(ref) -- hero y(me)
 
          if(found == false) then(
            # this is the first cactus we have found
            found := ref
            best x := dist x
            best y := dist y
          )else(
            # we already have a cactus, check if the current one is closer
            if(dist x^2 + dist y^2 << best x^2 + best y^2) then(
              # yes, it is closer
              found := ref
              best x := dist x
              best y := dist y
            )
          )
        )
      )
    )
  )
 
  # Now, if a cactus was found, do an action with it
  if(found) then(
    # do action here
  )
end
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Baconlabs
PURPLE IS MANLY




Joined: 15 Mar 2009
Posts: 335
Location: Tennessee

PostPosted: Fri Jun 26, 2009 1:21 pm    Post subject: Reply with quote

Even better! Thanks James!

EDIT: In realizing just how nifty this script is, I'm definitely going to try and find a use for it outside of just this one map.


Last edited by Baconlabs on Fri Jun 26, 2009 10:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
Baconlabs
PURPLE IS MANLY




Joined: 15 Mar 2009
Posts: 335
Location: Tennessee

PostPosted: Fri Jun 26, 2009 10:13 pm    Post subject: Reply with quote

Okay, quick thing that should be pointed out in case anyone else tries to do something similar to this.
When performing actions in this zone:
Code:
  # Now, if a cactus was found, do an action with it
  if(found) then(
    # do action here
  )
end

NPC ID numbers MUST be referred to as the variable found. For example:
Code:
walk npc (found,left,4)
wait for npc (found)


This is very important or else the NPCs might activate out of order.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP! All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group