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    : OFIRM2.CPP
22 //Description : Class Firm - part 2
23 
24 #include <OFIRM.h>
25 #include <OTOWN.h>
26 #include <OSPY.h>
27 #include <ONATION.h>
28 #include <ORACERES.h>
29 #include <OUNIT.h>
30 
31 //--------- Begin of function Firm::kill_overseer ---------//
32 //
33 // Kill the overeseer of the firm
34 //
kill_overseer()35 void Firm::kill_overseer()
36 {
37 	if( !overseer_recno )
38 		return;
39 
40 	//-------- if the overseer is a spy -------//
41 
42 	Unit* unitPtr = unit_array[overseer_recno];
43 
44 	if( unitPtr->spy_recno )
45 		spy_array[unitPtr->spy_recno]->set_place(SPY_UNDEFINED, 0);
46 
47 	//-- no need to del the spy here, unit_array.del() will del the spy --//
48 
49 	//-----------------------------------------//
50 
51 	if( overseer_town_recno )
52 		town_array[overseer_town_recno]->dec_pop(unit_array[overseer_recno]->race_id, 1);
53 
54 	unit_array.del(overseer_recno);
55 
56 	overseer_recno = 0;
57 }
58 //----------- End of function Firm::kill_overseer -----------//
59 
60 
61 //--------- Begin of function Firm::kill_all_worker ---------//
62 //
63 // All the workers in the firm are deleted
64 //
kill_all_worker()65 void Firm::kill_all_worker()
66 {
67 	for(int i=worker_count; i>0; i--)
68 		kill_worker(i);
69 }
70 //----------- End of function Firm::kill_all_worker -----------//
71 
72 
73 //--------- Begin of function Firm::kill_worker ---------//
74 //
75 // Kill a specific worker.
76 //
kill_worker(int workerId)77 void Firm::kill_worker(int workerId)
78 {
79 	err_when( !worker_array );    // this function shouldn't be called if this firm does not need worker
80 
81 	err_when( workerId<1 || workerId>worker_count );
82 
83 	//------- decrease worker no. and create an unit -----//
84 
85 	Worker* workerPtr = worker_array+workerId-1;
86 	int	  unitRecno = 0;
87 
88 	if( workerPtr->race_id && workerPtr->name_id )
89 		race_res[workerPtr->race_id]->free_name_id(workerPtr->name_id);
90 
91 	if( workerPtr->town_recno )      // town_recno is 0 if the workers in the firm do not live in towns
92 		town_array[workerPtr->town_recno]->dec_pop(workerPtr->race_id, 1);		// 1-has job
93 
94 	//-------- if this worker is a spy ---------//
95 
96 	if( workerPtr->spy_recno )
97 	{
98 		spy_array[workerPtr->spy_recno]->set_place(SPY_UNDEFINED, 0);
99 		spy_array.del_spy( workerPtr->spy_recno );
100 	}
101 
102 	//--- decrease the nation unit count as the Unit has already increased it ----//
103 
104 	if( !firm_res[firm_id]->live_in_town )		// if the unit does not live in town, increase the unit count now
105 		unit_res[workerPtr->unit_id]->dec_nation_unit_count(nation_recno);
106 
107 	//------- delete the record from the worker_array ------//
108 
109 	err_when( worker_count > MAX_WORKER );
110 	err_when( selected_worker_id > worker_count );
111 
112 	misc.del_array_rec(worker_array, worker_count, sizeof(Worker), workerId);
113 
114 	if( selected_worker_id > workerId || selected_worker_id == worker_count )
115 		selected_worker_id--;
116 
117 	worker_count--;
118 
119 	if( worker_count==0 )
120 		selected_worker_id = 0;
121 
122 	err_when( selected_worker_id > worker_count );
123 }
124 //----------- End of function Firm::kill_worker -----------//
125 
126 
127 //--------- Begin of function Firm::kill_builder ---------//
128 
kill_builder(short builderRecno)129 void Firm::kill_builder(short builderRecno)
130 {
131 	unit_array.del(builderRecno);
132 }
133 //----------- End of function Firm::kill_builder -----------//
134 
135 
136 //--------- Begin of function Firm::locate_space ---------//
locate_space(int removeFirm,int & xLoc,int & yLoc,int xLoc2,int yLoc2,int width,int height,int mobileType,int regionId)137 int Firm::locate_space(int removeFirm, int &xLoc, int &yLoc, int xLoc2, int yLoc2, int width, int height, int mobileType, int regionId)
138 {
139 	int checkXLoc, checkYLoc;
140 
141 	if(removeFirm)
142 	{
143 		//*** note only for land unit with size 1x1 ***//
144 		char	mType = UNIT_LAND;
145 
146 		for(checkYLoc=loc_y1; checkYLoc<=loc_y2; checkYLoc++)
147 		{
148 			for(checkXLoc=loc_x1; checkXLoc<=loc_x2; checkXLoc++)
149 			{
150 				if(world.get_loc(checkXLoc, checkYLoc)->can_move(mType))
151 				{
152 					xLoc = checkXLoc;
153 					yLoc = checkYLoc;
154 					return 1;
155 				}
156 			}
157 		}
158 	}
159 	else
160 	{
161 		checkXLoc = loc_x1;
162 		checkYLoc = loc_y1;
163 		if(!world.locate_space(&checkXLoc, &checkYLoc, xLoc2, yLoc2, width, height, mobileType, regionId))
164 			return 0;
165 		else
166 		{
167 			xLoc = checkXLoc;
168 			yLoc = checkYLoc;
169 			return 1;
170 		}
171 	}
172 
173 	return 0;
174 }
175 //----------- End of function Firm::locate_space -----------//
176 
177