int **SectorTable = NULL;
int *RoomCountBySector = NULL;
int *SectorTable_Inside = NULL;
int *SectorTable_City = NULL;
int *SectorTable_Field = NULL;
int *SectorTable_Forest = NULL;
int *SectorTable_Hills= NULL;
int *SectorTable_Mountain= NULL;
int *SectorTable_Water_swim= NULL;
int *SectorTable_Water_noswim= NULL;
int *SectorTable_Air= NULL;
int *SectorTable_Desert= NULL;
int *SectorTable_Fire= NULL;
int *SectorTable_Subzero= NULL;
int *SectorTable_Tornado= NULL;
int *SectorTable_Quicksand= NULL;
int *SectorTable_Acid= NULL;
int *SectorTable_Uwater= NULL;
bool_t initDone = FALSE;
void init( void ) {
int i;
SectorTable = (int **)calloc(SECT_MAX, sizeof(int *));
RoomCountBySector = (int *)calloc(SECT_MAX, sizeof(int));
/* Not needed, because calloc() already sets memory to zero-filled.
for( i = 0; i < SECT_MAX; i++ ) {
RoomCountBySector[i] = 0;
SectorTable[i] = NULL;
}
*/
}
ROOM_INDEX_DATA *get_random_room_sector(short int sector_type) {
ROOM_INDEX_DATA *room = NULL;
int vnum = 0;
//if not already done, build the table for the sector asked
if(!initDone) {
AREA_DATA *pArea = NULL;
int tempSectRoom[SECT_MAX];
for( vnum = 0; vnum < SECT_MAX; vnum++ )
tempSectRoom[vnum] = RoomCountBySector[vnum];
SectorTable[SECT_INSIDE] = RoomCountBySector[SECT_INSIDE] > 0 ? (int *)calloc(RoomCountBySector[SECT_INSIDE], sizeof(int)) : NULL;
SectorTable[SECT_CITY] = RoomCountBySector[SECT_CITY] > 0 ? (int *)calloc(RoomCountBySector[SECT_CITY], sizeof(int)) : NULL;
SectorTable[SECT_FIELD] = RoomCountBySector[SECT_FIELD] > 0 ? (int *)calloc(RoomCountBySector[SECT_FIELD], sizeof(int)) : NULL;
SectorTable[SECT_FOREST] = RoomCountBySector[SECT_FOREST] > 0 ? (int *)calloc(RoomCountBySector[SECT_FOREST], sizeof(int)) : NULL;
SectorTable[SECT_HILLS] = RoomCountBySector[SECT_HILLS] > 0 ? (int *)calloc(RoomCountBySector[SECT_HILLS], sizeof(int)) : NULL;
SectorTable[SECT_MOUNTAIN] = RoomCountBySector[SECT_MOUNTAIN] > 0 ? (int *)calloc(RoomCountBySector[SECT_MOUNTAIN], sizeof(int)) : NULL;
SectorTable[SECT_WATER_SWIM] = RoomCountBySector[SECT_WATER_SWIM] > 0 ? (int *)calloc(RoomCountBySector[SECT_WATER_SWIM], sizeof(int)) : NULL;
SectorTable[SECT_WATER_NOSWIM] = RoomCountBySector[SECT_WATER_NOSWIM] > 0 ? (int *)calloc(RoomCountBySector[SECT_WATER_NOSWIM], sizeof(int)) : NULL;
SectorTable[SECT_AIR] = RoomCountBySector[SECT_AIR] > 0 ? (int *)calloc(RoomCountBySector[SECT_AIR], sizeof(int)) : NULL;
SectorTable[SECT_DESERT] = RoomCountBySector[SECT_DESERT] > 0 ? (int *)calloc(RoomCountBySector[SECT_DESERT], sizeof(int)) : NULL;
SectorTable[SECT_FIRE] = RoomCountBySector[SECT_FIRE] > 0 ? (int *)calloc(RoomCountBySector[SECT_FIRE], sizeof(int)) : NULL;
SectorTable[SECT_SUBZERO] = RoomCountBySector[SECT_SUBZERO] > 0 ? (int *)calloc(RoomCountBySector[SECT_SUBZERO], sizeof(int)) : NULL;
SectorTable[SECT_TORNADO] = RoomCountBySector[SECT_TORNADO] > 0 ? (int *)calloc(RoomCountBySector[SECT_TORNADO], sizeof(int)) : NULL;
SectorTable[SECT_QUICKSAND] = RoomCountBySector[SECT_QUICKSAND] > 0 ? (int *)calloc(RoomCountBySector[SECT_QUICKSAND], sizeof(int)) : NULL;
SectorTable[SECT_ACID] = RoomCountBySector[SECT_ACID] > 0 ? (int *)calloc(RoomCountBySector[SECT_ACID], sizeof(int)) : NULL;
SectorTable[SECT_UWATER] = RoomCountBySector[SECT_UWATER] > 0 ? (int *)calloc(RoomCountBySector[SECT_UWATER], sizeof(int)) : NULL;
// If you want the seperate tables, here's where you copy them….
SectorTable_Inside = SectorTable[SECT_INSIDE];
SectorTable_City = SectorTable[SECT_CITY];
SectorTable_Field = SectorTable[SECT_FIELD];
SectorTable_Forest = SectorTable[SECT_FOREST];
SectorTable_Hills = SectorTable[SECT_HILLS];
SectorTable_Mountain = SectorTable[SECT_MOUNTAIN];
SectorTable_Water_swim = SectorTable[SECT_WATER_SWIM];
SectorTable_Water_noswim = SectorTable[SECT_WATER_NOSWIM];
SectorTable_Air = SectorTable[SECT_AIR];
SectorTable_Desert = SectorTable[SECT_DESERT];
SectorTable_Fire = SectorTable[SECT_FIRE];
SectorTable_Subzero = SectorTable[SECT_SUBZERO];
SectorTable_Tornado = SectorTable[SECT_TORNADO];
SectorTable_Quicksand = SectorTable[SECT_QUICKSAND];
SectorTable_Acid = SectorTable[SECT_ACID];
SectorTable_Uwater = SectorTable[SECT_UWATER];
for( pArea = area_first; pArea; pArea = pArea->next ) {
for( vnum = pArea->lvnum; vnum <= pArea->uvnum; vnum++ ) {
room = get_room_index(vnum);
if( !room ||
room->sectorType == SECT_UNUSED ||
room->sectorType == SECT_UNUSED2 ||
room->sectorType >= SECT_MAX
)
continue;
tempSectRoom[room->sectorType]–;
SectorTable[room->sectorType][tempSectRoom[room->sectorType]] = vnum;
}
}
}
vnum = 3;
room = NULL;
while( vnum > 0 ) {//so it never goes in infinite loop (should not but but it depends on areas loaded)
long roomIndex;
vnum–;
roomIndex = number_range(0, RoomCountBySector[sector_type] - 1);
if( RoomCountBySector[sector_type] > 0 ) {
bugf("sector:%d nb room:%d roomindex:%d", sector_type, RoomCountBySector[sector_type], roomIndex);
room = get_room_index(SectorTable[sector_type][roomIndex]);
}
if ( !room || !room->desc || strlen(room->desc) == 0)
continue;
if (IS_SET(room->room_flags, ROOM_NO_RANDOM) || IS_SET(room->room_flags,ROOM_LAW))
continue;
break;
}
return room;
}
ROOM_INDEX_DATA *get_random_room_sector(short int sector_type) {
ROOM_INDEX_DATA *room = NULL;
int vnum = 0;
//if not already done, build the table for the sector asked
if ( !initDone) {
SECT_TABLE = (int **)calloc(SECT_MAX, sizeof(int *));
initDone = TRUE;
int tempSectRoom[SECT_MAX];
for (vnum = 0;vnum < SECT_MAX; vnum++) {
tempSectRoom[vnum] = SECT_NBROOMS[vnum];
SECT_TABLE[vnum] = SECT_NBROOMS[vnum] > 0 ? (int*)calloc(SECT_NBROOMS[vnum] , sizeof(int)) : NULL;
}
AREA_DATA *pArea;
for (pArea = area_first;pArea;pArea = pArea->next) {
for (vnum = pArea->lvnum;vnum <= pArea->uvnum;vnum ++) {
room = get_room_index(vnum);
if ( !room || room->sectorType == SECT_UNUSED || room->sectorType == SECT_UNUSED2
|| room->sectorType >= SECT_MAX)
continue;
tempSectRoom[room->sectorType] –;
SECT_TABLE[room->sectorType][tempSectRoom[room->sectorType]] = vnum;
}
}
}
vnum = 3;
room = NULL;
while (vnum > 0) {//so it never goes in infinite loop (should not but but it depends on areas loaded)
vnum –;
long roomIndex = number_range(0, SECT_NBROOMS[sector_type] - 1);
if (SECT_NBROOMS[sector_type] > 0 )
room = get_room_index(SECT_TABLE[sector_type][roomIndex]);
if ( !room || !room->desc || strlen(room->desc) == 0)
continue;
if (IS_SET(room->room_flags, ROOM_NO_RANDOM) || IS_SET(room->room_flags,ROOM_LAW))
continue;
break;
}
return room;
}
The thing is that it happens for values that were previously working fine, so obviously the memory has been overwritten between two calls.
So either I have a bug somewhere else, or I did not allocated memory like I should (I think it is the second solution (and hope it is as a matter of fact))