/*----------------------------------------------------------------------------
	IPX
----------------------------------------------------------------------------*/

#include "soh.h"
#include "ipx.h"
#include "born.h"
#include "mesh.h"
#include "part.h"
#include "light.h"

/*--------------------------------------------------------------------------*/
long ixObject(void)
{
	SPRITE spr;
	long i,wk;
	ix_stat=OBJ(stat);
											/* オートデクリメントタイマー	*/
	for(i=8; i<16; i++) 
		if (OBJ(work[i]))
			OBJ(work[i])--;
											/* タスク実行					*/
	for(i=0; i<IXD_MAX_TASK; i++){
		ix->tsk=i;
		if (TASK(used) && (--TASK(cnt)==0)) {
			ix->poi=(u_char *)TASK(poi);
			if (ixRun()) return 0;
			TASK(poi)=(u_long)ix->poi;
			TASK(cnt)=(u_short)ix->cnt;
		}
	}
	if (OBJ(id)!=5) return 0;
	
/*--------------------------------------------------------------------------*/
	
	if (OBJ(local_x.cnt)) {
		OBJ(local_x.vec)=(OBJ(local_x.tar)-OBJ(local_x.now))/OBJ(local_x.cnt)--;
		if (OBJ(local_x.cnt)==0) {
			OBJ(local_x.vec)=0;
			OBJ(local_x.now)=OBJ(local_x.tar);
	}	}
	if (OBJ(local_y.cnt)) {
		OBJ(local_y.vec)=(OBJ(local_y.tar)-OBJ(local_y.now))/OBJ(local_y.cnt)--;
		if (OBJ(local_y.cnt)==0) {
			OBJ(local_y.vec)=0;
			OBJ(local_y.now)=OBJ(local_y.tar);
	}	}
	OBJ(local_x.now)+=OBJ(local_x.vec); OBJ(world_x)=OBJ(local_x.now);
	OBJ(local_y.now)+=OBJ(local_y.vec); OBJ(world_y)=OBJ(local_y.now);
												/* 回転計算					*/
	if (OBJ(local_roll.cnt)) {
		OBJ(local_roll.vec)=((short)OBJ(local_roll.tar)-(short)OBJ(local_roll.now))/OBJ(local_roll.cnt)--;
		if (OBJ(local_roll.cnt)==0) {
			OBJ(local_roll.vec)=0;
			OBJ(local_roll.now)=OBJ(local_roll.tar);
	}	}
	OBJ(local_roll.now)+=OBJ(local_roll.vec); OBJ(world_roll)=OBJ(local_roll.now);
										
	if (OBJ(local_zoom.cnt)) {					/* スケール計算				*/
		OBJ(local_zoom.vec)=(OBJ(local_zoom.tar)-OBJ(local_zoom.now))/OBJ(local_zoom.cnt)--;
		if (OBJ(local_zoom.cnt)==0) {
			OBJ(local_zoom.vec)=0;
			OBJ(local_zoom.now)=OBJ(local_zoom.tar);
	}	}
	OBJ(local_zoom.now)+=OBJ(local_zoom.vec); OBJ(world_zoom)=OBJ(local_zoom.now);


	if (GET_STAT(O_SYNC)) {						/* シンクロ処理				*/
		wk=OBJ(sync);							/* シンクロ先				*/
		OBJ(world_x) += OBJ2(wk, world_x);
		OBJ(world_y) += OBJ2(wk, world_y);
	}
	if (GET_STAT(O_SYNC_EX)) {
		wk=OBJ(sync);							/* シンクロ先				*/
		OBJ(world_x) += OBJ2(wk, ex_x);
		OBJ(world_y) += OBJ2(wk, ex_y);
	}
	
/*--------------------------------------------------------------------------*/
	
	if (!GET_STAT(O_SEC)&&(OBJ(ptn)>=0)) {		/* シークレットなら通過		*/
		
		if (GET_STAT(O_BREND)) {				/* αブレンディングの設定	*/
			sxSetBrendMode( SX_BREND_ON );
			if (GET_STAT(O_ALPHA)) sxSetBrendMode( SX_BREND_ALPHA );
							else   sxSetBrendMode( SX_BREND_ADD   );
		} else {
			sxSetBrendMode( SX_BREND_OFF );
		}
		if (GET_STAT(O_FILTER)) {				/* テクスチャフィルタの設定	*/
			sxSetFilterMode( SX_FILTER_BILINEAR );
		} else {
			sxSetFilterMode( SX_FILTER_OFF );
		}
		
		if (GET_STAT(O_FLASH)) njFogEnable();
		
		spr.px   =OBJ(world_x)>>16;
		spr.py   =OBJ(world_y)>>16;
		spr.prio =OBJ(world_z)+100;
		spr.zoom =OBJ(world_zoom)>>8;
		spr.roll =OBJ(world_roll);
		spr.mate =OBJ(color);
		spr.grp  =OBJ(grp);
		spr.ptn  =OBJ(ptn);
		spr.attr = 0x00000000;
		
		if (GET_STAT(O_ENLIGHT)) ixDispSpriteLight(&spr);
							else ixDispSprite(&spr);
		
		if (GET_STAT(O_BORN)) boDraw(OBJ(born)-1, (OBJECT *)ix->wok);
		if (GET_STAT(O_MESH)) msDraw(OBJ(mesh)-1, (OBJECT *)ix->wok);
		if (GET_STAT(O_PART)) ptDraw(OBJ(part)-1, (OBJECT *)ix->wok);
		if (GET_STAT(O_LIGHT)) ltDraw(OBJ(light)-1, (OBJECT *)ix->wok);
		
		if (GET_STAT(O_FLASH)) njFogDisable();
	}
	
/*--------------------------------------------------------------------------*/
	
	if ( OBJ(world_x)>( 50<<16) && OBJ(world_x)<(590<<16) &&
		 OBJ(world_y)>(-30<<16) && OBJ(world_y)<(510<<16) ) {
			SET_STAT(O_DISP);
			SET_STAT(O_READY);
	} else {
		RESET_STAT(O_DISP);
	}
	if ( GET_STAT(O_AUTO) && GET_STAT(O_READY) && (!GET_STAT(O_DISP)) )
		ixFreeJobsW(ix->wok);
	
	
	if (OBJ(id)==5&&GET_STAT(O_ATT)) {
		ix->att[ix->att_num] = ix->wok;
		ix->att_num++;
	}
	if (OBJ(id)==5&&GET_STAT(O_DEF)) {
		ix->def[ix->def_num] = ix->wok;
		ix->def_num++;
		OBJ(work[0x11])=0;					/* ダメージ値クリア				*/
	}
	
/*--------------------------------------------------------------------------*/
	
	OBJ(stat)=ix_stat;
	return 1;
}
/*--------------------------------------------------------------------------*/
