/*****************************************************************************
 * Copyright(C)SEGA 1995-1997/SYSTEM R&D DEPT                                *
 *                                                                           *
 * COMMAND :                                                                 *
 *                                                                           *
 * FILE:    SSRead.h                                                         *
 *                                                                           *
 * VERSION: 1.00                                                             *
 *                                                                           *
 * DATE: 1997/05/14                                                          *
 *                                                                           *
 *****************************************************************************/
#ifndef __SSReadNja_h
#define __SSReadNja_h

#include <stdlib.h>
#include <NjDef.h>
#include <NinjaDef.h>
#include <NinjaStr.h>
#include <SSObject.h>

#ifdef WIN32
#include "math.h"
#include "malloc.h"
#endif

#define NJV_MODE_NAM	0
#define NJV_MODE_NAS	1
/*****************************************************************
 *
 *  STRINGS
 *
 *****************************************************************/

#define STR_ROOT_OBJECT_NAME "// ROOT OBJECT : "
#define STR_TEXLIST_NAME     "// TEXLIST     : "

#define STR_NULL           "NULL"

#define STR_START          "START"
#define STR_END            "END"
#define STR_TEXTURE_START  "TEXTURE_START"
#define STR_TEXTURE_END    "TEXTURE_END"
#define STR_MATSTART       "MATSTART"
#define STR_MATEND         "MATEND"
#define STR_MESHSTART      "MESHSTART"
#define STR_MESHEND        "MESHEND"
#define STR_OBJECT_START   "OBJECT_START"
#define STR_OBJECT_END     "OBJECT_END"

#define STR_Diffuse        "Diffuse"
#define STR_Specular       "Specular"
#define STR_Exponent       "Exponent"
#define STR_AttrTexId      "AttrTexId"
#define STR_AttrFlags      "AttrFlags"

#define STR_TEXTURENAME    "TEXTURENAME"
#define STR_TEXTURELIST    "TEXTURELIST"
#define STR_TextureList    "TextureList"
#define STR_TextureNum     "TextureNum"
#define STR_MATERIAL       "MATERIAL"

#define STR_TEXN           "TEXN"
#define STR_TEXN3          "TEXN3"
#define STR_NORM           "NORM"
#define STR_VERT           "VERT"
#define STR_UV             "UV"

#define STR_POLYGON        "POLYGON"
#define STR_POLYNORMAL     "POLYNORMAL"
#define STR_VERTCOLOR      "VERTCOLOR"
#define STR_VERTUV         "VERTUV"
#define STR_TypeMatId      "TypeMatId"
#define STR_MeshNum        "MeshNum"
#define STR_Meshes         "Meshes"
#define STR_PolyAttrs      "PolyAttrs"
#define STR_PolyNormal     "PolyNormal"
#define STR_VertColor      "VertColor"
#define STR_VertUV         "VertUV"
#define STR_MESHSET        "MESHSET"
#define STR_POINT          "POINT"
#define STR_NORMAL         "NORMAL"
#define STR_MODEL          "MODEL"
#define STR_Points         "Points"
#define STR_Normal         "Normal"
#define STR_Meshset        "Meshset"
#define STR_PointNum       "PointNum"
#define STR_MeshsetNum     "MeshsetNum"
#define STR_Materials      "Materials"
#define STR_MatNum         "MatNum"
#define STR_Center         "Center"
#define STR_Radius         "Radius"
#define STR_OBJECT         "OBJECT"
#define STR_EvalFlags      "EvalFlags"
#define STR_Model          "Model"
#define STR_Position       "Position"
#define STR_Angle          "Angle"
#define STR_Scale          "Scale"
#define STR_Child          "Child"
#define STR_Sibling        "Sibling"

#define STR_MOTION_START     "MOTION_START"
#define STR_POSITION         "POSITION"
#define STR_XROTATION        "XROTATION"
#define STR_ROTATION         "ROTATION"
#define STR_SCALE            "SCALE"
#define STR_AXEVECT          "AXEVECT"
#define STR_POINTER          "POINTER"
#define STR_MDATA1           "MDATA1"
#define STR_MDATA2           "MDATA2"
#define STR_MDATA3           "MDATA3"
#define STR_MDATA4           "MDATA4"
#define STR_MOTION           "MOTION"
#define STR_ACTION           "ACTION"
#define STR_MOTION_END       "MOTION_END"

