Assimilation Tutorial #1: Day & Night Modes


 

Posted by WarZone (209.223.137.*) on March 12, 1999 at 23:21:58:

Assimilation Tutorial #1
By: WarZone


This tutorial will allow you to add an extremly realistic day and night mode into your mods. In order to use this code, you MUST credit to Assimilation in your documentation/web site, and link to Assimilation. The code itself is actually rather short, but is a bit difficult to understand. Oh well, that's life kiddo.

Okay, let's get started. Open up g_locals.h and add the following lines to the bottom:

int daycycletime;

int lightlevel;

int oldlightlevel;

extern cvar_t *day;

Now, open up g_main.c and add this line near the other cvar's (top of the file):

cvar_t *day;

Next open g_save.c and add this line to the middle of InitGame() (right after "fraglimit" is declared):

  day = gi.cvar ("day", "10", 0);

Finally, open up g_main.c and add this lines to G_RunFrame():

void G_RunFrame (void)

{

  int     i = 0;

  int     free = 0, num = 0;

  edict_t      *ent;

  char    lights[2]; // new line

  level.framenum++;
  level.time = level.framenum*FRAMETIME;

// new code starts here
  if (day->value)
    daycycletime = (int)((day->value) * 600 + 1);
  else
    daycycletime = 0;

  if (daycycletime > 0)
  {
    oldlightlevel = lightlevel;
    lightlevel = level.framenum % (daycycletime);
    if (lightlevel > (daycycletime / 2))
      lightlevel = daycycletime - lightlevel;
    lightlevel = 52 - (int)(((float) lightlevel / (float) (daycycletime/2)) * 52);
    if (lightlevel <= 13)
      lightlevel = 0;
    else if (lightlevel >= 35)
      lightlevel = 25;
    else
      lightlevel = lightlevel - 13;
    lightlevel = 'c' + lightlevel;
    if (lightlevel > 'z') lightlevel = 'z' - (lightlevel - 'z');
    lights[0] = lightlevel;
    lights[1] = '\0';
    gi.configstring(CS_LIGHTS+0, lights);
  }
// new code ends here

Welp, that's it! You should probably read throught this code a couple [hundred] times until it makes sense to you. Heh, I helped write it and it still confuses me at times. Oh yeah, one other thing.. this code freaks out quake2 demos. They don't seem to record the globals lights very well.

-- WarZone
[ Assimiltion -- Resistance Is Futile -- http://www.planetquake.com/assimilation ]


Follow Ups

Posted by WarZone (209.223.137.*) on March 13, 1999 at 14:19:39:

In Reply to: Assimilation Tutorial #1: Day & Night Modes posted by WarZone on March 12, 1999 at 23:21:58:

You control the length of the "day" with the cvar "day". So:
>set day 20
will give you a daylight cycle of twenty minutes.

-- WarZone