<commit_before>#include "DSGM.h"

void DSGM_SetupObjectGroups(DSGM_Room *room, u8 screen, int objectGroupCount) {
	room->objectGroupCount[screen] = objectGroupCount;
	if(objectGroupCount > 0) {
		DSGM_Debug("Allocating %d object group(s) on %s screen\n", objectGroupCount, screen == DSGM_TOP ? "top" : "bottom");
		room->objectGroups[screen] = malloc(objectGroupCount * sizeof(DSGM_ObjectGroup));
		DSGM_Debug("Address %p was given\n", room->objectGroups[screen]);
	}
	else room->objectGroups[screen] = NULL;
}

void DSGM_SetupObjectInstances(DSGM_ObjectGroup *group, DSGM_Object *object, u8 screen, int objectInstanceCount, ...) {
	va_list properties;
	int i;
	group->objectInstanceCount = objectInstanceCount;
	if(objectInstanceCount > 0) {
		DSGM_Debug("Allocating %d object instance(s) on %s screen\n", objectInstanceCount, screen == DSGM_TOP ? "top" : "bottom");
		group->objectInstances = malloc(objectInstanceCount * sizeof(DSGM_ObjectInstance));
		DSGM_Debug("Address %p was given\n", group->objectInstances);
	}
	else group->objectInstances = NULL;
	va_start(properties, objectInstanceCount);
	for(i = 0; i < objectInstanceCount; i++) {
		memset(&group->objectInstances[i], 0, sizeof(DSGM_ObjectInstance));
		group->objectInstances[i].object = object;
		group->objectInstances[i].screen = screen;
		group->objectInstances[i].x = va_arg(properties, int);
		group->objectInstances[i].y = va_arg(properties, int);
		group->objectInstances[i].variables = malloc(object->customVariablesSize);
		memset(group->objectInstances[group->objectInstanceCount].variables, 0, sizeof(object->customVariablesSize));
	}
	va_end(properties);
}

void DSGM_ActivateObjectInstance(DSGM_Room *room, DSGM_ObjectInstance *objectInstance) {
	{
		u8 screen;
		for(screen = 0; screen < 2; screen++) {
			int rotset;
			for(rotset = 0; rotset < 32; rotset++) {
				DSGM_SetRotset(screen, rotset, DSGM_rotations[screen][rotset], DSGM_scales[screen][rotset].x, DSGM_scales[screen][rotset].y);
			}
		}
	}
	
	DSGM_Sprite *sprite = objectInstance->object->sprite;
	DSGM_Palette *palette = sprite->palette;
	u8 screen = objectInstance->screen;
	int spriteNumber = -1;
	
	if(sprite != DSGM_NO_SPRITE) {
		int x, y;
		
		spriteNumber = DSGM_NextFreeSpriteNumber(screen);
		objectInstance->spriteNumber = spriteNumber;
		objectInstance->screen = screen;
		
		if(!DSGM_PaletteLoaded(screen, palette)) {
			DSGM_LoadPaletteFull(screen, palette);
		}
		
		if(!DSGM_SpriteLoaded(screen, sprite)) {
			DSGM_LoadSpriteFull(screen, sprite);
		}
		
		// Initially create sprite offscreen
		DSGM_CreateSprite(screen, spriteNumber, 255, 191, objectInstance->priority, objectInstance->frame, objectInstance->hFlip, objectInstance->vFlip, sprite);
		
		// Extract DSGM_SpriteEntry properties into objectInstance
		memcpy(&objectInstance->oam, &(objectInstance->screen == DSGM_TOP ? oamMain : oamSub).oamMemory[objectInstance->spriteNumber], sizeof(DSGM_SpriteEntry));
		
		// Run create event before showing sprite so the create event can change the position, frame, flipping, etc...
		if(objectInstance->object->create) objectInstance->object->create(objectInstance);
		
		// Don't need to take into account double size here
		x = objectInstance->x - room->view[screen].x;
		y = objectInstance->y - room->view[screen].y;
		if(!(x < 256 && x > -128 && y < 192 && y > -128 && !objectInstance->hide)) {
			x = 255;
			y = 191;
		}
		
		// Recreate sprite
		DSGM_CreateSprite(screen, spriteNumber, x, y, objectInstance->priority, objectInstance->frame, objectInstance->hFlip, objectInstance->vFlip, sprite);
	}
}

