doom

a minimalistic implementation of doom
git clone git://ssnf.xyz/doom
Log | Files | Refs

p_lights.c (5016B)


      1 #include "z_zone.h"
      2 #include "m_random.h"
      3 
      4 #include "doomdef.h"
      5 #include "p_local.h"
      6 
      7 
      8 
      9 #include "r_state.h"
     10 
     11 
     12 
     13 
     14 
     15 
     16 
     17 
     18 void T_FireFlicker (fireflicker_t* flick)
     19 {
     20     int	amount;
     21 	
     22     if (--flick->count)
     23 	return;
     24 	
     25     amount = (P_Random()&3)*16;
     26     
     27     if (flick->sector->lightlevel - amount < flick->minlight)
     28 	flick->sector->lightlevel = flick->minlight;
     29     else
     30 	flick->sector->lightlevel = flick->maxlight - amount;
     31 
     32     flick->count = 4;
     33 }
     34 
     35 
     36 
     37 
     38 
     39 
     40 void P_SpawnFireFlicker (sector_t*	sector)
     41 {
     42     fireflicker_t*	flick;
     43 	
     44     
     45     
     46     sector->special = 0; 
     47 	
     48     flick = Z_Malloc ( sizeof(*flick), PU_LEVSPEC, 0);
     49 
     50     P_AddThinker (&flick->thinker);
     51 
     52     flick->thinker.function.acp1 = (actionf_p1) T_FireFlicker;
     53     flick->sector = sector;
     54     flick->maxlight = sector->lightlevel;
     55     flick->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel)+16;
     56     flick->count = 4;
     57 }
     58 
     59 
     60 
     61 
     62 
     63 
     64 
     65 
     66 
     67 
     68 
     69 
     70 void T_LightFlash (lightflash_t* flash)
     71 {
     72     if (--flash->count)
     73 	return;
     74 	
     75     if (flash->sector->lightlevel == flash->maxlight)
     76     {
     77 	flash-> sector->lightlevel = flash->minlight;
     78 	flash->count = (P_Random()&flash->mintime)+1;
     79     }
     80     else
     81     {
     82 	flash-> sector->lightlevel = flash->maxlight;
     83 	flash->count = (P_Random()&flash->maxtime)+1;
     84     }
     85 
     86 }
     87 
     88 
     89 
     90 
     91 
     92 
     93 
     94 
     95 
     96 void P_SpawnLightFlash (sector_t*	sector)
     97 {
     98     lightflash_t*	flash;
     99 
    100     
    101     sector->special = 0;	
    102 	
    103     flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
    104 
    105     P_AddThinker (&flash->thinker);
    106 
    107     flash->thinker.function.acp1 = (actionf_p1) T_LightFlash;
    108     flash->sector = sector;
    109     flash->maxlight = sector->lightlevel;
    110 
    111     flash->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
    112     flash->maxtime = 64;
    113     flash->mintime = 7;
    114     flash->count = (P_Random()&flash->maxtime)+1;
    115 }
    116 
    117 
    118 
    119 
    120 
    121 
    122 
    123 
    124 
    125 
    126 
    127 void T_StrobeFlash (strobe_t*		flash)
    128 {
    129     if (--flash->count)
    130 	return;
    131 	
    132     if (flash->sector->lightlevel == flash->minlight)
    133     {
    134 	flash-> sector->lightlevel = flash->maxlight;
    135 	flash->count = flash->brighttime;
    136     }
    137     else
    138     {
    139 	flash-> sector->lightlevel = flash->minlight;
    140 	flash->count =flash->darktime;
    141     }
    142 
    143 }
    144 
    145 
    146 
    147 
    148 
    149 
    150 
    151 
    152 void
    153 P_SpawnStrobeFlash
    154 ( sector_t*	sector,
    155   int		fastOrSlow,
    156   int		inSync )
    157 {
    158     strobe_t*	flash;
    159 	
    160     flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
    161 
    162     P_AddThinker (&flash->thinker);
    163 
    164     flash->sector = sector;
    165     flash->darktime = fastOrSlow;
    166     flash->brighttime = STROBEBRIGHT;
    167     flash->thinker.function.acp1 = (actionf_p1) T_StrobeFlash;
    168     flash->maxlight = sector->lightlevel;
    169     flash->minlight = P_FindMinSurroundingLight(sector, sector->lightlevel);
    170 		
    171     if (flash->minlight == flash->maxlight)
    172 	flash->minlight = 0;
    173 
    174     
    175     sector->special = 0;	
    176 
    177     if (!inSync)
    178 	flash->count = (P_Random()&7)+1;
    179     else
    180 	flash->count = 1;
    181 }
    182 
    183 
    184 
    185 
    186 
    187 void EV_StartLightStrobing(line_t*	line)
    188 {
    189     int		secnum;
    190     sector_t*	sec;
    191 	
    192     secnum = -1;
    193     while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
    194     {
    195 	sec = &sectors[secnum];
    196 	if (sec->specialdata)
    197 	    continue;
    198 	
    199 	P_SpawnStrobeFlash (sec,SLOWDARK, 0);
    200     }
    201 }
    202 
    203 
    204 
    205 
    206 
    207 
    208 void EV_TurnTagLightsOff(line_t* line)
    209 {
    210     int			i;
    211     int			j;
    212     int			min;
    213     sector_t*		sector;
    214     sector_t*		tsec;
    215     line_t*		templine;
    216 	
    217     sector = sectors;
    218     
    219     for (j = 0;j < numsectors; j++, sector++)
    220     {
    221 	if (sector->tag == line->tag)
    222 	{
    223 	    min = sector->lightlevel;
    224 	    for (i = 0;i < sector->linecount; i++)
    225 	    {
    226 		templine = sector->lines[i];
    227 		tsec = getNextSector(templine,sector);
    228 		if (!tsec)
    229 		    continue;
    230 		if (tsec->lightlevel < min)
    231 		    min = tsec->lightlevel;
    232 	    }
    233 	    sector->lightlevel = min;
    234 	}
    235     }
    236 }
    237 
    238 
    239 
    240 
    241 
    242 void
    243 EV_LightTurnOn
    244 ( line_t*	line,
    245   int		bright )
    246 {
    247     int		i;
    248     int		j;
    249     sector_t*	sector;
    250     sector_t*	temp;
    251     line_t*	templine;
    252 	
    253     sector = sectors;
    254 	
    255     for (i=0;i<numsectors;i++, sector++)
    256     {
    257 	if (sector->tag == line->tag)
    258 	{
    259 	    
    260 	    
    261 	    
    262 	    if (!bright)
    263 	    {
    264 		for (j = 0;j < sector->linecount; j++)
    265 		{
    266 		    templine = sector->lines[j];
    267 		    temp = getNextSector(templine,sector);
    268 
    269 		    if (!temp)
    270 			continue;
    271 
    272 		    if (temp->lightlevel > bright)
    273 			bright = temp->lightlevel;
    274 		}
    275 	    }
    276 	    sector-> lightlevel = bright;
    277 	}
    278     }
    279 }
    280 
    281     
    282 
    283 
    284 
    285 
    286 void T_Glow(glow_t*	g)
    287 {
    288     switch(g->direction)
    289     {
    290       case -1:
    291 	
    292 	g->sector->lightlevel -= GLOWSPEED;
    293 	if (g->sector->lightlevel <= g->minlight)
    294 	{
    295 	    g->sector->lightlevel += GLOWSPEED;
    296 	    g->direction = 1;
    297 	}
    298 	break;
    299 	
    300       case 1:
    301 	
    302 	g->sector->lightlevel += GLOWSPEED;
    303 	if (g->sector->lightlevel >= g->maxlight)
    304 	{
    305 	    g->sector->lightlevel -= GLOWSPEED;
    306 	    g->direction = -1;
    307 	}
    308 	break;
    309     }
    310 }
    311 
    312 
    313 void P_SpawnGlowingLight(sector_t*	sector)
    314 {
    315     glow_t*	g;
    316 	
    317     g = Z_Malloc( sizeof(*g), PU_LEVSPEC, 0);
    318 
    319     P_AddThinker(&g->thinker);
    320 
    321     g->sector = sector;
    322     g->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
    323     g->maxlight = sector->lightlevel;
    324     g->thinker.function.acp1 = (actionf_p1) T_Glow;
    325     g->direction = -1;
    326 
    327     sector->special = 0;
    328 }
    329