I’m trying to test various kinds of “autonomous” NPC behaviors, where by “automomous” I mean that they’re not reacting to player input.
The “standard” way of scripting a T3 game is to create a file containing a list of commands to be executed and then feed it to the interpreter via -i
or -I
.
I guess I could just make a file full of line after line of z
s, but that seems kinda silly and a little difficult to control (having to adjust the length of a long string of "z"s in order to adjust the number of turns to run). So I put together this:
DefineIAction(AutoTestWait) execAction() { ""; };
VerbRule(AutoTestWait) 'autotestwait': AutoTestWaitAction
verbPhrase = 'autotestwait/autotestwaiting';
class AutoTestActor: Actor
autoTestMaxTurns = 100
autoTestScriptName = nil
autoTestTurnCounter = 0
executeTurn() {
if(autoTestTurnCounter == 0)
autoTestStartScript();
autoTestTurn();
autoTestTurnCounter += 1;
if(autoTestTurnCounter >= autoTestMaxTurns)
autoTestEnd();
}
autoTestStartScript() {
if(autoTestScriptName == nil)
return;
executeCommand(self, self, cmdTokenizer.tokenize('script "'
+ toString(autoTestScriptName) + '"'), true);
}
autoTestTurn() { autoTestWait(); }
autoTestWait() { newActorAction(self, AutoTestWait); }
autoTestEnd() { throw new QuittingException(); }
;
Then if you want the game to run ten turns and then exit, you can declare me
(or whatever you define gameMain.initialPlayerChar
to be) like:
me: AutoTestActor
autoTestMaxTurns = 10
;
Each of the “player” turns will use an action that doesn’t do anything apart from outputting a zero-length string (so no extraneous “Time passess…” messages in the transcript).
You can also define a file to save the transcript to via autoTestScriptName
, and do something else with each of the turns by changing the autoTestTurn()
method. So something like:
me: AutoTestActor
autoTestMaxTurns = 10
autoTestScriptName = 'test_output.txt'
autoTestTurn() {
"\nTaking turn <<toString(autoTestTurnCounter)>>\n ";
inherited();
}
;
…will run the game for ten turns, outputting “Taking turn [number]” every turn, writing the transcript to test_output.txt
.
Anyway, that’s what I’m currently using. Is there a cleaner/simpler way to do this kind of thing?