Merge Civs

Creating AI Superpowers

Based upon the concepts and scripts proposed in the according Apolyton-thread AI civilizations will eventually merge, if the following conditions are met:

2General Prerquisites For A Merger:2

- the civs in question are neighbors
- the “recieving” civ will stay within its city cap

2“Surrender” Of An Inferior Civ:2

- If a civ is extremely weak and threatened by its neighbor, they may surrender.
- If a civ is extremely weak and already has an alliance with its neighbor, they may proceed to an accession.
- If a civ is weak and their worst enemy is the same as their neighbor’s and both are already at war with their worst enemy and they already have an alliance they may opt for a “full federation” as well.
- If a civ is weak and their worst enemy is the same as their neighbor’s and they both have the desire to wage war against their worst enemy and they have the same government or their leaders have the same personality, they may decide to federate.

2Federation Of Equal Civs:2

- If two civs are extremly peaceful, the prerequisites for an alliance are already given, they have their worst enemy in common, they share the same government or their leaders have the same personality and at least one of them is unable to expand by settling, they may decide to federate in order to secure their — hopefully peaceful — development.

1The Merger Process1

All cities, units, gold and public works of the “surrendering” civ will be transferred to the “recieving” civ. Advances will be swapped by the chance defined in const.txt for acquiring advances when capturing cities.

1The Script1

//--------------------------------------------------------------------------
// MoT Mod for CTP2 (Apolyton Edition) by BureauBert
// v1.1
// http://motmod.ctp2.info
//--------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////
// MergeCivs
// Based upon:
// Combining of Civilisations Script
// by The Immortal Wombat (aka Ben Weaver)
// original idea: Lou Wigman
// Makes weak AIs surrender (or federate with their neighbors): Advances,
// gold, and PW as well as units and cities are given
// to the "receiving" civ.
////////////////////////////////////////////////////////////////////////////

///////////////////////////////////
// Common Functions
///////////////////////////////////

// IsNeighboringCiv(player1, player2) in main.slc

///////////////////////////////////
// Messages
///////////////////////////////////

messagebox 'MRG_MergeCivs_Unfriendly' {
        Show();
        Title(ID_MERGE_CIVS_UNFRIENDLY_TTL);
        Text(ID_MERGE_CIVS_UNFRIENDLY_MSG);
}

messagebox 'MRG_MergeCivs_Friendly' {
        Show();
        Title(ID_MERGE_CIVS_FRIENDLY_TTL);
        Text(ID_MERGE_CIVS_FRIENDLY_MSG);
}

///////////////////////////////////
// Event Handlers
///////////////////////////////////

