Here's a list of tips on how to manipulate the game data
structures in Jython. If you have one you need explained,
mail us and we'll add it.
We're doing code snippets here, not full examples.
Going Through a List
Some game APIs return sets or lists. You iterate through
them the same way - using an Iterator, which has 2 methods:
- hasNext() - checks if there are
more objects
- next() - gets the next object
Sets and Lists are both "bags" of objects. They differ in
several regards:
- If you already have a reference to the object, looking
for it in the set is very fast. So to see if there's
a particular player in your map, you could call
set = map.getPlayerList() , and then see if
set.contains(player.getName()) . This is
a very fast check.
If you had a java.util.List, the contains()
method would have to search every item in the list (on
average, half of them) to see if your object is in it.
- Removing an item from a List, or inserting it somewhere
other than the beginning or end of the List, is slow.
Adding/removing objects to Sets is fast.
- Sets keep their objects in no particular order,
and it can change. Lists keep them in a fixed order.
Generally speaking, Sets are better for keeping unordered
groups of objects, and Lists are better for keeping ordered
groups. That's why the game APIs sometimes return one or
the other.
Iterating through a Set or a List is identical, though,
both in the code and the performance:
<span class='keyword'></span>from<span class='keyword'></span> <span class='keyword'></span>java<span class='keyword'></span>.<span class='keyword'></span>util<span class='keyword'></span> <span class='keyword'></span>import<span class='keyword'></span> <span class='keyword'></span>Iterator<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><br><br><span class='keyword'>class</span> <span class='function'>test</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>hourglass<span class='keyword'></span>')<br><br> <span class='keyword'>def</span> <span class='function'>apply</span>(self, agent):<br><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'> # this returns a java.util.Set of Monster objects</span><br><span class='comment'> # sadly, it's a rather poorly-named function - oh well.</span><br> <span class='keyword'></span>monsters<span class='keyword'></span> = <span class='keyword'></span>map<span class='keyword'></span>.<span class='keyword'></span>getMonsterList<span class='keyword'></span>()<br> <span class='keyword'></span>if<span class='keyword'></span> <span class='keyword'></span>monsters<span class='keyword'></span> <span class='keyword'></span>is<span class='keyword'></span> <span class='keyword'></span>None<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>No<span class='keyword'></span> <span class='keyword'></span>monsters<span class='keyword'></span>.')<br><br> <span class='keyword'></span>it<span class='keyword'></span> = <span class='keyword'></span>monsters<span class='keyword'></span>.<span class='keyword'></span>iterator<span class='keyword'></span>()<br> <span class='keyword'></span>while<span class='keyword'></span> <span class='keyword'></span>it<span class='keyword'></span>.<span class='keyword'></span>hasNext<span class='keyword'></span>():<br> <span class='keyword'></span>mon<span class='keyword'></span> = <span class='keyword'></span>it<span class='keyword'></span>.<span class='keyword'></span>next<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>mon<span class='keyword'></span>.<span class='keyword'></span>toString<span class='keyword'></span>())
|
|