DSGM_ObjectGroup *DSGM_GetObjectGroupFull(DSGM_Room *room, u8 screen, DSGM_Object *object) {
	int i;
	for(i = 0; i < room->objectGroupCount[screen]; i++) {
		if(room->objectGroups[screen][i].objectInstanceCount > 0) {
			if(room->objectGroups[screen][i].objectInstances[0].object == object) return &room->objectGroups[screen][i];
		}
	}
	return NULL;
}

DSGM_ObjectInstance *DSGM_CreateObjectInstanceFull(DSGM_Room *room, u8 screen, int x, int y, DSGM_Object *object) {
	DSGM_ObjectGroup *group = DSGM_GetObjectGroupFull(room, screen, object);
	
	if(group) {
		DSGM_Debug("Reallocating object instances at %p\n", group->objectInstances);
		group->objectInstances = realloc(group->objectInstances, (group->objectInstanceCount + 1) * sizeof(DSGM_ObjectInstance));
		DSGM_Debug("Gave address %p for size %d\n", group->objectInstances, (group->objectInstanceCount + 1) * sizeof(DSGM_ObjectInstance));
		
		memset(&group->objectInstances[group->objectInstanceCount], 0, sizeof(DSGM_ObjectInstance));
		group->objectInstances[group->objectInstanceCount].object = object;
		group->objectInstances[group->objectInstanceCount].screen = screen;
		group->objectInstances[group->objectInstanceCount].x = x;
		group->objectInstances[group->objectInstanceCount].y = y;
		group->objectInstances[group->objectInstanceCount].variables = malloc(object->customVariablesSize);
		DSGM_Debug("Allocated custom variables %p for size %d\n", group->objectInstances[group->objectInstanceCount].variables, object->customVariablesSize);
		memset(group->objectInstances[group->objectInstanceCount].variables, 0, sizeof(object->customVariablesSize));
		
		DSGM_ActivateObjectInstance(room, &group->objectInstances[group->objectInstanceCount]);
		return &group->objectInstances[group->objectInstanceCount++];
	}
	else {
		// WARNING: untested!
		
		DSGM_Debug("Reallocating object groups at %p\n", room->objectGroups[screen]);
		group = realloc(room->objectGroups[screen], (room->objectGroupCount[screen] + 1) * sizeof(DSGM_ObjectGroup));
		DSGM_Debug("Gave address %p\n", room->objectGroups[screen]);
		
		group = &room->objectGroups[screen][room->objectGroupCount[screen]];
		DSGM_SetupObjectInstances(group, object, screen, 1, x, y);
		
		DSGM_ActivateObjectInstance(room, &group->objectInstances[0]);
		return &group->objectInstances[0];
	}
}

// To do: DSGM_DeleteObjectInstance

void (DSGM_AddCollisionEvent)(DSGM_Object *object, DSGM_Object *collider, DSGM_CollisionEventFunction function) {
	DSGM_Debug("Adding collision event\n");
	object->collisionEvents = realloc(object->collisionEvents, (object->collisionEventCount + 1) * sizeof(DSGM_CollisionEvent));
	object->collisionEvents[object->collisionEventCount].collider = collider;
	object->collisionEvents[object->collisionEventCount].function = function;
	object->collisionEventCount++;
}

inline bool DSGM_StylusOverObjectInstanceFull(DSGM_Room *room, DSGM_ObjectInstance *me) {
	return DSGM_stylus.px >= me->x - room->view[me->screen].x && DSGM_stylus.px <= me->x - room->view[me->screen].x + DSGM_GetSpriteWidth(me->object->sprite) && DSGM_stylus.py >= me->y - room->view[me->screen].y && DSGM_stylus.py <= me->y - room->view[me->screen].y + DSGM_GetSpriteHeight(me->object->sprite);
}

inline bool DSGM_ObjectInstanceOnScreenFull(DSGM_Room *room, DSGM_ObjectInstance *me) {
	return ((!me->x - room->view[me->screen].x >= 255) && (!me->y - room->view[me->screen].y >= 191)) &&\
	((!me->x - room->view[me->screen].x <= -DSGM_GetSpriteWidth(me->object->sprite)) && (!me->y - room->view[me->screen].y <= -DSGM_GetSpriteHeight(me->object->sprite))) &&\
	!me->hide;
}<commit_msg>Tested creating a new object group<commit_after>#include "DSGM.h"