#define STR_Strip            "Strip"
#define STR_PATH			"PATH:"
#define STR_EXTNJA			".nja"
#define STR_EXTNAM			".nam"
#define STR_EXTNAS			".nas"

#ifdef WIN32
#define NJV_ATOI             atoi
#define NJV_ATOF             (float)atof
#define NJV_STRTOL           strtoul
#else
#define NJV_ATOI             atoi
#define NJV_ATOF             atof
#define NJV_STRTOL           strtoul
#endif

/*****************************************************************
 *
 *  GetWords
 *
 *****************************************************************/
#define NJV_PWORDS_MAX            512
#define NJV_STRING_MAX            512
#define NJV_BUFFER_MAX           1024
#define NJV_POLYGON_VERTEX_MAX   1024

enum {
    NJV_CK_SPACE = 0,
    NJV_CK_STRING,
    NJV_CK_BRACE_IN,     /* {             */
    NJV_CK_BRACE_OUT,    /* }             */
    NJV_CK_LINEEND
};

/* simple line's getwords */
typedef struct {
    char                       ck[256];   /* char check table         */
    char       *pwords[NJV_PWORDS_MAX];
    int                         word_n;
    int                   bracePushPop;
} NJV_WordsList;

/*****************************************************************
 *
 *  MALLOC TABLE
 *
 *****************************************************************/
#define NJV_PAGE_SIZE    4096

typedef struct _NJV_MallocPage {
    char                               *buf;
    struct _NJV_MallocPage          *next_p;
    } NJV_MallocPage;

typedef struct NJV_MallocTable {
    NJV_MallocPage                 *pagetop;    /* chain list          */
    NJV_MallocPage                    *page;    /* current page        */
    NJV_MallocPage                  *prev_p;    /* prev page           */
    int                             current;    /* current pointer     */
    int                             pagenum;    /* page number         */
    int                            pagesize;    /* page size cash      */
    } NJV_MallocTable;

/*****************************************************************
 *
 *  STRUCTURES
 *
 *****************************************************************/
typedef struct {
    NJS_TEXNAME                  *data_p;
} NJV_WorkTexname;

typedef struct {
    NJS_MATERIAL                 *data_p;
    char            name[NJV_STRING_MAX];
} NJV_WorkMaterial;

typedef struct {
    Sint16                       *data_p;
    int                                n;
    char            name[NJV_STRING_MAX];
} NJV_WorkMeshes;

typedef struct {
    NJS_VECTOR                   *data_p;
    int                                n;
    char            name[NJV_STRING_MAX];
} NJV_WorkPolyNormal;

typedef struct {
    NJS_COLOR                    *data_p;
    int                                n;
    char            name[NJV_STRING_MAX];
} NJV_WorkVertUv;

typedef struct {
    NJS_MESHSET                  *data_p;
    char            name[NJV_STRING_MAX];
    char      meshesname[NJV_STRING_MAX];
    char       attrsname[NJV_STRING_MAX];
    char     normalsname[NJV_STRING_MAX];
    char   vertcolorname[NJV_STRING_MAX];
    char      vertuvname[NJV_STRING_MAX];
} NJV_WorkMeshset;

typedef struct {
    NJS_POINT3                   *data_p;
    int                                n;
    char            name[NJV_STRING_MAX];
} NJV_WorkPoint;

typedef struct {
    NJS_VECTOR                   *data_p;
    int                                n;
    char            name[NJV_STRING_MAX];
} NJV_WorkNormal;

typedef struct {
    NJS_MODEL                    *data_p;
    char            name[NJV_STRING_MAX];
    char      pointsname[NJV_STRING_MAX];
    char     normalsname[NJV_STRING_MAX];
    char     meshsetname[NJV_STRING_MAX];
    char        matsname[NJV_STRING_MAX];
} NJV_WorkModel;

