OLIGHTN2.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 : OLIGHTN2.CPP
  21. // Description : class YLightning
  22. // Ownership : Gilbert
  23. #include <math.h>
  24. #include <stdlib.h>
  25. #include <ALL.h>
  26. #include <OVGABUF.h>
  27. #include <OLIGHTN.h>
  28. //------------ Define constant ----------//
  29. #define PI 3.141592654
  30. //--------- Begin of function YLightning::YLightning --------//
  31. YLightning::YLightning()
  32. {
  33. used_branch = 0;
  34. for( int i = 0; i < MAX_BRANCH; ++i)
  35. {
  36. branch[i] = 0;
  37. }
  38. }
  39. //--------- End of function YLightning::YLightning --------//
  40. //--------- Begin of function YLightning::~YLightning --------//
  41. YLightning::~YLightning()
  42. {
  43. for( int i = 0; i < MAX_BRANCH; ++i)
  44. {
  45. delete branch[i];
  46. branch[i] = 0;
  47. }
  48. }
  49. //--------- End of function YLightning::~YLightning --------//
  50. //--------- Begin of function YLightning::init --------//
  51. void YLightning::init(double fromX, double fromY, double toX, double toY,
  52. char energy)
  53. {
  54. for( int i = 0; i < MAX_BRANCH; ++i)
  55. {
  56. delete branch[i];
  57. branch[i] = 0;
  58. }
  59. used_branch = 0;
  60. Lightning::init(fromX, fromY, toX, toY, energy);
  61. branch_prob = 1000 * MAX_BRANCH / expect_steps;
  62. if( branch_prob < 10 )
  63. branch_prob = 10;
  64. if( branch_prob > 300 )
  65. branch_prob = 300;
  66. branch_left = 0;
  67. }
  68. //--------- End of function YLightning::init --------//
  69. //--------- Begin of function YLightning::move_particle --------//
  70. void YLightning::move_particle()
  71. {
  72. Lightning::move_particle();
  73. // determine if branching occurs
  74. if( (rand_seed() % 1000) <= branch_prob && used_branch < MAX_BRANCH)
  75. {
  76. char branch_energy;
  77. if(energy_level > 4)
  78. {
  79. branch[used_branch] = new YLightning;
  80. branch_energy = 4;
  81. }
  82. else
  83. {
  84. branch[used_branch] = new Lightning;
  85. branch_energy = 1;
  86. }
  87. //------ determine new location ------//
  88. // angle : attraction angle + or - PI/8 to PI*3/8
  89. // distant : 1/2 to 3/4 from dist(destx-x, desty-y);
  90. double branchDist = Lightning::dist(destx-x, desty-y)
  91. * ( 32 + rand_seed() % 16) / 64.0;
  92. double branchAngle= atan2(desty-y, destx-x) +
  93. ( 4 + rand_seed() % 8 ) * (branch_left ? PI / -32.0 : PI / 32.0);
  94. branch_left = !branch_left;
  95. branch[used_branch]->init(x, y, x+branchDist*cos(branchAngle),
  96. y+branchDist*sin(branchAngle), branch_energy);
  97. used_branch++;
  98. }
  99. }
  100. //--------- End of function YLightning::move_particle --------//
  101. //--------- Begin of function YLightning::draw_step --------//
  102. void YLightning::draw_step(VgaBuf *vgabuf)
  103. {
  104. Lightning::draw_step(vgabuf);
  105. for( int i = 0; i < used_branch; ++i)
  106. {
  107. branch[i]->draw_step(vgabuf);
  108. }
  109. }
  110. //--------- End of function YLightning::draw_step --------//
  111. //--------- Begin of function YLightning::draw_whole --------//
  112. void YLightning::draw_whole(VgaBuf *vgabuf)
  113. {
  114. while(!goal())
  115. {
  116. draw_step(vgabuf);
  117. }
  118. }
  119. //--------- End of function YLightning::draw_whole --------//