Index: model/modelread.cpp
===================================================================
--- model/modelread.cpp	(revision 9365)
+++ model/modelread.cpp	(working copy)
@@ -54,7 +54,7 @@
 
 // info for special polygon lists
 
-polymodel *Polygon_models[MAX_POLYGON_MODELS];
+SCP_vector<polymodel*> Polygon_models;
 SCP_vector<polymodel_instance*> Polygon_model_instances;
 
 SCP_vector<bsp_collision_tree> Bsp_collision_tree_list;
@@ -125,15 +125,12 @@
 // With the basic page in system this can be called from outside of modelread.cpp
 void model_unload(int modelnum, int force)
 {
-	int i, j, num;
+	int i, j;
+	size_t num;
 
-	if ( modelnum >= MAX_POLYGON_MODELS ) {
-		num = modelnum % MAX_POLYGON_MODELS;
-	} else {
-		num = modelnum;
-	}
+	num = modelnum;
 
-	if ( (num < 0) || (num >= MAX_POLYGON_MODELS))	{
+	if ( (num < 0) || (num >= Polygon_models.size()))	{
 		return;
 	}
 
@@ -286,8 +283,6 @@
 
 void model_free_all()
 {
-	int i;
-
 	if ( !model_initted)	{
 		model_init();
 		return;
@@ -296,7 +291,7 @@
 	mprintf(( "Freeing all existing models...\n" ));
 	model_instance_free_all();
 
-	for (i=0;i<MAX_POLYGON_MODELS;i++) {
+	for (size_t i = 0; i<Polygon_models.size(); i++) {
 		// forcefully unload all loaded models (be careful with this)
 		model_unload(i, 1);		
 	}
@@ -318,8 +313,6 @@
 
 void model_page_in_start()
 {
-	int i;
-
 	if ( !model_initted ) {
 		model_init();
 		return;
@@ -327,35 +320,31 @@
 
 	mprintf(( "Starting model page in...\n" ));
 
-	for (i=0; i<MAX_POLYGON_MODELS; i++) {
-		if (Polygon_models[i] != NULL)
-			Polygon_models[i]->used_this_mission = 0;
+	for (SCP_vector<polymodel*>::iterator pmit = Polygon_models.begin(); pmit != Polygon_models.end(); ++pmit) {
+		if ((*pmit) != NULL)
+			(*pmit)->used_this_mission = 0;
 	}
 }
 
 void model_page_in_stop()
 {
-	int i;
-
 	Assert( model_initted );
 
 	mprintf(( "Stopping model page in...\n" ));
 
-	for (i=0; i<MAX_POLYGON_MODELS; i++) {
-		if (Polygon_models[i] == NULL)
+	for (SCP_vector<polymodel*>::iterator pmit = Polygon_models.begin(); pmit != Polygon_models.end(); ++pmit) {
+		if ((*pmit) == NULL)
 			continue;
 
-		if (Polygon_models[i]->used_this_mission)
+		if ((*pmit)->used_this_mission)
 			continue;
-	
-		model_unload(i);
+
+		model_unload(pmit - Polygon_models.begin());
 	}
 }
 
 void model_init()
 {
-	int i;
-
 	if ( model_initted )		{
 		Int3();		// Model_init shouldn't be called twice!
 		return;
@@ -365,9 +354,7 @@
 	Model_ram = 0;
 #endif
 
-	for (i=0;i<MAX_POLYGON_MODELS;i++) {
-		Polygon_models[i] = NULL;
-	}
+	Polygon_models.clear();
 
 	atexit( model_free_all );
 	model_initted = 1;
@@ -2408,7 +2395,7 @@
 
 	num = -1;
 
-	for (i=0; i< MAX_POLYGON_MODELS; i++)	{
+	for (i=0; i< (int)Polygon_models.size(); i++)	{
 		if ( Polygon_models[i] )	{
 			if (!stricmp(filename, Polygon_models[i]->filename) && !duplicate)		{
 				// Model already loaded; just return.
@@ -2421,6 +2408,9 @@
 		}
 	}
 
+	if (Polygon_models.size() == 0)
+		num = 0;
+
 	// No empty slot
 	if ( num == -1 )	{
 		Error( LOCATION, "Too many models" );
@@ -2431,22 +2421,15 @@
 
 	pm = (polymodel *)vm_malloc( sizeof(polymodel) );
 	Assert( pm != NULL );
+
+	memset(pm, 0, sizeof(polymodel));
 	
-	Polygon_models[num] = pm;
-	
-	memset(pm, 0, sizeof(polymodel));
+	Polygon_models.push_back(pm);
 
 	pm->n_paths = 0;
 	pm->paths = NULL;
 
-	int org_sig = Model_signature;
-	Model_signature+=MAX_POLYGON_MODELS;
-	if ( Model_signature < org_sig )	{
-		Model_signature = 0;
-	}
-	Assert( (Model_signature % MAX_POLYGON_MODELS) == 0 );
-	pm->id = Model_signature + num;
-	Assert( (pm->id % MAX_POLYGON_MODELS) == num );
+	pm->id = Polygon_models.size()-1;
 
 	extern int Parse_normal_problem_count;
 	Parse_normal_problem_count = 0;
@@ -2940,17 +2923,10 @@
 		return NULL;
 	}
 
-	int num = model_num % MAX_POLYGON_MODELS;
-	
-	Assertion( num >= 0, "Model id %d is invalid. Please backtrace and investigate.\n", num);
-	Assertion( num < MAX_POLYGON_MODELS, "Model id %d is larger than MAX_POLYGON_MODELS (%d). This is impossible, thus we have to conclude that math as we know it has ceased to work.\n", num, MAX_POLYGON_MODELS );
-	Assertion( Polygon_models[num], "No model with id %d found. Please backtrace and investigate.\n", num );
-	Assertion( Polygon_models[num]->id == model_num, "Index collision between model %s and requested model %d. Please backtrace and investigate.\n", Polygon_models[num]->filename, model_num );
-
-	if (num < 0 || num > MAX_POLYGON_MODELS || !Polygon_models[num] || Polygon_models[num]->id != model_num)
+	if (model_num < (int)Polygon_models.size())
+		return Polygon_models[model_num];
+	else
 		return NULL;
-
-	return Polygon_models[num];
 }
 
 polymodel_instance* model_get_instance(int model_instance_num)
