Company of Heroes: Eastern Front

Author Topic: 'Controlling' the AI  (Read 13828 times)

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: 'Controlling' the AI
« Reply #30 on: August 23, 2012, 01:55:39 PM »
What's a jar.scar?  ???

This :D, It is code that fixes reward units for AI so AI chooses 0....n reward units. Before it was all or none :P.

Here is code i will attach file later.
Just import it in scarUtil.scar and put in folder with scarUtil, ref: ..\Eastern_Front\Data\scar, script will be automatically activated when you import it. You can see output in console.

Code: [Select]
----------------------------------------------------------------
-- THIS SCRIPT WAS WRITTEN BY: jojorabbit
----------------------------------------------------------------
-- Fix for AI using reward units aka jojorabbit`s AI reward unit fix aka jar
----------------------------------------------------------------
-- @author: jojorabbit
-- @version: v 1.0 b270412
-- @filename: jar.scar
-- This file fixes AI usage of reward units, in vCoH ai uses all or none reward units
-- If you are gonna use this with other mod give me credits
----------------------------------------------------------------


----------------------------------------------------------------
-- REFERENCE
----------------------------------------------------------------
-- ALLIED UPGRADES
-- upgrade\upgrade_allied_105.lua
-- upgrade\upgrade_allied_hellcat.lua
-- upgrade\upgrade_allied_jumbo.lua
-- upgrade\upgrade_allied_staghound.lua

--  AXIS UPGRADES
-- upgrade\upgrade_axis_geschutzwagen.lua
-- upgrade\upgrade_axis_schwimmwagen.lua
-- upgrade\upgrade_axis_tiger_ace.rgd
-- upgrade\upgrade_axis_officer.rgd

-- COMMONWEALTH UPGRADES
-- upgrade\upgrade_british_comet.lua
-- upgrade\upgrade_british_kangaroo.lua
-- upgrade\upgrade_british_staghound.lua
-- upgrade\upgrade_british_sas.lua -- THIS IS/(WILL BE) HANDLED IN aiutil.scar

-- PANZER ELITE UPGRADES
-- upgrade\upgrade_panzer_elite_hotchkiss.lua
-- upgrade\upgrade_panzer_elite_jagdpanzer.lua
-- upgrade\upgrade_panzer_elite_jagdtiger.lua
-- upgrade\upgrade_panzer_elite_schwimmwagen.lua
-- upgrade\upgrade_panzer_elite_nashorn.rgd

-- SOVIET UPGRADES
-- upgrade\upgrade_soviets_is3
-- upgrade\upgrade_soviets_kv1
-- upgrade\upgrade_soviets_su_122.rgd

-- OSTHEER UPGRADES
-- NONE TILL IT IS FINISHED

----------------------------------------------------------------
-- INIT
----------------------------------------------------------------
function OnInit()
print("JAR Init")
AddRewardUnitsToAIPlayers()
end

Scar_AddInit(OnInit)

----------------------------------------------------------------
-- CONSTANTS
----------------------------------------------------------------
isDebug = true -- turn on/off logging

-- ALLIES
R_ALLIES_SHERMAN_105 = BP_GetUpgradeBlueprint("upgrade/upgrade_allied_105.lua")
R_ALLIES_HELLCAT = BP_GetUpgradeBlueprint("upgrade/upgrade_allied_hellcat.lua")
R_ALLIES_SHERMAN_JUMBO = BP_GetUpgradeBlueprint("upgrade/upgrade_allied_jumbo.lua")
R_ALLIES_STAGHOUND = BP_GetUpgradeBlueprint("upgrade/upgrade_allied_staghound.lua")

ALLIES_REWARD_UNITS = {
R_ALLIES_SHERMAN_105,
R_ALLIES_HELLCAT,
R_ALLIES_SHERMAN_JUMBO,
R_ALLIES_STAGHOUND,
}

-- AXIS
R_AXIS_GESCHUTZEWAGEN = BP_GetUpgradeBlueprint("upgrade/upgrade_axis_geschutzwagen.lua")
R_AXIS_SCHWIMMWAGEN = BP_GetUpgradeBlueprint("upgrade/upgrade_axis_schwimmwagen.lua")
R_AXIS_TIGER_ACE = BP_GetUpgradeBlueprint("upgrade/upgrade_axis_tiger_ace.lua")
R_AXIS_LW_OFFICIER = BP_GetUpgradeBlueprint("upgrade/upgrade_axis_officer.lua")

-- table
AXIS_REWARD_UNITS = {
R_AXIS_GESCHUTZEWAGEN,
R_AXIS_SCHWIMMWAGEN,
R_AXIS_TIGER_ACE,
R_AXIS_LW_OFFICIER,
}
-- COMMONWEALTH
R_CW_COMET = BP_GetUpgradeBlueprint("upgrade/upgrade_british_comet.lua")
R_CW_KANGAROO = BP_GetUpgradeBlueprint("upgrade/upgrade_british_kangaroo.lua")
R_CW_STAGHOUND = BP_GetUpgradeBlueprint("upgrade/upgrade_british_staghound.lua")

CW_REWARD_UNITS = {
R_CW_COMET,
R_CW_KANGAROO,
R_CW_STAGHOUND,
}
-- PE
R_PE_HOTCHKISS = BP_GetUpgradeBlueprint("upgrade/upgrade_panzer_elite_hotchkiss.lua")
R_PE_JAGDPANZER = BP_GetUpgradeBlueprint("upgrade/upgrade_panzer_elite_jagdpanzer.lua")
R_PE_JAGDTIGER = BP_GetUpgradeBlueprint("upgrade/upgrade_panzer_elite_jagdtiger.lua")
R_PE_SCHWIMMWAGEN = BP_GetUpgradeBlueprint("upgrade/upgrade_panzer_elite_schwimmwagen.lua")
R_PE_NASHORN = BP_GetUpgradeBlueprint("upgrade/upgrade_panzer_elite_nashorn.lua")

PE_REWARD_UNITS = {
R_PE_HOTCHKISS,
R_PE_JAGDPANZER,
R_PE_JAGDTIGER,
R_PE_SCHWIMMWAGEN,
R_PE_NASHORN,
}
-- SOV
R_SOV_IS3 = BP_GetUpgradeBlueprint("upgrade/upgrade_soviets_is3.lua")
R_SOV_KV1 = BP_GetUpgradeBlueprint("upgrade/upgrade_soviets_kv1.lua")
R_SOV_SU122 = BP_GetUpgradeBlueprint("upgrade/upgrade_soviets_su_122.lua")

SOVIET_REWARD_UNITS = {
R_SOV_IS3,
R_SOV_KV1,
R_SOV_SU122,
}
-- OST


----------------------------------------------------------------
-- METHODS
----------------------------------------------------------------
function AddRewardUnitsToAIPlayers()
for i = 1, World_GetPlayerCount() do
local player = World_GetPlayerAt(i)
-- only for AI players
if(AI_IsEnabled(player)) then
local race = Player_GetRaceName(player)

LogOutput("AI race= "..race)

-- ALLIES
if(race == TRACE_ALLIES) then
rewardTable = ALLIES_REWARD_UNITS
-- AXIS
elseif(race == TRACE_AXIS) then
rewardTable = AXIS_REWARD_UNITS
-- CW
elseif(race == TRACE_ALLIES_COMMONWEALTH) then
rewardTable = CW_REWARD_UNITS
-- PE
elseif(race == TRACE_AXIS_PANZER_ELITE) then
rewardTable = PE_REWARD_UNITS
-- SOV
elseif(race == TRACE_ALLIES_SOVIETS) then
rewardTable = SOVIET_REWARD_UNITS
-- OST
end

if(rewardTable ~= nil) then
LogOutput("rewardtable size "..#rewardTable)
ApplyRewardUnits(player, rewardTable)
end
end
end
end


function ApplyRewardUnits(player, rewardTable)
--~ local rndTable = Table_GetRandomItem()
local randomTest = World_GetRand(1, 100)
LogOutput("Test: rnd= "..randomTest)

for n = 1, table.getn(rewardTable) do
local rnd = World_GetRand(1, 100)
LogOutput("Test: "..n.." rnd= "..rnd)
if((rnd % 2) == 0) then
LogOutput("Activating - "..BP_GetName(rewardTable[n]))
Command_PlayerUpgrade(player, rewardTable[n], true, false)
end
end
end

function LogOutput(message)
if(isDebug) then
print(message)
end
end

Note: part of ScarUtil.scar code:

Code: [Select]
...
import("NIS.scar")
import("Debugger.scar")
import("GarrisonBuilding.scar")
import("jar.scar")
...

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #31 on: August 23, 2012, 02:08:22 PM »
Excellent! so if I download attached file and put it in folder - what will happen? Abit confused on how this works since I don't really understand your explanation sorry xD This will affect if AI will choose to build Jagdpanzer and not Hetzer?

Also what about my earlier issue with command_tree_pref? Did you manage to see my attached file and see if I did anything wrong? Please help me on that. :-*

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #32 on: August 23, 2012, 04:34:08 PM »
Still testing that issue jojo?  ;)

Offline Otto Halfhand

  • Donor
  • Mr. Spam
  • *
  • Posts: 1166
    • View Profile
Re: 'Controlling' the AI
« Reply #33 on: August 23, 2012, 06:15:23 PM »
viruzz upload the file for your command_tree issue here. I don't have time to futz with a rapidshare account right now. I can give you a looksee while jojo is fishing/working

孫 The
EF_v1.7.10
子 Art
Illegitimi non Carborundum -"Vinegar" Joe Stilwell
兵 of
Sun Tzu says: In warfare one compels and is not compelled by others
法 War

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #34 on: August 23, 2012, 06:52:29 PM »
Oops I didn't know this forum had the option to upload attachments that's why i did it through rapidshare. Now I've found it and here you go!

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: 'Controlling' the AI
« Reply #35 on: August 23, 2012, 11:37:34 PM »
Excellent! so if I download attached file and put it in folder - what will happen? Abit confused on how this works since I don't really understand your explanation sorry xD This will affect if AI will choose to build Jagdpanzer and not Hetzer?

Also what about my earlier issue with command_tree_pref? Did you manage to see my attached file and see if I did anything wrong? Please help me on that. :-*

0...n it is simple math explanation or [0,n] :D, to make it simple, before this file AI used all reward units or none. With this file AI will use sometimes 0, sometimes only 1 reward unit, like only geshutzewagen, sometimes only 2 like offiecer and schwimmwagen, etc... for PE it means AI could choose jagdpanzers and jagtiger or only jagdpanzers, or only nashorn.
So sometimes AI will attack you with jagdpanzers and jagdpanther, sometimes hetzer and jagdtiger etc etc....

Conclusion it makes AI more human alike cuz some ppl choose only some units :P.
Hope it helps.

I will help you dont worry i just dont have so much time to test and see everything in one day.

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #36 on: August 24, 2012, 03:47:36 PM »
Ahhh I see what you mean now that explains it better thanks jojo ^^ So will this file improve the chance that AI will build Jagdpanzer instead of Hetzer? Or is it still 50/50 chance? There's really no way at all to make AI 100% build Jagdpanzer instead of Hetzer by editing this file?

Again I really appreciate the time you're taking and the help you're giving me in teaching a noob like me jojo. I don't mean to sound impatient but I'm sorry if I gave you that impression. It's just that when I get started on a new piece of work that I have no experience on, I am eager to learn and move forward. So since I am stuck with this error/AI still building other doctrines I get frustrated that something went wrong and I want to know how to fix it otherwise I feel unsettled like work is unfinished. Hope you understand how I feel ^^

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: 'Controlling' the AI
« Reply #37 on: August 24, 2012, 03:58:09 PM »
Ahhh I see what you mean now that explains it better thanks jojo ^^ So will this file improve the chance that AI will build Jagdpanzer instead of Hetzer? Or is it still 50/50 chance? There's really no way at all to make AI 100% build Jagdpanzer instead of Hetzer by editing this file?

Again I really appreciate the time you're taking and the help you're giving me in teaching a noob like me jojo. I don't mean to sound impatient but I'm sorry if I gave you that impression. It's just that when I get started on a new piece of work that I have no experience on, I am eager to learn and move forward. So since I am stuck with this error/AI still building other doctrines I get frustrated that something went wrong and I want to know how to fix it otherwise I feel unsettled like work is unfinished. Hope you understand how I feel ^^

To make 100% just comment 2 lines in for loop:

it needs to look like this:
Quote
for n = 1, table.getn(rewardTable) do
      local rnd = World_GetRand(1, 100)
      LogOutput("Test: "..n.." rnd= "..rnd)
      --if((rnd % 2) == 0) then -> this is commented -> %2 basically means 50%-50% chances
         LogOutput("Activating - "..BP_GetName(rewardTable[n]))
         Command_PlayerUpgrade(player, rewardTable[n], true, false)
      --end -> this is commented
   end

With those 2 lines commented AI will always use reward units.
Hope it helps.

I know the feeling making and programming AI is painful i know. Will try to resolve doctrines asap.

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #38 on: August 24, 2012, 04:56:04 PM »
Hmm if that's the case then I think I will still have to keep restarting in order to get to fight the units I want. If I make AI choose 100% reward units then it's great that PE will definitely choose jagdtiger and jagdpanzer to fight me but I usually fight 1v2 Expert AI with 1 Wehr and 1 PE. The wehr AI will not build stug IV but Geschutzwagen instead which will be another long range headache -_-

So to explain it simply, having AI choose 100% reward units won't be good and having AI 100% not choose reward units is also not good so I will have to stick with 50/50 chance in order to get the units I want. What I thought would be possible is just to single out AI to use jagdpanzer only not hetzer and the rest of the reward units can stay normal but I guess this is not possible since earlier in the thread you said it is not possible to force AI to build what you want, even though it's a single reward unit like Jagdpanzer instead of Hetzer, since it is all or nothing am I correct? ^^

Anyway let me know when you've resolved it then we can move on to next step in preventing AI from building long range units + artillery. If you want to know the reason I am doing all this to give you a better understanding is because I love tank vs tank battles especially heavy tanks like Tiger, Pershing, IS-2 etc which is why I feel long range units spoil the fun if you know what I mean. Thanks jojo!

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: 'Controlling' the AI
« Reply #39 on: August 24, 2012, 10:32:08 PM »
Quote
So to explain it simply, having AI choose 100% reward units won't be good and having AI 100% not choose reward units is also not good so I will have to stick with 50/50 chance in order to get the units I want. What I thought would be possible is just to single out AI to use jagdpanzer only not hetzer and the rest of the reward units can stay normal but I guess this is not possible since earlier in the thread you said it is not possible to force AI to build what you want, even though it's a single reward unit like Jagdpanzer instead of Hetzer, since it is all or nothing am I correct? ^^

That is purpose of the file that AI is more human alike sometimes 1reward, sometimes 2 etc etc, so it is not all or nothing.
But it is possible to force PE to use jagdpanzer reward and WH not to use geschutzwagen reward :P. You just need to modify file a little bit :P.

By commenting one line you can easy turn off WH reward units :P.

Quote
if(race == TRACE_ALLIES) then
   rewardTable = ALLIES_REWARD_UNITS
   -- AXIS
elseif(race == TRACE_AXIS) then
   -- rewardTable = AXIS_REWARD_UNITS -> this needs to be commented.
        -- CW
elseif(race == TRACE_ALLIES_COMMONWEALTH) then
   rewardTable = CW_REWARD_UNITS
   -- PE

Here is part of code that should force PE to use Tank_Destroyer, And Wehr to use Blitz, i did not had much time to test it, so if it does not work inform me so i can speed up testing:
Quote
-- AXIS
         elseif(race == TRACE_AXIS) then
            --rewardTable = AXIS_REWARD_UNITS -- commented cuz STUG IV
            UI_ForceCommanderTreeChoice(player, COMMANDER_TREE.AXIS.BLITZKRIEG) -- this is added
         -- CW
         elseif(race == TRACE_ALLIES_COMMONWEALTH) then
            rewardTable = CW_REWARD_UNITS
         -- PE
         elseif(race == TRACE_AXIS_PANZER_ELITE) then
            rewardTable = PE_REWARD_UNITS
            UI_ForceCommanderTreeChoice(player, COMMANDER_TREE.ELITE.TANK_DESTROYER) -- force tank d.

Hope it helps.
« Last Edit: August 24, 2012, 10:51:59 PM by jojorabbit »

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #40 on: August 24, 2012, 11:51:20 PM »
Haha you should have said so sooner that I can actually still modify it. From the way you make it sound in 1st page of the thread I thought it was not possible:

Quote
It was before but now AI can take 1...n reward units means Wehr AI could take only Geshutzewagen or only schwimmwagen or both.

To make it simple.
AI goes through all reward units and takes 50% chances that AI will choose reward unit.
Before it was all reward units or none.

and

Quote
I wont explain this it is fixed in internal version as i told post ago, sometimes AI will choose 1...n units. You cant tell AI what units too build in late game, well you could make some sort of your AI mod

Based on what you said I thought it's just 3 options = 100%, 0% reward units or your modded code of 50%. I can't really test it now since I still can't set command_tree_pref.ai to choose that doctrine to see if I use the jar.scar with the commented line works. But it's really great to know that all this is possible when I first thought I could never really get the kind of AI I want. Thanks again jojo! :)

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #41 on: August 24, 2012, 11:55:25 PM »

Quote
-- AXIS
         elseif(race == TRACE_AXIS) then
            --rewardTable = AXIS_REWARD_UNITS -- commented cuz STUG IV
            UI_ForceCommanderTreeChoice(player, COMMANDER_TREE.AXIS.BLITZKRIEG) -- this is added
         -- CW
         elseif(race == TRACE_ALLIES_COMMONWEALTH) then
            rewardTable = CW_REWARD_UNITS
         -- PE
         elseif(race == TRACE_AXIS_PANZER_ELITE) then
            rewardTable = PE_REWARD_UNITS
            UI_ForceCommanderTreeChoice(player, COMMANDER_TREE.ELITE.TANK_DESTROYER) -- force tank d.

Oh I see you added new line of code! Is this in the jar.scar file that you sent me or in command_tree_pref.ai? The code looks different from the first file you told me to work on. So what will I do with the modified command_tree_pref.ai file?

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: 'Controlling' the AI
« Reply #42 on: August 25, 2012, 12:19:04 AM »
Quote
Oh I see you added new line of code! Is this in the jar.scar file that you sent me or in command_tree_pref.ai? The code looks different from the first file you told me to work on. So what will I do with the modified command_tree_pref.ai file?

Yes all code i told you to modifiy is jar.scar.
Remove that modified command_tree_pref.ai and use old one :P.

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #43 on: August 25, 2012, 01:46:26 AM »
Jojo do I just copy paste the whole modified jar.scar into ..\Eastern_Front\Data\scar or do I copy the entire text and paste at bottom of scarUtil.scar?

Offline viruz777

  • Ingenery
  • *
  • Posts: 46
    • View Profile
Re: 'Controlling' the AI
« Reply #44 on: August 25, 2012, 02:27:24 AM »
Hi jojo I copy pasted the edited jar.scar into /scar folder as you instructed. I also copied the entire text and pasted at the bottom of scarutil.scar and also added import "jar.scar" inside there. (if that is what you mean by import it in scarUtil.scar)

It still gave Fatal Scar Error - Execution paused.

here is screenshot :





I think it has something to do with the added lines of the command tree? Please help what should I do now?

« Last Edit: August 25, 2012, 02:34:55 AM by viruz777 »