Ejemplo n.º 1
0
function combat_commit($pid)
{
    global $combat;
    if (COMBAT_VERBOSE) {
        echo "Kampf wird durchgeführt..." . COMBAT_NEWLINE;
    }
    // Los geht's! Alle Einheiten werden durchlaufen...
    $puid = get_uid_by_pid($pid);
    $paid = get_alliance($puid);
    $ini_loop_qry = $GLOBALS["db"]->query("SELECT id FROM combat WHERE aid<>0 AND initiative >= 0 \nAND killed_by IS NULL AND position <> 'T' AND position <> 'L' ORDER BY initiative DESC, \nRand()") or die($GLOBALS["db"]->error);
    $time_target = 0;
    $time_shoot = 0;
    if (!$ini_loop_qry) {
        return false;
    }
    while ($ini_unit = $ini_loop_qry->fetch_assoc()) {
        $unit =& $combat[$ini_unit["id"]];
        $startcount = $unit["count"];
        for ($group = 1; $group <= $startcount; $group++) {
            if (COMBAT_MAXIMUM_VERBOSITY) {
                $verbose_info_qry = $GLOBALS["db"]->query("SELECT * FROM production WHERE prod_id=" . $unit["prod_id"]) or die($GLOBALS["db"]->error);
                $verbose_info = $verbose_info_qry->fetch_assoc();
                echo "&nbsp;&nbsp;" . $unit["uid"] . "/" . $unit["aid"] . "/" . $unit["initiative"] . "/" . $unit["ecm"] . " " . $verbose_info["name"] . " " . $unit["id"] . " Squad {$group}/{$startcount}: ";
            }
            if ($unit["position"] == "L") {
                if (COMBAT_MAXIMUM_VERBOSITY) {
                    echo "group has landed already!";
                }
                break;
            }
            // Lebe ich überhaupt noch?
            if (is_null($unit["killed_by"])) {
                // Bin ich ein beladener Transporter? Mag ich abladen?
                if ($unit["transport_capacity"] > 0 && $unit["mission"] == M_INVADE && dcount("id", "combat", "on_transport=" . $unit["id"]) > 0) {
                    // Und ist der Planet überhaupt feindlich?? Sind die Schilde schon aus?
                    // Oder darf ich durch Schilde durchfliegen?
                    if (is_enemy($paid, $unit["aid"])) {
                        if (dcount("id", "combat", "count > 0 AND special LIKE 'H%'") == 0 || $unit["special"] == "P") {
                            // Okaydo, los geht's mit Abwehrfeuer
                            if (COMBAT_MAXIMUM_VERBOSITY) {
                                echo "INVADE ";
                            }
                            $query = $GLOBALS["db"]->query("SELECT id FROM combat WHERE special='D' AND count>0 \n  AND counterfire<num_attacks*2") or die($GLOBALS["db"]->error);
                            while (list($d_id) = $query->fetch_row()) {
                                if (COMBAT_MAXIMUM_VERBOSITY) {
                                    echo "counterfire ";
                                }
                                for ($i = $combat[$d_id]["counterfire"]; $i < $combat[$d_id]["num_attacks"] * 2; $i++) {
                                    if (combat_shoot_unit($d_id, $unit["id"], true) == "KILLED") {
                                        break;
                                    }
                                }
                                if ($unit["count"] <= 0) {
                                    break;
                                }
                            }
                            if ($unit["count"] > 0) {
                                $combat["shots_fired"]++;
                                $sth = $GLOBALS["db"]->query("UPDATE combat SET position='U', on_transport=NULL WHERE \n        on_transport=" . $unit["id"] . " AND killed_by IS NULL") or die($GLOBALS["db"]->error);
                                // Transporter gelandet?
                                if ($GLOBALS["db"]->affected_rows > 0) {
                                    $sth = $GLOBALS["db"]->query("UPDATE combat SET position='L' WHERE id=" . $unit["id"]);
                                    $unit["position"] = "L";
                                    if (COMBAT_MAXIMUM_VERBOSITY) {
                                        echo "UNLOAD ";
                                    }
                                }
                            }
                        }
                    }
                }
                // Kann ich denn schießen?
                if ($unit["weaponpower"] > 0 && $unit["num_attacks"] > 0 && $unit["count"] > 0) {
                    // Jau, also schnell ein paar Werte geladen...
                    unset($target_pos_ar);
                    $target_pos = $unit["position"];
                    $target_pos_ar[] = $unit["position"];
                    if ($unit["special"] == "B" && ($unit["mission"] == M_BOMB || $unit["mission"] == M_INVADE)) {
                        $target_pos = "P,O";
                        $target_pos_ar[] = "P";
                    }
                    if ($unit["special"] == "D") {
                        if (COMBAT_MAXIMUM_VERBOSITY) {
                            echo "orbital defense ";
                        }
                        $target_pos = "O";
                        unset($target_pos_ar);
                        $target_pos_ar[] = "O";
                    }
                    // Dann schauen wir doch mal nach einem Primärziel
                    $guns_fired = 0;
                    $living = 1;
                    do {
                        $found_target = false;
                        // Sensoren ausreichend?
                        $sensor_check = mt_rand(0, $unit["sensor"]);
                        $digger_luck = mt_rand(1, 100 + $sensor_check);
                        // Letztes Ziel noch mal?
                        if ($last_target[$unit["aid"]] && $last_target[$unit["aid"]]["ecm"] <= $sensor_check && $last_target[$unit["aid"]]["size_num"] == $unit["target1_num"]) {
                            if (is_null($last_target[$unit["aid"]]["killed_by"]) && $digger_luck > $last_target[$unit["aid"]]["digin_depth"] + $last_target[$unit["aid"]]["digin_bonus"] && in_array($last_target[$unit["aid"]]["position"], $target_pos_ar)) {
                                if (COMBAT_MAXIMUM_VERBOSITY) {
                                    echo "(last trg) ";
                                }
                                $found_target = true;
                                $target =& $combat[$last_target[$unit["aid"]]["id"]];
                            } else {
                                unset($last_target[$unit["aid"]]);
                            }
                        } else {
                            unset($last_target[$unit["aid"]]);
                        }
                        start_timer(1);
                        if (!$found_target) {
                            $target_id = combat_acquire_target($unit, $target_pos, $sensor_check, $digger_luck);
                            if ($target_id) {
                                $target =& $combat[$target_id];
                                $found_target = true;
                                if ($target["aid"] == $unit["aid"]) {
                                    echo "ALERT({$target_id})!!";
                                    combat_validate_index("alert_trigger");
                                }
                            }
                        }
                        $time_target += read_timer(1);
                        if ($found_target) {
                            $last_target[$unit["aid"]] =& $combat[$target["id"]];
                            if (COMBAT_MAXIMUM_VERBOSITY) {
                                $verbose_info_qry = $GLOBALS["db"]->query("SELECT * FROM production WHERE prod_id=" . $target["prod_id"]) or die($GLOBALS["db"]->error);
                                $verbose_info = $verbose_info_qry->fetch_assoc();
                                echo "TARGET(" . $target["uid"] . "/" . $target["aid"] . " " . $verbose_info["name"] . " [" . $target["count"] . "] " . $target["id"] . ") ";
                            }
                            do {
                                $guns_fired++;
                                start_timer(1);
                                $shoot_val = combat_shoot_unit($unit["id"], $target["id"], false);
                                if ($shoot_val == "KILLED") {
                                    $target["counterfire"] = 0;
                                }
                                $time_shoot += read_timer(1);
                                if ($shoot_val != "KILLED" && $target["hull"] > 0 && $target["counterfire"] < $target["num_attacks"] && ($target["position"] == $unit["position"] || $target["position"] != $unit["position"] && $target["special"] == "D")) {
                                    if (COMBAT_MAXIMUM_VERBOSITY) {
                                        echo "counterfire ";
                                    }
                                    $target["counterfire"]++;
                                    start_timer(1);
                                    $shoot_val = combat_shoot_unit($target["id"], $unit["id"], true);
                                    if ($shoot_val == "KILLED") {
                                        $guns_fired = $unit["num_attacks"];
                                    }
                                    $time_shoot += read_timer(1);
                                }
                            } while ($guns_fired < $unit["num_attacks"] && $unit["hull"] > 0 && $target["hull"] > 0);
                            if ($target["hull"] > 0) {
                                $sth = $GLOBALS["db"]->query("UPDATE combat SET counterfire=" . $target["counterfire"] . " WHERE id=" . $target["id"]);
                            } else {
                                unset($last_target[$unit["aid"]]);
                            }
                        } else {
                            if (COMBAT_MAXIMUM_VERBOSITY) {
                                echo "no more targets, ";
                            }
                        }
                    } while ($found_target && $guns_fired < $unit["num_attacks"] && $unit["hull"] > 0);
                    if (COMBAT_MAXIMUM_VERBOSITY) {
                        echo "finished. Totally " . $guns_fired . " guns fired." . COMBAT_NEWLINE;
                    }
                } else {
                    if (COMBAT_MAXIMUM_VERBOSITY) {
                        echo "unarmed!" . COMBAT_NEWLINE;
                    }
                }
            } else {
                if (COMBAT_MAXIMUM_VERBOSITY) {
                    echo "dead already!" . COMBAT_NEWLINE;
                }
            }
        }
    }
    if (COMBAT_VERBOSE) {
        echo "...Kampf beendet! Schießen: " . round($time_shoot, 4) . "s; Zielen: " . round($time_target, 4) . "s" . COMBAT_NEWLINE . COMBAT_NEWLINE;
    }
}
 function get_minimap_values($colors = true, $scanrange = true, $fleets = true)
 {
     global $uid;
     $user_alliance = get_alliance($uid);
     if (!$colors) {
         $scanrange = false;
     }
     $sth = mysql_query("select id, uid from planets where sid=" . $this->id . " and uid != 0");
     if (!$sth) {
         return 0;
     }
     while ($its_planets = mysql_fetch_array($sth)) {
         $its_uid = $its_planets["uid"];
         $its_id = $its_planets["id"];
         // minimap_colors
         if ($colors) {
             if ($its_uid == $uid) {
                 $this->minimap_colors[] = "lime";
                 $this->scan = true;
                 // scanrange
                 if ($scanrange) {
                     $its_scanradius = get_max_scan_range_by_pid($its_id);
                 }
             } elseif (is_allied($its_uid, $uid)) {
                 $this->minimap_colors[] = "yellow";
                 $this->scan = true;
                 // scanrange
                 if ($scanrange) {
                     $its_scanradius = get_max_scan_range_by_pid($its_id);
                 }
             } else {
                 $planet_alliance = get_alliance($its_uid);
                 if ($user_alliance && $planet_alliance && is_friendly($user_alliance, $planet_alliance)) {
                     $this->minimap_colors[] = "orange";
                 } elseif ($user_alliance && $planet_alliance && is_enemy($user_alliance, $planet_alliance)) {
                     $this->minimap_colors[] = "red";
                 } else {
                     $this->minimap_colors[] = "blue";
                 }
             }
         }
         // max scanrange ermitteln
         if ($its_scanradius && $this->scanradius < $its_scanradius) {
             $this->scanradius = $its_scanradius;
         }
     }
     // minimap_fleets
     if ($fleets) {
         $sth = mysql_query("select distinct(uid) as unique_uid from fleet_info where sid=" . $this->id);
         if (!$sth || !mysql_num_rows($sth)) {
             return 0;
         }
         while ($its_fleets = mysql_fetch_array($sth)) {
             $its_uid = $its_fleets["unique_uid"];
             if ($its_uid == $uid) {
                 $this->minimap_fleets[] = "lime";
                 $this->scan = true;
                 // scanrange der Flotten
                 if ($scanrange) {
                     $fleet_scanradius = get_max_fleet_scanrange_by_sid($this->id);
                 }
             } elseif (is_allied($its_uid, $uid)) {
                 $this->minimap_fleets[] = "yellow";
                 $this->scan = true;
                 // scanrange der Flotten
                 if ($scanrange) {
                     $fleet_scanradius = get_max_fleet_scanrange_by_sid($this->id);
                 }
             } else {
                 $fleet_alliance = get_alliance($its_uid);
                 if ($user_alliance && $fleet_alliance && is_friendly($user_alliance, $fleet_alliance)) {
                     $this->minimap_fleets[] = "orange";
                 } elseif ($user_alliance && $fleet_alliance && is_enemy($user_alliance, $fleet_alliance)) {
                     $this->minimap_fleets[] = "red";
                 } else {
                     $this->minimap_fleets[] = "blue";
                 }
             }
             if ($fleet_scanradius && $this->scanradius < $fleet_scanradius) {
                 $this->scanradius = $fleet_scanradius;
             }
         }
         // Scanrange
         if ($scanrange) {
             // Systeme in Scanrange
             $visible_systems = get_systems_in_scanrange($this->id, $this->scanradius);
             if (is_array($visible_systems)) {
                 for ($j = 0; $j < sizeof($visible_systems); $j++) {
                     $this->systems_in_scanrange[] = new MINIMAP_SYSTEM($visible_systems[$j]);
                 }
             }
             for ($j = 0; $j < sizeof($this->systems_in_scanrange); $j++) {
                 $this->systems_in_scanrange[$j]->get_minimap_values(true, false, true);
                 $this->systems_in_scanrange[$j]->make_visible();
             }
         }
     }
     // ende WHILE
     if (isset($this->minimap_colors[0])) {
         $this->minimap_colors = array_unique($this->minimap_colors);
     }
     if (isset($this->minimap_fleets[0])) {
         $this->minimap_fleets = array_unique($this->minimap_fleets);
     }
 }
 function shipbattle($simulation = false, $aarray = null, $darray = null)
 {
     // einfach nur ne simulation?
     if ($simulation) {
         $this->prepare_simulation($aarray, $darray);
     } else {
         // Ersma alle flotten raussuchen, die mit anderen flotten, die einem nich gehören auf einem fleck hocken
         $sth = mysql_query("select f1.fid,f1.pid,f1.sid,f1.uid,f1.mission from fleet_info f1,fleet_info f2 where f1.uid!=f2.uid and f1.pid=f2.pid and f1.sid=f2.sid group by f1.fid order by f1.pid,f1.sid");
         if (!$sth) {
             echo "Database failure!";
         }
         while ($poss_battle = mysql_fetch_array($sth)) {
             // neue pid und sid?
             if ($poss_battle["pid"] != $last_pid && $poss_battle["sid"] != $last_sid) {
                 if (!isset($i)) {
                     $i = 0;
                 } else {
                     $i++;
                 }
                 // neuen container erzeugen
                 $poss_battle_locations[$i] = new possible_battle_container();
                 $poss_battle_locations[$i]->sid = $poss_battle["sid"];
                 $poss_battle_locations[$i]->pid = $poss_battle["pid"];
                 $last_pid = $poss_battle["pid"];
                 $last_sid = $poss_battle["sid"];
             }
             $poss_battle_locations[$i]->fleets[$poss_battle["uid"]][] = array("fid" => $poss_battle["fid"], "mission" => $poss_battle["mission"]);
         }
         // so jetzt die uids pro location vergleichen und schauen ob nen kampf zustande kommt und die battlelocations indizes sichern
         for ($i = 0; $i < sizeof($poss_battle_locations); $i++) {
             $battle_location[$i] = new possible_battle_container();
             $battle_location[$i]->pid = $poss_battle_locations[$i]->pid;
             $battle_location[$i]->sid = $poss_battle_locations[$i]->sid;
             $nachzuegler = array();
             $uids = array_keys($poss_battle_locations[$i]->fleets);
             $uids = array_flip($uids);
             $owner = get_uid_by_pid($poss_battle_locations[$i]->pid);
             $owner_alliance = get_alliance($owner);
             while (list($uid, $dummy) = each($poss_battle_locations[$i]->fleets)) {
                 $uids_temp = $uids;
                 unset($uids_temp[$uid]);
                 reset($uids_temp);
                 while (list($second_uid, $dummy) = each($uids_temp)) {
                     $alliance = get_alliance($uid);
                     $sec_alliance = get_alliance($second_uid);
                     if (is_enemy($alliance, $sec_alliance)) {
                         $battle_location[$i]->fleets[$uid] = $poss_battle_locations[$i]->fleets[$uid];
                         $battle_location[$i]->fleets[$second_uid] = $poss_battle_locations[$i]->fleets[$second_uid];
                         unset($poss_battle_locations[$i]->fleets[$second_uid]);
                         echo "BLA\n";
                     } elseif (is_allied($alliance, $sec_alliance) || is_friendly($alliance, $sec_alliance)) {
                         $nachzuegler[] = $poss_battle_locations[$i]->fleet[$second_uid];
                         unset($poss_battle_locations[$i]->fleets[$second_uid]);
                     } elseif (is_enemy($alliance, $owner_alliance)) {
                         for ($j = 0; $j < sizeof($poss_battle_locations[$i]->fleets[$uid]); $j++) {
                             if ($poss_battle_locations[$i]->fleets[$uid][$j]["mission"] == 3 || $poss_battle_locations[$i]->fleets[$uid][$j]["mission"] == 5) {
                                 // invading oder bombarding? ziemlicher gayer hack :S
                                 $battle_location[$i]->fleets[$uid] = $poss_battle_locations[$i]->fleets[$uid];
                             }
                         }
                     } else {
                         // neutrale müssen nochmal gesondert betrachtet werden deswegen kein unset
                     }
                 }
             }
             // mop: wenn es ne freund/feind situation irgendwie gibt, dann müssen die nachzuegler mit rein
             if (is_array($battle_location[$i]->fleets)) {
                 for ($j = 0; $j < sizeof($nachzuegler); $j++) {
                     $battle_location[$i]->fleets = array_merge($battle_location[$i]->fleets, $nachzuegler[$j]);
                 }
                 echo "BLA2\n";
             }
         }
         echo "BLA3\n";
         var_dump($battle_location);
         // jetzt haben wir die battlelocations...nun wirds interessant...wir müssen die ganze hundescheisse jetzt nach defenders und
         // attackers umdröseln
         for ($i = 0; $i < sizeof($battle_location); $i++) {
             echo "=> {$i}\n";
             $this->a_uids = array();
             $this->d_uids = array();
             $this->attackers = array();
             $this->defenders = array();
             $this->report = new battlereport();
             $this->report->pid = $battle_location[$i]->pid;
             $this->report->sid = $battle_location[$i]->sid;
             if (is_array($battle_location[$i]->fleets)) {
                 echo "JO\n";
                 // dann muss es da auch irgendwas zum kampftrollen geben
                 if ($battle_location[$i]->pid == 0) {
                     echo "JO=>2\n";
                     if ($intercepting = $battle_location[$i]->find_intercepting_uid()) {
                         $this->d_uids[] = $intercepting;
                         for ($j = 0; $j < sizeof($battle_location[$i]->fleets[$intercepting]); $j++) {
                             $battlefleet = new battlefleet($battle_location[$i]->fleets[$intercepting][$j]["fid"], 0);
                             $this->report->add_fleet($battlefleet);
                             $this->defenders[] = $battlefleet;
                         }
                         $d_alliance = get_alliance($intercepting);
                         // verteidigende allianz sichern
                         reset($battle_location[$i]->fleets);
                         while (list($uid, $fleet) = each($battle_location[$i]->fleets)) {
                             if ($uid != $this->d_uids[0]) {
                                 if (is_enemy($d_alliance, get_alliance($uid))) {
                                     if (!in_array($uid, $this->a_uids)) {
                                         $this->a_uids[] = $uid;
                                         for ($j = 0; $j < sizeof($fleet); $j++) {
                                             $battlefleet = new battlefleet($fleet[$j]["fid"], 1);
                                             $this->report->add_fleet($battlefleet);
                                             $this->attackers[] = $battlefleet;
                                         }
                                     }
                                 } elseif (is_friendly($d_alliance, get_alliance($uid)) || is_allied($d_alliance, get_alliance($uid))) {
                                     if (!in_array($uid, $this->d_uids)) {
                                         $this->d_uids[] = $uid;
                                         for ($j = 0; $j < sizeof($fleet); $j++) {
                                             $battlefleet = new battlefleet($fleet[$j]["fid"], 0);
                                             $this->report->add_fleet($battlefleet);
                                             $this->defenders[] = $battlefleet;
                                         }
                                     }
                                 } else {
                                     echo "Neutral Player detected :S\n";
                                 }
                             }
                         }
                     } else {
                         // :S
                     }
                 } else {
                     echo "JO2\n";
                     $owner = get_uid_by_pid($battle_location[$i]->pid);
                     $planet_active = false;
                     if (in_array($owner, array_keys($battle_location[$i]->fleets))) {
                         $this->d_uids[] = $owner;
                     } else {
                         $owner = array_rand($battle_location[$i]->fleets);
                         $this->d_uids[] = $owner;
                         echo "Owner ist => " . $owner . "\n";
                     }
                     for ($j = 0; $j < sizeof($battle_location[$i]->fleets[$owner]); $j++) {
                         $battlefleet = new battlefleet($battle_location[$i]->fleets[$owner][$j]["fid"], 0);
                         $this->report->add_fleet($battlefleet);
                         $this->defenders[] = $battlefleet;
                     }
                     $d_alliance = get_alliance($owner);
                     // verteidigende allianz sichern
                     reset($battle_location[$i]->fleets);
                     while (list($uid, $fleet) = each($battle_location[$i]->fleets)) {
                         if ($uid != $this->d_uids[0]) {
                             if (is_enemy($d_alliance, get_alliance($uid))) {
                                 if (!in_array($uid, $this->a_uids)) {
                                     $this->a_uids[] = $uid;
                                     for ($j = 0; $j < sizeof($fleet); $j++) {
                                         $battlefleet = new battlefleet($fleet[$j]["fid"], 1);
                                         if ($battlefleet->mission == 3) {
                                             $planet_active = true;
                                             $planet_attack_type = 0;
                                         } elseif ($battlefleet->mission == 5) {
                                             $planet_active = true;
                                             $planet_attack_type = 1;
                                         }
                                         $this->report->add_fleet($battlefleet);
                                         $this->attackers[] = $battlefleet;
                                     }
                                 }
                             } elseif (is_friendly($d_alliance, get_alliance($uid)) || is_allied($d_alliance, get_alliance($uid))) {
                                 if (!in_array($uid, $this->d_uids)) {
                                     $this->d_uids[] = $uid;
                                     for ($j = 0; $j < sizeof($fleet); $j++) {
                                         $battlefleet = new battlefleet($fleet[$j]["fid"], 0);
                                         $this->report->add_fleet($battlefleet);
                                         $this->defenders[] = $battlefleet;
                                     }
                                 }
                             } else {
                                 echo "Neutral Player detected :S\n";
                             }
                         }
                     }
                 }
                 if ($planet_active) {
                     // d_uids wurde schon vorher hinzugfügt
                     $battleplanet = new battleplanet($battle_location[0]->pid, $planet_attack_type);
                     $this->report->add_planet($battleplanet);
                     $this->defenders[] = $battleplanet;
                 }
             }
             if (sizeof($this->a_uids) > 0 && sizeof($this->d_uids) > 0) {
                 $this->attackers_count = sizeof($this->attackers);
                 $this->defenders_count = sizeof($this->defenders);
                 $this->prepare_battle();
                 $this->do_battle();
                 $this->destroy_ships();
                 $this->report->do_report();
             }
         }
     }
 }
Ejemplo n.º 4
0
function get_color_by_alliancestatus($aid1, $aid2)
{
    $color = "black";
    if ($aid1 == $aid2) {
        $color = "yellow";
    } elseif (is_enemy($aid1, $aid2)) {
        $color = "red";
    } elseif (is_friendly($aid1, $aid2)) {
        $color = "orange";
    } else {
        $color = "blue";
    }
    return $color;
}
Ejemplo n.º 5
0
function get_uids_relation($uid1, $uid2, $type = 0)
{
    if ($uid1 == $uid2) {
        $type == 0 ? $relation = "same" : ($relation = "colorOwn");
    } elseif (is_allied($uid1, $uid2)) {
        $type == 0 ? $relation = "allie" : ($relation = "colorAllied");
    } else {
        $alliance1 = get_alliance($uid1);
        $alliance2 = get_alliance($uid2);
        if ($alliance1 && $alliance2) {
            if (is_friendly($alliance1, $alliance2)) {
                $type == 0 ? $relation = "friend" : ($relation = "colorFriend");
            } elseif (is_enemy($alliance1, $alliance2)) {
                $type == 0 ? $relation = "enemy" : ($relation = "colorEnemy");
            } else {
                $type == 0 ? $relation = "neutral" : ($relation = "colorNeutral");
            }
        } else {
            $type == 0 ? $relation = "neutral" : ($relation = "colorNeutral");
        }
    }
    return $relation;
}