OWARPT.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OWARPT.CPP
  21. // Description : War point
  22. #include <COLOR.h>
  23. #include <OVGABUF.h>
  24. #include <OWORLD.h>
  25. #include <OWARPT.h>
  26. #include <ALL.h>
  27. // --------- define constant ---------//
  28. // divide the map into zone, each zone has size WARPOINT_ZONE_SIZE
  29. #define WARPOINT_ZONE_SIZE 8
  30. #define WARPOINT_ZONE_COLUMN ((MAX_MAP_WIDTH + WARPOINT_ZONE_SIZE -1) / WARPOINT_ZONE_SIZE)
  31. #define WARPOINT_ZONE_ROW ((MAX_MAP_HEIGHT + WARPOINT_ZONE_SIZE -1) / WARPOINT_ZONE_SIZE)
  32. #define WARPOINT_STRENGTH 0x100000
  33. #define WARPOINT_STRENGTH_MAX 0x1000000
  34. #define DRAW_PHASE_PERIOD 16
  35. // ---------- begin of function WarPoint::inc ----------//
  36. void WarPoint::inc()
  37. {
  38. if( (strength += WARPOINT_STRENGTH) > WARPOINT_STRENGTH_MAX )
  39. strength = WARPOINT_STRENGTH_MAX;
  40. }
  41. // ---------- end of function WarPoint::inc ----------//
  42. // ---------- begin of function WarPoint::decay ----------//
  43. inline void WarPoint::decay()
  44. {
  45. strength >>= 1;
  46. }
  47. // ---------- end of function WarPoint::decay ----------//
  48. // ------ begin of function WarPointArray::WarPointArray -------//
  49. WarPointArray::WarPointArray()
  50. {
  51. war_point = NULL;
  52. init_flag = 0;
  53. }
  54. // ------ end of function WarPointArray::WarPointArray -------//
  55. // ------ begin of function WarPointArray::~WarPointArray -------//
  56. WarPointArray::~WarPointArray()
  57. {
  58. deinit();
  59. }
  60. // ------ end of function WarPointArray::~WarPointArray -------//
  61. // ------ begin of function WarPointArray::init -------//
  62. void WarPointArray::init()
  63. {
  64. deinit();
  65. // allocate memory for war_point
  66. war_point = (WarPoint *)mem_add( sizeof(WarPoint) * WARPOINT_ZONE_COLUMN * WARPOINT_ZONE_ROW );
  67. memset(war_point, 0, sizeof(WarPoint) * WARPOINT_ZONE_COLUMN * WARPOINT_ZONE_ROW );
  68. draw_phase = 0;
  69. init_flag = 1;
  70. }
  71. // ------ end of function WarPointArray::init -------//
  72. // ------ begin of function WarPointArray::deinit -------//
  73. void WarPointArray::deinit()
  74. {
  75. if( init_flag )
  76. {
  77. mem_del(war_point);
  78. init_flag = 0;
  79. }
  80. }
  81. // ------ end of function WarPointArray::deinit -------//
  82. // ------ begin of function WarPointArray::draw_dot -------//
  83. void WarPointArray::draw_dot()
  84. {
  85. if( ++draw_phase >= DRAW_PHASE_PERIOD )
  86. draw_phase = 0;
  87. if( draw_phase & 1)
  88. return;
  89. static unsigned char dotColor[DRAW_PHASE_PERIOD/2] =
  90. { 0xa0, 0xa4, 0xa8, 0x00, 0xb0, 0xb4, 0xb8, 0x00 };
  91. unsigned char color = dotColor[draw_phase / 2];
  92. int x,y;
  93. short mapY;
  94. unsigned char *writePtr;
  95. unsigned char *vgaBufPtr = (unsigned char *)vga_back.buf_ptr();
  96. int vgaBufPitch = vga_back.buf_pitch();
  97. for( y = 0, mapY=MAP_Y1; y < WARPOINT_ZONE_ROW; ++y, mapY+=WARPOINT_ZONE_SIZE)
  98. {
  99. WarPoint *warPt = war_point + y * WARPOINT_ZONE_COLUMN;
  100. writePtr = vgaBufPtr + vgaBufPitch * mapY + MAP_X1;
  101. for( x = 0; x < WARPOINT_ZONE_COLUMN; ++x, ++warPt, writePtr+=WARPOINT_ZONE_SIZE)
  102. {
  103. if( warPt->strength > 0 )
  104. {
  105. // draw a cross, UNEXPLORED_COLOR is not needed to check
  106. unsigned char *map1Ptr = writePtr;
  107. unsigned char *map2Ptr = writePtr + WARPOINT_ZONE_SIZE-2;
  108. for( int i = 1; i < WARPOINT_ZONE_SIZE; ++i )
  109. {
  110. *map1Ptr = color;
  111. *map2Ptr = color;
  112. map1Ptr += vgaBufPitch + 1;
  113. map2Ptr += vgaBufPitch - 1;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. // ------ end of function WarPointArray::draw_dot -------//
  120. // ------ begin of function WarPointArray::process -------//
  121. void WarPointArray::process()
  122. {
  123. WarPoint *warPt = war_point;
  124. for( int i = WARPOINT_ZONE_COLUMN * WARPOINT_ZONE_ROW; i > 0; --i, warPt++)
  125. {
  126. warPt->decay();
  127. }
  128. }
  129. // ------ end of function WarPointArray::process -------//
  130. // ------ begin of function WarPointArray::get_ptr -------//
  131. WarPoint *WarPointArray::get_ptr(int xLoc, int yLoc)
  132. {
  133. err_when(!init_flag);
  134. int c = xLoc / WARPOINT_ZONE_SIZE;
  135. int r = yLoc / WARPOINT_ZONE_SIZE;
  136. err_when( c < 0 || c >= WARPOINT_ZONE_COLUMN);
  137. err_when( r < 0 || r >= WARPOINT_ZONE_ROW);
  138. return war_point+ (r * WARPOINT_ZONE_COLUMN + c);
  139. }
  140. // ------ end of function WarPointArray::get_ptr -------//
  141. // ------ begin of function WarPointArray::add_point -------//
  142. void WarPointArray::add_point(int xLoc, int yLoc)
  143. {
  144. get_ptr(xLoc, yLoc)->inc();
  145. }
  146. // ------ end of function WarPointArray::add_point -------//