1 /*
2  * Seven Kingdoms: Ancient Adversaries
3  *
4  * Copyright 1997,1998 Enlight Software Ltd.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 //Filename    : OGODRES.CPP
22 //Description : God resource class
23 
24 #include <OSYS.h>
25 #include <OGAMESET.h>
26 #include <OWORLD.h>
27 #include <OSPRTRES.h>
28 #include <ONATION.h>
29 #include <OUNIT.h>
30 #include <OUNITRES.h>
31 #include <OU_GOD.h>
32 #include <OF_BASE.h>
33 #include <OGODRES.h>
34 
35 //---------- #define constant ------------//
36 
37 #define GOD_DB   "GOD"
38 
39 //------- Begin of function GodRes::GodRes -----------//
40 
GodRes()41 GodRes::GodRes()
42 {
43 	init_flag=0;
44 }
45 //--------- End of function GodRes::GodRes -----------//
46 
47 
48 //---------- Begin of function GodRes::init -----------//
49 //
50 // This function must be called after a map is generated.
51 //
init()52 void GodRes::init()
53 {
54 	deinit();
55 
56 	//------- load database information --------//
57 
58 	load_god_info();
59 
60 	init_flag=1;
61 }
62 //---------- End of function GodRes::init -----------//
63 
64 
65 //---------- Begin of function GodRes::deinit -----------//
66 
deinit()67 void GodRes::deinit()
68 {
69 	if( init_flag )
70 	{
71 		mem_del(god_info_array);
72 		init_flag=0;
73 	}
74 }
75 //---------- End of function GodRes::deinit -----------//
76 
77 
78 //------- Begin of function GodRes::load_god_info -------//
79 //
80 // Read in information of GOD.DBF into memory array
81 //
load_god_info()82 void GodRes::load_god_info()
83 {
84 	GodRec  *godRec;
85 	GodInfo *godInfo;
86 	int      i;
87 	Database *dbGod = game_set.open_db(GOD_DB);
88 
89 	god_count      = (short) dbGod->rec_count();
90 	god_info_array = (GodInfo*) mem_add( sizeof(GodInfo)*god_count );
91 
92 	//------ read in god information array -------//
93 
94 	memset( god_info_array, 0, sizeof(GodInfo) * god_count );
95 
96 	for( i=0 ; i<god_count ; i++ )
97 	{
98 		godRec  = (GodRec*) dbGod->read(i+1);
99 		godInfo = god_info_array+i;
100 
101 		godInfo->god_id = i+1;
102 
103 		godInfo->race_id = misc.atoi(godRec->race_id, godRec->RACE_ID_LEN);
104 		godInfo->unit_id = misc.atoi(godRec->unit_id, godRec->UNIT_ID_LEN);
105 
106 		godInfo->exist_pray_points = misc.atoi(godRec->exist_pray_points, godRec->PRAY_POINTS_LEN);
107 		godInfo->power_pray_points = misc.atoi(godRec->power_pray_points, godRec->PRAY_POINTS_LEN);
108 
109 		godInfo->can_cast_power   = godRec->can_cast_power == '1';
110 		godInfo->cast_power_range = misc.atoi(godRec->cast_power_range, godRec->CAST_POWER_RANGE_LEN);
111 	}
112 }
113 //--------- End of function GodRes::load_god_info ---------//
114 
115 
116 //------- Begin of function GodRes::enable_know_all -------//
117 //
118 // Make all god creatures known to this nation. (A cheating function)
119 //
enable_know_all(int nationRecno)120 void GodRes::enable_know_all(int nationRecno)
121 {
122 	for( int i=1 ; i<=god_res.god_count ; i++ )
123 	{
124 		god_res[i]->enable_know(nationRecno);
125 	}
126 }
127 //--------- End of function GodRes::enable_know_all ---------//
128 
129 
130 //------- Begin of function GodRes::init_nation_know -------//
131 //
132 // Initialize the god know status of a new nation.
133 //
init_nation_know(int nationRecno)134 void GodRes::init_nation_know(int nationRecno)
135 {
136 	for( int i=1 ; i<=god_res.god_count ; i++ )
137 	{
138 		god_res[i]->disable_know(nationRecno);
139 	}
140 }
141 //--------- End of function GodRes::init_nation_know ---------//
142 
143 
144 //------- Begin of function GodInfo::enable_know -------//
145 //
146 // Make this god known to this nation.
147 //
enable_know(int nationRecno)148 void GodInfo::enable_know(int nationRecno)
149 {
150 	nation_know_array[nationRecno-1] = 1;
151 
152 	nation_array[nationRecno]->know_base_array[race_id-1] = 1;		// enable the nation to build the fortress of power
153 }
154 //--------- End of function GodInfo::enable_know ---------//
155 
156 
157 //------- Begin of function GodInfo::disable_know -------//
158 //
159 // Make this god known to this nation.
160 //
disable_know(int nationRecno)161 void GodInfo::disable_know(int nationRecno)
162 {
163 	nation_know_array[nationRecno-1] = 0;
164 
165 	nation_array[nationRecno]->know_base_array[race_id-1] = 0;		// enable the nation to build the fortress of power
166 }
167 //--------- End of function GodInfo::disable_know ---------//
168 
169 
170 //------- Begin of function GodInfo::invoke -------//
171 //
172 // <int> firmRecno  - the firm recno of the seat of power which invokes this god.
173 // <int> xLoc, yLoc - the x and y location of the god
174 //
175 // return: <int> the recno of the God unit created.
176 //
invoke(int firmRecno,int xLoc,int yLoc)177 short GodInfo::invoke(int firmRecno, int xLoc, int yLoc)
178 {
179 	FirmBase* firmBase = (FirmBase*) firm_array[firmRecno];
180 
181 	err_when( firmBase->firm_id != FIRM_BASE );
182 
183 	//------- create the god unit --------//
184 
185 	SpriteInfo* spriteInfo = sprite_res[unit_res[unit_id]->sprite_id];
186 
187 	if( !world.locate_space( &xLoc, &yLoc, xLoc, yLoc, spriteInfo->loc_width, spriteInfo->loc_height, UNIT_AIR ) )
188 		return 0;
189 
190 	//---------- add the god unit now -----------//
191 
192 	short unitRecno = unit_array.add_unit( unit_id, firmBase->nation_recno,
193 							RANK_SOLDIER, 0, xLoc, yLoc );
194 
195 	//------- set vars of the God unit ----------//
196 
197 	UnitGod* unitGod = (UnitGod*) unit_array[unitRecno];
198 
199 	unitGod->god_id			 = god_id;
200 	unitGod->base_firm_recno = firmRecno;
201 	unitGod->hit_points 		 = (short) firmBase->pray_points;
202 
203 	return unitRecno;
204 }
205 //--------- End of function GodInfo::invoke ---------//
206 
207 
208 //------- Begin of function GodRes::is_god_unit -------//
209 //
210 // Whether the given unit is a god unit or not.
211 //
is_god_unit(int unitId)212 int GodRes::is_god_unit(int unitId)
213 {
214 	for( int i=1 ; i<=god_res.god_count ; i++ )
215 	{
216 		if( god_res[i]->unit_id == unitId )
217 			return 1;
218 	}
219 
220 	return 0;
221 }
222 //--------- End of function GodRes::is_god_unit ---------//
223 
224 
225 //---------- Begin of function GodRes::operator[] -----------//
226 
operator [](int godId)227 GodInfo* GodRes::operator[](int godId)
228 {
229 	err_if( godId<1 || godId>god_count )
230 		err_now( "GodRes::operator[]" );
231 
232 	return god_info_array+godId-1;
233 }
234 
235 //------------ End of function GodRes::operator[] -----------//
236 
237 
238