1 /*
2  * Copyright (c) 1997 - 2002 Hansj�rg Malthaner
3  *
4  * This file is part of the Simutrans project under the artistic licence.
5  */
6 
7 #ifndef simplay_h
8 #define simplay_h
9 
10 #include "../network/pwd_hash.h"
11 #include "../simtypes.h"
12 #include "../simlinemgmt.h"
13 
14 #include "../halthandle_t.h"
15 #include "../convoihandle_t.h"
16 
17 #include "../dataobj/koord.h"
18 
19 #include "../tpl/slist_tpl.h"
20 #include "../tpl/vector_tpl.h"
21 
22 
23 class karte_ptr_t;
24 class fabrik_t;
25 class koord3d;
26 class tool_t;
27 class finance_t;
28 
29 /**
30  * Class to hold informations about one player/company. AI players are derived from this class.
31  */
32 class player_t
33 {
34 public:
35 	enum { EMPTY=0, HUMAN=1, AI_GOODS=2, AI_PASSENGER=3, AI_SCRIPTED=4, MAX_AI, PASSWORD_PROTECTED=128 };
36 
37 protected:
38 	char player_name_buf[256];
39 
40 	/* "new" finance history */
41 	finance_t *finance;
42 
43 	/**
44 	 * Die Welt in der gespielt wird.
45 	 *
46 	 * @author Hj. Malthaner
47 	 */
48 	static karte_ptr_t welt;
49 
50 	// when was the company founded
51 	uint16 player_age;
52 
53 
54 	/**
55 	 * Floating massages for all players here
56 	 */
57 	class income_message_t {
58 	public:
59 		char str[33];
60 		koord pos;
61 		sint64 amount;
62 		sint8 alter;
income_message_t()63 		income_message_t() { str[0]=0; alter=127; pos=koord::invalid; amount=0; }
64 		income_message_t( sint64 betrag, koord pos );
65 		void * operator new(size_t s);
66 		void operator delete(void *p);
67 	};
68 
69 	slist_tpl<income_message_t *>messages;
70 
71 	/**
72 	 * Creates new income message entry or merges with existing one if the
73 	 * most recent one is at the same coordinate
74 	 */
75 	void add_message(sint64 amount, koord k);
76 
77 	/**
78 	 * Displays amount of money when koordinates are on screen
79 	 */
80 	void add_money_message(sint64 amount, koord k);
81 
82 	/**
83 	 * Colors of the player
84 	 * @author Hj. Malthaner
85 	 */
86 	uint8 player_color_1, player_color_2;
87 
88 	/**
89 	 * Player number; only player 0 can do interaction
90 	 * @author Hj. Malthaner
91 	 */
92 	uint8 player_nr;
93 
94 	/**
95 	 * Adds some amount to the maintenance costs.
96 	 * @param change the change
97 	 * @return the new maintenance costs
98 	 * @author Hj. Malthaner
99 	 */
100 	sint64 add_maintenance(sint64 change, waytype_t const wt=ignore_wt);
101 
102 	/**
103 	 * Is this player an AI player?
104 	 * @author Hj. Malthaner
105 	 */
106 	bool active;
107 
108 	/**
109 	 * Are this player allowed to do any changes?
110 	 * @author Hj. Malthaner
111 	 */
112 	bool locked;
113 
114 	bool unlock_pending;
115 
116 	// contains the password hash for local games
117 	pwd_hash_t pwd_hash;
118 
119 public:
120 	/**
121 	 * Sums up "count" with number of convois in statistics,
122 	 * supersedes buche( count, COST_ALL_CONVOIS).
123 	 * @author jk271
124 	 */
125 	void book_convoi_number(int count);
126 
127 	/**
128 	 * Adds construction costs to accounting statistics.
129 	 * @param amount How much does it cost
130 	 * @param wt type of transport
131 	 * @author jk271
132 	 */
133 	static void book_construction_costs(player_t * const player, const sint64 amount, const koord k, const waytype_t wt=ignore_wt);
134 
135 	/**
136 	 * Accounts bought/sold vehicles.
137 	 * @param price money used for purchase of vehicle,
138 	 *              negative value = vehicle bought,
139 	 *              positive value = vehicle sold
140 	 * @param wt type of transport for accounting purpose
141 	 * @author jk271
142 	 */
143 	void book_new_vehicle(const sint64 price, const koord k, const waytype_t wt=ignore_wt);
144 
145 	/**
146 	 * Adds income to accounting statistics.
147 	 * @param amount earned money
148 	 * @param wt transport type used in accounting statistics
149 	 * @param cathegory parameter
150 	 * 	0 ... passenger
151 	 *	1 ... mail
152 	 *	2 ... good (and powerlines revenue)
153 	 * @author jk271
154 	 */
155 	void book_revenue(const sint64 amount, const koord k, const waytype_t wt=ignore_wt, sint32 cathegory=2);
156 
157 	/**
158 	 * Adds running costs to accounting statistics.
159 	 * @param amount How much does it cost
160 	 * @param wt type of transport used for accounting statistics
161 	 * @author jk271
162 	 */
163 	void book_running_costs(const sint64 amount, const waytype_t wt=ignore_wt);
164 
165 	/**
166 	 * Books toll paid by our company to someone else.
167 	 * @param amount money paid to our company
168 	 * @param wt type of transport used for accounting statistics
169 	 * @author jk271
170 	 */
171 	void book_toll_paid(const sint64 amount, const waytype_t wt=ignore_wt);
172 
173 	/**
174 	 * Books toll paid to our company by someone else.
175 	 * @param amount money paid for usage of our roads,railway,channels, ... ; positive sign
176 	 * @param wt type of transport used for accounting statistics
177 	 * @author jk271
178 	 */
179 	void book_toll_received(const sint64 amount, waytype_t wt=ignore_wt);
180 
181 	/**
182 	 * Add amount of transported passenger, mail, goods to accounting statistics.
183 	 * @param amount sum of money
184 	 * @param wt way type
185 	 * @param index 0 = passenger, 1 = mail, 2 = goods
186 	 * @author jk271
187 	 */
188 	void book_transported(const sint64 amount, const waytype_t wt=ignore_wt, int index=2);
189 
190 	/**
191 	 * Add amount of delivered passenger, mail, goods to accounting statistics.
192 	 * @param amount sum of money
193 	 * @param wt way type
194 	 * @param index 0 = passenger, 1 = mail, 2 = goods
195 	 */
196 	void book_delivered(const sint64 amount, const waytype_t wt=ignore_wt, int index=2);
197 
198 	bool has_money_or_assets() const;
199 
200 	/**
201 	* Test if the player has sufficient funds for an action.
202 	* Returns true if the player has the funds or does not need to.
203 	* @param cost the amount of funds that want to be added to the balance
204 	*/
205 	bool can_afford(sint64 cost) const;
206 
get_finance()207 	finance_t * get_finance() const { return finance; }
208 
set_active(bool b)209 	virtual bool set_active( bool b ) { return active = b; }
210 
is_active()211 	bool is_active() const { return active; }
212 
is_locked()213 	bool is_locked() const { return locked; }
214 
is_unlock_pending()215 	bool is_unlock_pending() const { return unlock_pending; }
216 
217 	void unlock(bool unlock_, bool unlock_pending_=false) { locked = !unlock_; unlock_pending = unlock_pending_; }
218 
check_unlock(const pwd_hash_t & hash)219 	void check_unlock( const pwd_hash_t& hash ) { locked = (pwd_hash != hash); }
220 
221 	// some routine needs this for direct manipulation
access_password_hash()222 	pwd_hash_t& access_password_hash() { return pwd_hash; }
223 
224 	// this type of AIs identifier
get_ai_id()225 	virtual uint8 get_ai_id() const { return HUMAN; }
226 
227 	// @author hsiegeln
228 	simlinemgmt_t simlinemgmt;
229 
230 	/**
231 	 * Age messages (move them upwards)
232 	 * @author Hj. Malthaner
233 	 */
234 	void age_messages(uint32 delta_t);
235 
236 	/* Handles player colors ...
237 	* @author prissi
238 	*/
get_player_color1()239 	uint8 get_player_color1() const { return player_color_1; }
get_player_color2()240 	uint8 get_player_color2() const { return player_color_2; }
241 	void set_player_color(uint8 col1, uint8 col2);
242 
243 	/**
244 	 * @return the name of the player; "player -1" sits in front of the screen
245 	 * @author prissi
246 	 */
247 	const char* get_name() const;
248 	void set_name(const char *);
249 
get_player_nr()250 	sint8 get_player_nr() const {return player_nr; }
251 
252 	/**
253 	 * Test if this player is a public service player.
254 	 * @return true if the player is a public service player, otherwise false.
255 	 */
256 	bool is_public_service() const;
257 
258 	/**
259 	 * return true, if the owner is none, myself or player(1), i.e. the ownership can be taken by player test
260 	 * @author prissi
261 	 */
262 	static bool check_owner( const player_t *owner, const player_t *test );
263 
264 	/**
265 	 * @param player_nr Number assigned to this player - it's an ID.
266 	 * @author Hj. Malthaner
267 	 */
268 	player_t(uint8 player_nr );
269 
270 	virtual ~player_t();
271 
272 	static sint32 add_maintenance(player_t *player, sint32 const change, waytype_t const wt=ignore_wt)
273 	{
274 		if(player) {
275 			return player->add_maintenance(change, wt);
276 		}
277 		return 0;
278 	}
279 
280 	/**
281 	 * Cached value of scenario completion percentage.
282 	 * To get correct values for clients call scenario_t::get_completion instead.
283 	 */
284 	sint32 get_scenario_completion() const;
285 
286 	void set_scenario_completion(sint32 percent);
287 
288 	/**
289 	 * @return Account balance as a double (floating point) value
290 	 * @author Hj. Malthaner
291 	 */
292 	double get_account_balance_as_double() const;
293 
294 	/**
295 	 * @return true when account balance is overdrawn
296 	 * @author Hj. Malthaner
297 	 */
298 	int get_account_overdrawn() const;
299 
300 	/**
301 	 * Displays messages from the queue of the player on the screen
302 	 * Show income messages
303 	 * @author prissi
304 	 */
305 	void display_messages();
306 
307 	/**
308 	 * Called often by simworld.cc during simulation
309 	 * @note Any action goes here (only need for AI at the moment)
310 	 * @author Hj. Malthaner
311 	 */
312 	virtual void step();
313 
314 	/**
315 	 * Called monthly by simworld.cc during simulation
316 	 * @author Hj. Malthaner
317 	 * @returns false if player has to be removed (bankrupt/inactive)
318 	 */
319 	virtual bool new_month();
320 
321 	/**
322 	 * Called yearly by simworld.cc during simulation
323 	 * @author Hj. Malthaner
324 	 */
new_year()325 	virtual void new_year() {}
326 
327 	/**
328 	 * Stores/loads the player state
329 	 * @param file where the data will be saved/loaded
330 	 * @author Hj. Malthaner
331 	 */
332 	virtual void rdwr(loadsave_t *file);
333 
334 	/*
335 	 * Called after game is fully loaded;
336 	 */
337 	virtual void finish_rd();
338 
339 	virtual void rotate90( const sint16 y_size );
340 
341 	/**
342 	* Calculates the assets of the player
343 	*/
344 	void calc_assets();
345 
346 	/**
347 	* Updates the assets value of the player
348 	*/
349 	void update_assets(sint64 const delta, const waytype_t wt = ignore_wt);
350 
351 	/**
352 	 * Report the player one of his vehicles has a problem
353 	 * @author Hansj�rg Malthaner
354 	 * @date 26-Nov-2001
355 	 */
356 	virtual void report_vehicle_problem(convoihandle_t cnv,const koord3d position);
357 
358 	/**
359 	 * Tells the player the result of tool-work commands.
360 	 * If player is active then play sound, popup error msg etc.
361 	 * @author Dwachs
362 	 */
363 	void tell_tool_result(tool_t *tool, koord3d pos, const char *err);
364 
365 	/**
366 	 * Tells the player that the factory
367 	 * is going to be deleted (flag==0)
368 	 * Bernd Gabriel, Dwachs
369 	 */
370 	enum notification_factory_t {
371 		notify_delete	// notified immediately before object is deleted (and before nulled in the slist_tpl<>)!
372 	};
notify_factory(notification_factory_t,const fabrik_t *)373 	virtual void notify_factory(notification_factory_t, const fabrik_t*) {}
374 
375 private:
376 	/* undo informations *
377 	 * @author prissi
378 	 */
379 	vector_tpl<koord3d> last_built;
380 	waytype_t undo_type;
381 
382 public:
383 	/**
384 	 * Function for UNDO
385 	 * @date 7-Feb-2005
386 	 * @author prissi
387 	 */
388 	void init_undo(waytype_t t, unsigned short max );
389 
390 	/**
391 	 * Function for UNDO
392 	 * @date 7-Feb-2005
393 	 * @author prissi
394 	 */
395 	void add_undo(koord3d k);
396 
397 	/**
398 	 * Function for UNDO
399 	 * @date 7-Feb-2005
400 	 * @author prissi
401 	 */
402 	sint64 undo();
403 
404 private:
405 	// headquarters stuff
406 	sint32 headquarter_level;
407 	koord headquarter_pos;
408 
409 public:
add_headquarter(short hq_level,koord hq_pos)410 	void add_headquarter(short hq_level, koord hq_pos)
411 	{
412 		headquarter_level = hq_level;
413 		headquarter_pos = hq_pos;
414 	}
get_headquarter_pos()415 	koord get_headquarter_pos() const { return headquarter_pos; }
get_headquarter_level()416 	short get_headquarter_level() const { return headquarter_level; }
417 
418 	void ai_bankrupt();
419 };
420 
421 #endif
422