<commit_before>import {moveToken} from '../../../common/gameState/positions';
import {knockDownCharacter} from '../../../common/gameState/knockedDownCharacters';
import {checkGameState} from '../room';

//thunks
export const move = (token, location) => (
	(dispatch) => {
		dispatch(moveToken(token, location));
		dispatch(checkGameState());
	}
);

function squishChar(room, location, pos, char, players, dispatch, distance = 1) {
	const options = [
		[pos[0], pos[1] + distance],
		[pos[0], pos[1] - distance],
		[pos[0] + distance, pos[1]],
		[pos[0] - distance, pos[1]],
	].filter((opt) => {
		const optDiffX = location[0] - opt[0];
		const optDiffY = opt[1] - location[1];

		if (
			(optDiffX <= 0 && optDiffX > -1 * room.gameState.monsterStats.size) &&
			(optDiffY <= 0 && optDiffY > -1 * room.gameState.monsterStats.size)
		) {
			return false;
		}

		if (players.filter((player) => player[0] === opt[0] && player[1] === opt[1]).length) {
			return false;
		}

		return true;
	});

	if (options.length === 0) {
		return squishChar(room, location, pos, distance); //Not an ideal way to handle this but it will be a rare case so...
	} else if (options.length === 1) {
		console.log('Moving', char, 'to', options[0]);
		dispatch(knockDownCharacter(char));
		dispatch(move(`player${char + 1}`, options[0]));
	} else {
		// TODO: Offer a choice
		dispatch(knockDownCharacter(char));
		dispatch(move(`player${char + 1}`, options[0]));
	}
}

export const moveMonster = (location) => (
	(dispatch, getState) => {
		location = [
			Math.max(Math.min(location[0], 20), 1),
			Math.max(Math.min(location[1], 14), 1)
		];
		// TODO: Knock over any players that the monster would run over
		const {room} = getState();
		const players = [
			room.gameState.positions.player1,
			room.gameState.positions.player2,
			room.gameState.positions.player3,
			room.gameState.positions.player4
		];
		console.log(players, location);
		players.forEach((pos, i) => {
			const diffX = location[0] - pos[0];
			const diffY = pos[1] - location[1];
			if (
				(diffX <= 0 && diffX > -1 * room.gameState.monsterStats.size) &&
				(diffY <= 0 && diffY > -1 * room.gameState.monsterStats.size)
			) {
				squishChar(room, location, pos, i, players, dispatch);
			}
		});

		dispatch(moveToken('monster', location));
	}
);<commit_msg>The monster now moves closer if the target is out of range<commit_after>import {moveToken} from '../../../common/gameState/positions';
import {knockDownCharacter} from '../../../common/gameState/knockedDownCharacters';
import {checkGameState} from '../room';

//thunks
export const move = (token, location) => (
	(dispatch) => {
		dispatch(moveToken(token, location));
		dispatch(checkGameState());
	}
);

function squishChar(room, location, pos, char, players, dispatch, distance = 1) {
	const options = [
		[pos[0], pos[1] + distance],
		[pos[0], pos[1] - distance],
		[pos[0] + distance, pos[1]],
		[pos[0] - distance, pos[1]],
	].filter((opt) => {
		const optDiffX = location[0] - opt[0];
		const optDiffY = opt[1] - location[1];

		if (
			(optDiffX <= 0 && optDiffX > -1 * room.gameState.monsterStats.size) &&
			(optDiffY <= 0 && optDiffY > -1 * room.gameState.monsterStats.size)
		) {
			return false;
		}

		if (players.filter((player) => player[0] === opt[0] && player[1] === opt[1]).length) {
			return false;
		}

		return true;
	});

	if (options.length === 0) {
		return squishChar(room, location, pos, distance); //Not an ideal way to handle this but it will be a rare case so...
	} else if (options.length === 1) {
		console.log('Moving', char, 'to', options[0]);
		dispatch(knockDownCharacter(char));
		dispatch(move(`player${char + 1}`, options[0]));
	} else {
		// TODO: Offer a choice
		dispatch(knockDownCharacter(char));
		dispatch(move(`player${char + 1}`, options[0]));
	}
}

function squishChars(room, location, dispatch) {
	const players = [
		room.gameState.positions.player1,
		room.gameState.positions.player2,
		room.gameState.positions.player3,
		room.gameState.positions.player4
	];
	players.forEach((pos, i) => {
		const diffX = location[0] - pos[0];
		const diffY = pos[1] - location[1];
		if (
			(diffX <= 0 && diffX > -1 * room.gameState.monsterStats.size) &&
			(diffY <= 0 && diffY > -1 * room.gameState.monsterStats.size)
		) {
			squishChar(room, location, pos, i, players, dispatch);
		}
	});

	dispatch(moveToken('monster', location));
}

function checkMaxRange(room, location) {
	const monsterLocation = room.gameState.positions.monster;
	const monsterMovement = room.gameState.monsterStats.movement;
	// TODO: Determine and Offer options
	let distance = monsterMovement;

	do {
		distance = Math.abs(monsterLocation[0] - location[0]) + Math.abs(monsterLocation[1] - location[1]);
		if (distance > monsterMovement) {
			location[0]--;
		}
		if (distance - 1 > monsterMovement) {
			location[1]--;
		}
	} while(distance - 2 > monsterMovement);

	return location;
}

export const moveMonster = (location) => (
	(dispatch, getState) => {
		location = [
			Math.max(Math.min(location[0], 20), 1),
			Math.max(Math.min(location[1], 14), 1)
		];
		// TODO: Knock over any players that the monster would run over
		const {room} = getState();
		location = checkMaxRange(room, location);

		squishChars(room, location, dispatch);
		dispatch(moveToken('monster', location));
	}
);
{'fraction_non_alphanumeric': 0.17896713615023474, 'fraction_numerical': 0.019530516431924883, 'mean_word_length': 3.8462238398544133, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5817033', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1820, 'n_words': 428}