mourne_rcpp_fw/Mourne-CI/application/models/map_model.php

275 lines
7.1 KiB
PHP
Raw Normal View History

2021-10-30 19:02:07 +02:00
<?php
class Map_model extends MO_Model
{
public function __construct()
{
parent::__construct();
}
2021-10-30 19:02:07 +02:00
public function get_village_by_name($name)
{
$sql = "SELECT id FROM villages WHERE name LIKE '%" . $name . "%'";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
return $this->get_village_coords($res['id']);
}
2021-10-30 19:02:07 +02:00
public function get_village_coords($vid)
{
$sql = "SELECT * FROM map WHERE type='3' AND villageid='$vid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
return $q->row_array();
}
2021-10-30 19:02:07 +02:00
public function get_map($x, $y)
{
if ($x < 7) {
$x = 7;
}
2021-10-30 19:02:07 +02:00
if ($y < 7) {
$y = 7;
}
2021-10-30 19:02:07 +02:00
if ($x > 235) {
$x = 235;
}
2021-10-30 19:02:07 +02:00
if ($y > 235) {
$y = 235;
}
2021-10-30 19:02:07 +02:00
$minx = $x - 6;
$maxx = $x + 6;
$miny = $y - 6;
$maxy = $y + 6;
2021-10-30 19:02:07 +02:00
//< at max so it gets 12 rows and cols
$sql = "SELECT map.*,ai_villages.name AS ai_name,villages.name FROM map
2021-10-30 19:02:07 +02:00
LEFT JOIN ai_villages ON map.villageid=ai_villages.id AND map.type='4'
LEFT JOIN villages ON map.villageid=villages.id AND map.type='3'
WHERE ((map.X >= '$minx') AND (map.X < '$maxx')
AND (map.Y >= '$miny') AND (map.Y < '$maxy'))";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
$res = $q->result_array();
2021-10-30 19:02:07 +02:00
//preprocess it, so view only have to do a foreach
for ($sy = $miny; $sy < ($miny + 12); $sy++) {
for ($sx = $minx; $sx < ($minx + 12); $sx++) {
foreach ($res as $row) {
if (($row['X'] == $sx) && ($row['Y'] == $sy)) {
$data[] = $row;
break;
}
}
}
}
return $data;
2021-10-30 19:02:07 +02:00
}
public function get_map_list_admin()
{
$this->load->helper('directory');
$map = directory_map('./sql/map/', 1);
2021-10-30 19:02:07 +02:00
foreach ($map as $row) {
$a = explode('.', $row);
2021-10-30 19:02:07 +02:00
if (isset($a[1])) {
if ($a[1] == 'sql') {
$data[] = $row;
}
}
}
if (isset($data)) {
return $data;
} else {
return false;
}
}
2021-10-30 19:02:07 +02:00
public function generate_map_admin()
2021-10-30 19:02:07 +02:00
{
//process ai village names (in different table) here
//they will be saved into a different file until everything here works correctly
//add .!
$a = "DROP TABLE IF EXISTS ai_villages; \n";
2021-10-30 19:02:07 +02:00
//create table
$a .= "CREATE TABLE IF NOT EXISTS `ai_villages` (
2021-10-30 19:02:07 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`attacked` tinyint(4) NOT NULL,
`X` int(11) NOT NULL,
`Y` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
$i = 1;
for ($x = 1; $x <= 240; $x++) {
for ($y = 1; $y <= 240; $y++) {
//if ai village space
if (!((($x + 24) % 48) || (($y + 24) % 48))) {
$name = $this->_rand_ai_name();
$a .= "INSERT INTO ai_villages
2021-10-30 19:02:07 +02:00
VALUES('$i', '$name', '0', '$x', '$y'); \n";
$i++;
}
}
}
2021-10-30 19:02:07 +02:00
$a .= "DROP TABLE IF EXISTS map; \n";
2021-10-30 19:02:07 +02:00
$a .= "CREATE TABLE IF NOT EXISTS `map` (
2021-10-30 19:02:07 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
`X` int(11) NOT NULL,
`Y` int(11) NOT NULL,
`type` tinyint(4) NOT NULL,
`villageid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `X` (`X`,`Y`,`type`,`villageid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
$a .= "\n";
$sql = "SELECT * FROM villages";
$q = $this->db->query($sql);
$villages = $q->result_array();
if ($villages) {
foreach ($villages as $row) {
$found = false;
while (!$found) {
$vx = mt_rand(1, 240);
$vy = mt_rand(1, 240);
if (((($vx + 24) % 48) && (($vy + 24) % 48))) {
//not in ai space
$vdata[] = array(
'id' => $row['id'],
'x' => $vx,
'y' => $vy);
$found = true;
}
}
}
} else {
$vdata = false;
}
$villageid = 0;
$ai = 1;
$found = false;
$i = 1;
for ($x = 1; $x <= 240; $x++) {
for ($y = 1; $y <= 240; $y++) {
//if not ai village space
if (((($x + 24) % 48) || (($y + 24) % 48))) {
if ($vdata) {
foreach ($vdata as $row) {
if ($row['x'] == $x && $row['y'] == $y) {
$villageid = $row['id'];
$type = 3;
$found = true;
}
}
}
if (!$found) {
//check for village here, dont let this run when true
$r = rand(0, 100);
if ($r < 70) {
$type = 0;
} elseif ($r >= 70 && $r < 85) {
$type = 1;
} else {
$type = 2;
}
}
$found = false;
} else {
//ai village space
$type = 4;
$villageid = $ai;
$ai++;
}
$a .= "INSERT INTO map VALUES('$i', '$x', '$y', '$type', '$villageid');\n";
$villageid = 0;
$i++;
}
}
$this->load->helper('file');
$f = './sql/map/map' . time() . '.sql';
write_file($f, $a);
2021-10-30 19:02:07 +02:00
}
public function apply_map_admin($file)
2021-10-30 19:02:07 +02:00
{
$this->load->helper('file');
2021-10-30 19:02:07 +02:00
$f = readfile(('./sql/map/' . $file));
2021-10-30 19:02:07 +02:00
$sql = explode(';', $f);
2021-10-30 19:02:07 +02:00
foreach ($sql as $row) {
$this->db->query($row);
}
2021-10-30 19:02:07 +02:00
}
public function _rand_ai_name()
2021-10-30 19:02:07 +02:00
{
//they are in keyboard order from left to right, and top to bottom
//21 entries
$a = array('q', 'w', 'r', 't', 'z', 'p', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'y', 'x',
'c', 'v', 'b', 'n', 'm');
//5 entries
$b = array('e', 'u', 'i', 'o', 'a');
//ai village names should be between 4-6 chars
$length = rand(4, 6);
$str = '';
for ($i = 1; $i <= $length; $i++) {
//start with $a
if ($i % 2) {
if ($i == 1) {
$str .= strtoupper($a[mt_rand(0, 20)]);
} else {
$str .= $a[mt_rand(0, 20)];
}
} else {
$str .= $b[mt_rand(0, 4)];
}
}
return $str;
2021-10-30 19:02:07 +02:00
}
}
//nowhitesp