Wiz Programming Tutorials

Often you'll need to search for an object - perhaps in a player or monster's inventory, or in a bag, or somewhere in a map. These tutorials show you how to do it.

Contents

Predicates

All searching is done with Predicates. A Predicate is an object that has a single method:

	public boolean predicate ( Object obj );

When you're doing a search, you pass your Predicate object to the inventory, bag, or map, and it will return the first object that passes your predicate function.

The most common predicate is wyvern.lib.predicates.NameMatchPredicate. You use it to see if something's description matches a string. For example, let's say you want to find the first object that matches "sword", such as "Sword of the Elements", or "Flamesword". (Note: the NameMatchPredicate ignores capitalization). You'd create a new NameMatchPredicate with "sword" and pass it to your player's inventory to find the first sword.

Examples

We'll cover some examples of each type of container you might want to search.

Searching an Inventory

Often you'll want to look in a player's (or monster's) inventory to see if they're carrying something specific. In our first example, we'll just look for an object matching the word 'sword'. It doesn't have to BE a sword - it could be a "sword sharpener" or anything else whose description has "sword" in it. We're just looking at the object's name.

<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Applyable<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>predicates<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>NameMatchPredicate<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>DynamicObject<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>predicate1</span>(DynamicObject, Applyable):<br><br>    <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>objects<span class='keyword'></span>/<span class='keyword'></span>treasure<span class='keyword'></span>/<span class='keyword'></span>diamond<span class='keyword'></span>')<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>desc<span class='keyword'></span>', '<span class='keyword'></span>Apply<span class='keyword'></span> <span class='keyword'></span>to<span class='keyword'></span> <span class='keyword'></span>look<span class='keyword'></span> <<span class='keyword'></span>font<span class='keyword'></span> <span class='keyword'></span>color<span class='keyword'></span>=<span class='string'>"<span class='keyword'></span>0000FF<span class='keyword'></span>"</span>><span class='keyword'></span>for<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>sword<span class='keyword'></span>'</<span class='keyword'></span>font<span class='keyword'></span>>)<br><br>    <span class='keyword'>def</span> <span class='function'>toString</span>(self):<br>        <span class='keyword'></span>return<span class='keyword'></span> '<span class='keyword'></span>Predicate<span class='keyword'></span> <span class='keyword'></span>Test<span class='keyword'></span> #<span class='keyword'></span>1<span class='keyword'></span>'<br><br>    <span class='keyword'>def</span> <span class='function'>apply</span>(self, player):<br><br>        <span class='keyword'></span>matcher<span class='keyword'></span> = <span class='keyword'></span>NameMatchPredicate<span class='keyword'></span>('<span class='keyword'></span>sword<span class='keyword'></span>')<br><br>        <span class='keyword'></span>inv<span class='keyword'></span> = <span class='keyword'></span>player<span class='keyword'></span>.<span class='keyword'></span>getInventory<span class='keyword'></span>()<br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>inv<span class='keyword'></span>.<span class='keyword'></span>isEmpty<span class='keyword'></span>():<br>            <span class='keyword'></span>return<span class='keyword'></span><br><br>        <span class='keyword'></span>sword<span class='keyword'></span> = <span class='keyword'></span>inv<span class='keyword'></span>.<span class='keyword'></span>find<span class='keyword'></span>(<span class='keyword'></span>matcher<span class='keyword'></span>)<br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>sword<span class='keyword'></span>:<br>            <span class='keyword'></span>player<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>Your<span class='keyword'></span> <span class='keyword'></span>first<span class='keyword'></span> <span class='keyword'></span>sword<span class='keyword'></span> <span class='keyword'></span>is<span class='keyword'></span>: ' + <span class='keyword'></span>sword<span class='keyword'></span>.<span class='keyword'></span>toString<span class='keyword'></span>())<br>        <span class='keyword'></span>else<span class='keyword'></span>:<br>            <span class='keyword'></span>player<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>(<span class='string'>"<span class='keyword'></span>Nothing<span class='keyword'></span> <span class='keyword'></span>found<span class='keyword'></span> <span class='keyword'></span>matching<span class='keyword'></span> <<span class='keyword'></span>font<span class='keyword'></span> <span class='keyword'></span>color<span class='keyword'></span>="</span><span class='keyword'></span>4169FF<span class='keyword'></span><span class='string'>">'<span class='keyword'></span>sword<span class='keyword'></span>'"</span></<span class='keyword'></span>font<span class='keyword'></span>>)

