/*-
 * Copyright (c) 2000
 * Tatsuya Kudoh(CDR/TK),ROYALPANDA.    All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include"jump.h"
#include"fontdata.h"

static int Fpos;
static int Cur_floor;
static int Fcount;
static int Cur_sprite;

static int Prob;


void FLOOR_delete( floor_t *fp )
{
	fp->x = 256;
	fp->sprite = 0;
}


void FLOOR_create( floor_t *fp, int level )
{
	int x1,x2;
	int prob;
	int i;

	Fcount = (Fcount+1) % 5;

	if( Fcount ){
		fp->x = 256;
		fp->sprite = 0;
		return;
	}

	if( Cur_floor < 30000 )
		Cur_floor++;
	fp->floor = Cur_floor;

	i = rnd_pm() * (rnd(4)+8);
	Fpos += i;
	if( Fpos < 4 )
		Fpos = 4 + rnd(4);
	else if( Fpos > 23 )
		Fpos = 23 - rnd(4);

	if( Cur_floor % 50 == 1 ){
		fp->sprite = 0;
		fp->x = 8;
		fp->xx = 8*COORD_SCALE;
		fp->width = 30;
		fp->fric = 1;
		fp->move_vector = 0;
		fp->blink_interval = 0;
		fp->force_vector = 0;
		fp->blink_count = 0;
		fp->pic = 0;
		fp->viewable = 1;
		Prob += (100-Prob)/4;
		return;
	}

	x1 = ( Fpos - rnd(3) - 2);
	x2 = ( Fpos + rnd(2) + 2);
	if( x1 < 1 ){
		x2 += 1 - x1;
		x1 = 1;
	}else if( x2 > 30 ){
		x1 -= x2 - 30;
		x2 = 30;
	}

	fp->x = x1 * 8;
	fp->xx = x1 * 8 * COORD_SCALE;
	fp->width = x2 - x1 + 1;

	fp->phase = 0;
	fp->viewable = 1;

	prob = Prob;

	if( rnd(100)+30 > prob ){
		fp->pic = 0;
		fp->fric = 1;
		fp->force_vector = 0;
	}else if( rnd(100) > 50 ){
		fp->pic = 1;
		fp->fric = 0;
		fp->force_vector = 0;
		prob -= prob/8;
	}else{
		fp->pic = 2;
		fp->force_vector = rnd_pm()*(rnd(COORD_SCALE*(level+1)/4)+1);
		fp->fric = 1;
		prob -= prob/8;
	}

	if( rnd(100)+30 < prob )
		fp->move_vector = rnd_pm()*(rnd(COORD_SCALE*(level+1)/4)+1);
	else
		fp->move_vector = 0;

	if( fp->move_vector ){
		Cur_sprite = (Cur_sprite + 1) & 7;
		fp->sprite = Cur_sprite*8 + 40;
		prob -= prob/4;
	}else{
		fp->sprite = 0;
	}

	if( rnd(100)+50 < prob ){
		fp->blink_interval = rnd(100)+50;
		fp->blink_count = fp->blink_interval
					- rnd(fp->blink_interval/4);
	}else{
		fp->blink_interval = 0;
		fp->blink_count = 0;
	}
}


void FLOOR_move( floor_t *fp )
{
	if( fp->x >= 256 )
		return;

	if( fp->move_vector ){
		fp->xx += fp->move_vector;
		if( fp->xx < 8*COORD_SCALE ){
			fp->xx = 8*COORD_SCALE;
			fp->move_vector = -fp->move_vector;
		}else if( fp->xx+fp->width*8*COORD_SCALE > 249*COORD_SCALE-1 ){
			fp->xx = 249*COORD_SCALE-1 - fp->width*8*COORD_SCALE;
			fp->move_vector = -fp->move_vector;
		}
		fp->x = fp->xx / COORD_SCALE;
	}

	if( fp->force_vector ){
		fp->phase = (fp->phase + fp->force_vector)&(16*COORD_SCALE-1);
		fp->pic = fp->phase/COORD_SCALE + 2;
	}

	if( fp->blink_interval ){
		fp->blink_count--;
		if( fp->blink_count == 0 ){
			fp->blink_count = fp->blink_interval;
			fp->viewable = !fp->viewable;
		}
	}
}


void FLOOR_init( void )
{
	Fpos = rnd(28) + 2;

	Cur_floor = 0;
	Fcount = 4;
	Prob = 0;
}
