Using tuProlog for games

I’m currently testing tuProlog as a scripting language and a simple relational database to use in games. In case someone is interested here’s my impressions.

tuProlog is a Prolog interpreter created in 100% pure Java code. The official site is here: http://www.alice.unibo.it/xwiki/bin/view/Tuprolog/ hosted by a research institute of an italian university. Last modification and download size is 2008-12-23 and 1.8M. The number of core files (excluding guis and extras) is around 80 classes. The documentation includes a pdf user guide (which i find very well written and complete) and the api documentation which is also very complete.

The scructure of the installation file 2p-2.1.1.zip:
COPYRIGHT
CHANGELOG
doc
lib
LICENSE
src
test

It’s refreshing to find a product like this, that is well organized, has a complete manual and doesn’t force you to read source code or hack through a disorganized wiki in order to know how to use it.

tuProlog can be run double clicking on it’s jar file (using a MacOSX) and a gui application will start. With this i can browse a theory, manage loaded libraries, enter queries to solve goals and see the results. In Prolog the source is called a theory which is a list of clauses. Running a program is solving a query goal which is the main method. Loading a library is like loading a package. This gui also let’s me run a prolog program step by step and manage the methods (clauses) to spy (breakpoints). It’s very sophisticated and user friendly for research stuff.

Let’s see how it works for a basic method to find a member in a list.

The source:


member(X,[X|R]).
member(X,[Y|R]) :- member(X,R).

Press the Set Theory button and the new theory is accepted. Now try to solve for the goal “member(1,[3,2,1]).” by writing the text on the query field. The final dot is necessary. Press Enter and solution view shows:


yes.
Solution: member(1,[3,2,1])

Pressing the Next button shows there are no more solutions:

This programming language is a bit weird but i find it very useful to store game data and execute queries.

For example, i entered the following theory:


inventory(player,1,item(shield,ac(5),price(100))).
inventory(player,10,item(gold)).
inventory(player,10,item(potion,name(potion_of_healing),effect(heal))).
actor(player,name(x),str(10),int(10),agi(10),spd(10)).

Notice we can mix structured data similar to XML with tabular data and still query information in the same maner.

Inventory can be understood as a table with a reference to the player actor. To know how if the player has any potions and what effect they have i used the query string “Id=player, actor(Id, name(N), _, _, _, _), inventory(Id, Q, item(potion, name(PN), effect(PE))).” and the result is:


yes.
Id / player  N / x  Q / 10  PN / potion_of_healing  PE / heal
Solution: ','('='(player,player),','(actor(player,name(x),str(10),int(10),agi(10),spd(10)),inventory(player,10,item(potion,name(potion_of_healing),effect(heal)))))

I didn’t made any tests to performance but the idea i have is that this should be much slower than SQL and SQL would be much slower than using optimized data structures, but we can use Prolog just to store data textually and process it to an internal data structure like we would do with XML and XSLT.

Extending the language with libraries is easy and is very well explained in the manual. A library is a Java class that extends Library and provides methods that will be added as predicates, functors or directives to the theory. Libraries can be loaded and unloaded dynamically. The libraries provided are Basic, ISO, DCG, IO.

Using tuProlog directly as a scripting language for moders, even with a Prolog engine configured to use only builtin clauses, would not be safe. The alternative is to encode a scripting language in Prolog terms, validate scripts when loading and interpret them. It’s very slow but it would give full control and safety options.


script(myscript_1,[
instruction(...),
instruction(...),
...
]).

Resuming, my opinion is tuProlog may be a very good alternative to xml, xquery and xslt. As a scripting language for moding games it needs a lot of work to make it secure and efficient.