In the predicate1.py example, we created a thing that looks like a diamond, and when you apply it, it looks in your inventory for something whose description contains "sword".

To run the example, we did:

> clone wiz/rhialto/python/predicate1.py
A new Predicate Test #1 has been placed in your inventory.
> apply Test
Nothing found matching 'sword'
> clone weapons/longsword
A new longsword has been placed in your inventory.
> apply Test
Your first sword is: longsword

How does this example work?

  • we created a NameMatchPredicate, passing 'sword' as the string to match. You can pass any string, including spaces.

  • we got the player's inventory with getInventory(), a method available for Player and Monster objects.

  • we called the inventory's find() method, which takes a Predicate as a parameter.

  • the inventory goes through each of its items, passing them one by one to our matcher. Our matcher checks the short-description of each object to see if it contains the 'sword' string. If so, it returns true.

  • the inventory returns the first object that passes the NameMatchPredicate, or None if there was no match.

The rest of our examples will use this same applyable-diamond, so you can focus on just the code for finding things.

Searching Bags

Searching a bag works exactly like searching an inventory, since the Inventory interface inherits from Container, which makes inventories bags.

You just get a reference to the bag you want, and search it using the find() method.

In this example, we'll search the first bag in the player's inventory to see if it has gold coins in it. However, this time we're not going to use a NameMatchPredicate, since we want to determine (reliably) that they're actually gold coins, and not just something that has the title "gold coins".

In Wyvern, gold coins are an archetype: objects/treasure/gold_coins. To check if an object is of a certain archetype, you use a wyvern.lib.predicates.ArchetypePredicate (naturally enough). Take a look at the other predicates in wyvern.lib.predicates - there are lots of useful "stock" predicates to choose from. We'll also cover making your own predicates later in this tutorial.

We also need to find the first bag. Bags can be different archetypes - chests, bags, quivers, and so on. All bags have one thing in common: they're instances of wyvern.lib.Bag, so we normally we'd use a ClassPredicate to see if they have the right java class (or in this case, interface). Unfortunately, you can't use ClassPredicates in jython, only in java, for, er, technical reasons. So we have to create our own predicate that checks the class for us.

Here's the code:

