Use an scripting language is a good choice. There are several ones that works on Free Pascal and Delphi as Pascal Script and Lua.
Create your own simple scripting language isn't as hard as most people think. I'm working in my very own one right now (documented in Spanish only, at the moment). Anyway, here you have a simple (incomplete and untested) interpretor:
Code:
TYPE
(* A helper type. *)
TWordList = ARRAY OF STRING;
(* This splits a string in to words separated by spaces. *)
FUNCTION SplitWords (aText: STRING): TWordList;
VAR
CharNdx, WordNdx: INTEGER;
ResultList: TWordList;
BEGIN
{ First word. }
SetLength (Resultlist, 1); WordNdx := 0;
{ Extract... }
FOR CharNdx := 1 TO Length (aText) DO
BEGIN
{ If found space or comma (,), then another word starts. }
IF aText[CharNdx] IN [' ', ','] THEN
BEGIN
{ This is to avoid multiple spaces. }
IF ResultList[WordNdx] <> '' THEN
BEGIN
INC (WordNdx);
SetLength (ResultList, WordNdx + 1)
END
END
ELSE
{ Adds character to word. Does it uppercase. }
ResultList[WordNdx] := ResultList[WordNdx] + UpCase (aText[CharNdx])
END;
EXIT (ResultList)
END;
PROCEDURE MyScriptInterpretor (aScript: TStrings);
VAR
CurrentLine, Ndx: INTEGER;
LineWords: TWordList;
FullLine: STRING;
BEGIN
IF aScript.Count > 0 THEN
FOR CurrentLine := 0 TO aScript.Count - 1 DO
BEGIN
{ Extract words. }
LineWords := SplitWords (aScript.Lines[CurrentLine]);
{ Checks "commands" }
IF LineWords[0] = 'SAY' THEN
BEGIN
IF Length (LineWords) < 3 THEN
RAISE Exception.CreateFmt ('Not enough parameters on line %d.', [CurrentLine + 1]);
FullLine := '';
FOR Ndx := 2 TO HIGH (LineWords) DO
FullLine := FullLine + ' ' + LineWords[Ndx];
SomebodySays (LineWords[1], FullLine)
END
ELSE IF LineWords[0] = 'WARP' THEN
BEGIN
IF Length (LineWords) <> 3 THEN
RAISE Exception.CreateFmt ('Wrong parameters for WARP on line line %d.', [CurrentLine + 1]);
Player.SetPosition (StrToInt (LineWords[1]), StrToInt (LineWords[2]))
END
END
END;
Free Pascal's FCL includes an expression evaluator you can use to add maths to your interpretor too. See this link.
[edit]
I've just remembered that my PGD Challenge entry Momen3D includes it's own mission script system, much like the one you're asking for. Download it from here and look at file src/mission.pas, function ParseFile, lines 191...650. It looks big and complex but it is actually quite simple, as it is divided in sub-routines for each task.
To see how the mission script files looks like, just see file bin/data/missions/default/tuto.md
Bookmarks