/*
   CMSC 427 - Term Project Candidate
   ---------------------------------

   Beth Weinstein - bweinste@wam.umd.edu
   Ben Stewart - stewartb@wam.umd.edu
   Nawfel Tricha - Topaz@endorproductions.com

   Minimal OpenGL project using Microsoft Visual C++.
   This project is a premilinary candidate for the term 
   project required by CMSC 427.

   ABSTRACT:
      In this project, we will develop several fast 
      rendering and collision routines which would 
      require C/C++ speed and accuracy and then 
      implement a VisualJ++ interface.

   MODULE:
      Zumlin Alias module.
*/

#include "global.h"

#define ZUMLIN_MODEL "c:/DiscoStu/gfx/zumlin.md2"
#define ZUMLIN_TEXTURE "c:/DiscoStu/gfx/zumlin0.tga"
#define RUN_FRAME_START 0
#define RUN_FRAME_END   5
#define DANCE_FRAME_START 6
#define DANCE_FRAME_END   22

#define FIELD_DESTINATION  0

extern edict_t *hatch;

/*
   NT:
   Listen to DSP.
*/
void ZumlinListen1(edict_t *self, struct winampVisModule *this_mod)
{
   register float kick = dsp.kick;
   self->rgba[3] = kick;
}

/*
   NT:
   Listen to DSP.
*/
void ZumlinListen2(edict_t *self, struct winampVisModule *this_mod)
{
   register float kick = dsp.kick;
   self->rgba[0] = .5 + kick;
   self->rgba[2] = 1.5 - kick;
}


/*
   NT:
   Camera renderer 1: Follow Zumlin and rotate around him
*/
void ZumlinCam(edict_t *self)
{
   static float xrotate = 0;
   vec3_t arrow = {-25, 0, 0};
   arrow[1] = xrotate;
   xrotate += 1.0f;
   AngleVectors (arrow, arrow, NULL, NULL);
   VectorMA(arrow, 10, self->origin, cam.origin);
   VectorSubtract (self->origin, cam.origin, arrow);
   VectorAngles(arrow, cam.angle);

   glLoadIdentity();
   glRotatef(-cam.angle[PITCH], 1, 0, 0);
   glRotatef(-cam.angle[YAW], 0, 1, 0);
   glRotatef(-cam.angle[ROLL], 0, 0, 1);
   glTranslatef(-cam.origin[0], -cam.origin[1], -cam.origin[2]);      
}

/*
   NT:
   Smoke Cam:
*/
void ZumlinSmokeCam(edict_t *self)
{
   static float xrotate = 0;
   float temp_y;
   vec3_t arrow = {30, 0, 0};
   arrow[1] = xrotate;
   xrotate += 1.0f;
   AngleVectors (arrow, arrow, NULL, NULL);
   temp_y = self->origin[1];
   self->origin[1] = 7;
   VectorMA(arrow, 30, self->origin, cam.origin);
   VectorSubtract (self->origin, cam.origin, arrow);
   VectorAngles(arrow, cam.angle);
   self->origin[1] = temp_y;

   glLoadIdentity();
   glRotatef(-cam.angle[PITCH], 1, 0, 0);
   glRotatef(-cam.angle[YAW], 0, 1, 0);
   glRotatef(-cam.angle[ROLL], 0, 0, 1);
   glTranslatef(-cam.origin[0], -cam.origin[1], -cam.origin[2]);      
}

/*
   NT:
   Render Zumlin.
*/
void ZumlinRender(edict_t *self)
{
   if (last_texture != self->skin_index)
      {
      glBindTexture (GL_TEXTURE_2D, self->skin_index); 
      last_texture = self->skin_index;
      }   
   glEnable(GL_DEPTH_TEST);
   RenderAliasModel(self);
   glRotatef(30, 0, 1, 0);
   RenderAliasShadow(self);
   glDisable(GL_DEPTH_TEST);
}

/*
   NT:
   Render Zumlin.
*/
void ZumlinRender2(edict_t *self)
{
   edict_t *smoke;
   vec3_t grey;
   int i = 0;
   vec3_t arrow = {0, 0, 0};

   VectorCopy(cam.RGB_filter, grey);
   if (self->origin[1] >= 7)
      {
      self->origin[1] = 7;
      self->velocity[1] = 0;
      self->next_think = eng.time + 5000;
      self->render = ZumlinRender;
      self->avelocity[YAW] = 0.0f;
      hatch->velocity[1] = 0;
      }
   if (!(rand() & 7))
      for (; i < 360; i+= 40)
         {
         arrow[0] = 0;
         arrow[1] = i;
         arrow[2] = 0;
         AngleVectors (arrow, arrow, NULL, NULL);
         VectorMA(arrow, 6, self->origin, arrow);
         smoke = FireFlySpawn(arrow, .5, grey, .4f);
         smoke->origin[1] = -2;
         smoke->speed = .1f;
         VectorSet(smoke->velocity, 0, 1, 0);
         }
   if (last_texture != self->skin_index)
      {
      glBindTexture (GL_TEXTURE_2D, self->skin_index); 
      last_texture = self->skin_index;
      }   
   glEnable(GL_DEPTH_TEST);
   RenderAliasModel(self);
   glDisable(GL_DEPTH_TEST);
}

