Clone wiki

inf225public / SOFA

Scripted Offline Fighting Arena (SOFA;))

Summary

The intent of the term project is to develop a DSL (Domain Specific Language) for controlling basic characters in a top down RPG game simulation, and inferring this DSL-code into Java source-code which may be used within a game.

The idea behind the DSL is to allow the "player" to code his characters behavior, using simple commands to traverse the map, attack enemies, or protect allies. One of the main features of the DSL is that it is logic-based, enabling the player to easily make the characters behavior event-dependent. In essence, if an ally is wounded, then you may want to protect him instead of chasing the enemy.

The game is played using scripts which players have made. Each player needs to make a script for each of his three characters, which can then interact with each other using their respective names. The simulation will then run all six scripts in parallel until there is a winner.

Results

  • 1: A working unambiguous grammar
  • 2: A JVM model-inferrer, which maps the DSL code to java.
  • 3: A functioning game, which takes the transformed DSL code and uses it control the in-game characters.

Technology choices

  • XText: a powerful tool for defining grammars

  • ANTLR: used by XText to create a parser for the grammar

  • XTend: used for inferring the DSL code into Java source code.

  • libGDX: used for rendering.

Plain ‘ol Java for the game mechanics.

Experience gained from this project

This project entailed using a diverse amount of technologies, of which all but java were new to us. We thus had to first learn how the technologies worked, before being able to make a proper plan regarding how we could implement or envisioned game DSL.

XText was also somewhat easy to pick up and use. The neat thing about it is that you can express your grammar in a form of Backus Naur Form. The challenging part was learning the additional syntax that XText has, which enables you to use powerful tools to enhance your grammar. An example of such a tool would be code completion/suggestions, or type-checking.

A properly defined grammar in XText can easily be used to generate a parser. This is done automatically by XText, using MWE2 and ANTLR to generate the parser after reverse-engineering the grammar.

XTend initially seemed like the easiest technology to learn, as it in essence is a slightly altered version of java. Further details can be found at its homepage. It is supposedly extremely user friendly, but lacked easily accessible documentation for the parts that we actually needed; namely inferring the DSL code to Java.

There were some podcasts and example DSL parser which enabled us to learn just enough to make our own inferrer. We sadly broke some of the code-completion, by implementing it in a manner that the initial developers hadn't intended. The language thus only shows where error occur, but not exactly what is wrong or expected (though it in some cases does). We now know about a book explaining exactly what we needed to know for this project, but we found that out a little too late (27th November).

Conclusion

All in all, this project thought us a lot of things about not only making, but also using a DSL, while expanding our toolbox vastly. Making a DSL is actually rather easy once you are familiar with the tools needed, and can greatly simplify/control user interactions within a specific program.

Implementing a project of this size ended up being far more valuable then we had initially thought. Our skills in a broad range of topics was tested in great detail, often revealing faults in our knowledge. It gave us a chance to get some hands on experience with not only the mandatory the curriculum of the course, but also different tools which we perhaps would never even have found out about if it weren't for this project.

How to run

To run the DSL and simulation you need to install the following libraries:

  • XText
  • LibGDX
  • Android SDK (also configure the Android Developer Tools plugin in Eclipse)

Then follow these steps:

  • Import all the projects into Eclipse.
  • Select the "com.tehforce.sofa.SofaLang" project and hit Alt+Shift+E X, to run the "Run as Eclipse Application" run configuration.
  • Create a new Java Project in the new Eclipse instance, and create a file with the .sofa extension. Answer yes to any questions regarding importing XText context.
  • Write an AI or import the example from the root of the git repository
  • Copy all generated classes from "src-gen/..." into the sofa project. Put them in the "com.tehforce.sofa.ai" package.
  • Run either sofa-desktop or sofa-android, based on which platform you wish to run the simulation.

Log

  • Nov. 5th: Grammar completed, started on mapping to Java. Created rendering projects, added classes for structuring gameworld, gamehud and time management.
  • Nov. 8th: Flow charts describing grammar visually have been made using Lucid Chart
  • Nov. 12th: Small changes to grammar to make mapping from SOFA to Java code using Xtent easier. The changes are reflected in the lucidchart. Mapping to Java 50% done.
  • Nov. 20th: Mapping from DSL code to Java almost done. Work on game will soon start.
  • Nov. 27th: Small changes in grammar and the model-inference. Changes are reflected in flowchart. The game runs!

Updated