P_LIGHTS.C 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //**************************************************************************
  2. //**
  3. //** p_lights.c : Heretic 2 : Raven Software, Corp.
  4. //**
  5. //** $RCSfile: p_lights.c,v $
  6. //** $Revision: 1.2 $
  7. //** $Date: 95/07/11 10:23:36 $
  8. //** $Author: cjr $
  9. //**
  10. //**************************************************************************
  11. #include "h2def.h"
  12. #include "p_local.h"
  13. //============================================================================
  14. //
  15. // T_Light
  16. //
  17. //============================================================================
  18. void T_Light(light_t *light)
  19. {
  20. if(light->count)
  21. {
  22. light->count--;
  23. return;
  24. }
  25. switch(light->type)
  26. {
  27. case LITE_FADE:
  28. light->sector->lightlevel = ((light->sector->lightlevel<<FRACBITS)
  29. +light->value2)>>FRACBITS;
  30. if(light->tics2 == 1)
  31. {
  32. if(light->sector->lightlevel >= light->value1)
  33. {
  34. light->sector->lightlevel = light->value1;
  35. P_RemoveThinker(&light->thinker);
  36. }
  37. }
  38. else if(light->sector->lightlevel <= light->value1)
  39. {
  40. light->sector->lightlevel = light->value1;
  41. P_RemoveThinker(&light->thinker);
  42. }
  43. break;
  44. case LITE_GLOW:
  45. light->sector->lightlevel = ((light->sector->lightlevel<<FRACBITS)
  46. +light->tics1)>>FRACBITS;
  47. if(light->tics2 == 1)
  48. {
  49. if(light->sector->lightlevel >= light->value1)
  50. {
  51. light->sector->lightlevel = light->value1;
  52. light->tics1 = -light->tics1;
  53. light->tics2 = -1; // reverse direction
  54. }
  55. }
  56. else if(light->sector->lightlevel <= light->value2)
  57. {
  58. light->sector->lightlevel = light->value2;
  59. light->tics1 = -light->tics1;
  60. light->tics2 = 1; // reverse direction
  61. }
  62. break;
  63. case LITE_FLICKER:
  64. if(light->sector->lightlevel == light->value1)
  65. {
  66. light->sector->lightlevel = light->value2;
  67. light->count = (P_Random()&7)+1;
  68. }
  69. else
  70. {
  71. light->sector->lightlevel = light->value1;
  72. light->count = (P_Random()&31)+1;
  73. }
  74. break;
  75. case LITE_STROBE:
  76. if(light->sector->lightlevel == light->value1)
  77. {
  78. light->sector->lightlevel = light->value2;
  79. light->count = light->tics2;
  80. }
  81. else
  82. {
  83. light->sector->lightlevel = light->value1;
  84. light->count = light->tics1;
  85. }
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. //============================================================================
  92. //
  93. // EV_SpawnLight
  94. //
  95. //============================================================================
  96. boolean EV_SpawnLight(line_t *line, byte *arg, lighttype_t type)
  97. {
  98. light_t *light;
  99. sector_t *sec;
  100. int secNum;
  101. int arg1, arg2, arg3, arg4;
  102. boolean think;
  103. boolean rtn;
  104. arg1 = arg[1] > 255 ? 255 : arg[1];
  105. arg1 = arg1 < 0 ? 0 : arg1;
  106. arg2 = arg[2] > 255 ? 255 : arg[2];
  107. arg2 = arg2 < 0 ? 0 : arg2;
  108. arg3 = arg[3] > 255 ? 255 : arg[3];
  109. arg3 = arg3 < 0 ? 0 : arg3;
  110. arg4 = arg[4] > 255 ? 255 : arg[4];
  111. arg4 = arg4 < 0 ? 0 : arg4;
  112. secNum = -1;
  113. rtn = false;
  114. think = false;
  115. while((secNum = P_FindSectorFromTag(arg[0], secNum)) >= 0)
  116. {
  117. think = false;
  118. sec = &sectors[secNum];
  119. light = (light_t *)Z_Malloc(sizeof(light_t), PU_LEVSPEC, 0);
  120. light->type = type;
  121. light->sector = sec;
  122. light->count = 0;
  123. rtn = true;
  124. switch(type)
  125. {
  126. case LITE_RAISEBYVALUE:
  127. sec->lightlevel += arg1;
  128. if(sec->lightlevel > 255)
  129. {
  130. sec->lightlevel = 255;
  131. }
  132. break;
  133. case LITE_LOWERBYVALUE:
  134. sec->lightlevel -= arg1;
  135. if(sec->lightlevel < 0)
  136. {
  137. sec->lightlevel = 0;
  138. }
  139. break;
  140. case LITE_CHANGETOVALUE:
  141. sec->lightlevel = arg1;
  142. if(sec->lightlevel < 0)
  143. {
  144. sec->lightlevel = 0;
  145. }
  146. else if(sec->lightlevel > 255)
  147. {
  148. sec->lightlevel = 255;
  149. }
  150. break;
  151. case LITE_FADE:
  152. think = true;
  153. light->value1 = arg1; // destination lightlevel
  154. light->value2 = FixedDiv((arg1-sec->lightlevel)<<FRACBITS,
  155. arg2<<FRACBITS); // delta lightlevel
  156. if(sec->lightlevel <= arg1)
  157. {
  158. light->tics2 = 1; // get brighter
  159. }
  160. else
  161. {
  162. light->tics2 = -1;
  163. }
  164. break;
  165. case LITE_GLOW:
  166. think = true;
  167. light->value1 = arg1; // upper lightlevel
  168. light->value2 = arg2; // lower lightlevel
  169. light->tics1 = FixedDiv((arg1-sec->lightlevel)<<FRACBITS,
  170. arg3<<FRACBITS); // lightlevel delta
  171. if(sec->lightlevel <= arg1)
  172. {
  173. light->tics2 = 1; // get brighter
  174. }
  175. else
  176. {
  177. light->tics2 = -1;
  178. }
  179. break;
  180. case LITE_FLICKER:
  181. think = true;
  182. light->value1 = arg1; // upper lightlevel
  183. light->value2 = arg2; // lower lightlevel
  184. sec->lightlevel = light->value1;
  185. light->count = (P_Random()&64)+1;
  186. break;
  187. case LITE_STROBE:
  188. think = true;
  189. light->value1 = arg1; // upper lightlevel
  190. light->value2 = arg2; // lower lightlevel
  191. light->tics1 = arg3; // upper tics
  192. light->tics2 = arg4; // lower tics
  193. light->count = arg3;
  194. sec->lightlevel = light->value1;
  195. break;
  196. default:
  197. rtn = false;
  198. break;
  199. }
  200. if(think)
  201. {
  202. P_AddThinker(&light->thinker);
  203. light->thinker.function = T_Light;
  204. }
  205. else
  206. {
  207. Z_Free(light);
  208. }
  209. }
  210. return rtn;
  211. }
  212. //============================================================================
  213. //
  214. // T_Phase
  215. //
  216. //============================================================================
  217. int PhaseTable[64] =
  218. {
  219. 128, 112, 96, 80, 64, 48, 32, 32,
  220. 16, 16, 16, 0, 0, 0, 0, 0,
  221. 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0,
  223. 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 16, 16, 16,
  226. 32, 32, 48, 64, 80, 96, 112, 128
  227. };
  228. void T_Phase(phase_t *phase)
  229. {
  230. phase->index = (phase->index+1)&63;
  231. phase->sector->lightlevel = phase->base+PhaseTable[phase->index];
  232. }
  233. //==========================================================================
  234. //
  235. // P_SpawnPhasedLight
  236. //
  237. //==========================================================================
  238. void P_SpawnPhasedLight(sector_t *sector, int base, int index)
  239. {
  240. phase_t *phase;
  241. phase = Z_Malloc(sizeof(*phase), PU_LEVSPEC, 0);
  242. P_AddThinker(&phase->thinker);
  243. phase->sector = sector;
  244. if(index == -1)
  245. { // sector->lightlevel as the index
  246. phase->index = sector->lightlevel&63;
  247. }
  248. else
  249. {
  250. phase->index = index&63;
  251. }
  252. phase->base = base&255;
  253. sector->lightlevel = phase->base+PhaseTable[phase->index];
  254. phase->thinker.function = T_Phase;
  255. sector->special = 0;
  256. }
  257. //==========================================================================
  258. //
  259. // P_SpawnLightSequence
  260. //
  261. //==========================================================================
  262. void P_SpawnLightSequence(sector_t *sector, int indexStep)
  263. {
  264. sector_t *sec;
  265. sector_t *nextSec;
  266. sector_t *tempSec;
  267. int seqSpecial;
  268. int i;
  269. int count;
  270. fixed_t index;
  271. fixed_t indexDelta;
  272. int base;
  273. seqSpecial = LIGHT_SEQUENCE; // look for Light_Sequence, first
  274. sec = sector;
  275. count = 1;
  276. do
  277. {
  278. nextSec = NULL;
  279. sec->special = LIGHT_SEQUENCE_START; // make sure that the search doesn't back up.
  280. for(i = 0; i < sec->linecount; i++)
  281. {
  282. tempSec = getNextSector(sec->lines[i], sec);
  283. if(!tempSec)
  284. {
  285. continue;
  286. }
  287. if(tempSec->special == seqSpecial)
  288. {
  289. if(seqSpecial == LIGHT_SEQUENCE)
  290. {
  291. seqSpecial = LIGHT_SEQUENCE_ALT;
  292. }
  293. else
  294. {
  295. seqSpecial = LIGHT_SEQUENCE;
  296. }
  297. nextSec = tempSec;
  298. count++;
  299. }
  300. }
  301. sec = nextSec;
  302. } while(sec);
  303. sec = sector;
  304. count *= indexStep;
  305. index = 0;
  306. indexDelta = FixedDiv(64*FRACUNIT, count*FRACUNIT);
  307. base = sector->lightlevel;
  308. do
  309. {
  310. nextSec = NULL;
  311. if(sec->lightlevel)
  312. {
  313. base = sec->lightlevel;
  314. }
  315. P_SpawnPhasedLight(sec, base, index>>FRACBITS);
  316. index += indexDelta;
  317. for(i = 0; i < sec->linecount; i++)
  318. {
  319. tempSec = getNextSector(sec->lines[i], sec);
  320. if(!tempSec)
  321. {
  322. continue;
  323. }
  324. if(tempSec->special == LIGHT_SEQUENCE_START)
  325. {
  326. nextSec = tempSec;
  327. }
  328. }
  329. sec = nextSec;
  330. } while(sec);
  331. }