Selador / forschung.php

<?php

/* forschung.php - Front-end code for the "Forschung" page
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

// Works but is a bit weird...

require_once("php/base/user.inc.php");
require_once("php/base.php");
require_once("php/event.php");
require_once("headers.php");

$research_path="preres/";
$precalcpath = $research_path.$me->uid;

$glob_build = array();
$buildingdur = 0;

// Handle past events
// $me->event_handler (true, $me->activevillageid, $gui);


// Then calculate ressources through checking income - this takes into account
// past events, because event_handler might have updated $secspassed
// $me->activevillage->do_income ();

require_once ("php/citycont.php");
$build = city_fetch_db ($me->activevillageid);
$research = research_fetch_db ($me->uid);

if (isset ($_REQUEST['r']) && isset ($_REQUEST['l']) && !isset ($gui->research_string) && (!$me->activeoutpostid))
{
	$enhance = $_REQUEST['r'];
	$tolevel = $_REQUEST['l'];
	$levelfac = $tolevel * pow (NEXTLEVEL_COSTS, $tolevel - 1);

	if (isset ($research[$enhance]))
	{
  	
		if (!( $errstr = Calc_Building_Dep ($enhance, $research[$enhance][1], $build, $research, 2)))
		{
			if (isset($gui->research_done[$enhance]))
			{
				$research_err = _("This one is already researched in another town");
				log_info ("Cheater: Wanted to research ".$enhance.", but this is already being researched somewhere else");
			}
			else
			{
				// Next test: Is the level that we want to enhance the thing to one more than the actual level?
				if (($research[$enhance][1] + 1 + $addtolevel) == $tolevel)
				{
					if ($me->activevillage->can_spend ((int)($research[$enhance][2]*$levelfac), (int)($research[$enhance][3]*$levelfac), (int)($research[$enhance][4]*$levelfac), (int)($research[$enhance][15]*$levelfac)))
					{
						// Counter reload build queue auto-submit
						if (!check_event_param2 ($me->uid, $me->activevillageid, 2, $tolevel, $enhance))
						{
							$costs = (int)(($research[$enhance][2] + $research[$enhance][3] + $research[$enhance][4] + $research[$enhance][15]) * $levelfac);
							$newbuiltdur =  max (1, (int)($costs * $me->activevillage->speed_research / WORLD_SPEED / BUILD_SPEED));
							// Insert into event queue FIXME: Type depends on tile type, timestamp depends on finish time of last building in queue
							$query = "insert into event (uid1, village1, uid2, village2, type, param, param2, timestamp, score, flag) values (".$me->uid.", ".$me->activevillageid.", 0, 0, 2, ".$tolevel.", ".$enhance.", addtime(now(), sec_to_time(".$newbuiltdur.")), ".$costs.", 1)";
							if (!mysql_query ($query))
								log_err ("Cannot insert research event: ".$query.": ".mysql_error());
							else
							{
								$eid = mysql_insert_id ();
								$query = "insert into eventlog (eid, uid1, village1, uid2, village2, type, param, param2, fired, happened, score, state) values (".$eid.", ".$me->uid.", ".$me->activevillageid.", 0, 0, 2, ".$tolevel.", ".$enhance.", NULL, addtime(now(), sec_to_time(".$newbuiltdur.")), ".$costs.", 0)";
								if (!mysql_query ($query))
									log_err ("Cannot log research event: ".$query.": ".mysql_error());

								log_info ($research[$enhance][0]." (".$enhance.") angefangen, wird ".$newbuiltdur." Sekunden dauern. Query: ".$query);
								$gui->displayedtimers++;
								$gui->research_string = $research[$enhance][0]." ".$tolevel."  <span id=\"timer".$gui->displayedtimers."\">";
								//echo  "<br><br>research: ".$enhance.'<br><br>';
								$gui->research_string .= external_calcdur ($newbuiltdur)."</span>";
								$me->activevillage->spend ((int)($research[$enhance][2]*$levelfac), (int)($research[$enhance][3]*$levelfac), (int)($research[$enhance][4]*$levelfac), (int)($research[$enhance][15]*$levelfac));
								$new_level = true;

								// We should pass this on to the tutorial, if it is still going strong
								if (is_object ($tutorial))
								{
									if ($tutorial->active)
										$tutorial->researching ($enhance);
								}
							}
						}
					}
				}
			}
		}
		else
		{
			$research_err = _("You don't meet all conditions for this research").": ".$errstr;
			log_info ("Cheater: Wanted to research ".$enhance." but missing ".$errstr);
		}
	}
	else
	{
		$research_err = _("This research doesn't exist");
		log_info ("Cheater: Wanted to research ".$enhance);
	}
}

$me->activevillage->update_stamp ();

// We include basic design parts here
require_once ("styles/php/base_design-".$_SESSION['theme'].".php");
require_once("blocked.php");

echo '<div id="contentbox">';

if ($me->activeoutpostid)
	echo "<span class=\"error\">"._("Research can't be done in outposts")."</span>\n</div>";
else
{

	if (!$gui->research_string)
		$slotsfree = true;
	else
		$slotsfree = false;

	if (isset ($_REQUEST['p']))
		$page = $_REQUEST['p'];
	else
		$page = 0;

	if ($page < 0)
		$page = 0;


	// We should pass this on to the tutorial, if it is still going strong
	if (is_object ($tutorial))
	{
		if ($tutorial->active)
			$tutorial->is_in_research ();
	}

	$display_rows = 6;
	$skip_rows = $page * $display_rows;

	require_once ("php/citycont.php");
	$researchspeed = $me->activevillage->speed_research;

	
//	echo "</div><div id=\"forschung\">";
	//echo "<center><b>Forschungs-&Uuml;bersicht von ".$me->activevillage->name." Seite ".($page+1)." von ".$pages."</b></center><br>\n
	echo "<div id=\"seite-links\">";

  foreach ($research as $rid => $thisres)
  {
  	unset ($errstr);
  	if ($skip_rows > 0)
  	{
  		if ($rowcounter >= $skip_rows)
  			$rowcounter = $skip_rows = 0;
  		else
  		{
  			$rowcounter++;
  			continue;
  		}
  	}

    // evil hack for the dependancy embedding in the array :(
    if ($rid == 1000)
      continue;

    if ($rowcounter == 3)
    	echo '</div><div id="seite-rechts">';

		if ($rowcounter >= 6)
			break;
    $rowcounter++;

  	$dont_display_timer = false;

    $tolevel = $research[$rid][1] + 1;



		if (isset($gui->research_done[$rid]))
		{
			$errstr = _("Researching  somewhere else");
			$tolevel += $gui->research_done[$rid];
		}
		if (($enhance == $rid) && ($new_level == true)) 
		{
			$tolevel ++;
		}
		$levelfac = $tolevel * pow (NEXTLEVEL_COSTS, $tolevel - 1);
    
		
		$tmp_notenough = false;
		$thiscost = array (1=>(int)($research[$rid][2] * $levelfac), 2=>(int)($research[$rid][3] * $levelfac), 3=>(int)($research[$rid][4] * $levelfac), 4=>(int)($research[$rid][15] * $levelfac));

    if (!$errstr)
    {
			if ($errstr = Calc_Building_Dep ($rid, $research[$rid][1], $build, $research, 2))
			{
				if (strlen ($errstr) > 50)
					$errstr = substr ($errstr, 0, 47)."...";
				$errstr = str_replace ("hhaamm", "<img src=\"gfx/hammer.gif\" width=\"16\" height=\"16\" border=\"0\">&nbsp;", $errstr);
				$errstr = sprintf(_("Needs %s %s"), "\n", $errstr)." \n".$errstr;
				$dont_display_timer = true;
			}
		}

	//print_r($thiscost);
		$costs = $me->activevillage->display_money($thiscost, $dont_display_timer);

		$this_dur = external_calcdur (max (1, (int)(($thiscost[1] + $thiscost[2] + $thiscost[3] + $thiscost[4]) * $researchspeed / WORLD_SPEED / BUILD_SPEED)));
		$unb_dur = external_calcdur (max (1, (int)($this_cost / WORLD_SPEED / BUILD_SPEED)));


		echo '<table class="border" >
			<tr>
				<td rowspan="2">';


		if (file_exists ("gfx/r/".$rid.".png"))
			echo '<a target="help" href="hilfe.php?rp='.$rid.'" title="'._('Help page about').' '.$research[$rid][0].'"><img src="gfx/r/'.$rid.'.png" height="100px" width="82px" ></a>';
		else
			echo '<a target="help" href="hilfe.php?rp='.$rid.'" title="'._('Help page about').' '.$research[$rid][0].'"><img src="gfx/r/null.png" height="60px" width="80px"></a>';
			
		echo '
			</td>
			<th class="bold">
				<a target="help" href="hilfe.php?rp='.$rid.'" title="'._('Help page about').' '.$research[$rid][0].'">'.$research[$rid][0].' Stufe '.$research[$rid][1].'</a>
			</th>
		</tr>
		<tr>
			<td>
				<span class="info">'.$research[$rid][5].'</span>
					'._('Costs').': ';//  f&uuml;r Stufe '.$tolevel.': ';
		if (($enhance == $rid) && ($new_level == true)) //although this seems to make no sense, it's the only way to get around a problem
		{
			$tolevel --;
		}
   echo $costs."<br>\n";
   echo '</td></tr><tr class="iterate"><td><img src="gfx/uhr.gif" width="12" height="12" border="0"> '.$this_dur.'</td><td >';

  if ($slotsfree)
  {
		if ($errstr)
		{
			 echo "<span class=\"error\">".$errstr.'</span></td>';
			 unset ($errstr);
		}
		else
		{
			if (!$tmp_notenough)
				echo '<a href="'.$_SERVER['PHP_SELF'].'?r='.$rid.'&l='.$tolevel.'&p='.$page.'"><img src="gfx/forschung.gif" width="12" height="12" border="0">'._('Research now!').'</a></td>';
			else
				echo '<span class="error">'._('Not enough ressources').'</span>';
		}
	}
	else
	{
		echo "<span id=\"busy\">"._("Researchers are busy...")."</span></td>";
	}
	 echo '</tr></table>';
	}



	if (isset ($research_err))
		echo "<b>".$research_err."</b>";
}

$pages = ceil (count ($research) / $display_rows);
	echo "</div><div id=\"leiste-unten\">";
	if ($page > $pages-1)
		$page = $pages-1;

	if (0 != $page)
		echo "<a href=\"".$_SERVER['PHP_SELF']."?p=".($page-1)."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_links.png\" alt=\""._("Turn left")."\" title=\""._("Turn left")."\"></a>\n";
	else
		echo "<img src=\"styles/".$_SESSION['theme']."/pfeil_lang_links_grau.png\" alt=\""._("Turn left")."\" title=\""._("Turn left")."\">";
	echo "&nbsp;&nbsp;&nbsp;&nbsp";
	if ($page != ($pages-1))
		echo "<a href=\"".$_SERVER['PHP_SELF']."?p=".($page+1)."\"><img src=\"styles/".$_SESSION['theme']."/pfeil_lang_rechts.png\" alt=\""._("Turn right")."\" title=\""._("Turn right")."\"></a>\n";
	else
		echo "<img src=\"styles/".$_SESSION['theme']."/pfeil_lang_rechts_grau.png\" alt=\""._("Turn right")."\" title=\""._("Turn right")."\">";

echo "</div></div>\n";
require_once("footer.php");
?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.