typedef struct {
    NJS_OBJECT                   *data_p;
    char            name[NJV_STRING_MAX];
    char       modelname[NJV_STRING_MAX];
    char       childname[NJV_STRING_MAX];
    char     siblingname[NJV_STRING_MAX];
} NJV_WorkObject;

/*
 * motion
 */

typedef struct {
	NJS_MKEY_F	*data_p;
	int         n;
	char		name[NJV_STRING_MAX];
} NJV_WorkMKEY_F;

typedef struct {
	NJS_MKEY_AX	*data_p;
	int         n;
	char		name[NJV_STRING_MAX];
} NJV_WorkMKEY_AX;

typedef struct {
	NJS_MKEY_A	*data_p;
	int         n;
	char		name[NJV_STRING_MAX];
} NJV_WorkMKEY_A;

typedef struct {
	NJS_MKEY_P	*data_p;
	int         n;
	char		name[NJV_STRING_MAX];
	char		keyname[NJV_STRING_MAX];
} NJV_WorkMKEY_P;

typedef struct {
    NJS_MDATA1	*data_p;
    char		name[NJV_STRING_MAX];
    char		pname[1][NJV_STRING_MAX];
} NJV_WorkMDATA1;

typedef struct {
    NJS_MDATA2	*data_p;
    char		name[NJV_STRING_MAX];
    char		pname[2][NJV_STRING_MAX];
} NJV_WorkMDATA2;

typedef struct {
    NJS_MDATA3	*data_p;
    char		name[NJV_STRING_MAX];
    char		pname[3][NJV_STRING_MAX];
} NJV_WorkMDATA3;

typedef struct {
    NJS_MDATA4	*data_p;
    char		name[NJV_STRING_MAX];
    char		pname[4][NJV_STRING_MAX];
} NJV_WorkMDATA4;

typedef struct {
    NJS_MOTION	*data_p;
    char		name[NJV_STRING_MAX];
    char		mdataname[NJV_STRING_MAX];
} NJV_WorkMOTION;

typedef struct {
    NJS_ACTION	*data_p;
    char		name[NJV_STRING_MAX];
    char		objectname[NJV_STRING_MAX];
    char		motionname[NJV_STRING_MAX];
} NJV_WorkACTION;


typedef struct {
    int                        texname_n;
    NJV_WorkTexname        *texname_list;

    int                       material_n;
    int                    material_last; /* default material */
    NJV_WorkMaterial      *material_list;

    int                           mesh_n;
    NJV_WorkMeshes            *mesh_list;

    int                     polynormal_n;
    NJV_WorkPolyNormal  *polynormal_list;

    int                         vertuv_n;
    NJV_WorkVertUv          *vertuv_list;

    int                        meshset_n;
    NJV_WorkMeshset        *meshset_list;

    int                          point_n;
    NJV_WorkPoint            *point_list;

    int                         normal_n;
    NJV_WorkNormal          *normal_list;

    int                          model_n;
    NJV_WorkModel            *model_list;

    int                         object_n;
    NJV_WorkObject          *object_list;

	int				position_n;
	NJV_WorkMKEY_F	*position_list;

	int				rotation_n;
	NJV_WorkMKEY_A	*rotation_list;

	int				scale_n;
	NJV_WorkMKEY_F	*scale_list;

	int				axevct_n;
	NJV_WorkMKEY_F	*axevct_list;

	int				xrotation_n;
	NJV_WorkMKEY_AX	*xrotation_list;

	int 			mdata1_n;
	NJV_WorkMDATA1	*mdata1_list;

	int 			mdata2_n;
	NJV_WorkMDATA2	*mdata2_list;

	int 			mdata3_n;
	NJV_WorkMDATA3	*mdata3_list;

	int 			mdata4_n;
	NJV_WorkMDATA4	*mdata4_list;

	int 			motion_n;
	NJV_WorkMOTION	*motion_list;
	
	int 			action_n;
	NJV_WorkACTION	*action_list;

	int 			pointer_n;
	NJV_WorkMKEY_P	*pointer_list;

	int				object_tree_n;

	int				pointer_total_n;

    char                       *filename;

    char             buf[NJV_BUFFER_MAX];   /* string buffer         */

    NJS_TEXLIST               *texlist_p;
    int                     current_line;   /* currentline for DEBUG */

    NJV_MallocTable               mtable;   /* malloc table          */
} NJV_WorkTab;

typedef struct _NJV_NjaInfo {
    NJS_OBJECT                 *object_p;   /* object tree           */
    NJS_TEXLIST               *texlist_p;   /* texlist               */
    NJS_MOTION                 *motion_p;   /* motion                */
	Uint16				   motion_tree_n;
    NJS_MOTION					*shape_p;   /* shape                 */
	Uint16				    shape_tree_n;
    NJS_ACTION                    action;   /* action                */
} NJV_Nja;

typedef struct{
	char			path[NJV_STRING_MAX];
	char			(*fname)[NJV_STRING_MAX];
	int				nbfname;
} NJV_Fname;


typedef struct {
    int (*func)(FILE *fp, NJV_WorkTab *t_p);
    char  *cmdstrings;
    } NJV_ReadCmd;

typedef struct {
    char *str;
    int  code;
    } NJV_TextureSize;

/**/
int NJV_ReadNja(NJV_Nja *nja_p, char *filename);
void NJV_ReadNjaFree(NJV_Nja *nja_p);

#endif /* __SSReadNja_c */

/*-------*/
/* macro */
/*-------*/
/*
 * NJV_MCHECK : check null for malloc
 */
#define NJV_MCHECK(_tmp, _mfunc)                                         \
 if (NULL == (_tmp = _mfunc)) {                                          \
  return FALSE;                                                          \
 }

/*
 * NJV_MCHECK2 : check null and print errorinfo for malloc
 */
#define NJV_MCHECK2(_tmp, _cast, _bytesize, _str)                        \
 if (NULL == (_tmp = (_cast)malloc(_bytesize))) {                   \
  fprintf(stderr, "%s %d\n", _str, _bytesize);                           \
  return FALSE;                                                          \
 }                                                                       \
 alloc_count++;

/*
 * NJV_WORD_N_CHECK : check arg count
 */
#define NJV_WORD_N_CHECK(_pwl, _i)                                       \
 if (_pwl->word_n < _i) {                                                \
  fprintf(stderr, "Bad arg count : Line %d\n",t_p->current_line);        \
  return FALSE;                                                          \
 }

/*
 * NJV_FREE : free
 */
#define NJV_FREE(_ptr) free_count++; free(_ptr); _ptr=NULL;

/*++*/

/*****************************************************************
 *
 *  EXTERN
 *
 *****************************************************************/
extern int NJV_ReadNja(NJV_Nja *nja_p, char *filename);
extern void NJV_ReadNjaFree(NJV_Nja *nja_p);

extern int NJV_ReadNam(NJV_Nja *nja_p, char *filename, int mode);
extern void NJV_MotionFree(NJS_MOTION *motion_p, Uint16 nbObject);
extern void NJV_ShapeFree(NJS_MOTION *motion_p, Uint16 nbObject);

extern void NJV_GetWordsInit(NJV_WordsList *pwl);
extern void NJV_GetWords(NJV_WordsList *pwl, char *buf);
extern void NJV_Error(char *format, char *message);
extern void NJV_MallocInit(NJV_WorkTab *worktab_p);
extern void *NJV_Malloc(NJV_WorkTab *worktab_p, int bytesize, char *message);
extern char *NJV_Strdup(NJV_WorkTab *worktab_p, char *filename);
extern void NJV_FreeAll(NJV_WorkTab *worktab_p);
extern void ssn_PrintTexname(NJS_TEXNAME *texname_p);
extern void ssn_PrintObject(NJS_OBJECT *object_p);
extern void ssn_PrintMotion(NJS_MOTION *motion_p);
extern NJV_WordsList wl, *pwl;

extern int NJV_GetNormalSize(NJV_WorkTab *t_p, FILE *fp);
extern int NJV_GetPointSize(NJV_WorkTab *t_p, FILE *fp);
extern int ssn_ReadPOINT(FILE *fp, NJV_WorkTab *t_p);
extern int ssn_ReadNORMAL(FILE *fp, NJV_WorkTab *t_p);
extern int NJV_ReadNsc(char *filename, NJV_Fname *fn_p);

/*
 * End of File
 */