void DSGM_SetupObjectGroups(DSGM_Room *room, u8 screen, int objectGroupCount) {
	room->objectGroupCount[screen] = objectGroupCount;
	if(objectGroupCount > 0) {
		DSGM_Debug("Allocating %d object group(s) on %s screen\n", objectGroupCount, screen == DSGM_TOP ? "top" : "bottom");
		room->objectGroups[screen] = malloc(objectGroupCount * sizeof(DSGM_ObjectGroup));
		DSGM_Debug("Address %p was given\n", room->objectGroups[screen]);
	}
	else room->objectGroups[screen] = NULL;
}

void DSGM_SetupObjectInstances(DSGM_ObjectGroup *group, DSGM_Object *object, u8 screen, int objectInstanceCount, ...) {
	va_list properties;
	int i;
	group->objectInstanceCount = objectInstanceCount;
	if(objectInstanceCount > 0) {
		DSGM_Debug("Allocating %d object instance(s) on %s screen\n", objectInstanceCount, screen == DSGM_TOP ? "top" : "bottom");
		group->objectInstances = malloc(objectInstanceCount * sizeof(DSGM_ObjectInstance));
		DSGM_Debug("Address %p was given\n", group->objectInstances);
	}
	else group->objectInstances = NULL;
	va_start(properties, objectInstanceCount);
	for(i = 0; i < objectInstanceCount; i++) {
		memset(&group->objectInstances[i], 0, sizeof(DSGM_ObjectInstance));
		group->objectInstances[i].object = object;
		group->objectInstances[i].screen = screen;
		group->objectInstances[i].x = va_arg(properties, int);
		group->objectInstances[i].y = va_arg(properties, int);
		group->objectInstances[i].variables = malloc(object->customVariablesSize);
		memset(group->objectInstances[group->objectInstanceCount].variables, 0, sizeof(object->customVariablesSize));
	}
	va_end(properties);
}

void DSGM_ActivateObjectInstance(DSGM_Room *room, DSGM_ObjectInstance *objectInstance) {
	{
		u8 screen;
		for(screen = 0; screen < 2; screen++) {
			int rotset;
			for(rotset = 0; rotset < 32; rotset++) {
				DSGM_SetRotset(screen, rotset, DSGM_rotations[screen][rotset], DSGM_scales[screen][rotset].x, DSGM_scales[screen][rotset].y);
			}
		}
	}
	
	DSGM_Sprite *sprite = objectInstance->object->sprite;
	DSGM_Palette *palette = sprite->palette;
	u8 screen = objectInstance->screen;
	int spriteNumber = -1;
	
	if(sprite != DSGM_NO_SPRITE) {
		int x, y;
		
		spriteNumber = DSGM_NextFreeSpriteNumber(screen);
		objectInstance->spriteNumber = spriteNumber;
		objectInstance->screen = screen;
		
		if(!DSGM_PaletteLoaded(screen, palette)) {
			DSGM_LoadPaletteFull(screen, palette);
		}
		
		if(!DSGM_SpriteLoaded(screen, sprite)) {
			DSGM_LoadSpriteFull(screen, sprite);
		}
		
		// Initially create sprite offscreen
		DSGM_CreateSprite(screen, spriteNumber, 255, 191, objectInstance->priority, objectInstance->frame, objectInstance->hFlip, objectInstance->vFlip, sprite);
		
		// Extract DSGM_SpriteEntry properties into objectInstance
		memcpy(&objectInstance->oam, &(objectInstance->screen == DSGM_TOP ? oamMain : oamSub).oamMemory[objectInstance->spriteNumber], sizeof(DSGM_SpriteEntry));
		
		// Run create event before showing sprite so the create event can change the position, frame, flipping, etc...
		if(objectInstance->object->create) objectInstance->object->create(objectInstance);
		
		// Don't need to take into account double size here
		x = objectInstance->x - room->view[screen].x;
		y = objectInstance->y - room->view[screen].y;
		if(!(x < 256 && x > -128 && y < 192 && y > -128 && !objectInstance->hide)) {
			x = 255;
			y = 191;
		}
		
		// Recreate sprite
		DSGM_CreateSprite(screen, spriteNumber, x, y, objectInstance->priority, objectInstance->frame, objectInstance->hFlip, objectInstance->vFlip, sprite);
	}
}

