<?php

class InputParser {

    private $owner;
    private $inputfileName;
    private $planOutputFileName;
    private $outputfileName;
    private $inputfile;
    private $outputfile;
    private $planOutputFile;
    private $databaseName;
    private $databaseAdress;
    private $username;
    private $userpassword;
    private $lastobject;
    private $abilitycost = array();
    private $ability = array();
    private $activated;
    private $type = array();
    private $subtype = array();
    private $supertype = array();

    public function seismic_assault($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $ability = "seismic_assault_deals_2_damage_to_target_creature_or_player";

        $str = "caused damage_dealt(Occurence, TR, normal, 2) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(TR, Occurence, Number). \n";
        fwrite($plan_outputfile, $str);

        $type = "creature";
        $str = "caused type_match_conditions(Occurence, " . $type . ") if\n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(Occurence, Number).\n";
        fwrite($plan_outputfile, $str);

        $str = "caused choose_criteria(Occurence, " . $type . ") if\n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(Occurence, Number).\n";
        fwrite($plan_outputfile, $str);

        $type = "player";
        $str = "caused choose_criteria(Occurrence, " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(Occurrence , Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused discard_criteria(Occurence, land) if\n";
        fwrite($plan_outputfile, $str);
        $str = "    discarding_cards(PL),\n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence," . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choosing_targets(Occurrence, 1) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_activating_phase(PL, c, Occurrence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    controller_owner(PL," . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not current_activating_phase(PL, c, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -other_partial_cost(Occurence, " . $cost_counter . ", discard_a_land) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    card_type(Crd, land) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    discard_card(PL, Crd), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_activating_phase(PL, g, Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    other_partial_cost(Occurence, " . $cost_counter . ", discard_a_land). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused discarding_cards(PL) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_activating_phase(PL, g, Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    other_partial_cost(Occurence, Cost_counter, discard_a_land). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused other_partial_cost(Occurrence, " . $cost_counter . ", discard_a_land) if\n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence, " . $last_object . "),\n";
        fwrite($plan_outputfile, $str);
        $str = "    current_activating_phase(PL, e, Obj),\n";
        fwrite($plan_outputfile, $str);
        $str = "    activating_ability(PL, Obj).\n";
        fwrite($plan_outputfile, $str);

        $str = "no_ability_modality(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $str = "no_ability_division(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $str = "no_casting_cost($last_object). \n";
        fwrite($dl_outputfile, $str);

        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function lightning_elemental($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "haste(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $cost_counter++;
        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function goblin_raider($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "cannot_block(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function goblin_matron($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $ability = "when_goblin_matron_enters_the_battlefield_comma_you_may_search_your_library_for_a_goblin_card_comma_reveal_that_card_comma_and_put_it_into_your_hand_dot_if_you_do_comma_shuffle_your_library";

        $str = "caused triggered_ability(" . $ability . ", Occurrence," . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    entered_battlefield(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused trigger_condition_satisfied(" . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    entered_battlefield(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    entered_battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_trigger_modality(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $str = "no_trigger_division(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);

        $str = "no_targeting(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $str = "no_modality(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function goblin_sky_raider($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function raging_goblin($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "haste(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);

        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function spitting_earth($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "caused damage_dealt(" . $last_object . ", TR, normal, Amount) if\n";
        fwrite($plan_outputfile, $str);
        $str = "    object_controller(Obj, PL),\n";
        fwrite($plan_outputfile, $str);
        $str = "    number_of_mountains_in_control(PL, Amount),\n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(TR," . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_modality(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . ").\n";
        fwrite($dl_outputfile, $str);
        $this->planDefaultTargetType($last_object, $plan_outputfile, "creature");
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function volcanic_hammer($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "caused damage_dealt(" . $last_object . ", TR, normal, 3) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(TR," . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $type = "creature";
        $str = "caused type_match_conditions(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choose_criteria(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $type = "player";
        $str = "caused choose_criteria(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choosing_targets(" . $last_object . ", 1) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_casting_phase(PL, c, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    controller_owner(PL," . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not current_casting_phase(PL, c," . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function shock($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $str = "caused damage_dealt(" . $last_object . ", TR, normal, 2) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(TR," . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $type = "creature";
        $str = "caused type_match_conditions(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choose_criteria(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $type = "player";
        $str = "caused choose_criteria(" . $last_object . ", " . $type . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choosing_targets(" . $last_object . ", 1) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_casting_phase(PL, c, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    controller_owner(PL," . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not current_casting_phase(PL, c," . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function mountain($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->planTapAbility("add_one_red_mana_to_your_mana_pool", $cost_counter, $plan_outputfile, $last_object);
        $str = "caused mana_pool(PL,U,G,B,W,R,C) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_controller(Occurence, PL), \n";
        fwrite($plan_outputfile, $str);
        $str = "    resolve_mana_ability(Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(add_one_red_mana_to_your_mana_pool, Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    R = R2+1 after \n";
        fwrite($plan_outputfile, $str);
        $str = "    mana_pool(PL,U,G,B,W,R2,C). \n";
        fwrite($plan_outputfile, $str);

        $this->dlTapAbility("add_one_red_mana_to_your_mana_pool", $cost_counter, $dl_outputfile, $last_object);
        $str = "mana_ability(add_one_red_mana_to_your_mana_pool, " . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $this->dlDefaultActivate($last_object, $dl_outputfile);

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function goblin_king($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $ability = "other_goblin_creatures_get_pos1_pos1_and_have_mountainwalk";
        $str = "caused static_ability(" . $ability . ", Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -static_ability(" . $ability . ", Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $this->dlDefaultResolve($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function wild_growth($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        // $ability = "whenever_enchanted_land_is_tapped_for_mana_comma_its_controller_adds_G_to_his_or_her_mana_pool";

        $str = "caused triggered_ability(add_one_green_mana, Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    trigger_condition_satisfied(" . $last_object . ").\n";
        fwrite($plan_outputfile, $str);

        $str = "caused mana_pool_guard(PL) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    triggered_ability(add_one_green_mana, Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    aura_attached_to(" . $last_object . ", Target), \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_controller(Target, PL). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -triggered_ability(add_one_green_mana, Occurence, " . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    triggered_ability(add_one_green_mana, Occurence, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -trigger_condition_satisfied(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    trigger_condition_satisfied(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused trigger_condition_satisfied(" . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_mana_ability(Ability, Occurrence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    aura_attached_to(" . $last_object . ", Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(Ability, Occurrence, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    tapping_source_during_activating(Ability, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    end_of_activating_ability(PL, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_mana_ability(Ability, Occurrence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    aura_attached_to(" . $last_object . ", Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(Ability, Occurrence, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    tapping_source_during_activating(Ability, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    end_of_activating_ability(PL, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused mana_pool(PL,U,G,B,W,R,C) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_controller(Target, PL), \n";
        fwrite($plan_outputfile, $str);
        $str = "    trigger_condition_satisfied(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_no_trigger_choices(Occurrence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_mana_ability(add_one_green_mana, Occurrence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    triggered_ability(add_one_green_mana, Occurrence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    G = G2+Number,  \n";
        fwrite($plan_outputfile, $str);
        fwrite($plan_outputfile, $str);
        $str = "    wild_growths_on_object(Target, Number),  \n";
        fwrite($plan_outputfile, $str);
        $str = "    aura_attached_to(" . $last_object . ", Target),  \n";
        fwrite($plan_outputfile, $str);
        $str = "    mana_pool(PL,U,G2,B,W,R,C). \n";
        fwrite($plan_outputfile, $str);

        //targetovanie
        $this->planDefaultTargetType($last_object, $plan_outputfile, "land");

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "ability(add_one_green_mana). \n";
        fwrite($dl_outputfile, $str);
        $str = "mana_ability(add_one_green_mana, " . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_trigger_choices(" . $last_object . ").";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $this->default_triggering($last_object, $dl_outputfile);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function default_triggering($last_object, $dl_outputfile) {
        $str = "no_trigger_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_trigger_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_trigger_targeting(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
    }

    public function elvish_champion($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $ability = "other_elf_creatures_get_pos1_pos1_and_have_forrestwalk";
        $str = "caused static_ability(" . $ability . ", Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -static_ability(" . $ability . ", Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    battlefield(" . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not battlefield(" . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $this->dlDefaultResolve($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function norwood_ranger($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->dlDefaultCasting($last_object, $dl_outputfile);
        $this->dlDefaultResolve($last_object, $dl_outputfile);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function regenerate($last_object, $plan_outputfile) {
        $str = "caused -damage_marked(" . $last_object . ", Amount) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_toughness(" . $last_object . ",Amount2), \n";
        fwrite($plan_outputfile, $str);
        $str = "    Amount>Amount2, \n";
        fwrite($plan_outputfile, $str);
        $str = "    prevention_effect(" . $last_object . ", regenerate), \n";
        fwrite($plan_outputfile, $str);
        $str = "    damage_marked(" . $last_object . ", Amount). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -prevention_effect(" . $last_object . ", regenerate) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_toughness(" . $last_object . ",Amount2), \n";
        fwrite($plan_outputfile, $str);
        $str = "    Amount>Amount2, \n";
        fwrite($plan_outputfile, $str);
        $str = "    prevention_effect(" . $last_object . ", regenerate), \n";
        fwrite($plan_outputfile, $str);
        $str = "    damage_marked(" . $last_object . ", Amount). \n";
        fwrite($plan_outputfile, $str);
    }

    public function ancient_silverback($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        //the tap cost of the ability
        $ability = "regenerate";
        //the pay green mana cost of the ability
        $str = "caused -other_partial_cost(Occurence, " . $cost_counter . ", pay_green_mana) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    total_mana_cost(Occurence,B,U,W,R,G,C) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    other_partial_cost(Occurence, " . $cost_counter . ", pay_green_mana). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused object_partial_mana_cost( Occurence," . $cost_counter . ",0,1,0,0,0,0) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurence, " . $last_object . ") after \n";
        fwrite($plan_outputfile, $str);
        $str = "    activate_ability(PL, " . $ability . ", " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);
        //ability_specification
        //resolve_process
        $str = "caused end_of_resolve_phase(PL, c, Occurence) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(regenerate, Occurence," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    controller(PL, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $this->dlDefaultActivate($last_object, $dl_outputfile);
        $this->dlDefaultResolve($last_object, $dl_outputfile);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
        //$this->dlDefaultCasting($last_object, $dl_outputfile);
    }

    public function might_of_oaks($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->planDefaultTargetType($last_object, $plan_outputfile, "creature");
        $str = "caused static_ability(target_creature_gets_pos7_pos7_until_end_of_turn, Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurence),";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused end_of_resolve_phase(PL, c, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -static_ability(target_creature_gets_pos7_pos7_until_end_of_turn, Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    end_of_turn after \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(target_creature_gets_pos7_pos7_until_end_of_turn, Occurence, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused target_of_static_ability(target_creature_gets_pos7_pos7_until_end_of_turn, Occurence, Target) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(target_creature_gets_pos7_pos7_until_end_of_turn, Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function giant_growth($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->planDefaultTargetType($last_object, $plan_outputfile, "creature");
        $str = "caused static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    last_object(Occurence), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused create_object if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused end_of_resolve_phase(PL, c, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    end_of_turn after \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused target_of_static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, Target) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_resolving(" . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target," . $last_object . ", Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, " . $last_object . "). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused increment_power_toughness(Target, 3, " . $last_object . ") if \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_static_phase(b),  \n";
        fwrite($plan_outputfile, $str);
        $str = "    not increment_guard after \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_of_static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, Target), \n";
        fwrite($plan_outputfile, $str);
        $str = "    static_ability(target_creature_gets_pos3_pos3_until_end_of_turn, Occurence, " . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    object_power(Target, Amount). \n";
        fwrite($plan_outputfile, $str);

        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function elvish_piper($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        //the tap cost of the ability
        $ability = "you_may_put_a_creature_card_from_your_hand_onto_the_battlefield";
        $this->dlTapAbility($ability, $cost_counter, $dl_outputfile, $last_object);
        $this->planTapAbility($ability, $cost_counter, $plan_outputfile, $last_object);
        $cost_counter++;
        //the pay green mana cost of the ability
        $str = "caused -other_partial_cost(Occurence, CC, pay_green_mana) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    total_mana_cost(Occurence,B,U,W,R,G,C) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    other_partial_cost(Occurence, CC, pay_green_mana). \n";
        fwrite($plan_outputfile, $str);

        //the target specification of the ability
        $str = "caused choose_criteria(Source, creature_card_in_hand) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    choosing_targets(Occurrence, Number). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused choosing_targets(Occurrence, 1) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence, Source), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_activating_phase(PL, c, Occurrence) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    not current_activating_phase(PL, c, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        //resolve action of the ability
        $str = "caused battlefield(Target) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target, Occurrence, Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused -hand(PL, Target) after \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target, Occurrence, Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        $str = "caused object_controller(Target, PL) if \n";
        fwrite($plan_outputfile, $str);
        $str = "    activated_ability(" . $ability . ", Occurrence," . $last_object . "), \n";
        fwrite($plan_outputfile, $str);
        $str = "    target_chosen(Target, Occurrence, Number), \n";
        fwrite($plan_outputfile, $str);
        $str = "    current_resolve_phase(PL, c, Occurrence). \n";
        fwrite($plan_outputfile, $str);

        //casting pararmeters of the creature
        $str = "partial_ability_cost(" . $ability . "," . $last_object . ", " . $cost_counter . ", pay_green_mana). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_targeting(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);

        $str = "no_ability_modality(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);
        $str = "no_ability_division(" . $last_object . "). \n";
        fwrite($dl_outputfile, $str);

        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function elvish_archers($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->dlDefaultResolve($last_object, $dl_outputfile);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function forrest($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->planForrest($cost_counter, $plan_outputfile, $last_object);
        $this->dlForrest($cost_counter, $dl_outputfile, $last_object);
        $cost_counter++;
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function planForrest($cost_counter, $file_handle, $last_object) {
    }

    public function dlForrest($cost_counter, $file_handle, $last_object) {
        $this->dlTapAbility("add_one_green_mana_to_your_mana_pool", $cost_counter, $file_handle, $last_object);
        $str = "mana_ability(add_one_green_mana_to_your_mana_pool, " . $last_object . "). \n";
        fwrite($file_handle, $str);
        $this->dlDefaultActivate($last_object, $file_handle);
    }

    public function planDefaultTargetType($last_object, $file_handle, $type) {
        $str = "caused type_match_conditions(" . $last_object . ", " . $type . ") if \n";
        fwrite($file_handle, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($file_handle, $str);

        $str = "caused choose_criteria(" . $last_object . ", " . $type . ") if \n";
        fwrite($file_handle, $str);
        $str = "    choosing_targets(" . $last_object . ", Number). \n";
        fwrite($file_handle, $str);

        $str = "caused choosing_targets(" . $last_object . ", 1) if \n";
        fwrite($file_handle, $str);
        $str = "    current_casting_phase(PL, c, " . $last_object . "), \n";
        fwrite($file_handle, $str);
        $str = "    controller_owner(PL," . $last_object . ") after \n";
        fwrite($file_handle, $str);
        $str = "    not current_casting_phase(PL, c," . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function blanchwood_armor($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->dlBlanchwood_armor($last_object, $dl_outputfile);
        $this->planBlanchwood_armor($plan_outputfile, $last_object);
        $this->dlDefaultCastingParts($last_object, $cost_counter, $dl_outputfile);
    }

    public function dlBlanchwood_armor($last_object, $file_handle) {
        $str = "no_modality(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function planBlanchwood_armor($file_handle, $last_object) {
        $str = "caused static_ability(enchanted_creature_gets_pos1_pos1_for_each_forrest_you_control, Occurence, " . $last_object . ") if \n";
        fwrite($file_handle, $str);
        $str = "    aura_attached_to(" . $last_object . ", Target), \n";
        fwrite($file_handle, $str);
        $str = "    last_object(Occurence) after \n";
        fwrite($file_handle, $str);
        $str = "    not aura_attached_to(" . $last_object . ", Target). \n";
        fwrite($file_handle, $str);


        $str = "caused create_object if \n";
        fwrite($file_handle, $str);
        $str = "    aura_attached_to(" . $last_object . ", Target) after not \n";
        fwrite($file_handle, $str);
        $str = "    aura_attached_to(" . $last_object . ", Target). \n";
        fwrite($file_handle, $str);

        $this->planDefaultTargetType($last_object, $file_handle, "creature");
    }

    public function birds_of_paradise($cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        $this->planBirdsOfParadise($cost_counter, $plan_outputfile, $last_object);
        $this->dlBirdsOfParadise($cost_counter, $dl_outputfile, $last_object);
        $cost_counter++;
    }

    public function dlDefaultCastingParts($last_object, $cost_counter, $file_handle) {
        fwrite($file_handle, "partial_cost_counter(" . $last_object . ", " . $cost_counter . ").\n");
    }

    public function planBirdsOfParadise($cost_counter, $file_handle, $last_object) {
        $this->planTapAbility("add_one_mana_of_any_color_to_your_mana_pool", $cost_counter, $file_handle, $last_object);
        $str = "caused mana_pool(PL,U,G,B,W,R,C) if \n";
        fwrite($file_handle, $str);
        $str = "    object_controller(Occurence, PL ), \n";
        fwrite($file_handle, $str);
        $str = "    resolve_mana_ability(Occurence), \n";
        fwrite($file_handle, $str);
        $str = "    activated_ability(add_one_mana_of_any_color_to_your_mana_pool, Occurence, " . $last_object . "), \n";
        fwrite($file_handle, $str);
        $str = "    G = G2+1 after \n";
        fwrite($file_handle, $str);
        $str = "    mana_pool(PL,U,G2,B,W,R,C). \n";
        fwrite($file_handle, $str);
    }

    public function dlBirdsOfParadise($cost_counter, $file_handle, $last_object) {
        $this->dlTapAbility("add_one_mana_of_any_color_to_your_mana_pool", $cost_counter, $file_handle, $last_object);
        $str = "mana_ability(add_one_mana_of_any_color_to_your_mana_pool, " . $last_object . ").\n";
        fwrite($file_handle, $str);
        $this->dlDefaultResolve($last_object, $file_handle);
        $this->dlDefaultActivate($last_object, $file_handle);
        $this->dlDefaultCasting($last_object, $file_handle);
    }

    public function dlDefaultCasting($last_object, $file_handle) {
        $str = "no_targeting(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_modality(" . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function dlDefaultActivate($last_object, $file_handle) {
        $str = "no_ability_targeting(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_ability_division(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_ability_modality(" . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function dlDefaultResolve($last_object, $file_handle) {
        $str = "no_modality(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_division(" . $last_object . "). \n";
        fwrite($file_handle, $str);
        $str = "no_targeting(" . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function planTapAbility($ability, $cost_counter, $file_handle, $last_object) {
        $str = "caused cost_paid(PL,Occurence, " . $cost_counter . ") if \n";
        fwrite($file_handle, $str);
        $str = "    activated_ability(" . $ability . ", Occurence, " . $last_object . "), \n";
        fwrite($file_handle, $str);
        $str = "    current_activating_phase(PL, g, Occurence), \n";
        fwrite($file_handle, $str);
        $str = "    not in_queue(Occurence), \n";
        fwrite($file_handle, $str);
        
        $str = "    tapped(" . $last_object . ") after \n";
        fwrite($file_handle, $str);
        $str = "    not tapped(" . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function dlTapAbility($ability, $cost_counter, $file_handle, $last_object) {
        $str = "partial_ability_cost(" . $ability . ", " . $last_object . ", " . $cost_counter . ", tap). \n";
        fwrite($file_handle, $str);
        $str = "tapping_source_during_activating(" . $ability . ", " . $last_object . "). \n";
        fwrite($file_handle, $str);
    }

    public function getAbilitycost($position) {
        if ($this->abilitycost[$position]) {
            return $this->abilitycost[$position];
        }
    }

    public function setAbilitycost($position, $value) {
        $this->abilitycost[$position] = $value;
    }

    public function addAbilitycost($value) {
        $this->abilitycost[count($this->abilitycost)] = $value;
    }

    public function removeAbilitycost($position) {
        unset($this->abilitycost[$position]);
    }

    public function abilitycostSize() {
        return count($this->abilitycost);
    }

    public function resetAbilitycost() {
        $this->abilitycost = array();
    }

    public function dumpAbilityCostIntoFile($outputfile, $last_object) {
        if ($this->getAbilitycost($this->abilitycostSize() - 1) != "") {
            fwrite($outputfile, "card_ability(" . $last_object . "," . $this->getAbilitycost($this->abilitycostSize() - 1) . "). \n");
        }
        $this->resetAbilitycost();
    }

    public function getType($position) {
        if ($this->type[$position]) {
            return $this->type[$position];
        }
    }

    public function setType($position, $value) {
        $this->type[$position] = $value;
    }

    public function addType($value) {
        if (!in_array($value, $this->type)) {
            $this->type[count($this->type)] = $value;
        }
    }

    public function removeType($position) {
        unset($this->type[$position]);
    }

    public function typeSize() {
        return count($this->type);
    }

    public function dumpTypeIntoFile($outputfile) {
        foreach ($this->type as $curr_type) {
            fwrite($outputfile, "type($curr_type). \n");
        }
        $this->resetType();
    }

    public function resetType() {
        $this->type = array();
    }

    public function getSubtype($position) {
        if ($this->subtype[$position]) {
            return $this->subtype[$position];
        }
    }

    public function setSubype($position, $value) {
        $this->subtype[$position] = $value;
    }

    public function addSubtype($value) {
        if (!in_array($value, $this->subtype)) {
            $this->subtype[count($this->subtype)] = $value;
        }
    }

    public function removeSubtype($position) {
        unset($this->subtype[$position]);
    }

    public function subtypeSize() {
        return count($this->subtype);
    }

    public function dumpSubtypeIntoFile($outputfile) {
        foreach ($this->subtype as $curr_subtype) {
            fwrite($outputfile, "subtype($curr_subtype). \n");
        }
        $this->resetSubtype();
    }

    public function resetSubtype() {
        $this->subtype = array();
    }

    public function getSupertype($position) {
        if ($this->supertype[$position]) {
            return $this->supertype[$position];
        }
    }

    public function setSupertype($position, $value) {
        $this->supertype[$position] = $value;
    }

    public function addSupertype($value) {
        if (!in_array($value, $this->supertype)) {
            $this->supertype[count($this->supertype)] = $value;
        }
    }

    public function removeSupertype($position) {
        unset($this->supertype[$position]);
    }

    public function supertypeSize() {
        return count($this->supertype);
    }

    public function dumpSupertypeIntoFile($outputfile) {
        foreach ($this->supertype as $curr_supertype) {
            fwrite($outputfile, "supertype($curr_supertype). \n");
        }
        $this->resetSupertype();
    }

    public function resetSupertype() {
        $this->supertype = array();
    }

    public function getAbility($position) {
        if ($this->abilitiy[$position]) {
            return $this->ability[$position];
        }
    }

    public function setAbility($position, $value) {
        $this->ability[$position] = $value;
    }

    public function addAbility($value) {
        if (!in_array($value, $this->ability)) {
            $this->ability[count($this->ability)] = $value;
        }
    }

    public function removeAbility($position) {
        unset($this->ability[$position]);
    }

    public function flushAbility() {
        unset($this->ability);
    }

    public function abilitySize() {
        return count($this->ability);
    }

    public function dumpAbilityIntoFile($outputfile) {
        foreach ($this->ability as $curr_ability) {
            if ($curr_ability != "") {
                fwrite($outputfile, "ability($curr_ability). \n");
            }
        }
        $this->resetAbility();
    }

    public function resetAbility() {
        $this->ability = array();
    }

    public function getLastObject() {
        return $this->lastobject;
    }

    public function setLastObject($value) {
        $this->lastobject = $value;
    }

    public function resetLastObject() {
        $this->lastobject = 1;
    }

    public function incrementLastObject() {
        $this->lastobject++;
    }

    public function getDatabaseName() {
        return $this->databaseName;
    }

    public function setDatabaseName($value) {
        $this->databaseName = $value;
    }

    public function getDatabaseAdress() {
        return $this->databaseAdress;
    }

    public function setDatabaseAdress($value) {
        $this->databaseAdress = $value;
    }

    public function getUserName() {
        return $this->username;
    }

    public function setUserName($value) {
        $this->username = $value;
    }

    public function getUserPassword() {
        return $this->userpassword;
    }

    public function setUserPassword($value) {
        $this->userpassword = $value;
    }

    public function getOwner() {
        return $this->owner;
    }

    public function setOwner($value) {
        $this->owner = $value;
    }

    public function getInputfileName() {
        return $this->inputfileName;
    }

    public function setInputfileName($value) {
        $this->inputfileName = $value;
    }

    public function getOutputfileName() {
        return $this->outputfileName;
    }

    public function setOutputfileName($value) {
        $this->outputfileName = $value;
    }

    public function openInputfile() {
        if ($this->inputfileName) {
            $this->inputfile = fopen($this->inputfileName, 'r') or die("cannot open $this->inputfileName");
        }
    }

    public function closeInputfile() {
        fclose($this->inputfile);
    }

    public function openOutputfile() {
        if ($this->outputfileName) {
            $this->outputfile = fopen($this->outputfileName, 'w') or die("cannot open $this->outputfileName");
        }
    }

    public function openPlanOutputfile() {
        if ($this->planOutputFileName) {
            $this->planOutputFile = fopen($this->planOutputFileName, 'w') or die("cannot open $this->planOutputFileName");
        }
    }

    public function closeOutputfile() {
        fclose($this->outputfile);
    }

    public function closePlanOutputFile() {
        fclose($this->planOutputFile);
    }

    public function abilityParser($ability) {
        //returns an array with the casting costs in the respective order and at the end the ability's name
        $start = strpos($ability, "{");
        $end = strpos($ability, "}");
        while ($end != FALSE) {
            $abcost = substr($ability, $start + 1, $end - 1);
            $this->addAbilitycost($abcost);
            $ability = substr($ability, $end);
            $start = strpos($ability, "{");
            $end = strpos($ability, "}");
        }
        $end = strpos($ability, ":");
        if ($end) {
            $this->activated = true;
            $value = substr($ability, $end + 1);
        } else {
            $this->activated = false;
            $value = $ability;
        }
        $this->addAbilitycost($value);
        $this->addAbility($value);
    }

    public function cardManaCostParser($inputStr) {
        $mana_cost = array(0, 0, 0, 0, 0, 0);
        $char = $inputStr[0];
        if (strlen($inputStr) > 1) {
            $lookahead = $inputStr[1];
        } else {
            $lookahead = "";
        }
        $chcounter = 0;
        $lcounter = 1;
        while (strlen($inputStr) != $chcounter) {
            if ((is_numeric($char)) && (is_numeric($lookahead))) {
                $char = $char . $lookahead;
                $mana_cost[5] = $mana_cost[5] + $char;
                $chcounter+=2;
                $lcounter+=2;
                $char = $inputStr[$chcounter];
                if ($lcounter <= strlen($inputStr)) {
                    $lookahead = $inputStr[$lcounter];
                }
            }
            if ((is_numeric($char) && (!is_numeric($lookahead)))) {
                $mana_cost[5] = $mana_cost[5] + $char;
                $char = $lookahead;
                if ($lcounter < strlen($inputStr)) {
                    $lookahead = $inputStr[$lcounter++];
                }
                $chcounter++;
            }
            switch ($char) {
                case "U":
                    $mana_cost[0]+=1;
                    $char = $lookahead;
                    if ($lcounter < strlen($inputStr)) {
                        $lookahead = $inputStr[$lcounter++];
                    }
                    $chcounter++;
                    break;
                case "G":
                    $mana_cost[1]+=1;
                    $char = $lookahead;
                    if ($lcounter < strlen($inputStr)) {
                        $lookahead = $inputStr[$lcounter++];
                    }
                    $chcounter++;
                    break;
                case "B":
                    $mana_cost[2]+=1;
                    $char = $lookahead;
                    if ($lcounter < strlen($inputStr)) {
                        $lookahead = $inputStr[$lcounter++];
                    }
                    $chcounter++;
                    break;
                case "W":
                    $mana_cost[3]+=1;
                    $char = $lookahead;
                    if ($lcounter < strlen($inputStr)) {
                        $lookahead = $inputStr[++$lcounter];
                    }
                    $chcounter++;
                    break;
                case "R":
                    $mana_cost[4]+=1;
                    $char = $lookahead;
                    if ($lcounter < strlen($inputStr)) {
                        $lookahead = $inputStr[$lcounter++];
                    }
                    $chcounter++;
                    break;
            }
        }
        return $mana_cost[0] . "," . $mana_cost[1] . "," . $mana_cost[2] . "," . $mana_cost[3] . "," . $mana_cost[4] . "," . $mana_cost[5];
    }

    public function instantLikeCardlite($row, $def_outputfile) {
        fwrite($def_outputfile, "card_object(" . $this->getLastObject() . ").\n");
        //card name
        $card_name = $row["name"];
        fwrite($def_outputfile, "card_name(" . $this->getLastObject() . ", $card_name).\n");
        //can have more types
        $type = $row["type"];
        $delimiter = strpos($type, ";");
        while ($delimiter != FALSE) {
            $current_type = substr($type, 0, $delimiter);
            $this->addType($current_type);
            fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $current_type).\n");
            $type = substr($type, $delimiter + 1, strlen($type) - $delimiter - 1);
            $delimiter = strpos($type, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $type).\n");
        $this->addType($type);
        //can have more subtypes
        if ($row["subtype"] != "") {
            $subtype = $row["subtype"];
            $delimiter = strpos($subtype, ";");
            while ($delimiter != FALSE) {
                $current_subtype = substr($subtype, 0, $delimiter);
                $this->addSubtype($current_subtype);
                fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $current_subtype).\n");
                $subtype = substr($subtype, $delimiter + 1, strlen($subtype) - $delimiter - 1);
                $delimiter = strpos($subtype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $subtype).\n");
            $this->addSubtype($subtype);
        }
        $this->incrementLastObject();
    }

    public function instantLikeCard($row, $plan_outputfile, $outputfile, $def_outputfile) {
        $ability = $row["ability"];
        $cost_counter = 1;
        $delimiter = strpos($ability, ";");
        while ($delimiter != FALSE) {
            $current_ability = substr($ability, 0, $delimiter);
            $this->abilityParser($current_ability);
            if ($this->activated) {
                fwrite($outputfile, "can_be_activated( last_object(), " . $this->getAbilitycost($this->abilitycostSize() - 1) . ").\n");
            }
            $this->dumpAbilityCostIntoFile($outputfile, "last_object()");
            $ability = substr($ability, $delimiter + 1, strlen($ability) - $delimiter - 1);
            $delimiter = strpos($ability, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        $this->abilityParser($ability);
        if ($this->activated) {
            fwrite($outputfile, "can_be_activated(last_object(), " . $this->getAbilitycost($this->abilitycostSize() - 1) . ").\n");
        }
        $this->dumpAbilityCostIntoFile($outputfile, "last_object()");
        //owner
        fwrite($outputfile, "card_owner(last_object()," . $this->getOwner() . ").\n");
        //manacost
        $mana_cost = $row["manacost"];
        $mana_string = $this->cardManaCostParser($mana_cost);
        fwrite($outputfile, "card_mana_cost( last_object()," . $mana_string . ").\n");
        //object identification
        $str = "partial_mana_cost(last_object(), " . ($cost_counter++) . "," . $mana_string . "). \n";
        fwrite($outputfile, $str);

        fwrite($def_outputfile, "card_object(" . $this->getLastObject() . ").\n");
        //card name
        $card_name = $row["name"];
        fwrite($def_outputfile, "card_name(" . $this->getLastObject() . ", $card_name).\n");
        //can have more types
        $type = $row["type"];
        $delimiter = strpos($type, ";");
        while ($delimiter != FALSE) {
            $current_type = substr($type, 0, $delimiter);
            $this->addType($current_type);
            fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $current_type).\n");
            $type = substr($type, $delimiter + 1, strlen($type) - $delimiter - 1);
            $delimiter = strpos($type, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $type).\n");
        $this->addType($type);
        //can have more subtypes
        if ($row["subtype"] != "") {
            $subtype = $row["subtype"];
            $delimiter = strpos($subtype, ";");
            while ($delimiter != FALSE) {
                $current_subtype = substr($subtype, 0, $delimiter);
                $this->addSubtype($current_subtype);
                fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $current_subtype).\n");
                $subtype = substr($subtype, $delimiter + 1, strlen($subtype) - $delimiter - 1);
                $delimiter = strpos($subtype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $subtype).\n");
            $this->addSubtype($subtype);
        }
        if ($row["supertype"] != "") {
            $supertype = $row["supertype"];
            $delimiter = strpos($supertype, ";");
            while ($delimiter != FALSE) {
                $current_supertype = substr($supertype, 0, $delimiter);
                $this->addSupertype($current_supertype);
                fwrite($outputfile, "card_supertype(last_object(), $current_supertype).\n");
                $supertype = substr($supertype, $delimiter + 1, strlen($supertype) - $delimiter - 1);
                $delimiter = strpos($supertype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($outputfile, "card_supertype(last_object(), $supertype).\n");
            $this->addSupertype($current_supertype);
        }
        $this->callFunc($card_name, $cost_counter, $plan_outputfile, $outputfile, "last_object()");
        $this->incrementLastObject();
    }

    public function creatureLikeCardlite($row, $def_outputfile) {
        //object identification
        fwrite($def_outputfile, "card_object(" . $this->getLastObject() . ").\n");
        //card name
        $card_name = $row["name"];
        fwrite($def_outputfile, "card_name(" . $this->getLastObject() . ", $card_name).\n");
        //can have more types
        $type = $row["type"];
        $delimiter = strpos($type, ";");
        while ($delimiter != FALSE) {
            $current_type = substr($type, 0, $delimiter);
            $this->addType($current_type);
            fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $current_type).\n");
            $type = substr($type, $delimiter + 1, strlen($type) - $delimiter - 1);
            $delimiter = strpos($type, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $type).\n");
        $this->addType($type);
        //can have more subtypes
        if ($row["subtype"] != "") {
            $subtype = $row["subtype"];
            $delimiter = strpos($subtype, ";");
            while ($delimiter != FALSE) {
                $current_subtype = substr($subtype, 0, $delimiter);
                $this->addSubtype($current_subtype);
                fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $current_subtype).\n");
                $subtype = substr($subtype, $delimiter + 1, strlen($subtype) - $delimiter - 1);
                $delimiter = strpos($subtype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $subtype).\n");
            $this->addSubtype($subtype);
        }
        $this->incrementLastObject();
    }

    public function creatureLikeCard($row, $plan_outputfile, $outputfile, $def_outputfile) {
        $ability = $row["ability"];
        $cost_counter = 1;
        $delimiter = strpos($ability, ";");
        while ($delimiter != FALSE) {
            $current_ability = substr($ability, 0, $delimiter);
            $this->abilityParser($current_ability);
            if ($this->activated) {
                fwrite($outputfile, "can_be_activated(last_object(), " . $this->getAbilitycost($this->abilitycostSize() - 1) . ").\n");
            }
            $this->dumpAbilityCostIntoFile($outputfile, "last_object()");
            $ability = substr($ability, $delimiter + 1, strlen($ability) - $delimiter - 1);
            $delimiter = strpos($ability, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        $this->abilityParser($ability);
        if ($this->activated) {
            fwrite($outputfile, "can_be_activated(last_object(), " . $this->getAbilitycost($this->abilitycostSize() - 1) . ").\n");
        }
        $this->dumpAbilityCostIntoFile($outputfile, "last_object()");
        //owner
        fwrite($outputfile, "card_owner(last_object()," . $this->getOwner() . ").\n");
        //manacost
        $mana_cost = $row["manacost"];
        $mana_string = $this->cardManaCostParser($mana_cost);
        fwrite($outputfile, "card_mana_cost(last_object()," . $mana_string . ").\n");
        //object identification
        $str = "partial_mana_cost(last_object(), " . ($cost_counter++) . "," . $mana_string . "). \n";
        fwrite($outputfile, $str);
        //object identification
        fwrite($def_outputfile, "card_object(" . $this->getLastObject() . ").\n");
        //card name
        $card_name = $row["name"];
        fwrite($def_outputfile, "card_name(" . $this->getLastObject() . ", $card_name).\n");
        //can have more types
        $type = $row["type"];
        $delimiter = strpos($type, ";");
        while ($delimiter != FALSE) {
            $current_type = substr($type, 0, $delimiter);
            $this->addType($current_type);
            fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $current_type).\n");
            $type = substr($type, $delimiter + 1, strlen($type) - $delimiter - 1);
            $delimiter = strpos($type, ";");
        }
        //last iteration is not included in the cycle, the following completes it
        fwrite($def_outputfile, "card_type(" . $this->getLastObject() . ", $type).\n");
        $this->addType($type);
        //can have more subtypes
        if ($row["subtype"] != "") {
            $subtype = $row["subtype"];
            $delimiter = strpos($subtype, ";");
            while ($delimiter != FALSE) {
                $current_subtype = substr($subtype, 0, $delimiter);
                $this->addSubtype($current_subtype);
                fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $current_subtype).\n");
                $subtype = substr($subtype, $delimiter + 1, strlen($subtype) - $delimiter - 1);
                $delimiter = strpos($subtype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($def_outputfile, "card_subtype(" . $this->getLastObject() . ", $subtype).\n");
            $this->addSubtype($subtype);
        }
        if ($row["supertype"] != "") {
            $supertype = $row["supertype"];
            $delimiter = strpos($supertype, ";");
            while ($delimiter != FALSE) {
                $current_supertype = substr($supertype, 0, $delimiter);
                $this->addSupertype($current_supertype);
                fwrite($outputfile, "card_supertype(last_object(), $current_supertype).\n");
                $supertype = substr($supertype, $delimiter + 1, strlen($supertype) - $delimiter - 1);
                $delimiter = strpos($supertype, ";");
            }
            //last iteration is not included in the cycle, the following completes it
            fwrite($outputfile, "card_supertype(last_object(), $supertype).\n");
            $this->addSupertype($current_supertype);
        }
        if ($row["power"] != "") {
            $cpower = $row["power"];
            fwrite($outputfile, "card_power(last_object(), $cpower).\n");
        }
        if ($row["toughness"] != "") {
            $ctoughness = $row["toughness"];
            fwrite($outputfile, "card_toughness(last_object(), $ctoughness).\n");
        }
        fwrite($outputfile, "partial_cost_counter(last_object(), " . $cost_counter . "). \n");
        $this->callFunc($card_name, $cost_counter, $plan_outputfile, $outputfile, "last_object()");
        $this->incrementLastObject();
    }

    public function callFunc($input, $cost_counter, $plan_outputfile, $dl_outputfile, $last_object) {
        if (method_exists($this, $input)) {
            $this->$input($cost_counter, $plan_outputfile, $dl_outputfile, $last_object);
        }
    }

    public function setPlanOutputfileName($name) {
        $this->planOutputFileName = $name;
    }

    public function constructDeck() {
        $this->openInputfile();
        $default_outputfile = fopen("./Game/Core/" . "/core.dl", "a");
        $handle = mysql_pconnect($this->databaseAdress, $this->username, $this->userpassword);
        $selected = mysql_select_db($this->databaseName, $handle);
        $str = fgets($this->inputfile);
        if (mysql_ping($handle)) {
            while ($str != FALSE) {
                $delimiter = strpos($str, ";");
                $card_name = substr($str, 0, $delimiter);
                $this->planOutputFile = fopen(realpath("./Game/Modules/Decks/") . "/plan_" . $card_name, 'w');
                $this->outputfile = fopen(realpath("./Game/Modules/Decks/") . "/dl_" . $card_name, 'w');
                $count = substr($str, $delimiter + 1, strlen($str) - $delimiter - 4);
                $query = "SELECT * FROM card_database WHERE name=\"$card_name\"";
                $result = mysql_query($query) or die("query didn't go through to database");
                $row = mysql_fetch_array($result);
                if ($result) {
                    if (($row["type"] === "sorcery") || ($row["type"] === "enchantment") || ($row["type"] === "land") || ($row["type"] === "instant")) {
                        $this->instantLikeCard($row, $this->planOutputFile, $this->outputfile, $default_outputfile);
                        for ($i = 1; $i < $count; $i++) {
                            $this->instantLikeCardlite($row, $default_outputfile);
                        }
                    }
                    if ($row["type"] === "creature") {
                        $this->creatureLikeCard($row, $this->planOutputFile, $this->outputfile, $default_outputfile);
                        for ($i = 1; $i < $count; $i++) {
                            $this->creatureLikeCardlite($row, $default_outputfile);
                        }
                    }
                }
                $str = fgets($this->inputfile);
            }
        }
        $this->dumpAbilityIntoFile($default_outputfile);
        $this->dumpSubtypeIntoFile($default_outputfile);
        $this->dumpSupertypeIntoFile($default_outputfile);
        $this->dumpTypeIntoFile($default_outputfile);
        $this->closeInputfile();
        $this->closeOutputfile();
        $this->closePlanOutputFile();
    }

}

class deckShuffler {

    private $outputfile;
    private $startIndex;
    private $endIndex;
    private $number = array();
    private $actualIndex;
    private $owner;

    public function getOwner() {
        return $this->owner;
    }

    public function setOwner($value) {
        $this->owner = $value;
    }

    public function getActualIndex() {
        return $this->actualIndex;
    }

    public function setActualIndex($value) {
        $this->actualIndex = $value;
    }

    public function incrementActualIndex() {
        $this->actualIndex++;
    }

    public function getNumber($position) {
        if ($this->number[$position]) {
            return $this->number[$position];
        }
    }

    public function setNumber($position, $value) {
        $this->number[$position] = $value;
    }

    public function addNumber($value) {
        $this->number[count($this->number)] = $value;
    }

    public function removeNumber($position) {
        if ($this->number[$position]) {
            $result = $this->number[$position];
            unset($this->number[$position]);
            $this->number = array_values($this->number);
            return $result;
        }
    }

    public function numberSize() {
        return count($this->number);
    }

    public function getStartIndex() {
        return $this->startIndex;
    }

    public function setStartIndex($value) {
        $this->startIndex = $value;
    }

    public function getEndIndex() {
        return $this->endIndex;
    }

    public function setEndIndex($value) {
        $this->endIndex = $value;
    }

    public function fillNumbers() {
        for ($i = $this->getStartIndex(); $i < $this->getEndIndex(); $i++) {
            $this->addNumber($i);
        }
    }

    public function __construct($startIndex, $endIndex, $outputfileName, $owner) {
        $this->outputfile = fopen($outputfileName, 'w');
        $this->setEndIndex($endIndex);
        $this->setOwner($owner);
        $this->setStartIndex($startIndex);
        $this->start();
    }

    public function start() {
        $this->setActualIndex(1);
        $counter = $this->getEndIndex() - $this->getStartIndex();
        $this->fillNumbers();
        for ($i = 0; $i < $counter; $i++) {
            $rand = mt_rand(0, $this->numberSize() - 1);
            $number = $this->removeNumber($rand);
            if ($this->getActualIndex() > 7) {
                if ($this->getActualIndex() == 8) {
                    fwrite($this->outputfile, "    on_top_of_library(" . $this->getOwner() . "," . $number . ").\n");
                }
                fwrite($this->outputfile, "    library(" . $this->getOwner() . "," . $number . "," . $this->getActualIndex() . ").\n");
                $this->incrementActualIndex();
            } else {
                fwrite($this->outputfile, "    hand(" . $this->getOwner() . "," . $number . ").\n");
                $this->incrementActualIndex();
            }
        }
    }

}

?>
