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    : OF_MARK.H
22 //Description : Header of FirmMarket
23 
24 #ifndef __OF_MARK_H
25 #define __OF_MARK_H
26 
27 #ifndef __OFIRM_H
28 #include <OFIRM.h>
29 #endif
30 
31 #ifndef __OINFO_H
32 #include <OINFO.h>
33 #endif
34 
35 #ifndef __ORAWRES_H
36 #include <ORAWRES.h>
37 #endif
38 
39 #ifndef __TOWN_H
40 #include <OTOWN.h>
41 #endif
42 
43 //---------- Define constant -----------//
44 
45 #define  PEASANT_GOODS_MONTH_DEMAND  0.5	 // No. of unit of goods a peasant buys in a month
46 #define  WORKER_GOODS_MONTH_DEMAND     1   // No. of unit of goods a worker buys in a month
47 
48 #define  MAX_MARKET_GOODS		3		// Maximum no. of types of goods the market trades for
49 
50 #define  MAX_MARKET_STOCK 		500
51 
52 //------- Define class MarketInfo --------//
53 
54 #pragma pack(1)
55 struct MarketGoods
56 {
57 	char		raw_id;
58 	char		product_raw_id;
59 	short		input_firm_recno;
60 
61 	float		stock_qty;
62 
63 	float 	cur_month_supply; 		// supply from direct linked firms only. One of its uses is determining whether we have enough supply to export
64 	float		last_month_supply;
supply_30daysMarketGoods65 	float		supply_30days()		{ return last_month_supply*(30-info.game_day)/30 +
66 											  cur_month_supply; }
67 	float		month_demand;
68 
69 	float 	cur_month_sale_qty;
70 	float		last_month_sale_qty;
sale_qty_30daysMarketGoods71 	float		sale_qty_30days()			{ return last_month_sale_qty*(30-info.game_day)/30 +
72 												  cur_month_sale_qty; }
73 	float 	cur_year_sales;
74 	float		last_year_sales;
sales_365daysMarketGoods75 	float		sales_365days()		{ return last_year_sales*(365-info.year_day)/365 +
76 											  cur_year_sales; }
77 };
78 #pragma pack()
79 
80 struct FirmMarketCrc;
81 
82 //------- Define class FirmMarket --------//
83 
84 #pragma pack(1)
85 class FirmMarket : public Firm
86 {
87 public:
88 	float			 max_stock_qty;		// maximum stock qty of each market goods
89 
90 	MarketGoods  market_goods_array[MAX_MARKET_GOODS];
91 	MarketGoods* market_raw_array[MAX_RAW];
92 	MarketGoods* market_product_array[MAX_PRODUCT];	// pointers to market_goods_array
93 
94 	int			 free_slot_count();
95 	int			 stock_value_index();		// for AI, a 0-100 index number telling the total value of the market's stock
96 
97 	short			 next_output_link_id;
98 	short			 next_output_firm_recno;
99 
100 	//------------ ai vars -----------//
101 
102 	int			 no_linked_town_since_date;
103 	int			 last_import_new_goods_date;
104 	char			 is_retail_market;					// if 1, then it sells consumer products only, if 0, it sells raw materials only
105 
106 public:
107 	FirmMarket();
108 	~FirmMarket();
109 
110 	void 		init_derived();
111 
112 	void		draw(int displayLayer=1);
113 
114 	void 		put_info(int refreshFlag);
115 	int		detect_info();
116 
117 	void		next_day();
118 	void		next_month();
119 	void		next_year();
120 
121 	void		sell_goods();
122 	short		hire_caravan(char remoteAction);
123 	int		can_hire_caravan();
124 
125 	void		set_goods(int isRaw, int goodsId, int position);
126 	void		clear_market_goods(int position);
127 
128 	int 		is_market_linked_to_town(int ownBaseTownOnly=0);
129 
cast_to_FirmMarket()130 	virtual 	FirmMarket*	cast_to_FirmMarket() { return this; };
131 
132 	void		process_ai();	// ai process entry point
133 
134 	int		read_derived_file(File* filePtr);
135 
136 	//-------------- multiplayer checking codes ---------------//
137 	virtual	uint8_t crc8();
138 	virtual	void	clear_ptr();
139 	virtual	void	init_crc(FirmMarketCrc *c);
140 
141 private:
142 	void		put_market_info(int dispY1, int refreshFlag);
143 	void 		disp_income(int dispY1, int refreshFlag);
144 	void 		input_goods(int maxInputQty);
145 	void 		set_next_output_firm();
146 	void		update_trade_link();
147 	void 		free_unused_slot();
148 
149 	//------------------ AI actions --------------------//
150 
151 	int		think_del();
152 	void 		ai_update_link_status();
153 	int  		think_import_new_product();
154 	int 		think_increase_existing_product_supply();
155 	int 		think_import_specific_product(int productId);
156 	int 		think_mft_specific_product(int rawId);
157 	int 		think_export_product();
158 	int 		think_build_export_market(int townRecno);
159 	void		think_demand_trade_treaty();
160 	void 		think_market_build_factory();
161 	int		ai_create_new_trade(Firm* firmPtr, int stop1PickUpType, int stop2PickUpType);
162 };
163 #pragma pack()
164 
165 //--------------------------------------//
166 
167 #endif
168