 |
Castle Paradox
|
View previous topic :: View next topic |
Author |
Message |
Baconlabs PURPLE IS MANLY

Joined: 15 Mar 2009 Posts: 335 Location: Tennessee
|
Posted: Thu Jun 25, 2009 8:54 pm Post subject: Working with many NPCs |
|
|
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 |
|
 |
Bob the Hamster OHRRPGCE Developer

Joined: 22 Feb 2003 Posts: 2526 Location: Hamster Republic (Southern California Enclave)
|
Posted: Fri Jun 26, 2009 7:41 am Post subject: |
|
|
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 |
|
 |
Baconlabs PURPLE IS MANLY

Joined: 15 Mar 2009 Posts: 335 Location: Tennessee
|
Posted: Fri Jun 26, 2009 1:21 pm Post subject: |
|
|
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 |
|
 |
Baconlabs PURPLE IS MANLY

Joined: 15 Mar 2009 Posts: 335 Location: Tennessee
|
Posted: Fri Jun 26, 2009 10:13 pm Post subject: |
|
|
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 |
|
 |
|
|
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
|