1 /*
2  * Holotz's Castle
3  * Copyright (C) 2004 Juan Carlos Seijo P�rez
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc., 59
17  * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Juan Carlos Seijo P�rez
20  * jacob@mainreactor.net
21  */
22 
23 /** Script engine for Holotz's Castle.
24  * @file    HCScript.cpp
25  * @author  Juan Carlos Seijo P�rez
26  * @date    03/07/2004
27  * @version 0.0.1 - 03/07/2004 - Primera versi�n.
28  */
29 
30 #include <HCScript.h>
31 
Init(HCLevel * _level)32 bool HCScript::Init(HCLevel *_level)
33 {
34 	if (!_level)
35 	{
36 		return false;
37 	}
38 
39 	level = _level;
40 
41 	return true;
42 }
43 
Load(const char * filename)44 bool HCScript::Load(const char *filename)
45 {
46 	JTextFile f;
47 
48 	if (!f.Load(filename, "rt"))
49 	{
50 		return false;
51 	}
52 
53 	// Creates the necessary blocks
54 	numBlocks = f.CountString("{");
55 
56 	if (numBlocks == 0)
57 	{
58 		fprintf(stderr, "HCScript error: No execution blocks.\n");
59 		return false;
60 	}
61 
62 	f.StartOfDocument();
63 
64 	if (numBlocks != (s32)f.CountString("}"))
65 	{
66 		fprintf(stderr, "HCScript error: Mismatched braces!\n");
67 		return false;
68 	}
69 
70 	blocks = new HCScriptBlock[numBlocks];
71 
72 	// Process each block
73 	s8 *begin, *end;
74 
75 	f.StartOfDocument();
76 
77 	for (s32 i = 0; i < numBlocks; ++i)
78 	{
79 		f.FindNext("{");
80 
81 		begin = f.GetPos();
82 		f.FindNext("}");
83 		end = f.GetPos();
84 
85 		f.SetPos(begin);
86 
87 		// Load the blocks
88 		blocks[i].Load(f);
89 	}
90 
91 	// Start!
92 	blocks[0].Current();
93 
94 	return true;
95 }
96 
Update()97 s32 HCScript::Update()
98 {
99 	if (curBlock == numBlocks)
100 	{
101 		return -1;
102 	}
103 
104 	s32 ret = blocks[curBlock].Update();
105 
106 	if (blocks[curBlock].Finished())
107 	{
108 		++curBlock;
109 
110 		if (curBlock == numBlocks)
111 		{
112 			return -1;
113 		}
114 
115 		// Prepares the block for execution
116 		blocks[curBlock].Current();
117 	}
118 
119 	return ret;
120 }
121 
Finished()122 bool HCScript::Finished()
123 {
124 	return curBlock == numBlocks;
125 }
126