Advertisement
Hajt

Loading CS2D map in PHP

Jul 26th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <?php
  2. function ReadByte($file) {
  3.     return unpack("C", fgets($file,2))[1];
  4. }
  5.  
  6. function ReadInt($file) {
  7.     return unpack("i", fgets($file,5))[1];
  8. }
  9.  
  10. function ReadString($file) {
  11.     return trim(fgets($file));
  12. }
  13.  
  14. $map = "de_cs2d";
  15. $file = fopen("$map.map", "r");
  16.  
  17. // Header
  18. $header = ReadString($file);
  19. if ($header != "Unreal Software's Counter-Strike 2D Map File (max)") {
  20.     exit("The header is not supported\n");
  21. }
  22.  
  23. // 10 bytes for map settings
  24. ReadByte($file);
  25. $modifiers = ReadByte($file);
  26. ReadByte($file);
  27. ReadByte($file);
  28. ReadByte($file);
  29. ReadByte($file);
  30. ReadByte($file);
  31. ReadByte($file);
  32. ReadByte($file);
  33. ReadByte($file);
  34.  
  35. // 10 ints for map settings
  36. ReadInt($file);
  37. ReadInt($file);
  38. ReadInt($file);
  39. ReadInt($file);
  40. ReadInt($file);
  41. ReadInt($file);
  42. ReadInt($file);
  43. ReadInt($file);
  44. ReadInt($file);
  45. ReadInt($file);
  46.  
  47. // 10 strings for map settings
  48. ReadString($file);
  49. ReadString($file);
  50. ReadString($file);
  51. ReadString($file);
  52. ReadString($file);
  53. ReadString($file);
  54. ReadString($file);
  55. ReadString($file);
  56. ReadString($file);
  57. ReadString($file);
  58.  
  59. // More map settings
  60. ReadString($file);
  61. ReadString($file);
  62. $tiles = ReadByte($file);
  63. $map_xsize = ReadInt($file);
  64. $map_ysize = ReadInt($file);
  65. ReadString($file);
  66. ReadInt($file);
  67. ReadInt($file);
  68. ReadByte($file);
  69. ReadByte($file);
  70. ReadByte($file);
  71.  
  72. // Header Test
  73. $test = ReadString($file);
  74. if ($test != "ed.erawtfoslaernu") {
  75.     exit("The header is corrupted\n");
  76. }
  77.  
  78. // Tile Modes
  79. for ($i = 0; $i <= $tiles; $i++) {
  80.     $mode = ReadByte($file);
  81.     echo "$i = $mode\n";
  82. }
  83.  
  84. // Map
  85. for ($x = 0; $x <= $map_xsize; $x++) {
  86.     for ($y = 0; $y <= $map_ysize; $y++) {
  87.         $tile = ReadByte($file);
  88.         echo "$x|$y = $tile\n";
  89.     }
  90. }
  91.  
  92. // Modifiers
  93. if ($modifiers == 1) {
  94.     exit("The modifiers are not supported\n");
  95. }
  96.  
  97. // Entities
  98. $entity_count = ReadInt($file);
  99. for ($j = 1; $j <= $entity_count; $j++) {
  100.     ReadString($file);
  101.     $entity_type = ReadByte($file);
  102.     $entity_x = ReadInt($file);
  103.     $entity_y = ReadInt($file);
  104.     ReadString($file);
  105.     echo "$entity_x|$entity_y = $entity_type\n";
  106.    
  107.     for ($k = 1; $k <= 10; $k++) {
  108.         ReadInt($file);
  109.         ReadString($file);
  110.     }
  111. }
  112.  
  113. // End of file
  114. fclose($file);
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement