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

#include "global.h"

#define NINJA_MODEL "c:/DiscoStu/gfx/ninja.md2"
#define NINJA_TEXTURE "c:/DiscoStu/gfx/ninja1.tga"
#define RUN_FRAME_START 0
#define RUN_FRAME_END   5
#define DANCE_FRAME_START 6
#define DANCE_FRAME_END   68

/*
   NT:
   Camera renderer 1: Follow Ninja and rotate around him
*/
void NinjaCam1(edict_t *self)
{
   static float xrotate = 135, x_skip = .3f;
   vec3_t arrow = {-30, 0, 0};
   arrow[1] = xrotate;
   xrotate += x_skip;
   if (xrotate < 90 || xrotate > 180)
      x_skip = -x_skip;
   AngleVectors (arrow, arrow, NULL, NULL);
   VectorMA(arrow, 25, self->origin, cam.origin);
   VectorSubtract (self->origin, cam.origin, arrow);
   VectorAngles(arrow, cam.angle);

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

/*
   NT:
   Render Ninja.
*/
void NinjaRender1(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);
   glDisable(GL_DEPTH_TEST);
}

/*
   NT:
   Render Ninja.
*/
void NinjaRender2(edict_t *self)
{
   glDisable(GL_TEXTURE_2D);
   glEnable(GL_DEPTH_TEST);
   glPolygonMode (GL_FRONT, GL_LINE);
   RenderAliasModel(self);
   glPolygonMode (GL_FRONT, GL_FILL);
   glDisable(GL_DEPTH_TEST);
   glEnable(GL_TEXTURE_2D);
}

/*
   NT:
   Think function for Ninja.
*/
void NinjaThink(edict_t *self)
{
   if (0 && self->end_frame == DANCE_FRAME_END)
      {
      float f = random();
      if (f < .3)
         self->velocity[0] = -.9f;
      else if (f < .6)
         self->velocity[0] = .9f;
      else
         self->velocity[0] = 0;
      f = random();
      if (f < .5)
         self->velocity[2] = -.9f;
      else 
         self->velocity[2] = .9f;
      self->angle[YAW] = VectorYaw(self->velocity);
      self->next_think = eng.time + 2000;
      self->next_frame = self->loop_frame = RUN_FRAME_START;
      self->end_frame = RUN_FRAME_END;
      }
   else 
      {
      self->next_think = eng.time + 10000 + (rand() & 0xFFF);
      VectorClear(self->velocity);
      self->next_frame = self->loop_frame = DANCE_FRAME_START;
      self->end_frame = DANCE_FRAME_END;
      }
}

/*
   NT:
   Spawn Ninja.
*/
edict_t *NinjaSpawn()
{
   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(NINJA_MODEL)) == NOT_FOUND)
         {
         ent->model = new CMD2;
         if (CMD2Spawn(NINJA_MODEL, ent->model) == NOT_FOUND)
            return NULL;
         if (ent->model->Read(NINJA_MODEL) != 1)
            EngineError("SpawnNinja: Error reading Alias md2 '%s'", NINJA_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(NINJA_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         skin = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(NINJA_TEXTURE, skin);
         if (!LoadTGA(NINJA_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", NINJA_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_VISIBLE;

   ent->origin[0] = 142;
   ent->origin[1] = 19;
   ent->origin[2] = -140;
   ent->rgba[0] = 1;
   ent->rgba[1] = 1;
   ent->rgba[2] = 1;
   ent->rgba[3] = 1;
   ent->angle[YAW] = 90;
   ent->frame = DANCE_FRAME_START;
   ent->next_frame = DANCE_FRAME_START + 1;
   ent->loop_frame = DANCE_FRAME_START;
   ent->end_frame = DANCE_FRAME_END;
   ent->scale = 0.3f;
   ent->render = NinjaRender1;
   ent->think = NinjaThink;
   ent->next_think = eng.time + 5000;
   cam.target = ent;
   ent->camera = NinjaCam1;
   
   return ent;
}