/*
   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:
      Jupiter Alias module.
*/

#include "global.h"
#include <math.h>

#define JUPITER_MODEL "c:/DiscoStu/gfx/Jupiter/tris.md2"
#define JUPITER_TEXTURE "c:/DiscoStu/gfx/Jupiter/Jupiter0.tga"
#define STAND_FRAME_START  0
#define STAND_FRAME_END    24
#define RUN_FRAME_START    40
#define RUN_FRAME_END      45
#define JUMP_FRAME_START   66
#define JUMP_FRAME_END     71
#define SALUTE_FRAME_START 84
#define SALUTE_FRAME_END   94
#define TAUNT_FRAME_START  95
#define TAUNT_FRAME_END    111
#define POINT_FRAME_START  123
#define POINT_FRAME_END    134

#define FIELD_DESTINATION 0

static int current_move; 
static float xrotate;
static float stop_angle;


void JupiterListen1(edict_t *self, struct winampVisModule *this_mod);
void JupiterListen2(edict_t *self, struct winampVisModule *this_mod);
//void JupiterListen3(edict_t *self, struct winampVisModule *this_mod);


void JupiterStationaryCam(edict_t *self)
{
   //xrotate = 180;
   vec3_t arrow = {30, 0, 0};
   arrow[1] = xrotate;
   AngleVectors (arrow, arrow, NULL, NULL);
   VectorMA(arrow, 35, 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:
   Camera renderer 1: Follow Jupiter and rotate around her
*/
void JupiterCam1(edict_t *self)
{
   vec3_t arrow = {30, 0, 0};
   arrow[1] = xrotate;
   xrotate += 0.8f;
   AngleVectors (arrow, arrow, NULL, NULL);
   VectorMA(arrow, 35, 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:
   Render Jupiter.
*/
void JupiterRender(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);

   VectorCopy(self->origin, cam.right);
   VectorCopy(self->angle, cam.forward);
}

/*
   NT:
   Think function for Jupiter.
*/
void JupiterThink(edict_t *self)
{
	self->frame = RUN_FRAME_START;
	self->loop_frame = RUN_FRAME_START;
	self->end_frame = RUN_FRAME_END;
	self->next_frame = RUN_FRAME_START;
   RunEdict(self);
}

/*
   Jupiter has reached destination:
*/
void JupiterReached(edict_t *self)
{
   switch(self->idata[FIELD_DESTINATION])
      {
      case 0:
         self->camera = JupiterStationaryCam;
         VectorSet(self->destination, -30, 7, -55);
         self->idata[FIELD_DESTINATION]++;
         break;
      case 1:
		   self->camera = JupiterCam1;
         VectorSet(self->destination, (-408 + 90) * 0.4f, 7, 0);
         self->idata[FIELD_DESTINATION]++;
         break;
      case 2:
         self->camera = JupiterStationaryCam;
         VectorSet(self->destination, -132, 7, 130);
         self->idata[FIELD_DESTINATION] = 0;
         break;
      }

   switch (rand() % 3) {
	case 0:
		self->frame = POINT_FRAME_START;
		self->loop_frame = POINT_FRAME_START;
		self->end_frame = POINT_FRAME_END;
		self->next_frame = POINT_FRAME_START;
		self->next_think = eng.time + 7000;
		break;
	case 1:
		self->frame = TAUNT_FRAME_START;
		self->loop_frame = TAUNT_FRAME_START;
		self->end_frame = TAUNT_FRAME_END;
		self->next_frame = TAUNT_FRAME_START;
		self->next_think = eng.time + 7000;
		break;
	case 2:
		self->frame = SALUTE_FRAME_START;
		self->loop_frame = SALUTE_FRAME_START;
		self->end_frame = SALUTE_FRAME_END;
		self->next_frame = SALUTE_FRAME_START;
		self->next_think = eng.time + 5000;
		break;
   }

}


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

   xrotate = 180;
   if (modeled == NOT_FOUND)
      {
      if ((model_index = CMD2Exist(JUPITER_MODEL)) == NOT_FOUND)
         {
         ent->model = new CMD2;
         if (CMD2Spawn(JUPITER_MODEL, ent->model) == NOT_FOUND)
            return NULL;
         if (ent->model->Read(JUPITER_MODEL) != 1)
            EngineError("SpawnJupiter: Error reading Alias md2 '%s'", JUPITER_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(JUPITER_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         skin = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(JUPITER_TEXTURE, skin);
         if (!LoadTGA(JUPITER_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", JUPITER_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] = -15;
   ent->origin[1] = 7;
   ent->origin[2] = 50;
   ent->rgba[0] = 1;
   ent->rgba[1] = 1;
   ent->rgba[2] = 1;
   ent->rgba[3] = 1;
   ent->frame = RUN_FRAME_START;
   ent->next_frame = RUN_FRAME_START;
   ent->loop_frame = RUN_FRAME_START;
   ent->end_frame = RUN_FRAME_END;
   ent->velocity[0] = -0.3;
   ent->velocity[1] = 0;
   ent->velocity[2] = -0.95;
   ent->angle[YAW] = VectorYaw(ent->velocity);
   current_move = RUN_FRAME_START;
   ent->scale = 0.3f;
   ent->render = JupiterRender;
   ent->think = JupiterThink;
   ent->next_think = eng.time + 10500;
   ent->camera = JupiterStationaryCam;
   ent->reached = JupiterReached;
   ent->speed = 1.1f;
   VectorSet(ent->destination, -132, 7, 130);
   return ent;
}