/*
   NT:
   Think function for Zumlin.
*/
void ZumlinThink(edict_t *self)
{
   self->render = ZumlinRender;
   self->camera = ZumlinCam;
   self->next_frame = self->loop_frame = RUN_FRAME_START;
   self->end_frame = RUN_FRAME_END;
   RunEdict(self);
}

/*
   NT:
   Trigger function for Zumlin.
*/
void ZumlinTrigger(edict_t *self)
{
   self->render = ZumlinRender2;
   self->camera = ZumlinSmokeCam;
   VectorClear(self->velocity);
   self->next_frame = self->loop_frame = DANCE_FRAME_START;
   self->end_frame = DANCE_FRAME_END;
   self->velocity[1] = .1f;
   hatch->velocity[1] = .1f;
   self->avelocity[YAW] = 2.0f;
   self->think = ZumlinThink;
}

/*
   NT:
   Zumlin has reached its destination
*/
void ZumlinReached(edict_t *self)
{
   switch(self->idata[FIELD_DESTINATION])
      {
      case 0:
         VectorSet(self->destination, 0, 7, 60);
         self->idata[FIELD_DESTINATION]++;
         break;
      case 1:
         VectorSet(self->destination, 0, 7, 0);
         self->idata[FIELD_DESTINATION]++;
         break;
      case 2:
         VectorSet(self->destination, 30, 7, 30);
         self->idata[FIELD_DESTINATION] = 0;
         break;
      }
   self->next_think = eng.time + 10000 + (rand() & 0xFFF);
   self->next_frame = self->loop_frame = DANCE_FRAME_START;
   self->end_frame = DANCE_FRAME_END;

}

/*
   NT:
   Spawn Zumlin.
*/
edict_t *ZumlinSpawn()
{
   static unsigned int skinned = NOT_FOUND, modeled = NOT_FOUND;
   int model_index;
   edict_t *ent;
   skin_t *skin;
   ent = EdictSpawn();

   if (modeled == NOT_FOUND)
      {
      if ((model_index = CMD2Exist(ZUMLIN_MODEL)) == NOT_FOUND)
         {
         ent->model = new CMD2;
         if (CMD2Spawn(ZUMLIN_MODEL, ent->model) == NOT_FOUND)
            return NULL;
         if (ent->model->Read(ZUMLIN_MODEL) != 1)
            EngineError("SpawnZumlin: Error reading Alias md2 '%s'", ZUMLIN_MODEL);
         }
      else
         {
         ent->model = g_CMD2s[model_index]->model;
         }
      }
   else
      ent->model = g_CMD2s[modeled]->model;

   // NT: Only load skins if they are not allocated already:
   if (skinned == NOT_FOUND)
      {
      // NT: Does skin exist in library?
      if ((ent->skin_num = SkinIndex(ZUMLIN_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         skin = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(ZUMLIN_TEXTURE, skin);
         if (!LoadTGA(ZUMLIN_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", ZUMLIN_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         ent->skin_index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, ent->skin_index);
         TGA2GLTexture (skin, 0, 255, ix, iy);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	      glTexImage2D (GL_TEXTURE_2D, 0, 4, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, skin->glTexture);
         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);	
         skin->index = ent->skin_index;
         skinned = ent->skin_index;
         UpdateTextureCoordinates(ent->model, skin);
         InitGL();      
         }
      else // Just use skin from already loaded list:
         ent->skin_index = g_skins[ent->skin_num]->skin->index;
      }
   else
      ent->skin_index = skinned;

   // State of entity:
   ent->state = ENT_ST_USED | ENT_ST_PHYS | ENT_ST_VISIBLE;

   ent->origin[0] = 0;
   ent->origin[1] = -17;
   ent->origin[2] = 0;
   ent->rgba[0] = 1;
   ent->rgba[1] = .5f;
   ent->rgba[2] = 1;
   ent->rgba[3] = 1;
   ent->frame = DANCE_FRAME_START;
   ent->loop_frame = DANCE_FRAME_START;
   ent->end_frame = DANCE_FRAME_END;
   ent->scale = 0.3f;
   //ent->render = ZumlinRender2;
   ent->listen = ZumlinListen2;
   //ent->camera = ZumlinCam;
   ent->think = ZumlinTrigger;
   ent->reached = ZumlinReached;
   ent->speed = 1.2f;
   VectorSet(ent->destination, 30, 7, 30);
   //ent->next_think  = eng.time + 14000;
   return ent;
}