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

365 lines
9.3 KiB
PHP
Raw Normal View History

2021-10-30 19:02:07 +02:00
<?php
class Ai_model extends MO_Model
{
protected $settings;
2021-10-30 19:02:07 +02:00
public function __construct()
2021-10-30 19:02:07 +02:00
{
parent::__construct();
2021-10-30 19:02:07 +02:00
}
public function attack()
2021-10-30 19:02:07 +02:00
{
$this->parse_settings();
if (!$this->settings['on']) {
return 'AI is off';
}
$sql = "SELECT * FROM ai_villages WHERE attacked='0'";
$q = $this->db->query($sql);
$ai_village_not_attacked = $q->num_rows();
$log = "ai_villages query returned " . $ai_village_not_attacked . " rows. \n";
$ai_villages = $q->result_array();
$attacking_num = $this->settings['max_attack_village_limit'];
//determining how much will attack
if ($this->settings['attack_village_rand']) {
$a = rand(0, $this->settings['attack_village_rand']);
$attacking_num -= $a;
}
$log .= $attacking_num . " villages attacking. \n";
$reset = false;
//resetting ai_villages's attack field if everything will be set to attacked
if ($ai_village_not_attacked < $attacking_num) {
$attacking_num = $ai_village_not_attacked;
$reset = true;
}
//determining which villages are going to attack
for ($i = 0; $i < $attacking_num; $i++) {
$found = false;
while (!$found) {
foreach ($ai_villages as $row) {
$r = rand(1, 100);
if ($r > 50 && !isset($s[$row['id']])) {
$s[$row['id']] = true;
$data[] = $row;
$found = true;
break;
}
}
}
}
//saving to the db that they are attacked
if (!$reset) {
$sql = "UPDATE ai_villages SET attacked='1' WHERE ";
$first = true;
foreach ($data as $row) {
if ($first) {
$first = false;
} else {
$sql .= " OR ";
}
$sql .= "id='" . $row['id'] . "'";
}
$this->db->query($sql);
} else {
$sql = "UPDATE ai_villages SET attacked='0'";
$this->db->query($sql);
$log .= "ai_villages's attacked field reseted.\n";
}
//getting ai_units
$sql = "SELECT * FROM ai_units";
$q = $this->db->query($sql);
$num_ai_units = $q->num_rows();
$res = $q->result_array();
if ($this->settings['ai_unit_max_diff'] < $num_ai_units) {
$num = 0;
$d = false;
while ($num != $this->settings['ai_unit_max_diff']) {
foreach ($res as $row) {
if (!isset($d[$row['id']])) {
$r = rand(1, 50);
if ($r > 50) {
$ai_units[] = $row;
$d[$row['id']] = true;
$num++;
break;
}
}
}
}
} else {
$ai_units = $res;
}
//adding villages to log
$log .= "Attacking villages: \n";
foreach ($data as $row) {
$log .= $row['name'] . "(" .$row['X'] . " " . $row['Y'] . ") \n";
}
//sending attackers
foreach ($data as $row) {
$sql = "SELECT map.X,map.Y,villages.*
2021-10-30 19:02:07 +02:00
FROM map
LEFT JOIN villages ON (map.villageid=villages.id AND map.type='3')
WHERE (map.type = 3 AND
map.X > '" . ($row['X'] - 24) . "'
AND map.X < '" . ($row['X'] + 24) . "'
AND map.Y > '" . ($row['Y'] - 24) . "'
AND map.Y < '" . ($row['Y'] + 24) . "')";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
//no villages in range
if (!$q->num_rows()) {
continue;
}
2021-10-30 19:02:07 +02:00
$log .= $row['name'] . " is attacking: \n";
2021-10-30 19:02:07 +02:00
$res = $q->result_array();
//sending attackers
foreach ($res as $rrow) {
if ($rrow['ai_on'] || $rrow['ai_flagged']) {
$log .= "Sending attackers to: " . $rrow['name'] . "[" . $rrow['id'] . "]";
$log .= " (S: " . $rrow['score'] . ")\n";
$log .= "Sent: \n";
2021-10-30 19:02:07 +02:00
foreach ($ai_units as $unit) {
$a = ($rrow['score'] / $unit['rate']) / $unit['per_score'];
$num = floor($a);
2021-10-30 19:02:07 +02:00
$log .= $unit['name'] . "->" . $num . "\n";
2021-10-30 19:02:07 +02:00
$send[] = array('unitid' => $unit['id'],
'num' => $num);
}
2021-10-30 19:02:07 +02:00
$this->send_attack($send, $row, $rrow);
}
}
}
2021-10-30 19:02:07 +02:00
$this->load->helper('file');
2021-10-30 19:02:07 +02:00
$f = './logs/ai_log/log_' . time() . '.txt';
2021-10-30 19:02:07 +02:00
write_file($f, $log);
2021-10-30 19:02:07 +02:00
return $log;
}
2021-10-30 19:02:07 +02:00
public function send_attack($send, $ai_village, $village)
{
$villageid = $village['id'];
2021-10-30 19:02:07 +02:00
$sql = "SELECT max(attackid) AS attackid FROM attacks WHERE villageid='$villageid'";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
if ($res['attackid']) {
$atkid = ($res['attackid'] + 1);
} else {
$atkid = 1;
}
2021-10-30 19:02:07 +02:00
$first = true;
$sql = "INSERT INTO attacks VALUES";
foreach ($send as $row) {
if ($first) {
$first = false;
} else {
$sql .= ',';
}
2021-10-30 19:02:07 +02:00
$sql .= "(default, '$villageid', '$atkid', '" . $row['unitid'] . "', '" . $row['num']. "')";
}
2021-10-30 19:02:07 +02:00
$sql .= ";";
2021-10-30 19:02:07 +02:00
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//calculate distance
$dist = sqrt(pow($ai_village['X'] - $village['X'], 2) + pow($ai_village['Y'] - $village['Y'], 2));
2021-10-30 19:02:07 +02:00
//15 min 1 square
$time = $dist * 900;
$r = rand(3600, 10800);
$end = $time + $r;
2021-10-30 19:02:07 +02:00
//todo remove
$end = 20;
2021-10-30 19:02:07 +02:00
//attack random sides
$dir = rand(1, 4);
2021-10-30 19:02:07 +02:00
$ev['villageid'] = $villageid;
$ev['slotid'] = 0;
$ev['type'] = parent::EVENT_ATTACK;
$ev['time'] = $end;
$ev['data1'] = $atkid;
$ev['data2'] = $dir;
2021-10-30 19:02:07 +02:00
$this->add_event($ev);
}
2021-10-30 19:02:07 +02:00
public function parse_settings()
{
$sql = "SELECT * FROM ai_settings";
2021-10-30 19:02:07 +02:00
$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
foreach ($res as $row) {
$data[$row['setting']] = $row['value'];
}
2021-10-30 19:02:07 +02:00
$this->settings = $data;
2021-10-30 19:02:07 +02:00
}
public function get_unit_list_drop_admin()
{
$sql = "SELECT * FROM ai_units";
2021-10-30 19:02:07 +02:00
$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
$data[0] = 'Nothing';
2021-10-30 19:02:07 +02:00
foreach ($res as $row) {
$data[$row['id']] = $row['name'];
}
2021-10-30 19:02:07 +02:00
return $data;
2021-10-30 19:02:07 +02:00
}
public function list_units_admin()
{
$sql = "SELECT * FROM ai_units";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
return $q->result_array();
}
2021-10-30 19:02:07 +02:00
public function get_unit_admin($id)
{
$sql = "SELECT * FROM ai_units WHERE id='$id'";
$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 add_unit_admin($data)
{
$sql = "INSERT INTO ai_units
2021-10-30 19:02:07 +02:00
VALUES(default,
'" . $data['name'] . "',
'" . $data['icon'] . "',
'" . $data['ability'] . "',
'" . $data['can_carry'] . "',
'" . $data['attack'] . "',
'" . $data['defense'] . "',
'" . $data['rate'] . "',
'" . $data['per_score'] . "',
'" . $data['turn'] . "',
'" . $data['strong_against'] . "',
'" . $data['weak_against'] . "')";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
public function edit_unit_admin($data)
{
$sql = "UPDATE ai_units
2021-10-30 19:02:07 +02:00
SET name='" . $data['name'] . "',
icon='" . $data['icon'] . "',
ability='" . $data['ability'] . "',
can_carry='" . $data['can_carry'] . "',
attack='" . $data['attack'] . "',
defense='" . $data['defense'] . "',
rate='" . $data['rate'] . "',
per_score='" . $data['per_score'] . "',
turn='" . $data['turn'] . "',
strong_against='" . $data['strong_against'] . "',
weak_against='" . $data['weak_against'] . "'
WHERE id='" . $data['id'] . "'";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
public function get_settings_list_admin()
{
$sql = "SELECT * FROM ai_settings";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
return $q->result_array();
}
2021-10-30 19:02:07 +02:00
public function get_setting_admin($id)
{
$sql = "SELECT * FROM ai_settings WHERE id='$id'";
$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 edit_setting_admin($data)
{
$sql = "UPDATE ai_settings
2021-10-30 19:02:07 +02:00
SET setting='" . $data['setting'] . "',
value='" . $data['value'] . "',
description='" . $data['description'] . "'
WHERE id='" . $data['id'] . "'";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
public function add_setting_admin($data)
{
$sql = "INSERT INTO ai_settings
2021-10-30 19:02:07 +02:00
VALUES(default,
'" . $data['setting'] . "',
'" . $data['value'] . "' ,
'" . $data['description'] . "')";
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
$this->_create_sql($sql);
}
2021-10-30 19:02:07 +02:00
}
//nowhitesp