mosolar /solar27/include/game/classes/empire.php

Language PHP Lines 675
MD5 Hash 4bb08a9f84e9146affa911306c3a31b1 Estimated Cost $11,137 (why?)
Repository https://bitbucket.org/sebs/mosolar View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?php

// Solar Imperium is licensed under GPL2, Check LICENSE.TXT for mode details //

class Empire
{

	var $DB;
	var $TEMPLATE;
	var $data;
	var $data_footprint;
	var $gameplay_costs;
	var $army;
	var $planets;
	var $coalition;
	var $production;
	var $supply;
	var $diplomacy;
	var $research;
	var $game_id;


	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function Empire($DB,$TEMPLATE,$gameplay_costs)
	{
		$this->DB = $DB;
		$this->game_id = round($_SESSION["game"]);
		$this->TEMPLATE = $TEMPLATE;
		$this->gameplay_costs = $gameplay_costs;
		$this->army = new Army($DB,$TEMPLATE);
		$this->planets = new Planets($DB,$TEMPLATE);
		$this->coalition = new Coalition($DB);
		$this->production = new Production($DB,$TEMPLATE);
		$this->supply = new Supply($DB,$TEMPLATE);
		$this->diplomacy = new Diplomacy($DB);
		$this->research = new Research($DB,$TEMPLATE);
		

	}

	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function load($id)
	{
		$query = "SELECT * FROM game".$this->game_id."_tb_empire WHERE id='".intval($id)."'";
		$this->data = $this->DB->Execute($query);
		if (!$this->data) trigger_error($query ." = ".$this->DB->ErrorMsg());
		if ($this->data->EOF) return array("code"=>false,"desc"=>T_("Fatal error while loading empire object"));
		$this->data = $this->data->fields;


		$this->data_footprint = md5(serialize($this->data));

		if (!$this->army->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading army object"));
		if (!$this->planets->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading planets object"));
		if (!$this->coalition->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading coalition object"));
		if (!$this->production->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading petroleum object"));
		if (!$this->supply->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading supply object"));
		if (!$this->diplomacy->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading diplomacy object"));
		if (!$this->research->load($id)) return array("code"=>false,"desc"=>T_("Fatal error while loading research object"));
		return array("code"=>true,"desc"=>null);
	}

	/////////////////////////////////////////////////////////////
	// save, but only if data have changed !
	/////////////////////////////////////////////////////////////
	function save()
	{

		$query = "UPDATE game".$this->game_id."_tb_empire SET ";
		reset($this->data);
		while (list($key,$value) = each($this->data))
		{
			if ($key == "id") continue;
			if (is_numeric($key)) continue;

            if ((is_numeric($value)) && ($key != "logo"))
				$query .= "$key=$value,";
			else
				$query .= "$key='".addslashes($value)."',";
			
		}

		$query = substr($query,0,strlen($query)-1); // removing remaining ,
		$query .= " WHERE id='".$this->data["id"]."'";
		
//		$this->DB->beginTrans();		
		if (!$this->DB->Execute($query)) trigger_error($this->DB->ErrorMsg());
		
		$this->army->save();
		$this->planets->save();
		$this->coalition->save();
		$this->production->save();
		$this->supply->save();

//		$this->DB->completeTrans();
	}

	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function collapse()
	{	
		global $NEWS_DATA;

		$this->data["active"] = 2;
		$this->data["population"] = 0;
		
		$evt = new EventCreator($this->DB);
		$evt->type = CONF_EVENT_COLLAPSEDEMPIRE;
		$evt->from = -1;
		$empire_data = $this->data;
		$evt->params  = array("empire_emperor"=>$this->data["emperor"],"empire_name"=>$this->data["name"],"gender"=>$this->data["gender"]);
		$evt->broadcast();

		// notice player
		$message = T_("Sorry but your empire have collapsed in game")." <b>".CONF_GAME_NAME."</b> <br/>";
		if (!$this->DB->Execute("INSERT INTO system_tb_messages (player_id,date,message) VALUES(".$this->data["player_id"].",".time(NULL).",'".addslashes($message)."')")) trigger_error($this->DB->ErrorMsg());

		$this->coalition->transferRandomOwnership(-1);

		// update history

		$military_might = $this->data["networth"]-($this->data["population"] * CONF_NETWORTH_POPULATION) - ($this->planets->getCount() * CONF_NETWORTH_PLANETS);


		$this->DB->Execute(
			"INSERT INTO system_tb_history (game_id,player_id,date,rank,empire_name,networth,military_might,planets,
			population,turns_played) 
			VALUES(".$_SESSION["game"].",".$this->data["player_id"].",".time(NULL).",0,'".$this->data["name"]."',
			".$this->data["networth"].",
			".$military_might.",
			".$this->planets->getCount().",
			".$this->data["population"].",
			".$this->data["turns_played"]."
			)");

		
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_armyconvoy WHERE empire_from='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());


	}

	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function delete()
	{
		
		if (!$this->DB->Execute("UPDATE game".$this->game_id."_tb_empire SET active='3' WHERE id='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_army WHERE empire='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_event WHERE event_from='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_event WHERE event_to='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_loan WHERE empire='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_planets WHERE empire='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_production WHERE empire='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_stats WHERE empire='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_supply WHERE empire='".$this->data["id"]."'"))trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_tradeconvoy WHERE empire_from='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_tradeconvoy WHERE empire_to='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_armyconvoy WHERE empire_from='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());


		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_treaty WHERE empire_from='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());
		if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_treaty WHERE empire_to='".$this->data["id"]."'")) trigger_error($this->DB->ErrorMsg());

        // Time to give coalition to a different member, if not exist, just simply delete
        // and send a event.
        if ($this->coalition->isMember())
        	if ($this->coalition->isOwner())
                $this->coalition->disband();

	}

	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function checkForEnoughFood()
	{
		global $CONF_CIVIL_STATUS;

		if ($this->data["active"] != 1) return "";
		
		if ($this->data["food"] >= 0) return "";
 
		
		$msg = "<table width=\"100%\"><tr><td><img src=\"../images/game/starving.gif\" style=\"border:0px solid yellow\"></td><td align=\"center\" width=\"100%\">&nbsp;<b style=\"color:yellow\">".T_("Your population is starving!")."</b><br/>";

		// First we evaluate how much food is needed
		$food_needed = abs($this->data["food"]);
		
		// we get the market supply
		$market = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_market");
		if (!$market) trigger_error($this->DB->ErrorMsg());
		$market = $market->fields;

		// Now we check if the food market have what we need
		if ($market["food"] >= $food_needed)
		{
			$food_price = round((CONF_COST_FOOD * $market["food_ratio"]));
			$food_total_cost = $food_needed * $food_price;
			
			// then we check for credits
			if ($this->data["credits"] >= $food_total_cost)
			{
				$msg_food = T_("Bought food: {food} for {credits}");
				
				
				$this->DB->Execute("UPDATE game".$this->game_id."_tb_market SET food='".($market["food"] - $food_needed)."',food_buy='".($market["food_buy"] + $food_needed)."'");	
				
				$msg_food = str_replace("{food}",
					$this->TEMPLATE->formatFood($food_needed),
					$msg_food
					);

				$msg_food = str_replace("{credits}",
					$this->TEMPLATE->formatCredits($food_total_cost),
					$msg_food
					);

				$msg .= $msg_food;
				
				$this->data["credits"] -= $food_total_cost;
				$this->data["food"] = 0;
				$msg .= "</td></tr></table>";
				
				return $msg;	
			}
		}
		
		$pop_lost = round(($this->data["population"]/100
		*CONF_POPULATION_STARVING));

		$msg_death = T_("{population} civilians starved to death!");
		$msg_death = str_replace("{percent}",CONF_POPULATION_STARVING,$msg_death);
		$msg_death = str_replace("{population}",
			$this->TEMPLATE->formatNumber($pop_lost),$msg_death);

		$msg .= $msg_death;
		$this->data["population"] -= $pop_lost;
		$soldiers_lost = round(($this->army->data["soldiers"]/100)
		*CONF_SOLDIERS_STARVING);
		
		$msg_death = T_("{soldiers} soldiers starved to death!");
		$msg_death = str_replace("{percent}",CONF_SOLDIERS_STARVING,$msg_death);
		$msg_death = str_replace("{soldiers}",
			$this->TEMPLATE->formatNumber($soldiers_lost),$msg_death);
		$msg .= $msg_death;


		$this->army->data["soldiers"] -= $soldiers_lost;

		$this->data["civil_status"]++;

		if ($this->data["civil_status"] > 7) 
			$this->data["civil_status"] = 7;

		$msg .= str_replace("{civil}",
		$CONF_CIVIL_STATUS[$this->data["civil_status"]],
		T_("Population is angry! New civil status: {civil}"))."<br/>\r\n";

		$this->data["food"] = 0;
		
		$msg .= "</td></tr></table>";
		return $msg;

	}

	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function checkForEnoughCredits()
	{
		if ($this->data["active"] != 1) return "";
		
		global $CONF_CIVIL_STATUS;
		
		if ($this->data["credits"] >= 0) return ""; 
	
		srand(time(NULL));
	
		$msg = "<table width=\"100%\"><tr><td><img src=\"../images/game/bankrupt.gif\" style=\"border:0px solid yellow\"></td><td valign=\"top\" align=\"center\" width=\"100%\">&nbsp;<b style=\"color:yellow\">".T_("Cannot afford maintenance costs!")."<br/>\r\n";


	
		$msg .= str_replace("{percent}",CONF_BANKRUPT_PLANETS,T_("{percent}% planets released!"))."<br/>\r\n";
	
		$this->planets->data["food_planets"] -= ceil(($this->planets->data["food_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["ore_planets"] -= ceil(($this->planets->data["ore_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["tourism_planets"] -= ceil(($this->planets->data["tourism_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["supply_planets"] -= ceil(($this->planets->data["supply_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["gov_planets"] -= ceil(($this->planets->data["gov_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["edu_planets"] -= ceil(($this->planets->data["edu_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["research_planets"] -= ceil(($this->planets->data["research_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["urban_planets"] -= ceil(($this->planets->data["urban_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["petro_planets"] -= ceil(($this->planets->data["petro_planets"]/100)*CONF_BANKRUPT_PLANETS);
		$this->planets->data["antipollu_planets"] -= ceil(($this->planets->data["antipollu_planets"]/100)*CONF_BANKRUPT_PLANETS);

		

		$msg .= str_replace("{percent}",CONF_BANKRUPT_MILITARY,T_("{percent}% army disbanded!"))."<br/>\r\n";

		$this->army->data["soldiers"] -= ceil(($this->army->data["soldiers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["fighters"] -= ceil(($this->army->data["fighters"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["stations"] -= ceil(($this->army->data["stations"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["lightcruisers"] -= ceil(($this->army->data["lightcruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["heavycruisers"] -= ceil(($this->army->data["heavycruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["carriers"] -= ceil(($this->army->data["carriers"]/100) * CONF_BANKRUPT_MILITARY);
		
		
		if (rand(0,5) == 0)
		{
			$this->army->data["effectiveness"] -= 5;
			if ($this->army->data["effectiveness"] < 10) $this->army->data["effectiveness"] = 10;
			$msg .= str_replace("{percent}",$this->army->data["effectiveness"],T_("Army effectiveness going down!"))."<br/>\r\n";

		}

		
		if (rand(0,5) == 0)
		{
			$this->data["civil_status"]++;
			if ($this->data["civil_status"] > 7) $this->data["civil_status"] = 7;
			$msg .= str_replace("{civil}",
			$CONF_CIVIL_STATUS[$this->data["civil_status"]],
			T_("Population is angry! New civil status: {civil}"))."<br/>\r\n";
		}
				
		$this->data["credits"] = 0;

		$msg .="</td></tr></table>";
		return $msg;

	}


	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function checkForEnoughOre()
	{
		if ($this->data["active"] != 1) return "";
		
		global $CONF_CIVIL_STATUS;
		
		if ($this->data["ore"] >= 0) return ""; 
	
		srand(time(NULL));
	
		$msg = "<table width=\"100%\"><tr><td><img src=\"../images/game/no_ore.jpg\" style=\"border:0px solid yellow\"></td><td valign=\"top\" align=\"center\" width=\"100%\">&nbsp;<b style=\"color:yellow\">".T_("No enough ore!")."<br/>\r\n";

		// First we evaluate how much ore is needed
		$ore_needed = abs($this->data["ore"]);
		
		// we get the market supply
		$market = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_market");
		if (!$market) trigger_error($this->DB->ErrorMsg());
		$market = $market->fields;

		// Now we check if the market have what we need
		if ($market["ore"] >= $ore_needed)
		{
			$ore_price = round((CONF_COST_ORE * $market["ore_ratio"]));
			$ore_total_cost = $ore_needed * $ore_price;
			
			// then we check for credits
			if ($this->data["credits"] >= $ore_total_cost)
			{
				$msg_ore = T_("bought ore: {ore} for {credits}");
				
				
				$this->DB->Execute("UPDATE game".$this->game_id."_tb_market SET ore=".($market["ore"] - $ore_needed).",ore_buy=".($market["ore_buy"] + $ore_needed));	
				
				$msg_ore = str_replace("{ore}",
					$this->TEMPLATE->formatFood($ore_needed),
					$msg_ore
					);

				$msg_ore = str_replace("{credits}",
					$this->TEMPLATE->formatCredits($ore_total_cost),
					$msg_ore
					);

				$msg .= $msg_ore;
				
				$this->data["credits"] -= $ore_total_cost;
				$this->data["ore"] = 0;
				$msg .= "</td></tr></table>";
				
				return $msg;	
			}
		}
		


		$msg .= str_replace("{percent}",CONF_BANKRUPT_MILITARY,T_("Army disbanded!"))."<br/>\r\n";

		$this->army->data["fighters"] -= ceil(($this->army->data["fighters"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["lightcruisers"] -= ceil(($this->army->data["lightcruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["heavycruisers"] -= ceil(($this->army->data["heavycruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["carriers"] -= ceil(($this->army->data["carriers"]/100) * CONF_BANKRUPT_MILITARY);
		
		
		if (rand(0,5) == 0)
		{
			$this->army->data["effectiveness"] -= 5;
			if ($this->army->data["effectiveness"] < 10) $this->army->data["effectiveness"] = 10;
			$msg .= str_replace("{percent}",$this->army->data["effectiveness"],T_("Army effectiveness going down!"))."<br/>\r\n";

		}

		
		if (rand(0,5) == 0)
		{
			$this->data["civil_status"]++;
			if ($this->data["civil_status"] > 7) $this->data["civil_status"] = 7;
			$msg .= str_replace("{civil}",
			$CONF_CIVIL_STATUS[$this->data["civil_status"]],
			T_("Population is angry! New civil status: {civil}"))."<br/>\r\n";
		}
				
		$this->data["ore"] = 0;

		$msg .="</td></tr></table>";
		return $msg;

	}


	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function checkForEnoughPetroleum()
	{
		if ($this->data["active"] != 1) return "";
		
		global $CONF_CIVIL_STATUS;
		
		if ($this->data["petroleum"] >= 0) return ""; 
	
		srand(time(NULL));
	
		$msg = "<table width=\"100%\"><tr><td><img src=\"../images/game/no_petro.jpg\" style=\"border:0px solid yellow\"></td><td valign=\"top\" align=\"center\" width=\"100%\">&nbsp;<b style=\"color:yellow\">".T_("No enough petroleum!")."<br/>\r\n";

		// First we evaluate how much petroleum is needed
		$petroleum_needed = abs($this->data["petroleum"]);
		
		// we get the market supply
		$market = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_market");
		if (!$market) trigger_error($this->DB->ErrorMsg());
		$market = $market->fields;

		// Now we check if the market have what we need
		if ($market["petroleum"] >= $petroleum_needed)
		{
			$petroleum_price = round((CONF_COST_PETROLEUM * $market["petroleum_ratio"]));
			$petroleum_total_cost = $petroleum_needed * $petroleum_price;
			
			// then we check for credits
			if ($this->data["credits"] >= $petroleum_total_cost)
			{
				$msg_petroleum = T_("Bought petroleum: {petroleum} for {credits}");
				
				
				$this->DB->Execute("UPDATE game".$this->game_id."_tb_market SET petroleum=".($market["petroleum"] - $petroleum_needed).",petroleum_buy=".($market["petroleum_buy"] + $petroleum_needed));	
				
				$msg_petroleum = str_replace("{petroleum}",
					$this->TEMPLATE->formatFood($petroleum_needed),
					$msg_petroleum
					);

				$msg_petroleum = str_replace("{credits}",
					$this->TEMPLATE->formatCredits($petroleum_total_cost),
					$msg_petroleum
					);

				$msg .= $msg_petroleum;
				
				$this->data["credits"] -= $petroleum_total_cost;
				$this->data["petroleum"] = 0;
				$msg .= "</td></tr></table>";
				
				return $msg;	
			}
		}
		

		$msg .= str_replace("{percent}",CONF_BANKRUPT_MILITARY,T_("{percent}% army disbanded!"))."<br/>\r\n";

		$this->army->data["fighters"] -= ceil(($this->army->data["fighters"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["stations"] -= ceil(($this->army->data["stations"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["lightcruisers"] -= ceil(($this->army->data["lightcruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["heavycruisers"] -= ceil(($this->army->data["heavycruisers"]/100) * CONF_BANKRUPT_MILITARY);
		$this->army->data["carriers"] -= ceil(($this->army->data["carriers"]/100) * CONF_BANKRUPT_MILITARY);
		
		
		if (rand(0,5) == 0)
		{
			$this->army->data["effectiveness"] -= 5;
			if ($this->army->data["effectiveness"] < 10) $this->army->data["effectiveness"] = 10;
			$msg .= str_replace("{percent}",$this->army->data["effectiveness"],T_("{percent}% rmy effectiveness going down!"))."<br/>\r\n";

		}

		
		if (rand(0,5) == 0)
		{
			$this->data["civil_status"]++;
			if ($this->data["civil_status"] > 7) $this->data["civil_status"] = 7;
			$msg .= str_replace("{civil}",
			$CONF_CIVIL_STATUS[$this->data["civil_status"]],
			T_("Population is angry! New civil status: {civil}"))."<br/>\r\n";
		}
				
		$this->data["petroleum"] = 0;

		$msg .="</td></tr></table>";
		return $msg;

	}


	/////////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////////
	function checkForEnoughPopulation()
	{

		// you are dead, jim
		if (($this->data["population"] < 10) || ($this->planets->getCount() == 0))
			return false;

		return true;
		
	
	}


	/////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////
	function updateNetworth()
	{

		$networth = 0;
		$networth += ($this->data["population"] * CONF_NETWORTH_POPULATION);
		$networth += ($this->data["credits"] * CONF_NETWORTH_CREDITS);
	
		$planets = 0;
		$planets += $this->planets->data["food_planets"];
		$planets += $this->planets->data["ore_planets"];
		$planets += $this->planets->data["tourism_planets"];
		$planets += $this->planets->data["supply_planets"];
		$planets += $this->planets->data["gov_planets"];
		$planets += $this->planets->data["edu_planets"];
		$planets += $this->planets->data["research_planets"];
		$planets += $this->planets->data["urban_planets"];
		$planets += $this->planets->data["petro_planets"];
		$planets += $this->planets->data["antipollu_planets"];
		$networth += ($planets * CONF_NETWORTH_PLANETS);

		$army = 0;
		$army += $this->army->data["soldiers"] * CONF_NETWORTH_MILITARY_SOLDIER;
		$army += $this->army->data["fighters"]* CONF_NETWORTH_MILITARY_FIGHTER;
		$army += $this->army->data["stations"]* CONF_NETWORTH_MILITARY_STATION;
		$army += $this->army->data["lightcruisers"]* CONF_NETWORTH_MILITARY_LIGHTCRUISER;
		$army += $this->army->data["heavycruisers"]* CONF_NETWORTH_MILITARY_HEAVYCRUISER;
		$army += $this->army->data["carriers"]* CONF_NETWORTH_MILITARY_CARRIER;
		$army += $this->army->data["covertagents"]* CONF_NETWORTH_MILITARY_COVERT;

		$rs = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_armyconvoy WHERE empire_from=".$this->data["id"]." OR empire_to=".$this->data["id"]);
		if (!$rs) trigger_error($DB->ErrorMsg());

		while (!$rs->EOF) {

			$army += $rs->fields["convoy_soldiers"] * CONF_NETWORTH_MILITARY_SOLDIER;
			$army += $rs->fields["convoy_fighters"]* CONF_NETWORTH_MILITARY_FIGHTER;
			$army += $rs->fields["convoy_lightcruisers"]* CONF_NETWORTH_MILITARY_LIGHTCRUISER;
			$army += $rs->fields["convoy_heavycruisers"]* CONF_NETWORTH_MILITARY_HEAVYCRUISER;
			$army += $rs->fields["carriers"]* CONF_NETWORTH_MILITARY_CARRIER;
			$rs->MoveNext();
		}



		$networth += ($army);
		$this->data["networth"] = floor($networth);

                if ($this->coalition->IsMember())
               		$this->coalition->updateNetworth($this->data["networth"]);
	}


	/////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////
    function calcAntiPollution()
    {
        if ($this->planets->data["antipollu_planets"]!=0) {
            $antipollution = $this->planets->data["antipollu_planets"] * CONF_ANTIPOLLUTION;
            $antipollution = round(($antipollution/100)* $this->production->data["antipollu_short"]);
            if ($antipollution == 0) $antipollution = 1;

            return $antipollution;
        } else return 1;
    }

	/////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////
    function calcPollution()
    {

        $pollution = floor((int) (($this->planets->data["petro_planets"]/100) * $this->production->data["petro_short"]) * CONF_PETRO_POLLUTION);
        $pollution += floor((int) $this->data["population"] * CONF_POP_POLLUTION);
        $pollution = round($pollution,2);
        return $pollution;
    }

	/////////////////////////////////////////////////////////
	//
	/////////////////////////////////////////////////////////
	function updateStats()
	{
        $pollution = $this->calcPollution();
		$army = 0;
		$army += $this->army->data["soldiers"] * CONF_NETWORTH_MILITARY_SOLDIER;
		$army += $this->army->data["fighters"]* CONF_NETWORTH_MILITARY_FIGHTER;
		$army += $this->army->data["stations"]* CONF_NETWORTH_MILITARY_STATION;
		$army += $this->army->data["lightcruisers"]* CONF_NETWORTH_MILITARY_LIGHTCRUISER;
		$army += $this->army->data["heavycruisers"]* CONF_NETWORTH_MILITARY_HEAVYCRUISER;
		$army += $this->army->data["carriers"]* CONF_NETWORTH_MILITARY_CARRIER;
		$army += $this->army->data["covertagents"]* CONF_NETWORTH_MILITARY_COVERT;

		$rs = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_armyconvoy WHERE empire_from=".$this->data["id"]." OR empire_to=".$this->data["id"]);
		if (!$rs) trigger_error($DB->ErrorMsg());


		while (!$rs->EOF) {

			$army += $rs->fields["convoy_soldiers"] * CONF_NETWORTH_MILITARY_SOLDIER;
			$army += $rs->fields["convoy_fighters"]* CONF_NETWORTH_MILITARY_FIGHTER;
			$army += $rs->fields["convoy_lightcruisers"]* CONF_NETWORTH_MILITARY_LIGHTCRUISER;
			$army += $rs->fields["convoy_heavycruisers"]* CONF_NETWORTH_MILITARY_HEAVYCRUISER;
			$army += $rs->fields["carriers"]* CONF_NETWORTH_MILITARY_CARRIER;
			$rs->MoveNext();
		}


		$query = "INSERT INTO game".$this->game_id."_tb_stats (empire,date,credits,food,networth,military,planets,population,pollution,turn) 
		VALUES(".
		$this->data["id"].",".
		time(NULL).",".
		$this->data["credits"].",".
		$this->data["food"].",".
		$this->data["networth"].",".
		$army.",".
		$this->planets->getCount().",".
		$this->data["population"].",".
		$pollution.",".
		$this->data["turns_played"].");";

		if (!$this->DB->Execute($query)) trigger_error($this->DB->ErrorMsg());
	}


}



?>
Back to Top