DSGM_ObjectGroup *DSGM_GetObjectGroupFull(DSGM_Room *room, u8 screen, DSGM_Object *object) {
	int i;
	for(i = 0; i < room->objectGroupCount[screen]; i++) {
		if(room->objectGroups[screen][i].objectInstanceCount > 0) {
			if(room->objectGroups[screen][i].objectInstances[0].object == object) return &room->objectGroups[screen][i];
		}
	}
	return NULL;
}

DSGM_ObjectInstance *DSGM_CreateObjectInstanceFull(DSGM_Room *room, u8 screen, int x, int y, DSGM_Object *object) {
	DSGM_ObjectGroup *group = DSGM_GetObjectGroupFull(room, screen, object);
	
	if(group) {
		DSGM_Debug("Reallocating object instances at %p\n", group->objectInstances);
		group->objectInstances = realloc(group->objectInstances, (group->objectInstanceCount + 1) * sizeof(DSGM_ObjectInstance));
		DSGM_Debug("Gave address %p for size %d\n", group->objectInstances, (group->objectInstanceCount + 1) * sizeof(DSGM_ObjectInstance));
		
		memset(&group->objectInstances[group->objectInstanceCount], 0, sizeof(DSGM_ObjectInstance));
		group->objectInstances[group->objectInstanceCount].object = object;
		group->objectInstances[group->objectInstanceCount].screen = screen;
		group->objectInstances[group->objectInstanceCount].x = x;
		group->objectInstances[group->objectInstanceCount].y = y;
		group->objectInstances[group->objectInstanceCount].variables = malloc(object->customVariablesSize);
		DSGM_Debug("Allocated custom variables %p for size %d\n", group->objectInstances[group->objectInstanceCount].variables, object->customVariablesSize);
		memset(group->objectInstances[group->objectInstanceCount].variables, 0, sizeof(object->customVariablesSize));
		
		DSGM_ActivateObjectInstance(room, &group->objectInstances[group->objectInstanceCount]);
		return &group->objectInstances[group->objectInstanceCount++];
	}
	else {
		// To do, fix this
		
		/*DSGM_Debug("Reallocating object groups at %p\n", room->objectGroups[screen]);
		group = realloc(room->objectGroups[screen], (room->objectGroupCount[screen] + 1) * sizeof(DSGM_ObjectGroup));
		DSGM_Debug("Gave address %p\n", room->objectGroups[screen]);
		
		group = &room->objectGroups[screen][room->objectGroupCount[screen]];
		DSGM_SetupObjectInstances(group, object, screen, 1, x, y);
		
		DSGM_ActivateObjectInstance(room, &group->objectInstances[0]);
		return &group->objectInstances[0];*/
		
		return NULL;
	}
}

// To do: DSGM_DeleteObjectInstance

void (DSGM_AddCollisionEvent)(DSGM_Object *object, DSGM_Object *collider, DSGM_CollisionEventFunction function) {
	DSGM_Debug("Adding collision event\n");
	object->collisionEvents = realloc(object->collisionEvents, (object->collisionEventCount + 1) * sizeof(DSGM_CollisionEvent));
	object->collisionEvents[object->collisionEventCount].collider = collider;
	object->collisionEvents[object->collisionEventCount].function = function;
	object->collisionEventCount++;
}

inline bool DSGM_StylusOverObjectInstanceFull(DSGM_Room *room, DSGM_ObjectInstance *me) {
	return DSGM_stylus.px >= me->x - room->view[me->screen].x && DSGM_stylus.px <= me->x - room->view[me->screen].x + DSGM_GetSpriteWidth(me->object->sprite) && DSGM_stylus.py >= me->y - room->view[me->screen].y && DSGM_stylus.py <= me->y - room->view[me->screen].y + DSGM_GetSpriteHeight(me->object->sprite);
}

inline bool DSGM_ObjectInstanceOnScreenFull(DSGM_Room *room, DSGM_ObjectInstance *me) {
	return ((!me->x - room->view[me->screen].x >= 255) && (!me->y - room->view[me->screen].y >= 191)) &&\
	((!me->x - room->view[me->screen].x <= -DSGM_GetSpriteWidth(me->object->sprite)) && (!me->y - room->view[me->screen].y <= -DSGM_GetSpriteHeight(me->object->sprite))) &&\
	!me->hide;
}
{'fraction_non_alphanumeric': 0.14696485623003194, 'fraction_numerical': 0.008195582719822198, 'mean_word_length': 5.966134494436381, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 273, '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': '1656078', 'n_tokens_mistral': 5021, 'n_tokens_neox': 4653, 'n_words': 853}