Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34

Thread: [Scripting] Add money to AI controlled carthage

  1. #21

    Default Re: [Scripting] Add money to AI controlled carthage

    OK, so I've finally written up my script using your hints. This is what I got:
    Spoiler Alert, click show to read: 
    Code:
    local function AddMoneyToAI(context)
    if context.string == "rom_rome" 
    and context:faction():is_human() == false then
    local region_count = context:faction():region_list():num_items();
    if region_count <= 15 then
    scripting.game_interface:treasury_mod("rom_rome", 1000);
    elseif region_count <= 10 then
    scripting.game_interface:treasury_mod("rom_rome", 2000);
    if context.string == "rom_carthage" 
    and context:faction():is_human() == false then
    local region_count = context:faction():region_list():num_items();
    if region_count <= 15 then
    scripting.game_interface:treasury_mod("rom_carthage", 1000);
    elseif region_count <= 10 then
    scripting.game_interface:treasury_mod("rom_carthage", 2000);
    if context.string == "rom_macedon" 
    and context:faction():is_human() == false then
    local region_count = context:faction():region_list():num_items();
    if region_count <= 15 then
    scripting.game_interface:treasury_mod("rom_macedon", 1000);
    elseif region_count <= 10 then
    scripting.game_interface:treasury_mod("rom_macedon", 2000);
    end

    Will this piece of code work as intended? It should check if any of the mentioned factions is controlling less than 10 or 15 regions and thus give respective financial boni.

    A further question: is there a way to get access to a factions income (not the treasury, it's the income per turn)? What I want to do is something like:
    if
    faction.income >= 5000 then
    do bad things
    Thanks in advance!

  2. #22
    Inevitability won
    Patrician Citizen

    Join Date
    Mar 2010
    Posts
    9,594

    Default Re: [Scripting] Add money to AI controlled carthage

    Almost perfect, you're just missing the end clause on the if statements, just as the function is required to end with end so is an if statement

    For example:

    local function MyFunction()
    if region_count > 5 then
    -- do stuff
    elseif region_count > 2 then
    -- do stuff
    end
    end

    Your code fixed, I formatted it differently, but your formatting is fine too:
    Spoiler Alert, click show to read: 

    Code:
    local function AddMoneyToAI(context)
        if context.string == "rom_rome" and context:faction():is_human() == false then
            local region_count = context:faction():region_list():num_items();
            if region_count <= 15 then
                scripting.game_interface:treasury_mod("rom_rome", 1000);
            elseif region_count <= 10 then
                scripting.game_interface:treasury_mod("rom_rome", 2000);
            end
        end
                    
        if context.string == "rom_carthage" and context:faction():is_human() == false then
            local region_count = context:faction():region_list():num_items();
            if region_count <= 15 then
                scripting.game_interface:treasury_mod("rom_carthage", 1000);
            elseif region_count <= 10 then
                scripting.game_interface:treasury_mod("rom_carthage", 2000);
            end
        end
            
        if context.string == "rom_macedon" and context:faction():is_human() == false then
            local region_count = context:faction():region_list():num_items();
            if region_count <= 15 then
                scripting.game_interface:treasury_mod("rom_macedon", 1000);
            elseif region_count <= 10 then
                scripting.game_interface:treasury_mod("rom_macedon", 2000);
            end
        end
    end


    I'll look into your income question, but I don't think there is a way to check faction income easily..

    EDIT:

    All you have access to are these for the economy:

    context:faction():treasury()
    context:faction():tax_level()
    context:faction():upkeep_expenditure_percent()
    Last edited by .Mitch.; October 26, 2014 at 05:16 PM.

  3. #23

    Default Re: [Scripting] Add money to AI controlled carthage

    Arrgh, that's kind of typical for me. Don't ask about missing "}" when I tried some things in Java some time ago... Anyway, thanks for your help!
    All you have access to are these for the economy:

    context:faction():treasury()
    context:faction():tax_level()
    context:faction():upkeep_expenditure_percent()
    I suspected that - thanks for looking it up. I'll think of something different then.

  4. #24

    Default Re: [Scripting] Add money to AI controlled carthage

    I currently have a script running to add money to some AI factions.

    each one follows this pattern

    local function ScriptRome(context)
    if context:faction():name() == "rom_rome"
    and context:faction():is_human() == false
    then
    scripting.game_interface:treasury_mod("rom_rome", 1500)
    end
    end

    and at the end their is an event callback for each one.

    could i just put them all under local function AImoneyScript(context) and only have one call back event?
    Balbor

    Former Creative Director
    Former Head Unit Modeller and Texture
    Fourth Age:Total War

  5. #25
    Inevitability won
    Patrician Citizen

    Join Date
    Mar 2010
    Posts
    9,594

    Default Re: [Scripting] Add money to AI controlled carthage

    You can have multiple callbacks, that works just fine, but equally so if you want one callback for whatever reason you can just use the function for the callback to do whatever you want.

    For instance:

    scripting.AddEventCallBack("FactionTurnStart", AImoneyScript);

    local function AImoneyScript(context)
    DO ALL OF THE MONEY GIVING AND CHECKS IN HERE
    end
    Or you can also externalize each faction into a function while retaining one callback:

    scripting.AddEventCallBack("FactionTurnStart", AImoneyScript);

    local function AImoneyScript(context)
    DoRome(context);
    DoCarthage(context);
    end

    function DoRome(context)
    MONEY SCRIPT FOR ROME
    end

    function DoCarthage(context)
    MONEY SCRIPT FOR CARTHAGE
    end

  6. #26

    Default Re: [Scripting] Add money to AI controlled carthage

    cheer Mitch

    so if I'm going it under one call back should it finish

    end
    end
    end
    Balbor

    Former Creative Director
    Former Head Unit Modeller and Texture
    Fourth Age:Total War

  7. #27
    Inevitability won
    Patrician Citizen

    Join Date
    Mar 2010
    Posts
    9,594

    Default Re: [Scripting] Add money to AI controlled carthage

    Well that depends on how many you need.

    You need to close the function with an "end", and you need to close any if statements with an "end".

    I imagine your script will look something like this, I've colour coded where the "ends" are and what made you require one.

    local function MoneyScript(context)
    if context:faction():name() == "rom_rome" and context:faction():is_human() == false then
    scripting.game_interface:treasury_mod("rom_rome", 1500)
    end
    if context:faction():name() == "rom_carthage" and context:faction():is_human() == false then
    scripting.game_interface:treasury_mod("rom_carthage", 1500)
    end
    if context:faction():name() == "rom_suebi" and context:faction():is_human() == false then
    scripting.game_interface:treasury_mod("rom_suebi", 1500)
    end
    end
    So you can see here you require two at the end of the function, one to close the function and one to close the preceding if statement.

  8. #28

    Default Re: [Scripting] Add money to AI controlled carthage

    Exellent!In my opinion, it's a good idea to add AI some money in the conservative way, both by turn and by region are fit. God knows how AI will use large amounts of money, maybe makes a peace with your both enemy or more funny.

  9. #29
    Semisalis
    Join Date
    Sep 2011
    Location
    New Zealand
    Posts
    412

    Default Re: [Scripting] Add money to AI controlled carthage

    Has anyone tried using this to subtract money? You used to be able to add negative money in MTW2 but in this thread Yarkis says you cant subtract in Shogun 2 and probably cant in Rome 2
    http://www.twcenter.net/forums/showt...n-now%29/page2
    ~ Too soon old, too late smart ~

  10. #30
    Litharion's Avatar Artifex
    Join Date
    Sep 2013
    Location
    Germany
    Posts
    2,622

    Default Re: [Scripting] Add money to AI controlled carthage

    I tried it, but I was not able to get it to work with scripting.game_interface:treasury_mod("FACTION", -1500)

  11. #31
    Inevitability won
    Patrician Citizen

    Join Date
    Mar 2010
    Posts
    9,594

    Default Re: [Scripting] Add money to AI controlled carthage

    Yes, as Litharion points out you can't unfortunately.

    This has always been one of my "why the hell doesn't this work" things.

    You'd think the function would do: "money = money + value"

    Which even if it was negative would still then work... who knows... lol

  12. #32
    Semisalis
    Join Date
    Sep 2011
    Location
    New Zealand
    Posts
    412

    Default Re: [Scripting] Add money to AI controlled carthage

    Darn, there goes some nifty scripting to hurt the player. i will try some effect bundles instead
    ~ Too soon old, too late smart ~

  13. #33

    Default Re: [Scripting] Add money to AI controlled carthage

    Haha, scripts usually weighed me down, sometimes we have to do something instead, have made it this far, the experience is better than the results, after all CA doesn't owe us about this.

  14. #34
    Alex1987's Avatar Tiro
    Join Date
    Apr 2009
    Location
    Russia, Krasnodar region
    Posts
    247

    Default Re: [Scripting] Add money to AI controlled carthage

    Hi guys! I want to make persian escalation in Wrath of Sparta on 1 turn. I make script, bat nothing happens in game. What s wrong?

    Code:
    
    persian_faction_str = "pel_persia";
    persian_home_region = "pel_karia_kolossai";
    
    
    
    
    SAFE_DISTANCE_FROM_OTHER_FACTIONS = 8;
    SAFE_DISTANCE_FROM_PERSIAN = 2;
    SAFE_DISTANCE_FROM_PLAYER = 20;
    ATTEMPTS = 10;
    CHANGE_ZONE = 4;
    
    
    unit_list = {};
    unit_list["1"] =  	"Pel_Baktrian_Cavalry,"..
    					"Pel_Baktrian_Cavalry,"..
    					"Pel_Baktrian_Cavalry,"..
    					"Pel_Baktrian_Cavalry,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Spearmen,"..
    					"Pel_Immortal_Archers,"..
    					"Pel_Immortal_Archers,"..
    					"Pel_Immortal_Archers,"..
    					"Pel_Immortal_Archers,"..
    					"Pel_Sakae_Horse_Archers,"..
    					"Pel_Sakae_Horse_Archers,"..
    					"Pel_Sakae_Horse_Archers,"..
    					"Pel_Sakae_Horse_Archers,"..
    					"Pel_Sakae_Horse_Archers";
    
    
    zone_1 = { x1 = 623 , x2 = 649 , y1 = 484, y2 = 503 };
    zone_2 = { x1 = 557 , x2 = 576 , y1 = 421, y2 = 430 };
    zone_3 = { x1 = 535 , x2 = 549 , y1 = 381, y2 = 396 };
    zone_4 = { x1 = 532 , x2 = 540 , y1 = 356, y2 = 375 };
    zone_5 = { x1 = 540 , x2 = 565 , y1 = 330, y2 = 337 };
    zone_6 = { x1 = 560 , x2 = 579 , y1 = 279, y2 = 291 };
    zone_7 = { x1 = 635 , x2 = 646 , y1 = 266, y2 = 273 };
    zone_8 = { x1 = 667 , x2 = 682 , y1 = 201, y2 = 214 };
    zone_9 = { x1 = 670 , x2 = 695 , y1 = 134, y2 = 141 };
    
    
    
    
    zone_list = {zone_1 , zone_2 , zone_3, zone_4, zone_5, zone_6, zone_7, zone_8, zone_9 };
    
    
    --[[This script summons armies for the persian empire. There are three ways for the "summoning" to be activated.
    
    
    - The Siege Path: Player sieges certain settlements, that are under control of the persian empire
    - The Hate Path: Player's imperium rises too much..
    - Time path: Script will activate no matter what on 60 turns 
    
    
    Both these paths activate a new listener , the "new turn listener" , that waits for the player's new turn to show the pesrian escalation 
    message and to summon the armies.
    
    
    
    
    ]]
    
    
    
    
    
    
    function GetTurnNum()
    return scripting.game_interface:model():turn_number();
    end
    
    
    local function ScriptPersian(context)
    turn_num = GetTurnNum();
    if context:faction():name() == "pel_persia" 
    and context:faction():is_human() == false 
    and turn_num == 1 
    then scripting.game_interface:treasury_mod("pel_persia", 10000), function() persian_escalation_message() end)
    end
    
    
    function persian_escalation_message()
    	cm:show_message_event("custom_event_1", 349,238), function() begin_spawn_sequence() end)
    end
    
    
    
    
    --[[************************************************************************************************************************************************************************************
    **************************************************************************************************************************************************************************************]]
    
    
    function begin_spawn_sequence()
    	spawn_army(0, 0,persian_faction_str,unit_list["1"],"1", function() spawn_2_army() end)
    end
    
    
    function spawn_2_army()
    	spawn_army(0, 0,persian_faction_str,unit_list["1"],"2", function() spawn_3_army() end)
    end
    
    
    function spawn_3_army()
    	spawn_army(0, 0,persian_faction_str,unit_list["1"],"3", function() spawn_4_army() end)
    end;
    
    
    function spawn_4_army()
    	spawn_army(0, 0,persian_faction_str,unit_list["1"],"4", function() spawn_5_army() end)
    end;
    
    
    function spawn_5_army()
    	spawn_army(0, 0,persian_faction_str,unit_list["1"],"5", false )
    end;
    
    
    
    
    
    
    function spawn_army(x,y,faction,unit_list,army_id, callback)
    
    
    	if x == 0 and y == 0 then 
    		x,y = generate_coordinates();
    	end
    
    
    	output("x is " ..  tostring(x) .. "y is " .. tostring(y));
    
    
    
    
    	cm:create_force(
    		faction, 																-- name of faction
    		unit_list,		 														-- comma-separated units
    		persian_home_region, 													-- home region
    		x, 																		-- logical x
    		y, 																		-- logical y
    		army_id, 																-- string id for army
    		true,
    		function(cqi) 
    			reduce_army_upkeep(cqi)			--reduce upkeep is standard issue
    			if is_function(callback) then	
    				callback();					--this is where we call the next one in the chain
    			end;
    		end									-- goes via command queue
    	);
    end
    
    
    function generate_coordinates()
    	--pick a zone
    	for j = 0 , CHANGE_ZONE , 1 do 	
    		local zone = zone_list[math.random(1, #zone_list)];
    		for i = 0 , ATTEMPTS , 1 do
    			local x_cord = math.random(zone.x1 , zone.x2)
    			local y_cord = math.random(zone.y1 , zone.y2) --Pay attention here y2 is actually smaller!!
    
    
    			if is_valid_spawn_point(x_cord,y_cord,true) then 
    				return x_cord,y_cord;
    			end
    		end
    	end
    
    
    	-- do it again no safe-checks
    	output("SAFETY DEACTIVATED");
    	for j = 0 , CHANGE_ZONE , 1 do 	
    		local zone = zone_list[math.random(1, #zone_list)];
    		for i = 0 , ATTEMPTS , 1 do
    			local x_cord = math.random(zone.x1 , zone.x2)
    			local y_cord = math.random(zone.y1 , zone.y2) --Pay attention here y2 is actually smaller!!
    
    
    			if is_valid_spawn_point(x_cord,y_cord,false) then   --run it with no safety checks
    				return x_cord,y_cord;
    			end
    		end
    	end
    
    
    end
    
    
    function is_valid_spawn_point(x,y,safe_check)
    
    
    	local faction_list = cm:model():world():faction_list();
    	local safe_distance;
    	for i = 0, faction_list:num_items() - 1 do
    
    
    		local current_faction = faction_list:item_at(i);
    		if current_faction:name() == persian_faction_str then
    			 safe_distance = SAFE_DISTANCE_FROM_PERSIAN;
    		end
    		if current_faction:is_human() then 
    			safe_distance = SAFE_DISTANCE_FROM_PLAYER;
    		else
    			 safe_distance = SAFE_DISTANCE_FROM_OTHER_FACTIONS;
    		end
    
    
    		-- if the safe check is false it means that we failed already
    		if safe_check == false then
    			safe_distance = 3;
    		end
    
    
    		local military_force_list = current_faction:military_force_list();
    
    
    		for j = 0, military_force_list:num_items() - 1 do
    			local current_military_force = military_force_list:item_at(j);
    			
    			if current_military_force:has_general() then
    				local char = current_military_force:general_character();
    				if distance(char:logical_position_x(), char:logical_position_y(), x, y) < safe_distance then
    					return false;
    				end
    			end
    		end
    	end
    
    
    	return true;
    end
    
    
    
    
    function reduce_army_upkeep( cqi)
    	output("reducing upkeep, general cqi is " .. tostring(cqi));
     	cm:apply_effect_bundle_to_characters_force("pel_persian_army_upkeep",cqi,-1);
     	output("Done");
    end
    
    
    
    
    
    
    
    
    function distance( x1, y1, x2, y2 )
    	return math.sqrt( (x2-x1)^2 + (y2-y1)^2 )
    end
    Candidate of sociological sciences. RUSSIAN LIBERAL OPPOSITIONIST AND ACTIVIST.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •