map = array();
$this->map_file = 'default';
$this->map_width = 16;
$this->map_height = 16;
$this->map_dimension = 256;
$this->player_number = 4;
$this->player_id = array(123456,123456,123456,123456);
$this->player_alive = array(1,1,1,1);
$this->track_total = 100;
$this->track_current = 0;
$this->js_speed = 250;
}
public function set_map($map)
{
$this->map_file = $map;
}
public function set_dimension($width, $height)
{
$this->map_width = $width;
$this->map_height = $height;
$this->map_dimension = $width*$height;
}
public function set_players($array_id)
{
$this->player_number = count($array_id);
$this->player_id = $array_id;
}
public function init_map()
{
$temp_file = fopen('MAP/'.$this->map_file.'.txt', 'r');
for ($line = 0; $line < ($this->map_width); $line++)
{
$this->map = array_merge($this->map, explode(';', rtrim(fgets($temp_file))));
}
}
public function init_player()
{
for ($player = 0; $player < ($this->player_number); $player++)
{
include_once('BOT/bot_'.$this->player_id[$player].'.php');
$temp_function = 'bot_'.$this->player_id[$player];
$this->player_function[$player] = new $temp_function();
}
}
public function display_javascript()
{
echo '';
}
public function display_arena()
{
echo '
';
}
public function launch()
{
$this->print_map();
$this->track_current++;
$turn = 1;
while ($this->track_current <= $this->track_total)
{
$player_current = $turn % (($this->player_number)+1);
if ($player_current == 0)
{
$temp_map = $this->map;
$this->update_map_world();
if ($temp_map != $this->map)
{
$this->print_map();
$this->track_current++;
}
}
else
{
$player_current--;
if ($this->player_alive[$player_current] != 0)
{
$this->update_map_player($player_current);
$this->print_map();
$this->track_current++;
}
}
$turn++;
}
}
public function print_log()
{
echo '';
for ($player = 0; $player < $this->player_number; $player++)
{
if (method_exists($this->player_function[$player],'log'))
{
echo '| JOUEUR-'.($player+1).' | '.$this->player_function[$player]->log().' |
';
}
}
echo '
';
}
/*---------------------------------------*/
/*----------[Fonctions privées]----------*/
/*---------------------------------------*/
private function update_map_player($player_id)
{
$current_position = $this->scan_player($player_id);
$operation = $this->player_function[$player_id]->main($this->map);
if (($operation == 0) OR ($operation == 1) OR ($operation == 2) OR ($operation == 3))
{
if ($this->verif_operation($current_position, $operation))
{
$new_position = $this->case_move($current_position, $operation);
if ($this->map[$new_position] == '[_]')
{
$this->map[$current_position] = '[_]';
$this->map[$new_position] = '[P'.($player_id+1).']';
}
}
}
}
private function update_map_world()
{
$this->map = str_replace('[E]', '[_]', $this->map);
$array_missile[0] = array_keys($this->map, '[M0]');
$array_missile[1] = array_keys($this->map, '[M1]');
$array_missile[2] = array_keys($this->map, '[M2]');
$array_missile[3] = array_keys($this->map, '[M3]');
for ($direction = 0; $direction < 4; $direction++)
{
foreach ($array_missile[$direction] as $missile)
{
if ($this->verif_operation ($missile, $direction))
{
$new_position = $this->case_move($missile, $direction);
if ($this->map[$new_position] == '[_]')
{
$this->map[$missile] = '[_]';
$this->map[$new_position] = '[M'.$direction.']';
}
else
{
switch ($this->map[$new_position])
{
case '[C]':
$this->map[$missile] = '[_]';
$this->map[$new_position] = '[E]';
break;
case '[M]':
$this->map[$missile] = '[E]';
break;
default:
$this->map[$missile] = '[_]';
$this->map[$new_position] = '[E]';
}
}
}
else
{
$this->map[$missile] = '[E]';
}
}
}
}
private function scan_player($player_id)
{
return array_search('[P'.($player_id+1).']', $this->map);
}
private function verif_operation ($case_id, $direction)
{
if (((($case_id % $this->map_width) == 0) AND ($direction == 0)) OR (((($case_id+1) % $this->map_width) == 0) AND ($direction == 2)) OR (($case_id < $this->map_width) AND ($direction == 1)) OR (($case_id >= $this->map_width*($this->map_height-1)) AND ($direction == 3)))
{
return FALSE;
}
else
{
return TRUE;
}
}
private function case_move($case_id, $direction)
{
switch ($direction)
{
case 0:
$new_case_id = $case_id-1;
break;
case 1:
$new_case_id = $case_id-16;
break;
case 2:
$new_case_id = $case_id+1;
break;
case 3:
$new_case_id = $case_id+16;
break;
}
return $new_case_id;
}
private function print_map()
{
echo '';
for ($case = 0; $case < ($this->map_dimension); $case++)
{
if($case == ($this->map_dimension-1))
{
echo $this->map[$case];
}
else
{
echo $this->map[$case].';';
}
}
echo '
';
}
}
?>