convert_monstermode.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/perl
  2. # rAthena Monster Mode Converter
  3. # Upgrades monster's mode to the new structure.
  4. #
  5. use strict;
  6. use warnings;
  7. use Getopt::Long;
  8. use File::Basename;
  9. use Scalar::Util qw(looks_like_number);
  10. my $sFileins;
  11. my @aFilein = ("../db/re/mob_db.txt","../db/pre-re/mob_db.txt","../db/mob_db2.txt");
  12. my $sFileouts;
  13. my @aFileout = ("../db/re/mob_db.txt","../db/pre-re/mob_db.txt","../db/mob_db2.txt");
  14. my $sHelp = 0;
  15. Main();
  16. sub convertmode {
  17. my $bits = hex(shift);
  18. my $mexp = shift;
  19. if ($bits&32) { # MD_BOSS
  20. $bits -= 32;
  21. $bits |= 69206016; # Doesn't include MD_DETECTOR
  22. }
  23. if ($bits&64) { # MD_PLANT
  24. $bits -= 64;
  25. $bits |= 1507328;
  26. }
  27. if ($bits&256) { # MD_DETECTOR
  28. $bits -= 256;
  29. $bits |= 33554432;
  30. }
  31. if ($bits&4194304) { # MD_NORANDOM_WALK
  32. $bits -= 4194304;
  33. $bits |= 32;
  34. }
  35. if ($bits&8388608) { # MD_NOCAST_SKILL
  36. $bits -= 8388608;
  37. $bits |= 64;
  38. }
  39. if ($mexp > 0) # MD_MVP
  40. $bits |= 524288;
  41. return $bits;
  42. }
  43. sub GetArgs {
  44. GetOptions(
  45. 'i=s' => \$sFileins, #Output file name.
  46. 'o=s' => \$sFileouts, #Input file name.
  47. 'help!' => \$sHelp,
  48. ) or $sHelp=1; #Display help if invalid options are supplied.
  49. if( $sHelp ) {
  50. print "Incorrect option specified. Available options:\n"
  51. ."\t --o=filename => Output file name. (file must be separate by coma if multiple) \n"
  52. ."\t --i=filenames => Input files name. (file must be separate by coma if multiple) \n";
  53. exit;
  54. }
  55. if($sFileins){
  56. chomp($sFileins);
  57. @aFilein = split(",",$sFileins);
  58. }
  59. if($sFileouts){
  60. chomp($sFileouts);
  61. @aFileout = split(",",$sFileouts);
  62. }
  63. unless(scalar(@aFileout)==scalar(@aFilein)){
  64. print "ERROR: Number of input files doesn't match number of output files. You must specify an output for each input:\n"
  65. ."afilein = [ @aFilein ] \n"
  66. ."afileout = [ @aFileout ] \n";
  67. exit;
  68. }
  69. }
  70. sub Main {
  71. my $sI=0;
  72. my($filename, $dir, $suffix) = fileparse($0);
  73. chdir $dir; #put ourself like was called in tool folder
  74. GetArgs();
  75. print "Running rAthena's Monster Mode Converter...\n";
  76. print "Files to be converted: '@aFilein' into '@aFileout'.\n";
  77. foreach my $sFile (@aFilein){
  78. my $sReplace=0; #should we replace file when finished
  79. my $sFileouttmp=$aFileout[$sI];
  80. if($sFile eq $sFileouttmp){
  81. $sReplace=1;
  82. $sFileouttmp = $sFileouttmp.".out";
  83. print "Asking to replace file tmp fileout= $sFileouttmp \n";
  84. }
  85. unless(open FHIN,"$sFile"){
  86. print "ERROR: Can't read or locate $sFile.\n";
  87. next;
  88. }
  89. unless(open FHOUT,">$sFileouttmp"){
  90. print "ERROR: Can't write or locate $aFileout[$sI].\n";
  91. next;
  92. }
  93. $sI++;
  94. while (<FHIN>){
  95. if( $_ =~ /^\s*$/) { #ignore empty line
  96. print FHOUT $_;
  97. next;
  98. }
  99. my @champ = split(",",$_);
  100. my $sDoconvertion=0; #should this comment be converted
  101. if( $_ =~ /^\/\// ) { # // line
  102. if(scalar(@champ)>0){
  103. $champ[0] =~ s!\/\/!!g;
  104. $sDoconvertion=looks_like_number($champ[0]);
  105. $champ[0] = "//".$champ[0]; #recomment it
  106. }
  107. if($sDoconvertion==0) {
  108. print FHOUT $_;
  109. next;
  110. }
  111. }
  112. if(scalar(@champ>0)){
  113. $mode = $champ[25];
  114. $mexp = $champ[30];
  115. $champ[25] = sprintf("0x%X", convertmode($mode, $mexp));
  116. my $newline = join(",",@champ);
  117. print FHOUT $newline;
  118. }
  119. else { print FHOUT $_; }
  120. }
  121. close FHOUT;
  122. close FHIN;
  123. if($sReplace){
  124. unlink $sFile;
  125. rename $sFileouttmp, $sFile;
  126. }
  127. }
  128. }