<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Predicate<span class='keyword'></span>, <span class='keyword'></span>Bag<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Applyable<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>predicates<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>ArchetypePredicate<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>DynamicObject<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>predicate2</span>(DynamicObject, Applyable):<br><br>    <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>objects<span class='keyword'></span>/<span class='keyword'></span>treasure<span class='keyword'></span>/<span class='keyword'></span>diamond<span class='keyword'></span>')<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>desc<span class='keyword'></span>', '<span class='keyword'></span>Apply<span class='keyword'></span> <span class='keyword'></span>to<span class='keyword'></span> <span class='keyword'></span>look<span class='keyword'></span> <<span class='keyword'></span>font<span class='keyword'></span> <span class='keyword'></span>color<span class='keyword'></span>=<span class='string'>"<span class='keyword'></span>0000FF<span class='keyword'></span>"</span>><span class='keyword'></span>for<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>bag<span class='keyword'></span> <span class='keyword'></span>with<span class='keyword'></span> <span class='keyword'></span>gold<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>it<span class='keyword'></span>.'</<span class='keyword'></span>font<span class='keyword'></span>>)<br><br>    <span class='keyword'>def</span> <span class='function'>toString</span>(self):<br>        <span class='keyword'></span>return<span class='keyword'></span> '<span class='keyword'></span>Predicate<span class='keyword'></span> <span class='keyword'></span>Test<span class='keyword'></span> #<span class='keyword'></span>2<span class='keyword'></span>'<br><br><span class='comment'>    # we can't use a ClassPredicate in jython, so we create</span><br><span class='comment'>    # our own little Predicate implementation that looks to</span><br><span class='comment'>    # see if the passed object is a Bag</span><br><br>    <span class='keyword'>class</span> <span class='function'>bag_matcher</span>(Predicate):<br>        <span class='keyword'>def</span> <span class='function'>predicate</span>(self, obj):<br>            <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span>isinstance<span class='keyword'></span>(<span class='keyword'></span>obj<span class='keyword'></span>, <span class='keyword'></span>Bag<span class='keyword'></span>)<br><br>    <span class='keyword'>def</span> <span class='function'>apply</span>(self, agent):<br><br>        <span class='keyword'></span>inv<span class='keyword'></span> = <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>getInventory<span class='keyword'></span>()<br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>inv<span class='keyword'></span>.<span class='keyword'></span>isEmpty<span class='keyword'></span>():<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>(<span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>aren<span class='keyword'></span>'<span class='keyword'></span>t<span class='keyword'></span> <span class='keyword'></span>carrying<span class='keyword'></span> <span class='keyword'></span>anything<span class='keyword'></span>."</span>)<br>            <span class='keyword'></span>return<span class='keyword'></span><br><br><span class='comment'>        # find first bag in inventory</span><br><br>        <span class='keyword'></span>find_bag<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>bag_matcher<span class='keyword'></span>()<br>        <span class='keyword'></span>bag<span class='keyword'></span> = <span class='keyword'></span>inv<span class='keyword'></span>.<span class='keyword'></span>find<span class='keyword'></span>(<span class='keyword'></span>find_bag<span class='keyword'></span>)<br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>not<span class='keyword'></span> <span class='keyword'></span>bag<span class='keyword'></span>:<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>(<span class='string'>"<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>don<span class='keyword'></span>'<span class='keyword'></span>t<span class='keyword'></span> <span class='keyword'></span>have<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>bag<span class='keyword'></span>."</span>)<br>            <span class='keyword'></span>return<span class='keyword'></span><br><br><span class='comment'>        # found a bag - look for gold</span><br><br>        <span class='keyword'></span>gold_matcher<span class='keyword'></span> = <span class='keyword'></span>ArchetypePredicate<span class='keyword'></span>('<span class='keyword'></span>objects<span class='keyword'></span>/<span class='keyword'></span>treasure<span class='keyword'></span>/<span class='keyword'></span>gold_coins<span class='keyword'></span>')<br><br>        <span class='keyword'></span>gold<span class='keyword'></span> = <span class='keyword'></span>bag<span class='keyword'></span>.<span class='keyword'></span>find<span class='keyword'></span>(<span class='keyword'></span>gold_matcher<span class='keyword'></span>)<br><br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>gold<span class='keyword'></span>:<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>Your<span class='keyword'></span> ' + <span class='keyword'></span>str<span class='keyword'></span>(<span class='keyword'></span>bag<span class='keyword'></span>) + ' <span class='keyword'></span>has<span class='keyword'></span> ' +<br>                      <span class='keyword'></span>str<span class='keyword'></span>(<span class='keyword'></span>gold<span class='keyword'></span>.<span class='keyword'></span>getQuantity<span class='keyword'></span>()) + ' <span class='keyword'></span>gold<span class='keyword'></span> <span class='keyword'></span>coins<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>it<span class='keyword'></span>.')<br>        <span class='keyword'></span>else<span class='keyword'></span>:<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>Your<span class='keyword'></span> ' + <span class='keyword'></span>str<span class='keyword'></span>(<span class='keyword'></span>bag<span class='keyword'></span>) + ' <span class='keyword'></span>has<span class='keyword'></span> <span class='keyword'></span>no<span class='keyword'></span> <span class='keyword'></span>gold<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>it<span class='keyword'></span>.')

This example is a bit more complicated, since we have to find 2 things - first a bag, then gold coins in the bag.

To run the example, here's what we did:

> clone wiz/rhialto/python/predicate2.py
A new Predicate Test #2 has been placed in your inventory.
> apply Test
You don't have a bag.
> clone objects/bags/bag
A new bag has been placed in your inventory.
> coins 58
You create gold coins (58).
> put coins in bag
You put 58 gold coins in the bag.
> apply Test
Your bag has 58 gold coins in it.

Running the example is a bit more complicated, since we have to create a bag, then some coins, then put the coins in the bag. There's a convenient Wizard command called coins that lets you create any number of gold coins, which we used here.

