// Copyright (c) 2023 Alex Diener. All rights reserved.

#ifndef __Utilities_H__
#define __Utilities_H__

#include "gamemath/Vector2i.h"
#include "PROJECT_NAME/RoomState.h"
#include "PROJECT_NAME/SharedDefinitions.h"
#include <stdint.h>

#define memreadPrimitive(context, variable, type) { \
	type typedValue; \
	memread(context, sizeof(typedValue), &typedValue); \
	variable = typedValue; \
}
#define memwritePrimitive(context, variable, type) { \
	type typedValue = variable; \
	memwrite(context, sizeof(typedValue), &typedValue); \
}
#define memreadXorPrimitive(context, variable, type) { \
	type typedValue = variable, xorValue; \
	memread(context, sizeof(xorValue), &xorValue); \
	variable = typedValue ^ xorValue; \
}

void memxor(void * to, const void * from, size_t size);

bool isMoveBlockedAtPosition(RoomState * roomState, Vector2i position);

static inline Vector2i facingToVector2i(FacingDirection facing) {
	switch (facing) {
		case FACING_NORTH:
			return VECTOR2i(0, -1);
		case FACING_EAST:
			return VECTOR2i(1, 0);
		case FACING_SOUTH:
			return VECTOR2i(0, 1);
		case FACING_WEST:
			return VECTOR2i(-1, 0);
	}
	return VECTOR2i_ZERO;
}

static inline FacingDirection rotateFacingCW(FacingDirection facing) {
	return (facing + 1) % 4;
}

static inline FacingDirection rotateFacingCCW(FacingDirection facing) {
	return (facing + 3) % 4;
}

#endif
