Results 1 to 7 of 7

Thread: Perl script to count all units in a saved game

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member Member afrit's Avatar
    Join Date
    Jun 2004
    Location
    USA
    Posts
    321

    Default Perl script to count all units in a saved game

    This may be of interest to some. I wrote a Perl script to count the number of units of each type in a save game file. It also counts the types in the EDU file as a side effect.

    To run it, you need perl installed (www.perl.com).

    EXAMPLE: This is the census of all units at the beginning of a BI game that I saved as saxons.sav. I got this output by running the script and entering the savegame name when asked for it.

    Code:
    There are  210 units defined in bi/data/export_descr_unit.txt
    
    ENTER NAME OF SAVE FILEsaxons.sav
    african peasant = 2
    alan horse archers = 2
    alemanni warhounds = 1
    axe heerbann = 1
    berber axemen = 2
    berber lancers = 1
    bucellarii = 2
    burgundian lancer = 3
    camel raiders = 3
    celtic slingers = 1
    celtic spearman = 1
    celtic warlord = 2
    celtish wolfhounds = 1
    chosen warlord alemanni = 8
    clibinarii = 4
    clibinarii immortals = 3
    comitatenses = 31
    desert archers african = 1
    desert archers sassanid = 4
    desert cavalry = 2
    desert chieftain = 2
    eastern archer = 12
    equites auxilia = 1
    foederati cavalry = 19
    foederati infantry = 14
    frankish paladin bodyguard = 2
    gallowglass = 1
    goth raiders = 1
    goth warlords = 2
    gothic lancer = 1
    gothic spearmen = 2
    herdsmen sarmatian = 5
    hillmen = 1
    hippo toxotai = 7
    hun horde chosen warriors = 31
    hun horde horse = 32
    hun horde peasants = 28
    hun horde spearmen = 36
    hunnic elite warriors = 1
    hunnic heavy cavalry = 1
    hunnic lancers = 3
    hunnic warlords = 4
    imperial german bodyguard = 19
    imperial household bodyguard = 16
    kurdish javelinmen = 2
    legio lanciarii = 6
    levy spearman frank = 3
    levy spearman saxon = 1
    levy spearmen sassanid = 18
    limitanei = 68
    lombard archers = 6
    merc alan horse archers = 4
    merc alan noble cavalry = 1
    merc armoured camel riders = 4
    merc ballistae = 4
    merc bosphoran infantry = 1
    merc bucellarii = 7
    merc camel raiders = 5
    merc elephants = 2
    merc equites veteranii = 10
    merc foederati cavalry = 2
    merc foederati infantry = 2
    merc gallowglass = 1
    merc golden band = 2
    merc graal knights = 2
    merc hippo toxotai = 6
    merc moorish raiders = 1
    merc sarmatian armoured archers = 5
    merc vandal lancers = 1
    merc vandal raiders = 1
    merc veteranii = 11
    moorish raiders = 2
    mountain men = 2
    mountain slinger = 5
    naval biremes = 10
    naval boats = 5
    naval corvus quinquireme = 1
    naval quinquiremes = 3
    naval triremes = 8
    nomad archer cavalry = 1
    peasant gothic = 1
    roman priest = 2
    runaway slave spearmen = 9
    sarmatian armoured archers = 1
    sarmatian auxilia = 2
    sarmatian warlords = 5
    sassanid peasant = 10
    saxon hunters = 6
    saxon keels = 1
    saxon warlord = 4
    scholae palatinae = 1
    scotti chariots = 1
    spear warband alemanni = 9
    steppe horde chosen warriors = 12
    steppe horde horse = 36
    steppe horde peasants = 24
    steppe horde spearmen = 24
    steppe horse archers = 5
    steppe lancers = 5
    steppe raiders = 14
    steppe spearmen vandal = 2
    steppe warlords = 5
    tribal cavalry = 2
    western archer = 21
    western peasant = 2
    western peasant alemanni = 4
    western peasant celt = 1
    western peasant frank = 1
    western peasant saxon = 1
    HIT ENTER TO EXIT

    The script code follows (save this as a text file with the name units.pl . You must associate the .pl extension with the Perl application to make it run). The script also generates a file that lists all the units and their counts. The file is saved in the same folder as the script. The script refers to the default location of the TW executables. If you have them installed elsewhere, edit the $tw_path variable at the top of the script.

    Enjoy!
    Code:
    #!usr/bin/perl
    
    $unitsfile="export_descr_unit.txt";
    $buildings="export_descr_buildings.txt";
    
    $bi_saves="bi/saves/";
    $tw_path = 'C:/Program Files/Activision/Rome - Total War/';											
    $bi_data="bi/data/";
    
    
    $fn=$tw_path.$bi_data.$unitsfile;
    
    open (FIL, '<', $fn ) or die "cannot open units file $!";
    @lines=<FIL>;
    close FIL;
    
    foreach $line (@lines) { push @units, $line =~ m/^type\s+(\w.*)$/;};  #match unit type to beginning of line followed the word type then whitespace and collect the unit name in @units
    
    $count=@units;
    @units= sort @units;
    
    print "There are  $count units defined in $bi_data$unitsfile\n\n";
    
    print "ENTER NAME OF SAVE FILE";
    
    $savefile=<STDIN>;
    chomp($savefile);
    
    $fn=$tw_path.$bi_saves.$savefile;
    open ( FIL,'<', $fn)					or die "Can't open $!";
    binmode FIL;
    
    while (read(FIL,$buf,200000))				#read 200K at a time. 
    {
     	@ascii=  $buf =~/[\w ]{5,}/g ;			#match all that look like names. 
    	push @names,@ascii;						#collect them.
    }
    close (FIL);
    
    
    
    %counts;										#hash to count
    for (@names) {$counts{$_}++;}; 				#hash contains names as keys with counts as values.
    
    
    $dest=$savefile.".unitcounts.txt";
    
    open (TO , '>', $dest);
    for (@units)
    {
    
     print TO  $_," = ", $counts{$_},"\n";
     if ($counts{$_}) { print $_, " = ", $counts{$_},"\n";};
    };
    
    close (TO);
    
    print "HIT ENTER TO EXIT";
    <STDIN>;
    DISCLAIMER: I'm still learning to write perl, so I'm sorry for the messy code.
    The plural of anectode is not data - Anonymous Scientist

    I don't believe in superstition. It brings bad luck. - Umberto Eco

  2. #2

    Default Re: Perl script to count all units in a saved game

    hmm.. very interesting, so if there is more than one unit of the same type, couldn't u just have them use the same unit file in the export descr unit text. and u would free up space to add a new unit? im not sure if anyone has though of this yet, (probably have) but it would make it possible to add a lot more units for a mod.

  3. #3
    The Philosopher Duke Member Suraknar's Avatar
    Join Date
    Dec 2002
    Location
    Navigating the realm of Ideas
    Posts
    707

    Default Re: Perl script to count all units in a saved game

    Very very Interesting...

    Specially the part about counting units from EDU...could you enhance it to Count the Units in descr_model_battle.txt?

    I do not have BI yet, but in 1.2 there is a max of 255 models in dmb and a max of 500 types in edu...

    This could really be handy indeed!
    Duke Surak'nar
    "Η ΤΑΝ Η ΕΠΙ ΤΑΣ"
    From: Residing:
    Traveled to: Over 70 Countries, most recent: and

    ~ Ask not what modding can do for you, rather ask what you can do for modding ~
    ~ Everyone dies, not everyone really fights ~

  4. #4
    Member Member afrit's Avatar
    Join Date
    Jun 2004
    Location
    USA
    Posts
    321

    Default Re: Perl script to count all units in a saved game

    Suraknar,
    I'm curious. Why do you need to count the models in descr_model_battle?

    A simple text search shows 235 type declarations and 360 skeleton declarations.
    The plural of anectode is not data - Anonymous Scientist

    I don't believe in superstition. It brings bad luck. - Umberto Eco

  5. #5
    The Philosopher Duke Member Suraknar's Avatar
    Join Date
    Dec 2002
    Location
    Navigating the realm of Ideas
    Posts
    707

    Default Re: Perl script to count all units in a saved game

    Hrm...

    Maybe for the same reason that your script counts the types in EDU? I mean a simple Text Search can do it there too, yes? ;)

    Seriously, you worked on something, and I am trying to find a use for it that is all. If it can help modders in some way, the way that I see is that, as there are some limits here to work with, and I am working on integrating new models and units mainly, so for me it would be handy to keep track in this way, rather than dismiss your good work, by doing simple text searches...

    Duke Surak'nar
    "Η ΤΑΝ Η ΕΠΙ ΤΑΣ"
    From: Residing:
    Traveled to: Over 70 Countries, most recent: and

    ~ Ask not what modding can do for you, rather ask what you can do for modding ~
    ~ Everyone dies, not everyone really fights ~

  6. #6
    Member Member afrit's Avatar
    Join Date
    Jun 2004
    Location
    USA
    Posts
    321

    Default Re: Perl script to count all units in a saved game

    Suraknar,
    thanks for your interest. My script needed the names of all units so that it can look them up in the .SAV file (to avoid counting non-unit names such as buildings).

    The only thing I'm using my script for now is to check on the AI and see if it is making units of a certain type, and when. It is A LOT faster to run the script on a saved game than to toggle_fow and activate perfect_spy.


    My goal is to be able to edit the .SAV file to introduce interesting aspects in the game. e.g have the AI faction destroy buildings I know it doesn't use, or spawn armies outside of cities etc...


    Too bad very little is known about the saved game format.

    The plural of anectode is not data - Anonymous Scientist

    I don't believe in superstition. It brings bad luck. - Umberto Eco

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Single Sign On provided by vBSSO