Our bag_matcher class is an example of making a new Predicate that performs your own home-grown test. You just have to make a predicate(self, obj) method that returns 0 or 1. We used the Jython built-in method isinstance() to check whether the object was a certain java class.

Next, instead of calling toString() on the objects when we're constructing the message for the player, we used the Jython builtin function str(), which calls toString() for us.

Lastly, we called getQuantity(), which is a method on all GameObjects to tell you how many there are in the group. Normally it returns 1, but for groupable items like coins or arrows, it can return more than 1.

This example covered a lot of ground:

  • doing multiple, different kinds of searches
  • using an ArchetypePredicate
  • creating your own custom predicate
  • displaying quantities
Now we'll move on to searching in maps.

Searching in Maps

Searching in maps is a little bit more complicated, but not much. You still use Predicates, just like in the other examples we've done. The big difference is that you have to tell the map where to search for your object.

To do this, the wyvern.lib.GameMap interface provides you with a findAt() method that takes an (x, y) location and a Predicate. Our next example will look right where the agent is standing to see if there's an invisible Shop object there - this is how you'd tell if the agent is in a shop.

Here's the code:

<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Predicate<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>properties<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Applyable<span class='keyword'></span><br><span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>wyvern<span class='keyword'></span>.<span class='keyword'></span>lib<span class='keyword'></span>.<span class='keyword'></span>classes<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>DynamicObject<span class='keyword'></span>, <span class='keyword'></span>Shop<span class='keyword'></span><br><br><span class='keyword'>class</span> <span class='function'>predicate3</span>(DynamicObject, Applyable):<br><br>    <span class='keyword'>def</span> <span class='function'>initialize</span>(self):<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>super__initialize<span class='keyword'></span>()<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setImage<span class='keyword'></span>('<span class='keyword'></span>objects<span class='keyword'></span>/<span class='keyword'></span>treasure<span class='keyword'></span>/<span class='keyword'></span>diamond<span class='keyword'></span>')<br>        <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>setProperty<span class='keyword'></span>('<span class='keyword'></span>desc<span class='keyword'></span>', '<span class='keyword'></span>Apply<span class='keyword'></span> <span class='keyword'></span>to<span class='keyword'></span> <span class='keyword'></span>look<span class='keyword'></span> <span class='keyword'></span>under<span class='keyword'></span> <span class='keyword'></span>you<span class='keyword'></span> <<span class='keyword'></span>font<span class='keyword'></span> <span class='keyword'></span>color<span class='keyword'></span>=<span class='string'>"<span class='keyword'></span>0000FF<span class='keyword'></span>"</span>><span class='keyword'></span>for<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>Shop<span class='keyword'></span>.'</<span class='keyword'></span>font<span class='keyword'></span>>)<br><br>    <span class='keyword'>def</span> <span class='function'>toString</span>(self):<br>        <span class='keyword'></span>return<span class='keyword'></span> '<span class='keyword'></span>Predicate<span class='keyword'></span> <span class='keyword'></span>Test<span class='keyword'></span> #<span class='keyword'></span>3<span class='keyword'></span>'<br><br><span class='comment'>    # we can't use a ClassPredicate in jython, so we create</span><br><span class='comment'>    # our own little Predicate implementation that looks to</span><br><span class='comment'>    # see if the passed object is a Shop</span><br><br>    <span class='keyword'>class</span> <span class='function'>shop_matcher</span>(Predicate):<br>        <span class='keyword'>def</span> <span class='function'>predicate</span>(self, obj):<br>            <span class='keyword'></span>return<span class='keyword'></span> <span class='keyword'></span>isinstance<span class='keyword'></span>(<span class='keyword'></span>obj<span class='keyword'></span>, <span class='keyword'></span>Shop<span class='keyword'></span>)<br><br>    <span class='keyword'>def</span> <span class='function'>apply</span>(self, agent):<br><br><span class='comment'>        # get the wyvern.lib.Point where the agent is standing</span><br>        <span class='keyword'></span>map<span class='keyword'></span> = <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>getMap<span class='keyword'></span>()<br><br><span class='comment'>        # get the agent's map</span><br>        <span class='keyword'></span>ref<span class='keyword'></span> = <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>getReferenceLoc<span class='keyword'></span>()<br><br><span class='comment'>        # create a matcher to look for a Shop</span><br>        <span class='keyword'></span>matcher<span class='keyword'></span> = <span class='keyword'></span><span class='instance'>self</span><span class='keyword'></span>.<span class='keyword'></span>shop_matcher<span class='keyword'></span>()<br><br><span class='comment'>        # find it</span><br>        <span class='keyword'></span>shop<span class='keyword'></span> = <span class='keyword'></span>map<span class='keyword'></span>.<span class='keyword'></span>findAt<span class='keyword'></span>(<span class='keyword'></span>ref<span class='keyword'></span>.<span class='keyword'></span>x<span class='keyword'></span>, <span class='keyword'></span>ref<span class='keyword'></span>.<span class='keyword'></span>y<span class='keyword'></span>, <span class='keyword'></span>matcher<span class='keyword'></span>)<br><br>        <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>shop<span class='keyword'></span>:<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>are<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>shop<span class='keyword'></span>.')<br>        <span class='keyword'></span>else<span class='keyword'></span>:<br>            <span class='keyword'></span>agent<span class='keyword'></span>.<span class='keyword'></span>message<span class='keyword'></span>('<span class='keyword'></span>You<span class='keyword'></span> <span class='keyword'></span>are<span class='keyword'></span> <span class='keyword'></span>not<span class='keyword'></span> <span class='keyword'></span>in<span class='keyword'></span> <span class='keyword'></span>a<span class='keyword'></span> <span class='keyword'></span>shop<span class='keyword'></span>.')</<span class='keyword'></span>pre<span class='keyword'></span>>

To run this example, you first have to apply the object while you're not in a shop, then when you're in a shop:

> clone wiz/rhialto/python/predicate3.py
A new Predicate Test #3 has been placed in your inventory.
> apply test
You are not in a shop.
> goto village/general_store
> apply test
You are in a shop.

Again, we were looking for an object of a specific Java class (wyvern.lib.classes.Shop), so we created our own shop_matcher predicate.

We called 2 helper functions:

  • getMap(), which returns us the map the player (or monster) is currently in.

  • getReferenceLoc(), which returns us the player's upper-left corner in the map.

These helper functions are used all the time, so you should get used to seeing them.

Fancier Searching

There are four very common searching situations - so common that we created special functions for them:

  1. Look on the ground beneath a player. Has to check every square beneath large players. This is used, for example, in the PickupCommand when you want to pick something up.

  2. Look in the player's inventory, then on the ground. First checks the player's inventory, and if it doesn't find a match, looks at every square on the ground under the player. This is used, for example, by the "apply" command.

  3. Look beneath the player, then next to the player. This search looks at every square under the player, and if it still hasn't found a match, it looks at all the locations next to (i.e. one square away from) the player. For example, the Shop uses this in the "buy" command.

  4. Look in the player's inventory, then on the ground, then next to the player. Has to check the player's inventory, then every square on the ground under the player, then every square that's next to the player. This is used, for example, in the Wizard command "dispose" to find an object that matches what you typed.

These 4 searches are used all the time, so we've created some methods you can call in wyvern.lib.Kernel. The methods take a player or monster, and a predicate, and they return the first object that matches your predicate.

The 4 Kernel methods are:

  • Kernel.findNeighbor(agent, predicate) - Looks under the agent, then next to the agent.

  • Kernel.findObject(agent, predicate) - Looks in inventory, then under the agent, then next to the agent.

  • Kernel.findTarget(agent, predicate) - Looks in inventory, then under the agent.

  • Kernel.findInMap(agent, predicate) - Looks under the agent only.

These functions work just like the find() (for inventories/bags) and findAt() (for maps) functions we used in earlier examples. They just use different rules for how to look for the first matching object.

Searching a Wide Area

You can search a larger area for an object using GameMap's findInRect function. It takes a rectangle (x, y, width, height) and a Predicate, and returns the first match it finds in the rectangle.

We don't recommend using this function, though, since it's slow. It requires a lot of searching through (potentially) hundreds of objects to find a match. If you need to do something like this, contact an Arch Wizard and we'll try to help you redesign your code so you don't need to use it.

<< Previous Chapter Next Chapter >>