/* * Seven Kingdoms: Ancient Adversaries * * Copyright 1997,1998 Enlight Software Ltd. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ // Filename : OREGION.CPP // Description : Class RegionArray , info on regions #include #include #include #include //--------- Define static function ---------// static int sort_region_function( const void *a, const void *b ); // --------- Begin of function RegionArray::RegionArray -------// RegionArray::RegionArray() { init_flag = 0; } // --------- End of function RegionArray::RegionArray -------// // --------- Begin of function RegionArray::~RegionArray -------// RegionArray::~RegionArray() { deinit(); } // --------- End of function RegionArray::~RegionArray -------// // --------- Begin of function RegionArray::init -------// void RegionArray::init(int maxRegion) { deinit(); region_info_count = maxRegion; int connectBit; if( maxRegion > 0) { // --------- allocate memory for RegionInfo --------// region_info_array = (RegionInfo *)mem_add( sizeof(RegionInfo) * maxRegion); memset(region_info_array, 0, sizeof(RegionInfo) * maxRegion ); // ---- calculate the no. of bit required to store connection ----// connectBit = (maxRegion-1) * (maxRegion) /2; // region 1 needs 0 bit // region 2 needs 1 bit // region 3 needs 2 bits // region 4 needs 3 bits... } else { region_info_array = NULL; connectBit = 0; } if( connectBit > 0) { connect_bits = (unsigned char *) mem_add( (connectBit + 7) / 8 ); memset(connect_bits, 0, (connectBit + 7) /8 ); } else { connect_bits = NULL; } //------ initialize adj_offset_bit and area -------// int j = 0; for(int i=0 ; i region_info_count); region_info_array[reg-1].region_type = regType; } //--------- End of function RegionArray::set_region -------// //--------- Begin of function RegionArray::inc_size -------// void RegionArray::inc_size(int reg) { err_when( reg <= 0 || reg > region_info_count); region_info_array[reg-1].region_size++; } //--------- End of function RegionArray::inc_size -------// //--------- Begin of function RegionArray::set_adjacent -------// void RegionArray::set_adjacent(int reg1, int reg2) { err_when( reg1 < 0 || reg1 > region_info_count); err_when( reg2 < 0 || reg2 > region_info_count); if( reg1 == 0 || reg2 == 0) return; int bitOffset; if( reg1 == reg2 ) return; if( reg1 > reg2) { bitOffset = region_info_array[reg1 -1].adj_offset_bit + (reg2 -1); } else { bitOffset = region_info_array[reg2 -1].adj_offset_bit + (reg1 -1); } connect_bits[bitOffset / 8] |= 1 << (bitOffset % 8); } //--------- End of function RegionArray::set_adjacent -------// //--------- Begin of function RegionArray::is_adjacent -------// int RegionArray::is_adjacent(int reg1, int reg2) { err_when( reg1 <= 0 || reg1 > region_info_count); err_when( reg2 <= 0 || reg2 > region_info_count); int bitOffset; if( reg1 == reg2 ) return TRUE; if( reg1 > reg2 ) { bitOffset = region_info_array[reg1 -1].adj_offset_bit + (reg2 -1); } else { bitOffset = region_info_array[reg2 -1].adj_offset_bit + (reg1 -1); } return connect_bits[bitOffset / 8] & (1 << (bitOffset % 8)); } //--------- End of function RegionArray::is_adjacent -------// //--------- Begin of function RegionArray::sort_region -------// void RegionArray::sort_region() { //--- initialize the region_sorted_array first ----// for( int i=0 ; iregion_size - region_array[*((BYTE*)a)]->region_size; } //------- End of function sort_region_function ------// #ifdef DEBUG //--------- Begin of function RegionArray::get_sorted_region -------// RegionInfo* RegionArray::get_sorted_region(int recNo) { err_when( recNo<1 || recNo>region_info_count ); return operator[]( region_sorted_array[recNo-1] ); } //--------- End of function RegionArray::get_sorted_region -------// //--------- Begin of function RegionArray::get_region_stat -------// RegionStat* RegionArray::get_region_stat(int regionId) { err_when( regionId<1 || regionId>region_info_count ); int regionStatId = region_info_array[regionId-1].region_stat_id; err_when( regionStatId<1 || regionStatId>region_stat_count ); return region_stat_array+regionStatId-1; } //--------- End of function RegionArray::get_region_stat -------// //--------- Begin of function RegionArray::get_region_stat2 -------// RegionStat* RegionArray::get_region_stat2(int regionStatId) { err_when( regionStatId<1 || regionStatId>region_stat_count ); return region_stat_array+regionStatId-1; } //--------- End of function RegionArray::get_region_stat2 -------// // --------- Begin of function RegionArray::region_type -------// RegionType RegionArray::region_type(int region) { err_when(region <= 0 || region > region_info_count); return region_info_array[region-1].region_type; } // --------- End of function RegionArray::region_type -------// //--------- Begin of function RegionArray::operator[] -------// RegionInfo *RegionArray::operator[](int region) { err_when(region <= 0 || region > region_info_count); return region_info_array+region-1; } //--------- End of function RegionArray::operator[] -------// #endif