Example #1
0
void dfaShiftTransitionRelation(pTransitionRelation p_transitionRelation, int sink){
    assert(p_transitionRelation != NULL);
    int i, j;

    p_transitionRelation->num_of_nodes++;
    p_transitionRelation->degrees = mem_resize(p_transitionRelation->degrees, p_transitionRelation->num_of_nodes * sizeof(t_st_word));
    p_transitionRelation->adjList = mem_resize(p_transitionRelation->adjList, p_transitionRelation->num_of_nodes * sizeof(unsigned*));

    for (i = p_transitionRelation->num_of_nodes - 1; i >= 0; i--){
        if (i > sink) {
            p_transitionRelation->adjList[i] = p_transitionRelation->adjList[i - 1];
            p_transitionRelation->degrees[i] = p_transitionRelation->degrees[i - 1];
        }
        else if (i == sink){
            p_transitionRelation->adjList[i] = NULL;
            p_transitionRelation->degrees[i] = 0;
        }
        if (p_transitionRelation->degrees[i] == 0){
            continue;
        }
        for (j = 0; j < p_transitionRelation->degrees[i]; j++){
            p_transitionRelation->adjList[i][j] = (p_transitionRelation->adjList[i][j] < p_transitionRelation->sink) ? p_transitionRelation->adjList[i][j] : p_transitionRelation->adjList[i][j] + 1;
        }
    }

}
Example #2
0
//------------ Begin of function Tutor::get_intro -------------//
//
// <int> tutorId - id. of the tutorial
//
// return: <char*> return the introduction text of the tutorial.
//
char* Tutor::get_intro(int tutorId)
{
	//------- get the tutor msgs from the resource file -------//

	int   dataSize;
	File* filePtr = res_tutor_intro.get_file( tutor[tutorId]->code, dataSize);

	if( !filePtr )       // if error getting the tutor resource
	{
		err_here();
		return NULL;
	}

	//------ Open the file and allocate buffer -----//

	FileTxt fileTxt( filePtr, dataSize );  // initialize fileTxt with an existing file stream

	if( dataSize > tutor_intro_buf_size )
	{
		tutor_intro_buf      = mem_resize( tutor_intro_buf, dataSize );       // allocate a buffer larger than we need for the largest size possible
		tutor_intro_buf_size = dataSize;
	}

	// #### begin Gilbert 23/9 #######//
	fileTxt.read_paragraph(tutor_intro_buf, tutor_intro_buf_size);
	// #### end Gilbert 23/9 #######//

	return tutor_intro_buf;
}
Example #3
0
void addLengthToNodeLengths(P_NodeLengths nodeLengths, unsigned len){
    assert(!(nodeLengths->index > 0 && nodeLengths->lengths == NULL));
    //if first time visiting this node then init nodeLengths then add len
    if (nodeLengths->lengths == NULL){
        fflush(stdout);
        nodeLengths->index = 0;
        nodeLengths->size = 32;
        nodeLengths->lengths = (unsigned*) mem_alloc((size_t) nodeLengths->size * sizeof(unsigned));
        mem_zero(nodeLengths->lengths, (size_t) nodeLengths->size * sizeof(unsigned));
        nodeLengths->lengths[nodeLengths->index] = len;
        (nodeLengths->index)++;
    }
    else {
        //add new length
        if (isNewLength(nodeLengths, len)){
            nodeLengths->lengths[nodeLengths->index] = len;
            (nodeLengths->index)++;
            //need resize
            if (nodeLengths->index == nodeLengths->size){
                nodeLengths->size *= 2;
//                printf("new size = %u\n", nodeLengths->size);
                nodeLengths->lengths = (unsigned*) mem_resize(nodeLengths->lengths, (size_t) nodeLengths->size * sizeof(unsigned));
                mem_zero(nodeLengths->lengths + nodeLengths->index, (size_t) (nodeLengths->size - nodeLengths->index) * sizeof(unsigned));
            }
        }
    }
}
Example #4
0
//---------- Begin of function ResourceDb::read_imported ----------//
//
// If ResourceDb is initialized using init_imported(),
// then use read_imported to read the record
//
// <long> offset = offset to the data in the resource file
//
// Return : <char*> data pointer
//          NULL    if the record has not index to data
//
char* ResourceDb::read_imported(long offset)
{
	err_when(!init_flag);

	//-------- read from buffer ---------//
	// ##### begin Gilbert 4/10 #######//
	if( read_all )
	{
		err_when(offset < 0 || offset >= data_buf_size);
		return data_buf + offset + sizeof(int32_t);  // bypass the long parameters which is the size of the data
	}
	// ##### end Gilbert 4/10 #######//

	//---------- read from file ---------//

	// ##### begin Gilbert 2/10 ######//
	err_when(offset >= file_size());
	// ##### end Gilbert 2/10 ######//
	file_seek( offset );

	data_buf_size = file_get_long();

	err_when(use_common_buf && data_buf_size > COMMON_DATA_BUF_SIZE);

   if( !use_common_buf )
		data_buf = mem_resize( data_buf, data_buf_size );

	file_read( data_buf, data_buf_size );

   return data_buf;
}
Example #5
0
// ------ begin of function SerialRepository::load --------//
//
int SerialRepository::load()
{
	if( !is_persistant() )
		return 0;

	if( !m.is_file_exist(file_name) )
		return 0;

	int rc;

	File file;
	file.file_open( file_name );

	last_serial_id = file.file_get_long();
	max_history = file.file_get_long();
	history_array = (long *)mem_resize( history_array, sizeof(history_array[0]) * max_history );

	cur_history_size = file.file_get_long();
	(void) file.file_get_long();		// dummy
	(void) file.file_get_long();
	(void) file.file_get_long();
	(void) file.file_get_long();
	(void) file.file_get_long();

	rc = file.file_read( history_array, sizeof(history_array[0]) * cur_history_size );

	file.file_close();
	return rc;
}
Example #6
0
//---------- Begin of function ResourceDb::read ----------//
//
// Read in data from the resource file and store in an the buffer of this class
//
// The index field of the current record in the database object is
// used to locate the data in the resource file.
//
// Syntax : read(int recNo)
//
// [int] recNo = the record no. in the database.
//               (default : current record no.)
//
// Return : <char*> data pointer
//          NULL    if the record has not index to data
//
char* ResourceDb::read(int recNo)
{
   err_when( !init_flag || !db_obj );

   long* indexFieldPtr;
	char* recPtr;

   if( (recPtr = db_obj->read(recNo)) == NULL )
      return NULL;

   indexFieldPtr = (long*) (recPtr+index_field_offset);

   if( memcmp( indexFieldPtr, "    ", 4 ) == 0 )
      return NULL;      // no sample screen for this file

   file_seek( *indexFieldPtr );
	data_buf_size = file_get_long();

	err_when( use_common_buf && data_buf_size > COMMON_DATA_BUF_SIZE );

   if( !use_common_buf )
		data_buf = mem_resize( data_buf, data_buf_size );

	file_read( data_buf, data_buf_size );

   return data_buf;
}
Example #7
0
// ----- begin of function WorldScanTraverser::append -------//
//
void WorldScanTraverser::append(short unitRecno)
{
	if( scan_count >= alloc_count )
	{
		// find new alloc_count

		alloc_count = alloc_count + DEFAULT_ARRAY_SIZE;
		err_when( alloc_count <= scan_count);	// still now enough

		if( unit_recno_array == default_recno_array )
		{
			// default array is not enough, use own array

			unit_recno_array = (ScannedObj *)mem_add( alloc_count*sizeof(*unit_recno_array) );
			memcpy( unit_recno_array, default_recno_array, scan_count*sizeof(*unit_recno_array) );
		}
		else
		{
			unit_recno_array = (ScannedObj *)mem_resize( unit_recno_array, alloc_count*sizeof(*unit_recno_array) );
		}
	}

	unit_recno_array[scan_count].unit_recno = unitRecno;
	++scan_count;
}
Example #8
0
void tableInsert(Table *t, BddNode *elm)
{
  if (t->allocated == t->noelems) {
    t->allocated = t->allocated * 2 + 5;
    t->elms = (BddNode *) mem_resize(t->elms, sizeof(BddNode)*t->allocated); 
  }
  t->elms[t->noelems++] = *elm;
}
Example #9
0
// -------- begin of function VLenQueue::expand ----------//
void VLenQueue::expand( int newSize) {
    if( newSize > queued_size ) {
        char *oldBuf = queue_buf;
        queue_buf_size = newSize + (QUEUE_SIZE_INC - newSize % QUEUE_SIZE_INC);
        queue_buf = mem_resize( queue_buf, queue_buf_size);
        queue_ptr = queue_buf + (queue_ptr - oldBuf);
    }
}
Example #10
0
void Game::set_load_game_in_main_menu( char *fileName )
{
	int l = strlen( fileName );
	if( l < MAX_PATH )
		l = MAX_PATH;

	auto_load_file_name = mem_resize( auto_load_file_name, l+1);
	strcpy( auto_load_file_name, fileName );
}
Example #11
0
void paInsert(PairArray *q, State i, State j) 
{ 
  if (q->used == q->allocated) { 
    q->allocated = q->used*2+2; 
    q->m = (State *) mem_resize(q->m, sizeof(State)*q->allocated*2); 
  } 
  q->m[q->used*2+0] = i; 
  q->m[q->used*2+1] = j; 
  q->used++; 
} 
Example #12
0
//--------- BEGIN OF FUNCTION DynArray::zap ---------//
//
// Zap the whole dynamic array, clear all elements
//
// [int] resizeFlag - whether resize the array to its initial size
//							 or keep its current size.
//							 (default:1)
//
void DynArray::zap(int resizeFlag) {
    if( resizeFlag ) {
	if( ele_num != block_num ) {                  // if the current record no. is already block_num, no resizing needed
	    ele_num  = block_num;
	    body_buf = mem_resize(body_buf, ele_size*ele_num );
	}
    }

    cur_pos=0;
    last_ele=0;
}
Example #13
0
//---------- Begin of function ResourceIdx::get_data ----------//
//!
//! Read in data from the resource file and store in an the buffer of this class
//!
//! The index field of the current record in the database object is
//! used to locate the data in the resource file.
//!
//! <int> indexId = index of the data pointing to the resource file
//!
//! Return : <char*> data pointer
//!
char* ResourceIdx::get_data(int indexId) {
    err_when( !init_flag );

    unsigned dataSize;

    err_when( indexId < 1 || indexId > rec_count );

    indexId--;                                      // make it start from 0, instead of starting from 1 for easy array accessing

    //------ all data pre-loaded to memory ------//

    if( read_all )
	return data_buf + index_buf[indexId].pointer - index_buf[0].pointer;

    //------ all data NOT pre-loaded to memory -------//

    if( indexId == cur_rec_no && !use_common_buf )
	return data_buf;

    dataSize = index_buf[indexId+1].pointer - index_buf[indexId].pointer;

    file_seek( index_buf[indexId].pointer );

    //--- if the user has custom assigned a buffer, read into that buffer ---//

    if( user_data_buf ) {
	if( user_start_read_pos > 0 ) {
	    file_seek(user_start_read_pos,FILE_CURRENT);// skip the width and height info
	    dataSize -= user_start_read_pos;
	}

	if( dataSize > user_data_buf_size )
	    return NULL;

	file_read( user_data_buf, dataSize );

	return user_data_buf;
    }
    else {
	err_when( use_common_buf && dataSize > COMMON_DATA_BUF_SIZE );

	if( !use_common_buf && data_buf_size < dataSize )
	    data_buf = mem_resize( data_buf, dataSize );

	data_buf_size = dataSize;

	file_read( data_buf, dataSize );

	return data_buf;
    }
}
Example #14
0
void double_table_sequential(bdd_manager *bddm) {
  bddm->table_total_size += bddm->table_size;
  bddm->table_size += bddm->table_size;
  
/*  printf("Doubling table to: Size %u Total %u\n", bddm->table_size, bddm->table_total_size); */

  bddm->node_table = mem_resize(bddm->node_table,
				(size_t)(sizeof (bdd_record)) * 
				(bddm->table_total_size));
  bddm->table_log_size++;
#ifdef _BDD_STAT_
  bddm->number_double++;
#endif
}
Example #15
0
//--------- BEGIN OF FUNCTION DynArray::resize ---------//
//
// change the size of the storage block - this will always be an increase
//
void DynArray::resize( int newNum ) {
    //-------------------------------------------------------//
    //
    // The Mem::resize() and realloc() may not function properly in
    // some case when the memory block has a considerable size.
    //
    // Calling function resize_keep_data will do additional effort
    // to preserve the original data.
    //
    //-------------------------------------------------------//

    // give both the original data size and the new data size
    body_buf = mem_resize( body_buf, newNum*ele_size );

    ele_num = newNum;
}
Example #16
0
//-------- Begin of function SeekPathReuse::add_result ---------//
// add the result node in the reuse result node array and resize
// the array size if default size is not large enough to hold the
// data
// 
void SeekPathReuse::add_result(int x, int y)
{
	debug_reuse_check_sub_mode_node(x, y);

	if(num_of_result_node>=result_node_array_def_size)	// the array is not enough to hold the data
	{
		result_node_array_def_size += result_node_array_reset_amount;
		path_reuse_result_node_ptr = (ResultNode*) mem_resize(path_reuse_result_node_ptr, sizeof(ResultNode)* result_node_array_def_size);
		cur_result_node_ptr = path_reuse_result_node_ptr+num_of_result_node;
	}

	cur_result_node_ptr->node_x = x;
	cur_result_node_ptr->node_y = y;
	cur_result_node_ptr++;
	num_of_result_node++;
}
Example #17
0
void test_mem_resize()
{
    fprintf( stderr, "   Testing mem_resize ... " );

    char *pt0 = "foo";
    char *pt1 = NULL;

    pt1 = mem_get( 3 + 1 );

    memcpy( pt1, pt0, 3 );

    pt1 = mem_resize( pt1, 1 );

    mem_free( ( void * ) &pt1 );

    fprintf( stderr, "OK\n" );
}
Example #18
0
//---------- Begin of function DynArray::read_file -------------//
//!
//! Read a saved dynamic array from file, it must be saved with write_file()
//!
//! <File*> readFile = the pointer to the writing file
//!
//! Return : 1 - read successfully
//!          0 - writing error
//!
int DynArray::read_file(File* filePtr) {
    char* bodyBuf = body_buf;                       // preserve body_buf which has been allocated

    if( !filePtr->file_read( this, sizeof(DynArray) ) )
	return 0;

    body_buf = mem_resize( bodyBuf, ele_num*ele_size );

    if( last_ele > 0 ) {
	if( !filePtr->file_read( body_buf, ele_size*last_ele ) )
	    return 0;
    }

    start();                                        // go top

    return 1;
}
Example #19
0
void *mem_resize_zero( void *pt, size_t old_size, size_t new_size )
{
    /* Martin A. Hansen, May 2008 */

    /* Unit test done.*/

    /* Resize an allocated chunk of memory for a given pointer and zero any extra memory. */
    
    void *pt_new;

    pt_new = mem_resize( pt, new_size );

    if ( new_size > old_size ) {
        bzero( ( ( void * ) pt_new ) + old_size, new_size - old_size );
    }

    return pt_new;
}
Example #20
0
unsigned dfaGetDegree(DFA *M, unsigned state){
    int ssink = find_sink(M);
    unsigned sink;
    if (ssink == -1)
        sink = UINT_MAX;
    else
        sink = ssink;
    assert(state != sink);
    
    paths state_paths, pp;
    unsigned nextStatesSize = 16, nextStatesIndex = 0, degree = 0;
    unsigned *nextStates = (unsigned*) malloc((size_t) (nextStatesSize) * sizeof(unsigned));
    mem_zero(nextStates, (size_t) (nextStatesSize) * sizeof(unsigned));
    /*******************  find node degree *********************/
    state_paths = pp = make_paths(M->bddm, M->q[state]);
    while (pp) {
        if (pp->to != sink){
            bool found = false;
            for (nextStatesIndex = 0; nextStatesIndex < degree; nextStatesIndex++) {
                if (pp->to == nextStates[nextStatesIndex]){
                    found = true;
                    break;
                }
            }
            if (!found){
                if (degree < nextStatesSize){
                    nextStates[degree] = pp->to;
                }
                else {
                    unsigned oldSize = nextStatesSize;
                    nextStatesSize *= 2;
                    nextStates = (unsigned*) mem_resize(nextStates, (size_t)(nextStatesSize) * sizeof(unsigned));
                    mem_zero(nextStates + oldSize, (size_t) (nextStatesSize - oldSize) * sizeof(unsigned));
                    nextStates[degree] = pp->to;
                }
                degree++;
            }
        }
        pp = pp->next;
    }
    kill_paths(state_paths);
    free(nextStates);
    return degree;
}
Example #21
0
File: ores.cpp Project: 112212/7k2
//---------- Begin of function Resource::read ----------//
//
// Read in data from the resource file and store in an the buffer of this class
//
// The index field of the current record in the database object is
// used to locate the data in the resource file.
//
// Syntax : read(int recNo)
//
// [int] recNo = the record no of the data going to read
//               (default : current record no.)
//
// Return : <char*> data pointer
//          NULL    if the record has not index to data
//
char* Resource::read(int recNo)
{
   err_when( !init_flag );

   unsigned dataSize;

   if( recNo < 1 )
      recNo = cur_rec_no;

   err_when( recNo < 1 || recNo > rec_count );

   if( recNo < 1 || recNo > rec_count )	   // when no in debug mode, err_when() will be removed
      return NULL;

   //------ all data pre-loaded to memory ------//

   if( read_all )
      return data_buf + index_buf[recNo-1] - index_buf[0];

   //------ all data NOT pre-loaded to memory -------//

   if( recNo == cur_rec_no && !use_common_buf )
      return data_buf;

   dataSize = index_buf[recNo] - index_buf[recNo-1];

   err_when( use_common_buf && dataSize > COMMON_DATA_BUF_SIZE );

   if( !use_common_buf && data_buf_size < dataSize )
      data_buf = mem_resize( data_buf, dataSize );

   data_buf_size = dataSize;

   //------------ read data ------------//

   file_seek( index_buf[recNo-1] );

   file_read( data_buf, dataSize );

   return data_buf;
}
Example #22
0
void successors(bdd_manager *bddm, bdd_ptr p)
{
  if (bdd_is_leaf(bddm, p)) { 
    int i;
    int s = bdd_leaf_value(bddm, p); /* current_state is a predecessor of s */

    for (i = 0; i < predused[s]; i++) /* already there? */
      if (preds[s][i] == current_state)
	return;

    if (predalloc[s] == predused[s]) { /* need to reallocate? */
      predalloc[s] = predalloc[s]*2+8;
      preds[s] = (int *) mem_resize(preds[s], sizeof(int) * predalloc[s]);
    }

    preds[s][predused[s]++] = current_state;
  }
  else {
    successors(bddm, bdd_else(bddm, p));
    successors(bddm, bdd_then(bddm, p));
  }
  
}
Example #23
0
void getLengthsHelper(DFA *M, pTransitionRelation p_transitionRelation, unsigned currentNode, P_NodeLengths * nodesLengths){
//    printf("%u, ", (currentNode < p_transitionRelation->sink)?currentNode : currentNode + 1);

    //if current state is accept state then register length before moving on with DFS
    if (findStateBS(p_transitionRelation->accepts, currentNode, 0, p_transitionRelation->acceptsSize - 1)){
        addLengthToNodeLengths(nodesLengths[currentNode], 0);
    }
    
    unsigned succNodeIndex;
    // if current node has out edges
    if (p_transitionRelation->degrees[currentNode] > 0){
        for (succNodeIndex = 0; succNodeIndex < p_transitionRelation->degrees[currentNode]; succNodeIndex++){
            unsigned succNode = p_transitionRelation->adjList[currentNode][succNodeIndex];
            //first time to see next node --> carry on DFS
            if (nodesLengths[succNode]->visited == false){
                nodesLengths[succNode]->visited = true;
                getLengthsHelper(M, p_transitionRelation, succNode, nodesLengths);
            }
            int i;
            assert(nodesLengths[succNode] != NULL);
            for (i = 0; i < nodesLengths[succNode]->index; i++ ){
                addLengthToNodeLengths(nodesLengths[currentNode], nodesLengths[succNode]->lengths[i] + 1);
            }
            //avoid overflow
            if(nodesLengths[succNode]->numOfPaths < (ULONG_MAX - nodesLengths[currentNode]->numOfPaths))
                nodesLengths[currentNode]->numOfPaths += nodesLengths[succNode]->numOfPaths;
            else
                nodesLengths[currentNode]->numOfPaths = ULONG_MAX;
        }
        if (nodesLengths[currentNode]->index > 0){
            nodesLengths[currentNode]->lengths = (unsigned*) mem_resize(nodesLengths[currentNode]->lengths, (size_t) nodesLengths[currentNode]->index * sizeof(unsigned));
            nodesLengths[currentNode]->size = nodesLengths[currentNode]->index;
        }
    }
    else
        nodesLengths[currentNode]->numOfPaths = 1;
}
Example #24
0
void resetpchard()
{
    if (!modelchanged)
        device_close_all();
    else
        modelchanged = 0;
    device_init();

    midi_close();
    midi_init();

    timer_reset();
    sound_reset();
    mem_resize();

    if (pcfirsttime)
    {
        fdc_init();
        pcfirsttime = 0;
    }
    else
        fdc_hard_reset();

    model_init();
    video_init();
    speaker_init();

// #ifdef USE_NETWORKING
    vlan_reset();	//NETWORK
    network_card_init(network_card_current);
// #endif

    sound_card_init(sound_card_current);
    if (GUS)
        device_add(&gus_device);
    if (GAMEBLASTER)
        device_add(&cms_device);
    if (SSI2001)
        device_add(&ssi2001_device);
    if (voodoo_enabled)
        device_add(&voodoo_device);
    pc_reset();

    resetide();

    loadnvr();

//        cpuspeed2 = (AT)?2:1;
//        atfullspeed = 0;
//        setpitclock(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed);

    shadowbios = 0;
    ali1429_reset();

    keyboard_at_reset();

//        output=3;

#if __unix
    if (cdrom_drive == -1)
        cdrom_null_reset();
    else
#endif
        ioctl_reset();
}
Example #25
0
//-------- Begin of function World::generate_map ----------//
//
void World::generate_map(int loadGame)
{
	if( !loadGame )
		game.disp_gen_game_status(0);

	//--- loc_matrix, first store terrain height, then world map icon id ---//

	loc_matrix = (Location*) mem_resize( loc_matrix, MAX_WORLD_X_LOC * MAX_WORLD_Y_LOC * sizeof(Location) );
	corner_matrix = (Corner *) mem_resize( corner_matrix, (MAX_WORLD_X_LOC+1) * (MAX_WORLD_Y_LOC+1) * sizeof(Corner));

	max_x_loc = MAX_WORLD_X_LOC;
	max_y_loc = MAX_WORLD_Y_LOC;

	// -------- save random seed ----------//

	long backupRandomSeed;
	if( !loadGame )
	{
		gen_map_random_seed = m.get_random_seed();
	}
	else
	{
		backupRandomSeed = m.get_random_seed();
		m.set_random_seed( gen_map_random_seed );
	}

	// ---------- generate plasma map ----------//

	Plasma heightMap, terrainMap;
	memset( loc_matrix , 0, sizeof(Location) * MAX_WORLD_X_LOC * MAX_WORLD_Y_LOC );
	heightMap.init(max_x_loc, max_y_loc);

//	if( !sys.testing_session )
		heightMap.generate( m.random(2), 5, m.rand() );

	terrainMap = heightMap;

	if( !loadGame )
		game.disp_gen_game_status(10);

	//--------- remove odd terrain --------//

	for(short y = 0; y <= terrainMap.max_y; ++y)
		for(short x = 0; x <= terrainMap.max_x; ++x)
	{
		remove_odd(terrainMap, x, y, 5);
	}

	if( !loadGame )
		game.disp_gen_game_status(5);

	//--------- shuffle sub-terrain level ---------//

	set_tera_id(terrainMap);

	if( !loadGame )
		game.disp_gen_game_status(5);

	//---------------------------------//

	smooth_altitude(heightMap);

	if( !loadGame )
		game.disp_gen_game_status(5);

	//---------------------------------//

	set_tera_height(heightMap);

	if( !loadGame )
		game.disp_gen_game_status(5);

	//---------------------------------//

	terrainMap.deinit();		// free memory early
	heightMap.deinit();		// free memory early

	substitute_pattern();

	if( !loadGame )
		game.disp_gen_game_status(5);

	//---------------------------------//

	if( !loadGame )
	{
		set_loc_flags();
		assign_map();
		set_region_id();

		//--- if campaign's mountain layout is enabled, we leave it to the campaign functions to generate the mountains ---//

		if( !game.is_campaign_mode() )
			gen_mountains();

		else if( !game.campaign()->mountain_layout_enabled )
			world.gen_mountains(0, 50);		// 50% the amount of mountains in a campaign game is 50% less than a normal game.

		game.disp_gen_game_status(5);
	}

	// ---------- restore random seed ---------//

	if( loadGame )
	{
		m.set_random_seed( backupRandomSeed );
	}

}
Example #26
0
int ssLookupAndInsert(Subsets *s, unsigned c1, unsigned c2, unsigned *n)
{
    unsigned t, len = 0, i1 = 0, i2 = 0;
    unsigned *unionSet;
    SubsetsEntry *e, *ee, *eee;
    SubsetsEntry *e1, *e2;

    if (c1 == c2) {
        /* same set, trivial union */
        *n = c1;
        return 1;
    }

    /* ensure that c1<c2 */
    if (c2 < c1) {
        t = c1;
        c1 = c2;
        c2 = t;
    }

    /* MAKE UNION SET */

    if (c2 < s->singletons) {
        /* both c1 and c2 are singletons */
        unionSet = (unsigned *) mem_alloc(sizeof(unsigned)*2);
        unionSet[len++] = c1;
        unionSet[len++] = c2;
    }
    else if (c1 >= s->singletons) {
        /* both c1 and c2 are non-singletons, merge the element arrays */
        e1 = s->inverse[c1 - s->singletons];
        e2 = s->inverse[c2 - s->singletons];
        unionSet =
            (unsigned *) mem_alloc(sizeof(unsigned)*(e1->length + e2->length));

        while (i1 < e1->length && i2 < e2->length)
            if (e1->elements[i1] < e2->elements[i2])
                unionSet[len++] = e1->elements[i1++];
            else if (e1->elements[i1] >= e2->elements[i2]) {
                unionSet[len++] = e2->elements[i2++];
                if (e1->elements[i1] == e2->elements[i2-1])
                    i1++;
            }
        while (i1 < e1->length)
            unionSet[len++] = e1->elements[i1++];
        while (i2 < e2->length)
            unionSet[len++] = e2->elements[i2++];
    }
    else {
        /* c1 is singleton, c2 is non-singleton */
        e2 = s->inverse[c2 - s->singletons];
        unionSet = (unsigned *) mem_alloc(sizeof(unsigned)*(e2->length+1));

        while (i2 < e2->length && c1 > e2->elements[i2])
            unionSet[len++] = e2->elements[i2++];
        if (i2 == e2->length || c1 != e2->elements[i2])
            unionSet[len++] = c1;
        while (i2 < e2->length)
            unionSet[len++] = e2->elements[i2++];
    }

    /* SEARCH FOR THE UNION SET */

    e = ee = &s->t[ssHash(unionSet, len, s->size)];
    do {
        if (e->length == len &&
                memcmp(e->elements, unionSet, sizeof(unsigned)*len) == 0) {

            /* found it */
            *n = e->n;
            mem_free(unionSet);
            return 1;
        }
        eee = e;
        e = e->overflow;
    } while (e);

    /* NOT FOUND, INSERT IT */

    if (ee->length != 0) { /* main entry occupied? */
        ee = (SubsetsEntry *) mem_alloc(sizeof(SubsetsEntry));
        eee->overflow = ee;
        s->overflows++;
    } /* ee is now the new entry */

    /* insert into inverse table */
    if (s->num == s->inverseAllocated) {
        s->inverseAllocated = s->inverseAllocated*2 + 5;
        s->inverse = (SubsetsEntry **)
                     mem_resize(s->inverse, sizeof(SubsetsEntry *)*s->inverseAllocated);
    }
    s->inverse[s->num] = ee;

    /* assign it a number (successively) */
    *n =  s->singletons + s->num++;

    /* fill entry */
    ee->elements = unionSet;
    ee->length = len;
    ee->n = *n;
    ee->c1 = c1;
    ee->c2 = c2;
    ee->overflow = 0;

    if (s->overflows > s->size*2) {

        /* TOO MANY OVERFLOWS, REHASH */
        unsigned newsize = primes[++(s->prime)];
        SubsetsEntry *r =
            (SubsetsEntry *) mem_alloc(sizeof(SubsetsEntry)*newsize);
        int i;
        s->overflows = 0;

        /* clear new array */
        for (i = 0; i < newsize; i++) {
            r[i].length = 0;
            r[i].overflow = 0;
        }
        /* rehash */
        for (i = 0; i < s->size; i++) {
            SubsetsEntry *w = &s->t[i], *ww;
            if (w->length != 0)
                while (w) {
                    SubsetsEntry *d = &r[ssHash(w->elements, w->length, newsize)];

                    /* find back of list */
                    if (d->length != 0) {
                        while (d->overflow)
                            d = d->overflow;
                        d->overflow =
                            (SubsetsEntry *) mem_alloc(sizeof(SubsetsEntry));
                        d = d->overflow;
                        s->overflows++;
                    }
                    d->elements = w->elements;
                    d->length = w->length;
                    d->n = w->n;
                    d->c1 = w->c1;
                    d->c2 = w->c2;
                    d->overflow = 0;
                    s->inverse[d->n - s->singletons] = d;
                    ww = w;
                    w = w->overflow;
                    if (ww != &s->t[i])
                        mem_free(ww);
                }
        }
        mem_free(s->t);
        s->t = r;
        s->size = newsize;
    }

    return 0;
}
Example #27
0
void Blob2D::resize(short destLeft, short destTop, short destWidth, short destHeight)
{
	if( width == 0 && height == 0)
	{
		size_t siz = Bitmap::size(destWidth,destHeight);
		if( alloc_size < siz )
		{
			if(ptr)
				mem_del(ptr);
			ptr = (Bitmap *)mem_add((alloc_size = siz + DEFAULT_BLOB2D_INC));
		}
		memset(ptr, BACKGROUND_COLOR, siz);
		ptr->init(destWidth, destHeight);
	}
	else if( destWidth == 0 && destHeight == 0 )
	{
		err_when(!ptr);
		ptr->init(destWidth, destHeight);
	}
	else if( left_edge == destLeft && top_edge == destTop && width == destWidth )
	{
		if( destHeight == height )
			return;		// unchange

		size_t siz = Bitmap::size(destWidth,destHeight);
		if( alloc_size < siz )
		{
			ptr = (Bitmap *)mem_resize(ptr, (alloc_size = siz + DEFAULT_BLOB2D_INC));
		}

		if( destHeight > height && destWidth > 0 )
		{
			int y2 = top_edge+height;	// must keep the old instant
			height = destHeight;			// as height must be change to make fill_area correct

			// extend, then fill the new with the background color
			fill_area(destLeft, top_edge+height, destLeft+destWidth-1, destTop+destHeight-1, BACKGROUND_COLOR, 0);
		}
	}
	else if( left_edge <= destLeft && top_edge <= destTop &&
		left_edge+width >= destLeft+destWidth && top_edge+height >= destTop+destHeight)
	{
		// clipping
		unsigned char *src = ptr->get_ptr(destLeft-left_edge, destTop-top_edge);
		int srcPitch = ptr->get_pitch();
		ptr->init(destWidth, destHeight);
		unsigned char *dest = ptr->get_ptr();
		int destPitch = ptr->get_pitch();

		for(int y = 0; y < destHeight; ++y, src += srcPitch, dest += destPitch )
			memmove(dest, src, destWidth);

		// ptr = (Bitmap *)mem_resize(ptr, ptr->size());
	}
	else
	{
		// general resize, new another buffer
		// copy range, intersection of two area :
		short copyLeft, copyTop, copyWidth, copyHeight;
		copyLeft = max(destLeft, left_edge);
		copyTop = max(destTop, top_edge);
		copyWidth = min(destLeft + destWidth, left_edge + width) - copyLeft;
		copyHeight = min(destTop + destHeight, top_edge + height) - copyTop;

		{
			size_t siz = Bitmap::size(destWidth, destHeight);
			Bitmap *newPtr = (Bitmap *)mem_add(siz + DEFAULT_BLOB2D_INC);
			memset(newPtr, BACKGROUND_COLOR, siz);
			newPtr->init(destWidth, destHeight);

			if( copyWidth > 0 && copyHeight > 0 )
			{
				int yCount = 0;
				unsigned char *src = ptr->get_ptr(copyLeft-left_edge, yCount+copyTop-top_edge );
				unsigned char *dest = newPtr->get_ptr(copyLeft-destLeft, yCount+copyTop-destTop );

				for( ; yCount < copyHeight; ++yCount, src += ptr->get_pitch(), dest += ptr->get_pitch() )
				{
					// unsigned char *src = (yCount+copyTop-top_edge)*width + copyLeft-left_edge;
					// unsigned char *dest = (yCount+copyTop-destTop)*destWdith + copyLeft-destLeft;
					memcpy(dest, src, copyWidth);
				}
			}

			// assign to the newPtr now
			left_edge = destLeft;
			top_edge = destTop;
			width = destWidth;
			height = destHeight;
			if(ptr)
				mem_del(ptr);
			ptr = newPtr;
		}

		// fill rest area with background color
		if( top_edge < copyTop && width > 0)
		{
			fill_area(left_edge, top_edge, left_edge+width-1, copyTop, BACKGROUND_COLOR, 0 );
		}

		// fill bottom
		if( top_edge+height > copyTop+copyHeight && width > 0)
		{
			fill_area(left_edge, copyTop+copyHeight, left_edge+width-1, top_edge+height-1, BACKGROUND_COLOR, 0 );
		}

		// fill left
		if( left_edge < copyLeft && destHeight > 0)
		{
			fill_area(left_edge, copyTop, copyLeft-1, copyTop+copyHeight-1,
				BACKGROUND_COLOR, 0);
		}

		// fill right
		if( left_edge+width > copyLeft+copyWidth && destHeight > 0 )
		{
			fill_area(copyLeft+copyWidth, copyTop, left_edge+width, copyTop+copyHeight-1,
				BACKGROUND_COLOR, 0);
		}
	}

	left_edge = destLeft;
	top_edge = destTop;
	width = destWidth;
	height = destHeight;
}
Example #28
0
void Blob2D::auto_clip( short *pLeft, short *pTop, short *pWidth, short *pHeight, int autoResize)
{
	short x1, y1, x2, y2;
	int pitch = ptr->get_pitch();

	y1 = 0;
	y2 = height - 1;
	x1 = 0;
	x2 = width - 1;

	// clip bottom
	for( ; y1 <= y2; --y2)
	{
		int x = x1;
		unsigned char *src = ptr->get_ptr(x, y2);
		for( ; x <= x2; ++x, ++src )
		{
			if( *src != BACKGROUND_COLOR )
				break;		// dirty
		}
		if( x <= x2 )
			break;			// dirty
	}	// y2 is now the bottom margin

	// clip top
	for( ; y1 <= y2; ++y1 )
	{
		int x = x1;
		unsigned char *src = ptr->get_ptr(x, y1);
		for( ; x <= x2; ++x, ++src )
		{
			if( *src != BACKGROUND_COLOR )
				break;		// dirty
		}
		if( x <= x2 )
			break;			// dirty
	}	// y1 is now the top margin

	// clip right
	for( ; x1 <= x2; --x2)
	{
		int y = y1;
		unsigned char *src = ptr->get_ptr(x1, y);
		for( ; y <= y2; ++y, src+=pitch )
		{
			if( *src != BACKGROUND_COLOR )
				break;		// dirty
		}
		if( y <= y2 )
			break;			// dirty
	}	// x2 is now the right margin

	// clip left
	for( ; x1 <= x2; ++x1 )
	{
		int y = y1;
		unsigned char *src = ptr->get_ptr(x1, y);
		for( ; y <= y2; ++y, src+=pitch )
		{
			if( *src != BACKGROUND_COLOR )
				break;		// dirty
		}
		if( y <= y2 )
			break;			// dirty
	}	// x1 is now the left margin

	if( pLeft )
		*pLeft = x1 + left_edge;
	if( pTop )
		*pTop = y1 + top_edge;
	if( pWidth )
		*pWidth = x2 - x1 + 1;
	if( pHeight )
		*pHeight = y2 - y1 + 1;

	if( autoResize )
	{
		resize(x1+left_edge, y1+top_edge, x2-x1+1, y2-y1+1);
		ptr = (Bitmap *)mem_resize(ptr, (alloc_size = ptr->size()) );
	}
}
Example #29
0
// --------- begin of function SERes::build_index -------//
// build index on (subject_type, subject_id)
void SERes::build_index()
{
	// ---------- first pass, count the size of index ---------//

	int i,j,k;
	SEInfo *seInfo;
	char lastType = -1;
	short lastId;

	type_index_count = 0;
	se_index_count = 0;

	for(i = 0, seInfo = se_array; i < se_array_count; ++i, ++seInfo)
	{
		if( lastType != seInfo->subject_type)
		{
			type_index_count++;
			se_index_count++;
			lastType = seInfo->subject_type;
			lastId = seInfo->subject_id;
		}
		else if( lastId != seInfo->subject_id)
		{
			se_index_count++;
			lastId = seInfo->subject_id;
		}
	}

	// --------- allocate memory for index ----------//

	SEInfoIndex *seIndex = se_index_array = (SEInfoIndex *)
		mem_resize( se_index_array, sizeof(SEInfoIndex) * se_index_count);
	memset( se_index_array, 0, sizeof(SEInfoIndex) * se_index_count);

	SETypeIndex *typeIndex = type_index_array = (SETypeIndex *)
		mem_resize( type_index_array, sizeof(SETypeIndex) * type_index_count);
	memset( type_index_array, 0, sizeof(SETypeIndex) * type_index_count);

	// ---------- pass 2, build indices -----------//
	seIndex--;					// move one step backward
	typeIndex--;
	j = -1;
	k = -1;
	lastType = -1;
	for(i = 0, seInfo = se_array; i < se_array_count; ++i, ++seInfo)
	{
		if( lastType != seInfo->subject_type)
		{
			// ----------- new (type,Id) ---------//
			++seIndex;
			++j;
			seIndex->subject_type = seInfo->subject_type;
			seIndex->subject_id = seInfo->subject_id;
			seIndex->start_rec = seIndex->end_rec = i;

			// ----------- new type ---------//
			++typeIndex;
			++k;
			typeIndex->subject_type = seInfo->subject_type;
			typeIndex->start_rec = typeIndex->end_rec = j;

			lastType = seInfo->subject_type;
			lastId = seInfo->subject_id;
		}
		else
		{
			err_when(typeIndex < type_index_array);	// must not enter here for the first time

			if( lastId != seInfo->subject_id)
			{
				// ----------- new (type,Id) ---------//
				++seIndex;
				++j;
				seIndex->subject_type = seInfo->subject_type;
				seIndex->subject_id = seInfo->subject_id;
				seIndex->start_rec = i;
				seIndex->end_rec = i;

				lastId = seInfo->subject_id;
			}
			else
			{
				err_when(seIndex < se_index_array);
				seIndex->end_rec = i;
			}
			typeIndex->end_rec = j;
		}
	}
}
Example #30
0
File: ohelp.cpp Project: mecirt/7k2
//------- Begin of function Help::load ----------//
//
// <char*> helpFileName = name of the help file.
//
void Help::load(char* helpFileName)
{
	//------ Open the file and allocate buffer -----//

	FileTxt fileTxt( helpFileName );

	int dataSize = fileTxt.file_size();

	if( dataSize > help_text_buf_size )
	{
		help_text_buf      = mem_resize( help_text_buf, dataSize );       // allocate a buffer larger than we need for the largest size possible
		help_text_buf_size = dataSize;
	}

	//-------- read in help info one by one --------//

	HelpInfo* iptr    = help_info_array;
	char*     textPtr = help_text_buf;
	int       readLen, totalReadLen=0;    // total length of text read into the help_text_buf
	int		 loopCount=0;
	char*		 tokenStr;
	
	help_info_count=0;

	first_help_by_help_code = first_help_by_area = -1;
	last_help_by_help_code = last_help_by_area = 0;

	while( !fileTxt.is_eof() )
	{
		err_when( loopCount++ > 10000 );

		tokenStr = fileTxt.get_token(0);		// don't advance the pointer

		if( !tokenStr )
			break;

		// ##### begin Gilbert 9/9 ######//
		iptr->help_code[0] = NULL;		// to identitfy search help text by help_code or coordinate
		// ##### end Gilbert 9/9 ######//

		//--------- if it's a help code ----------//

		if( tokenStr[0] >= 'A' && tokenStr[0] <= 'Z' )
		{
			strncpy( iptr->help_code, tokenStr, iptr->HELP_CODE_LEN );
			iptr->help_code[iptr->HELP_CODE_LEN] = NULL;

			// ###### begin Gilbert 9/9 #######//
			if( first_help_by_help_code == -1)
				first_help_by_help_code = help_info_count;
			last_help_by_help_code = help_info_count+1;		// record one after last
			// ###### end Gilbert 9/9 #######//
		}

		//------ if it's a help area position ------//

		else if( tokenStr[0] >= '0' && tokenStr[0] <= '9' )
		{
			iptr->area_x1 = (short) fileTxt.get_num();
			iptr->area_y1 = (short) fileTxt.get_num();
			iptr->area_x2 = (short) fileTxt.get_num();
			iptr->area_y2 = (short) fileTxt.get_num();

                        // skip the 1024x768 numbers
			fileTxt.get_num();
			fileTxt.get_num();
			fileTxt.get_num();
			fileTxt.get_num();
			iptr->monster_human_interface = (short) fileTxt.get_num();// 0 when display for both interfaces
																						 // 1 when display only for monster interface
																						 // 2 when display only for human interface
			
			// ###### begin Gilbert 15/9 #######//
			if( first_help_by_area == -1)
				first_help_by_area = help_info_count;
			last_help_by_area = help_info_count+1;			// record one after last
			// ###### end Gilbert 15/9 #######//
		}
		else
			err_here();

		//---------- next line -----------//

		fileTxt.next_line();

		if( fileTxt.is_eof() )
			break;

		//--------------------------------------------//
		// get help title
		//--------------------------------------------//

		fileTxt.read_line( iptr->help_title, iptr->HELP_TITLE_LEN );

		//---------------------------------------------------------//
		// get help description
		//---------------------------------------------------------//

		readLen = fileTxt.read_paragraph(textPtr, help_text_buf_size-totalReadLen);

		iptr->help_text_ptr = textPtr;
		iptr->help_text_len = readLen;

		textPtr      += readLen;
		totalReadLen += readLen;

		err_when( totalReadLen>help_text_buf_size );

		//----------- next help block -------------//

		fileTxt.next_line();      // pass the page break line

		help_info_count++;
		iptr++;

		err_when( help_info_count >= MAX_HELP_INFO );
	}

	if( first_help_by_help_code == -1 )
		first_help_by_help_code = 0;
	if( first_help_by_area == -1 )
		first_help_by_area = 0;

	err_when( last_help_by_help_code > help_info_count );
	err_when( last_help_by_area > help_info_count );
}