OTOWN.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. //Filename : OTOWN.H
  21. //Description : Header file of Object Town
  22. #ifndef __OTOWN_H
  23. #define __OTOWN_H
  24. #ifndef __ODYNARRB_H
  25. #include <ODYNARRB.h>
  26. #endif
  27. #ifndef __OSKILL_H
  28. #include <OSKILL.h>
  29. #endif
  30. #ifndef __OFIRMID_H
  31. #include <OFIRMID.h>
  32. #endif
  33. #ifndef __ORAWRES_H
  34. #include <ORAWRES.h>
  35. #endif
  36. #ifndef __OTOWNRES_H
  37. #include <OTOWNRES.h>
  38. #endif
  39. //------- define constant --------//
  40. #define MAX_TOWN_GROWTH_POPULATION 60 // grow up to 60 persons in a town
  41. #define MAX_TOWN_POPULATION 60 // max number of unitw in a town
  42. //-------- Define constant -----------//
  43. #define STD_TOWN_LOC_WIDTH 4
  44. #define STD_TOWN_LOC_HEIGHT 4
  45. //--------- Define training cost and level --------//
  46. #define TRAIN_SKILL_COST 30
  47. #define TRAIN_SKILL_LEVEL 20
  48. #define TOTAL_TRAIN_DAYS 5 // No. of days needed for training an unit
  49. #define MIN_RECRUIT_LOYALTY 30 // only when loyalty > this, the unit can be recruited
  50. #define MIN_NATION_DEFEND_LOYALTY 50 // if the loyalty of the town people > this, they will defense against the attack
  51. #define MIN_INDEPENDENT_DEFEND_LOYALTY 30
  52. #define SURRENDER_LOYALTY 29 // when the loyalty of the town is <= this, and being attacked by a nation, the town will surrender
  53. #define REBEL_LOYALTY 29 // if loyalty <= REBEL_LOYALTY, the unit may rebel
  54. #define INDEPENDENT_LINK_RESISTANCE 50 // only if the resistance of an independent town is lower than this, the town will enable its link with a foreign firm
  55. #define TAX_PER_PERSON 5 // amount of tax can be collected from each person
  56. #define COLLECT_TAX_LOYALTY_DECREASE 10 // Reduce 10 loyalty points per tax collection
  57. #define TOWN_REWARD_PER_PERSON 10
  58. #define TOWN_REWARD_LOYALTY_INCREASE 10
  59. #define IND_TOWN_GRANT_PER_PERSON 30 // independent town granting cost per person
  60. #define IND_TOWN_GRANT_RESISTANCE_DECREASE 10 // resistance decrease per grant
  61. #define RECEIVED_HIT_PER_KILL (200/ATTACK_SLOW_DOWN) // no. of received hits will result in one death of town people
  62. #define MAX_TRAIN_QUEUE 10
  63. //-------- Define class Town ----------//
  64. class Unit;
  65. #pragma pack(1)
  66. class Town
  67. {
  68. public:
  69. enum { TOWN_NAME_LEN=20 };
  70. short town_recno;
  71. short town_name_id;
  72. char* town_name() { return town_res.get_name(town_name_id); }
  73. short nation_recno;
  74. short rebel_recno; // whether this town is controlled by a rebel
  75. char race_id;
  76. int setup_date; // the setup date of this town
  77. char ai_town;
  78. char ai_link_checked; // AI check firm and town locatin by links, disable checking by setting this parameter to 1
  79. char independ_town_nation_relation; // each bit n is high representing this independent town will attack nation n.
  80. char has_linked_own_camp; // whether the town has linked military camps of the same nation
  81. char has_linked_enemy_camp; // whether the town has linked military camps of the same nation
  82. char is_base_town; // whether this town is base town or not
  83. short loc_x1, loc_y1, loc_x2, loc_y2;
  84. short abs_x1, abs_y1, abs_x2, abs_y2;
  85. short loc_width() { return loc_x2-loc_x1+1; }
  86. short loc_height() { return loc_y2-loc_y1+1; }
  87. short center_x;
  88. short center_y;
  89. BYTE region_id;
  90. short layout_id; // town layout id.
  91. short first_slot_id; // the first slot id. of the layout
  92. short slot_object_id_array[MAX_TOWN_LAYOUT_SLOT]; // the race id. of each slot building
  93. //---------- game vars ----------//
  94. short population;
  95. short jobless_population;
  96. short worker_population() { return population-jobless_population; }
  97. short max_race_pop_array[MAX_RACE]; // the max population the current town layout supports
  98. short race_pop_array[MAX_RACE]; // population of each race
  99. unsigned char race_pop_growth_array[MAX_RACE]; // population growth, when it reaches 100, there will be one more person in the town
  100. short jobless_race_pop_array[MAX_RACE];
  101. int recruitable_race_pop(int raceId, int recruitSpy);
  102. float race_loyalty_array[MAX_RACE];
  103. char race_target_loyalty_array[MAX_RACE];
  104. short race_spy_count_array[MAX_RACE]; // no. of spies in each race
  105. float race_resistance_array[MAX_RACE][MAX_NATION];
  106. char race_target_resistance_array[MAX_RACE][MAX_NATION];
  107. int race_harmony(int raceId);
  108. int majority_race(); // the race that has the majority of the population
  109. int average_loyalty();
  110. int average_target_loyalty();
  111. int average_resistance(int nationRecno);
  112. int average_target_resistance(int nationRecno);
  113. short town_defender_count; // no. of units currently defending this town
  114. int last_being_attacked_date;
  115. float received_hit_count; // no. of received hit by attackers, when this > RECEIVED_HIT_PER_KILL, a town people will be killed
  116. char train_queue_skill_array[MAX_TRAIN_QUEUE]; // it stores the skill id.
  117. char train_queue_race_array[MAX_TRAIN_QUEUE]; // it stores the race id.
  118. char train_queue_count;
  119. short train_unit_recno; // race id. of the unit the town is currently training, 0-if currently not training any
  120. int train_unit_action_id; // id. of the action to be assigned to this unit when it is finished training.
  121. DWORD start_train_frame_no;
  122. short defend_target_recno; // used in defend mode, store recno of latest target atttacking this town
  123. //-------- other vars ----------//
  124. int accumulated_collect_tax_penalty;
  125. int accumulated_reward_penalty;
  126. int accumulated_recruit_penalty;
  127. int accumulated_enemy_grant_penalty;
  128. int last_rebel_date;
  129. short independent_unit_join_nation_min_rating;
  130. short quality_of_life;
  131. void update_quality_of_life();
  132. //------- auto policy -------------//
  133. short auto_collect_tax_loyalty; // auto collect tax if the loyalty reaches this level
  134. short auto_grant_loyalty; // auto grant if the loyalty drop below this level
  135. //----------- AI vars ------------//
  136. char town_combat_level; // combat level of the people in this town
  137. char has_product_supply[MAX_PRODUCT]; // whether this town has the supply of these products
  138. char no_neighbor_space; // 1 if there is no space to build firms/towns next to this town
  139. //------ inter-relationship -------//
  140. short linked_firm_count;
  141. short linked_town_count;
  142. short linked_firm_array[MAX_LINKED_FIRM_TOWN];
  143. short linked_town_array[MAX_LINKED_TOWN_TOWN];
  144. char linked_firm_enable_array[MAX_LINKED_FIRM_TOWN];
  145. char linked_town_enable_array[MAX_LINKED_TOWN_TOWN];
  146. int closest_own_camp();
  147. //------ static class member var ------//
  148. static short if_town_recno;
  149. public:
  150. Town();
  151. void init(int,int,int,int);
  152. void deinit();
  153. void next_day();
  154. void draw(int displayLayer=1);
  155. void process_ai();
  156. void disp_info(int refreshFlag);
  157. void detect_info();
  158. int browse_selected_race_id();
  159. void assign_unit(int unitRecno);
  160. int recruit(int withTraining, int raceId, char remoteAction);
  161. int recruit_dec_loyalty(int raceId, int decNow=1);
  162. void cancel_train_unit();
  163. int form_new_nation();
  164. int can_recruit(int raceId);
  165. int can_train(int raceId);
  166. int can_migrate(int destTownRecno, int migrateNow=0, int raceId=0); // if not called by Town::mirgate, don't set migrateNow to TRUE
  167. void move_pop(Town* destTown, int raceId, int hasJob);
  168. int pick_random_race(int pickNonRecruitableAlso, int pickSpyFlag);
  169. int camp_influence(int unitRecno);
  170. void setup_link();
  171. void release_link();
  172. void release_firm_link(int);
  173. void release_town_link(int);
  174. int linked_active_camp_count();
  175. int can_toggle_firm_link(int firmRecno);
  176. void update_camp_link();
  177. void init_pop(int raceId, int addPop, int loyalty, int hasJob=0, int firstInit=0);
  178. void inc_pop(int raceId, int unitHasJob, int unitLoyalty);
  179. void inc_pop_overcrowded(int raceId, int unitHasJob, int unitLoyalty);
  180. void dec_pop(int raceId, int unitHasJob);
  181. void draw_selected();
  182. int draw_detect_link_line(int);
  183. int is_in_zoom_win();
  184. int has_linked_camp(int nationRecno, int needOverseer);
  185. void auto_set_layout();
  186. void set_nation(int nationRecno);
  187. void surrender(int toNationRecno);
  188. void set_hostile_nation(int nationRecno);
  189. void reset_hostile_nation(int nationRecno);
  190. int is_hostile_nation(int nationRecno);
  191. int create_rebel_unit(int raceId, int isLeader);
  192. int mobilize_town_people(int raceId, int decPop, int mobilizeSpy);
  193. int mobilize_defender(int attackerNationRecno);
  194. int migrate_to(int destTownRecno, char remoteAction, int raceId=0);
  195. void collect_yearly_tax();
  196. void collect_tax(char remoteAction);
  197. void reward(char remoteAction);
  198. void distribute_demand();
  199. void being_attacked(int attackerUnitRecno, float attackDamage);
  200. void clear_defense_mode();
  201. void reduce_defender_count();
  202. void kill_town_people(int raceId, int attackerUnitRecno=0);
  203. int can_grant_to_non_own_town(int grantNationRecno);
  204. int grant_to_non_own_town(int grantNationRecno, int remoteAction);
  205. void get_most_populated_race(int& mostRaceId1, int& mostRaceId2);
  206. void update_target_loyalty();
  207. void update_target_resistance();
  208. void update_loyalty();
  209. void update_resistance();
  210. void update_product_supply();
  211. void change_loyalty(int raceId, float loyaltyChange);
  212. void change_resistance(int raceId, int nationRecno, float loyaltyChange);
  213. void toggle_firm_link(int linkId, int toggleFlag, char remoteAction, int setBoth=0);
  214. void toggle_town_link(int linkId, int toggleFlag, char remoteAction, int setBoth=0);
  215. void auto_defense(short targetRecno);
  216. int has_player_spy();
  217. void verify_slot_object_id_array();
  218. void set_auto_collect_tax_loyalty(int loyaltyLevel);
  219. void set_auto_grant_loyalty(int loyaltyLevel);
  220. void disp_auto_loyalty();
  221. void add_queue(char skillId, char raceId);
  222. void remove_queue(char skillId);
  223. int write_file(File*);
  224. int read_file(File*);
  225. //-------- ai functions ---------//
  226. void think_collect_tax();
  227. void think_reward();
  228. int think_build_firm(int firmId, int maxFirm);
  229. int think_build_market();
  230. int think_build_camp();
  231. int think_build_research();
  232. int think_build_war_factory();
  233. int think_build_base();
  234. int think_build_inn();
  235. int think_ai_migrate();
  236. int think_ai_migrate_to_town();
  237. void think_defense();
  238. int think_split_town();
  239. void think_move_between_town();
  240. int think_attack_nearby_enemy();
  241. int think_attack_linked_enemy();
  242. void think_capture_linked_firm();
  243. int think_capture_enemy_town();
  244. int think_scout();
  245. void update_base_town_status();
  246. int new_base_town_status();
  247. int think_counter_spy();
  248. int needed_anti_spy_level();
  249. int think_spying_town();
  250. int think_spying_town_assign_to(int raceId);
  251. int should_ai_migrate();
  252. int detect_enemy(int);
  253. int protection_needed(); // an index from 0 to 100 indicating the military protection needed for this town
  254. int protection_available();
  255. int ai_build_neighbor_firm(int firmId, int firmRaceId=0);
  256. int ai_settle_new(int raceId);
  257. //-------- independent town ai functions ---------//
  258. void think_independent_town();
  259. void think_independent_set_link();
  260. int think_independent_form_new_nation();
  261. int think_independent_unit_join_nation();
  262. int independent_unit_join_nation(int raceId, int toNationRecno);
  263. //--------- function for cheat key ----------//
  264. int get_selected_race();
  265. //-------------- multiplayer checking codes ---------------//
  266. UCHAR crc8();
  267. void clear_ptr();
  268. //-------------------------------//
  269. private:
  270. void set_world_matrix();
  271. void restore_world_matrix();
  272. void establish_contact_with_player();
  273. void process_food();
  274. void process_auto();
  275. void process_train();
  276. void finish_train(Unit*);
  277. void population_grow();
  278. void process_queue();
  279. void think_migrate();
  280. int think_migrate_one(Town* targetTown, int raceId, int townDistance);
  281. void migrate(int raceId, int destTownZoneRecno, int newLoyalty);
  282. // int can_migrate(int destTownRecno, int migrateNow=0, int raceId=0);
  283. int unjob_town_people(int raceId, int unjobOverseer, int killOverseer=0);
  284. // void kill_town_people(int raceId);
  285. int think_layout_id();
  286. void draw_flag(int,int);
  287. void draw_farm(int,int,int);
  288. void disp_basic_info(int refreshFlag);
  289. void disp_train_info(int refreshFlag);
  290. void disp_main_menu(int refreshFlag);
  291. void detect_main_menu();
  292. void disp_debug_resistance(int refreshFlag);
  293. void disp_train_menu(int refreshFlag);
  294. void disp_train_button(int y, int skillId, int buttonUp);
  295. void disp_queue_button(int y, int skillId, int buttonUp);
  296. void detect_train_menu();
  297. void disp_auto_menu(int modeCollectTax);
  298. void detect_auto_menu(int modeCollectTax);
  299. void disp_spy_menu(int refreshFlag);
  300. void detect_spy_menu();
  301. void daily_update_loyalty();
  302. void calc_loyalty();
  303. void think_rebel();
  304. int think_surrender();
  305. };
  306. #pragma pack()
  307. //-------- Begin of class TownArray ------------//
  308. class TownArray : public DynArrayB
  309. {
  310. public:
  311. int selected_recno; // the firm current being selected
  312. int race_wander_pop_array[MAX_RACE]; // no. of wandering people of each race. They are people for setting up independent towns later
  313. public:
  314. TownArray();
  315. ~TownArray();
  316. void init();
  317. void deinit();
  318. int add_town(int nationRecno, int raceId, int xLoc, int yLoc);
  319. void del_town(int townRecno);
  320. Town* create_town();
  321. void draw();
  322. void draw_dot();
  323. void draw_profile();
  324. void process();
  325. int independent_town_resistance();
  326. void think_new_independent_town();
  327. int think_town_loc(int maxTries, int& xLoc, int& yLoc);
  328. int find_nearest_town(int xLoc, int yLoc, int nationRecno=0);
  329. int settle(int unitRecno, int xLoc, int yLoc);
  330. void distribute_demand();
  331. void stop_attack_nation(short nationRecno);
  332. int write_file(File*);
  333. int read_file(File*);
  334. int is_deleted(int recNo);
  335. #ifdef DEBUG
  336. Town* operator[](int recNo);
  337. #else
  338. Town* operator[](int recNo) { return (Town*) get_ptr(recNo); }
  339. #endif
  340. };
  341. extern TownArray town_array;
  342. //---------------------------------------------------//
  343. #endif