/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHYSICS_BODY_MANAGER_QUAD_TREE
#define RAT_PHYSICS_BODY_MANAGER_QUAD_TREE

#include "common.h"
#include "body_manager.h"
#include "quad_tree.h"

typedef struct rat_body_manager_quad_tree
{
	rat_body_manager_type type;
	rat_body_manager_class *clas;

	struct rat_world *world_ref;

	unsigned int numbods;
	unsigned int numbodsrmv;
	unsigned int numbodsrmvdel;
	unsigned int numstatic;
	unsigned int numdynamic;
	unsigned int numarbs,maxarbs;

	rat_body **bodies;
	rat_body **bodiestoremove;
	rat_body **bodiestodel;
	rat_arbiter **arbiters;

	rat_body **static_bodies;
	rat_body **dynamic_bodies;

	rat_quad_tree_matrix *quadtreematrix;
} rat_body_manager_quad_tree;

#ifdef __cplusplus
extern "C" {
#endif

rat_body_manager *rat_body_manager_quad_tree_create(struct rat_world *world_ref,aabb worldsize,vector2 approxcell);
void rat_body_manager_quad_tree_destroy(rat_body_manager *table);
void rat_body_manager_quad_tree_add_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_quad_tree_remove_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_quad_tree_remove_and_destroy_body(rat_body_manager *table,rat_body *body);
void rat_body_manager_quad_tree_clear_bodies(rat_body_manager *table);
void rat_body_manager_quad_tree_free_bodies(rat_body_manager *table);

void rat_body_manager_quad_tree_build_spatial_index(rat_body_manager *table);
void rat_body_manager_quad_tree_collide_bodies(rat_body_manager *table);

unsigned int rat_body_manager_quad_tree_count_all_bodies(rat_body_manager *table);
unsigned int rat_body_manager_quad_tree_count_static_bodies(rat_body_manager *table);
unsigned int rat_body_manager_quad_tree_count_dynamic_bodies(rat_body_manager *table);
unsigned int rat_body_manager_quad_tree_count_arbiters(rat_body_manager *table);

rat_iterator *rat_body_manager_quad_tree_all_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_quad_tree_static_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_quad_tree_dynamic_bodies_iterator(rat_body_manager *table);
rat_iterator *rat_body_manager_quad_tree_arbiters_iterator(rat_body_manager *table);

void rat_quad_tree_collide(rat_body *a,rat_body *b,void *data);

#ifdef __cplusplus
};
#endif

#endif
