/*-
 * 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<stdlib.h>
#include"jump.h"


static floor_t *get_floor( hero_t *hp )
{
	floor_t *fp;

	fp = GAME_get_floor((hp->y+24)/8);
	if( hp->x+12 < fp->x || hp->x+2 > fp->x + fp->width*8 )
		return NULL;
	else
		return fp;
}


int HERO_move( hero_t *hp, int key )
{
	int st;
	int fv,fric,rv;
	floor_t *fp;
	int fl;

	hp->xx = (hp->xx + hp->vx) & COORD_MASK;
	hp->yy = (hp->yy + hp->vy) & COORD_MASK;

	if( hp->xx < 8*COORD_SCALE ){
		hp->xx = 8*COORD_SCALE;
		hp->vx = -hp->vx/2;
	}else if( hp->xx > 233*COORD_SCALE-1 ){
		hp->xx = 233*COORD_SCALE-1;
		hp->vx = -hp->vx/2;
	}

	hp->x = hp->xx / COORD_SCALE;
	hp->y = hp->yy / COORD_SCALE;

	if( hp->vy >= 0 && (fp = get_floor(hp)) != NULL ){
		fl = fp->floor;
		st = 1;
		hp->pic &= 1;
		if( ++hp->count == 20 ){
			hp->count = 0;
			hp->pic ^= 1;
		}
		hp->y &= 504;
		hp->vy = 0;

		fv = fp->move_vector + fp->force_vector;
		fric = fp->fric;
	}else{
		fl = 0;
		st = 0;
		fv = 0;
		fric = 0;
	}

	rv = hp->vx - fv;

	if( fric ){
		if( key & K_LEFT ){
			rv -= (48+15 + rv) / 16;
			hp->dir = 0;
		}else if( key & K_RIGHT ){
			rv += (48+15 - rv) / 16;
			hp->dir = 1;
		}else{
			if( abs(rv) < 2 )
				rv = 0;
			else
				rv += (rv > 0) ? -2 : 2;
		}
	}else{
		if( key & K_LEFT ){
			if( rv > -64 ){
				rv -= rv < 0 ? (48+31 + rv) / 32 : 2;
				hp->dir = 0;
			}
		}else if( key & K_RIGHT ){
			if( rv < 64 ){
				rv += rv > 0 ? (48+31 - rv) / 32 : 2;
				hp->dir = 1;
			}
		}
	}
	hp->vx = rv + fv;

	if( !st ){
		if( hp->jump > 0 ){
			hp->vy = -hp->jump - 40;
			if( key & K_JUMP )
				hp->jump -= 2;
			else
				hp->jump = 0;
		}else{
			hp->vy+=4;
			if( hp->vy > 0 )
				hp->pic = 3;

			if( hp->vy > 64 )
				hp->vy = 64;
		}
	}else if( key & K_JUMP ){
		hp->jump = abs(rv)/2 + 32;
		hp->vy = -hp->jump - 40;
		hp->pic = 2;
	}

	return fl;
}
