Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.63 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. # Scans an input file for items in any of ranged weapons category
  3. # and creates a file for melee damage, similar to "UFOextender: Gun Melee".
  4. # YAML module 1.23 is recommended.
  5. # Thanks to tinita and ingy from #yaml@freenode.net
  6. use strict;
  7. use warnings;
  8. use 5.010;
  9. # install YAML or YAML::XS with
  10. # cpan YAML
  11. # cpan YAML::XS
  12. # or
  13. # apt-get install libyaml-perl
  14. # apt-get install libyaml-libyaml-perl
  15.  
  16. use YAML qw/ LoadFile DumpFile /;
  17. #use YAML::XS qw/ LoadFile DumpFile /;
  18. use List::Util qw/ any none /;
  19. $YAML::UseHeader = 0;
  20. $YAML::Preserve = 1;
  21.  
  22. my ($filename) = @ARGV;
  23. #my ($filename) = '/usr/share/openxcom/mods/XComFiles/Ruleset/items_XCOMFILES.rul';
  24. #my ($filename) = 'items_XCOMFILES.rul';
  25. my $data = LoadFile($filename);
  26.  
  27. my $items_new = [];
  28.  
  29. #for my $item (@{ $data->{items} }) {
  30. # In case the single root key isn't 'items'
  31. my @key_list = keys(%$data);
  32. my $single_key = $key_list[0];
  33. for my $item (@{ $data->{ $single_key } }) {
  34.     my $categories = $item->{categories};
  35.     # if categories doesn't exist or is empty
  36.     if (not $categories or not @$categories) {
  37.         next;
  38.     }
  39.     # if categories doesn't contain any ranged weapon
  40.     if ( ( none { $_ eq "STR_PISTOLS" or
  41.                   $_ eq "STR_BOWS" or
  42.                   $_ eq "STR_SMGS" or
  43.                   $_ eq "STR_SHOTGUNS" or
  44.                   $_ eq "STR_RIFLES"  or
  45.                   $_ eq "STR_SNIPER_RIFLES" or
  46.                   $_ eq "STR_CANNONS" or
  47.                   $_ eq "STR_MACHINE_GUNS" or
  48.                   $_ eq "STR_LAUNCHERS" } @$categories ) or
  49.          ( any { $_ eq "STR_CLIPS" } @$categories ) ) {
  50.         next;
  51.     }
  52.     # delete all unnecessary fields; 'categories' is still needed
  53.     foreach my $key (keys %$item) {
  54.         if ( ($key ne 'categories') and ($key ne 'type') ) {
  55.             delete $item->{$key};
  56.         }
  57.     }
  58.     # add melee power/TUs/accuracy fields
  59.     if (any { $_ eq "STR_PISTOLS" } @$categories) {
  60.         $item->{meleePower}    =  20;
  61.         $item->{tuMelee}       =  15;
  62.         $item->{accuracyMelee} = 100;
  63.     }
  64.     if (any { $_ eq "STR_BOWS" } @$categories) {
  65.         $item->{meleePower}    =  20;
  66.         $item->{tuMelee}       =  15;
  67.         $item->{accuracyMelee} = 100;
  68.     }
  69.     if (any { $_ eq "STR_SMGS" } @$categories) {
  70.         $item->{meleePower}    =  20;
  71.         $item->{tuMelee}       =  15;
  72.         $item->{accuracyMelee} = 100;
  73.     }
  74.     if (any { $_ eq "STR_SHOTGUNS" } @$categories) {
  75.         $item->{meleePower}    =  50;
  76.         $item->{tuMelee}       =  40;
  77.         $item->{accuracyMelee} = 100;
  78.     }
  79.     if (any { $_ eq "STR_RIFLES" } @$categories) {
  80.         $item->{meleePower}    =  50;
  81.         $item->{tuMelee}       =  40;
  82.         $item->{accuracyMelee} = 100;
  83.     }
  84.     if (any { $_ eq "STR_SNIPER_RIFLES" } @$categories) {
  85.         $item->{meleePower}    =  50;
  86.         $item->{tuMelee}       =  40;
  87.         $item->{accuracyMelee} = 100;
  88.     }
  89.     if (any { $_ eq "STR_CANNONS" } @$categories) {
  90.         $item->{meleePower}    =  65;
  91.         $item->{tuMelee}       =  50;
  92.         $item->{accuracyMelee} = 100;
  93.     }
  94.     if (any { $_ eq "STR_MACHINE_GUNS" } @$categories) {
  95.         $item->{meleePower}    =  65;
  96.         $item->{tuMelee}       =  50;
  97.         $item->{accuracyMelee} = 100;
  98.     }
  99.     if (any { $_ eq "STR_LAUNCHERS" } @$categories) {
  100.         $item->{meleePower}    =  80;
  101.         $item->{tuMelee}       =  80;
  102.         $item->{accuracyMelee} = 100;
  103.     }
  104.     delete $item->{categories};
  105.     push @$items_new, $item;
  106. }
  107.  
  108. $data->{ $single_key } = $items_new;
  109.  
  110. DumpFile "Gun_Melee_nonstandard.rul", $data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement