convert_monstermode.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  42. return $bits;
  43. }
  44. sub GetArgs {
  45. GetOptions(
  46. 'i=s' => \$sFileins, #Output file name.
  47. 'o=s' => \$sFileouts, #Input file name.
  48. 'help!' => \$sHelp,
  49. ) or $sHelp=1; #Display help if invalid options are supplied.
  50. if( $sHelp ) {
  51. print "Incorrect option specified. Available options:\n"
  52. ."\t --o=filename => Output file name. (file must be separate by coma if multiple) \n"
  53. ."\t --i=filenames => Input files name. (file must be separate by coma if multiple) \n";
  54. exit;
  55. }
  56. if($sFileins){
  57. chomp($sFileins);
  58. @aFilein = split(",",$sFileins);
  59. }
  60. if($sFileouts){
  61. chomp($sFileouts);
  62. @aFileout = split(",",$sFileouts);
  63. }
  64. unless(scalar(@aFileout)==scalar(@aFilein)){
  65. print "ERROR: Number of input files doesn't match number of output files. You must specify an output for each input:\n"
  66. ."afilein = [ @aFilein ] \n"
  67. ."afileout = [ @aFileout ] \n";
  68. exit;
  69. }
  70. }
  71. sub Main {
  72. my $sI=0;
  73. my($filename, $dir, $suffix) = fileparse($0);
  74. chdir $dir; #put ourself like was called in tool folder
  75. GetArgs();
  76. print "Running rAthena's Monster Mode Converter...\n";
  77. print "Files to be converted: '@aFilein' into '@aFileout'.\n";
  78. foreach my $sFile (@aFilein){
  79. my $sReplace=0; #should we replace file when finished
  80. my $sFileouttmp=$aFileout[$sI];
  81. if($sFile eq $sFileouttmp){
  82. $sReplace=1;
  83. $sFileouttmp = $sFileouttmp.".out";
  84. print "Asking to replace file tmp fileout= $sFileouttmp \n";
  85. }
  86. unless(open FHIN,"$sFile"){
  87. print "ERROR: Can't read or locate $sFile.\n";
  88. next;
  89. }
  90. unless(open FHOUT,">$sFileouttmp"){
  91. print "ERROR: Can't write or locate $aFileout[$sI].\n";
  92. next;
  93. }
  94. $sI++;
  95. while (<FHIN>){
  96. if( $_ =~ /^\s*$/) { #ignore empty line
  97. print FHOUT $_;
  98. next;
  99. }
  100. my @champ = split(",",$_);
  101. my $sDoconvertion=0; #should this comment be converted
  102. if( $_ =~ /^\/\// ) { # // line
  103. if(scalar(@champ)>0){
  104. $champ[0] =~ s!\/\/!!g;
  105. $sDoconvertion=looks_like_number($champ[0]);
  106. $champ[0] = "//".$champ[0]; #recomment it
  107. }
  108. if($sDoconvertion==0) {
  109. print FHOUT $_;
  110. next;
  111. }
  112. }
  113. if(scalar(@champ>0)){
  114. if($champ[25] =~ /^0[xX]/) {
  115. $champ[25] = sprintf("0x%X", convertmode($champ[25], $champ[30]));
  116. } else {
  117. $champ[25] = sprintf("0x%X", convertmode(sprintf("0x%X", $champ[25]), $champ[30]));
  118. }
  119. my $newline = join(",",@champ);
  120. print FHOUT $newline;
  121. }
  122. else { print FHOUT $_; }
  123. }
  124. close FHOUT;
  125. close FHIN;
  126. if($sReplace){
  127. unlink $sFile;
  128. rename $sFileouttmp, $sFile;
  129. }
  130. }
  131. }