HandleEvent(EndTurn) 'MRG_ShallWeMerge' pre {
        int_t mrgCountA;
        int_t mrgCountB;
        int_t mrgWe;
        int_t mrgThey;
        int_t mrgContinue;
        int_t mrgAdvances;
        int_t mrgAdvanceChance;
        int_t mrgAdvanceRandom;
        int_t mrgOurCities;
        int_t mrgTheirCities;
        int_t mrgTotalCities;
        int_t mrgOurGovernment;
        int_t mrgTheirGovernment;
        int_t mrgOurUnits;
        int_t mrgOurMilitaryUnits;
        int_t mrgTheirMilitaryUnits;
        int_t mrgOurPw;
        int_t mrgTheirPw;
        int_t mrgTotalPw;
        int_t mrgOurGold;
        int_t mrgTheirGold;
        int_t mrgTheirMaxCities;
        unit_t mrgUnit;
        city_t mrgCity;

        mrgWe = player[0].owner;
        mrgOurGovernment = player[0].government;
        mrgOurUnits = player[0].units;
        mrgOurMilitaryUnits = player[0].militaryunits;
        mrgOurPw = player[0].publicworkslevel;
       
        mrgAdvances = AdvanceDB();
        mrgAdvanceChance = ConstDB(0).CaptureCityAdvanceChance;
        mrgContinue = 0;

        if(GetCurrentRound() > 100
        && !IsHumanPlayer(mrgWe)
        && mrgWe != 0
        && mrgWe != 1) {
                for(mrgCountB = 1; mrgCountB < g.num_of_players; mrgCountB = mrgCountB + 1) {
                        player[3] = mrgCountB;
                        mrgThey = player[3].owner;
                        mrgTheirGovernment = player[3].government;
                        mrgTheirMilitaryUnits = player[3].militaryunits;
                        mrgTheirPw = player[3].publicworkslevel;
                        mrgTheirMaxCities = GovernmentDB(mrgTheirGovernment).TooManyCitiesThreshold;
                        if(IsPlayerAlive(mrgThey)
                        && !IsHumanPlayer(mrgThey)
                        && mrgThey != 0
                        && mrgWe != mrgThey) {
                                // General prerequisites for a merger
                                mrgOurCities = Cities(mrgWe);
                                mrgTheirCities = Cities(mrgThey);
                                mrgTotalCities = mrgOurCities + mrgTheirCities;
                                if(mrgTotalCities <= mrgTheirMaxCities) {
                                        if(IsNeighboringCiv(mrgThey, mrgWe)) {
                                                mrgContinue = 1;
                                        }
                                        else {
                                                mrgContinue = 0;
                                        }
                                }
                                else {
                                        mrgContinue = 0;
                                }
                                if(mrgContinue == 1) {
                                        mrgContinue = 0;
                                        if(GetRelativeStrength(mrgWe, mrgThey) == 0) {        // DIPLOMATIC_STRENGTH_VERY_WEAK
                                                // Unfriendly takeover - surrender
                                                if(EffectiveWarWith(mrgWe, mrgThey)) {
                                                        // if their military is three times as big as ours
                                                        if((mrgOurMilitaryUnits * 3) < mrgTheirMilitaryUnits) {
                                                                MessageAllBut(mrgThey, 'MRG_MergeCivs_Unfriendly');
                                                                mrgContinue = 1;
                                                        }
                                                        // if more than 50% of our cities are threatened by them
                                                        elseif(GetAtRiskCitiesValue(mrgWe, mrgThey) > 50) {
                                                                MessageAllBut(mrgThey, 'MRG_MergeCivs_Unfriendly');
                                                                mrgContinue = 1;
                                                        }
                                                }
                                                // Friendly takeover - submission
                                                elseif(HasAgreement(mrgWe, mrgThey, 38)) {        // ALLIANCE
                                                        if(EffectiveAtWarCount(mrgWe) > 0 && EffectiveAtWarCount(mrgThey) > 0) {
                                                                MessageAllBut(mrgThey, 'MRG_MergeCivs_Friendly');
                                                                mrgContinue = 1;
                                                        }
                                                }
                                        }
                                        elseif(GetRelativeStrength(mrgWe, mrgThey) == 1) {        // DIPLOMATIC_STRENGTH_WEAK
                                                // Friendly merger if we have much in common
                                                if(!EffectiveWarWith(mrgWe, mrgThey)) {
                                                        if(HasAgreement(mrgWe, mrgThey, 38)) {
                                                                if(FAI_WorstEnemyNum[mrgWe] != 0
                                                                && FAI_WorstEnemyNum[mrgWe] == FAI_WorstEnemyNum[mrgThey]) {
                                                                        if(EffectiveWarWith(mrgWe, FAI_WorstEnemyNum[mrgWe])
                                                                        && EffectiveWarWith(mrgThey, FAI_WorstEnemyNum[mrgThey])) {
                                                                                MessageAllBut(mrgThey, 'MRG_MergeCivs_Friendly');
                                                                                mrgContinue = 1;
                                                                        }
                                                                }
                                                                elseif(FAI_WorstEnemyNum[mrgWe] != 0
                                                                && FAI_WorstEnemyNum[mrgWe] == FAI_WorstEnemyNum[mrgThey]) {
                                                                        if(GetDesireWarWith(mrgWe, FAI_WorstEnemyNum[mrgWe])
                                                                        && GetDesireWarWith(mrgThey, FAI_WorstEnemyNum[mrgThey])) {
                                                                                if(mrgOurGovernment == mrgTheirGovernment
                                                                                || GetPersonalityType(mrgWe) == GetPersonalityType(mrgThey)) {
                                                                                        MessageAllBut(mrgThey, 'MRG_MergeCivs_Friendly');
                                                                                        mrgContinue = 1;
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                        elseif(GetRelativeStrength(mrgWe, mrgThey) == 2) {        // DIPLOMATIC_STRENGTH_AVERAGE
                                                // If we are both extremely peaceful, have very much in common,
                                                // and we are limited in settling
                                                if(AtWarCount(mrgWe) == 0 && AtWarCount(mrgThey) == 0) {
                                                        if(CanFormAlliance(mrgWe, mrgThey)
                                                        && FAI_WorstEnemyNum[mrgWe] != 0
                                                        && FAI_WorstEnemyNum[mrgWe] == FAI_WorstEnemyNum[mrgThey]) {
                                                                if(mrgOurGovernment == mrgTheirGovernment
                                                                || GetPersonalityType(mrgWe) == GetPersonalityType(mrgThey)) {
                                                                        if(STL_FRUSTRATION[mrgWe] > 20) {
                                                                                MessageAllBut(mrgThey, 'MRG_MergeCivs_Friendly');
                                                                                mrgContinue = 1;
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }
                if(mrgContinue == 1) {
                        // Swap resources
                        mrgTotalPw = mrgTheirPw + mrgOurPw;
                        mrgOurGold = PlayerGold(mrgWe);
                        mrgTheirGold = PlayerGold(mrgThey);

                        AddGold(mrgThey, mrgOurGold);
                        SetPW(mrgThey, mrgTotalPw);

                        // Swap advances
                        for(mrgCountA = 0; mrgCountA < mrgAdvances; mrgCountA = mrgCountA + 1) {
                                if(HasAdvance(mrgWe, mrgCountA) && !HasAdvance(mrgThey, mrgCountA)) {
                                        mrgAdvanceRandom = Random(99);
                                        if(mrgAdvanceRandom < mrgAdvanceChance) {
                                                GrantAdvance(mrgThey, mrgCountA);
                                        }
                                }
                        }
                        // Store units and their locations
                        MRG_Receiver = mrgThey;
                        MRG_PreKillUnitCount = 0;
                        for(mrgCountA = 0; mrgCountA < mrgOurUnits; mrgCountA = mrgCountA + 1) {
                                if(GetUnitByIndex(mrgWe, mrgCountA, mrgUnit)) {
                                        MRG_UnitTypes[mrgCountA] = mrgUnit.type;
                                        MRG_UnitLocations[mrgCountA] = mrgUnit.location;
                                        MRG_PreKillUnitCount = MRG_PreKillUnitCount + 1;
                                }
                        }
                        // Swap cities
                        for(mrgCountA = 0; mrgCountA < mrgOurCities; mrgCountA = mrgCountA + 1) {
                                if(GetCityByIndex(mrgWe, mrgCountA, mrgCity)) {
                                        Event:GiveCity(mrgCity, mrgThey);
                                }
                        }
                }
        }
        return CONTINUE;
}

HandleEvent(KillPlayer) 'MRG_SwapUnits' post {
        int_t mrgCountA;
        int_t mrgUnitTypes;
        unit_t mrgUnit;
       
        mrgUnitTypes = UnitDB();

        if(MRG_Receiver > -1) {
                for(mrgCountA = 0; mrgCountA < MRG_PreKillUnitCount; mrgCountA = mrgCountA + 1) {
                        if(MRG_UnitTypes[mrgCountA] < mrgUnitTypes) {
                                if(GetUnitsAtLocation(MRG_UnitLocations[mrgCountA]) < 12) {
                                        CreateUnit(MRG_Receiver, MRG_UnitTypes[mrgCountA], MRG_UnitLocations[mrgCountA], 0, mrgUnit);
                                }
                                else {
                                        CreateUnit(MRG_Receiver, MRG_UnitTypes[mrgCountA], MRG_UnitLocations[mrgCountA], 1, mrgUnit);
                                }
                                //if(mrgUnit.valid) {
                                //        Event:EntrenchUnit(mrgUnit);
                                //}
                        }
                }
                MRG_Receiver = -1;
                for(mrgCountA = 0; mrgCountA < MRG_UnitTypes.#; mrgCountA = mrgCountA + 1) {
                        MRG_UnitTypes[mrgCountA] = mrgUnitTypes;
                }
        }
}