int main(void) { int i, j, i1, j1, d, t, **tt; srand((unsigned)time(NULL)); for(i = 0; i <= XMAX; i++) for(j = 0; j <= YMAX; j++) map[i][j] = 1; for(i = 3; i <= XMAX - 3; i++) for(j = 3; j <= YMAX - 3; j++) map[i][j] = 0; map[2][3] = 0; map[XMAX - 2][YMAX - 3] = 0; for(i = 4; i <= XMAX - 4; i += 2) { add_site(i, 2); add_site(XMAX - 2, j); } for(j = 4; j <= YMAX - 4; j += 2) { add_site(2, j); add_site(XMAX - 2, j); } while(select_site(&i, &j)) { for( ; ; ) { tt = dirtable[(int)(24 * (rand() / (RAND_MAX + 1.0)))]; for(d = 3; d >= 0; d--) { t = tt[d]; i1 = i + dx[t]; j1 = j + dy[t]; if(map[i1][j1] == 0) break; } if(d < 0) break; map[(i + i1) / 2][(j + j1) / 2] = 1; i = i1; j = j1; map[i][j] = 1; add_site(i, j); } } for(j = 2; j <= YMAX - 2; j++) { for(i = 2; i <= XMAX - 2; i++) if(map[i][j]) putchar('X'); else putchar(' '); putchar('\n'); } return(0); }
void Motif::read(istream& motin) { char line[200]; vector<int> c; vector<int> p; vector<bool> s; // Read sites // (don't add yet, as they will get screwed up by the column changes) while(motin.getline(line, 200)) { if(line[0] == '*') break; strtok(line, "\t"); c.push_back(atoi(strtok(NULL, "\t"))); p.push_back(atoi(strtok(NULL, "\t"))); s.push_back(atoi(strtok(NULL, "\0"))); } int motwidth = strlen(line); columns.clear(); for(int i = 0; i < motwidth; i++) { if(line[i] == '*') add_col(i); } // Add sites sitelist.clear(); int num_sites = c.size(); for(int i = 0; i < num_sites; i++) { assert(p[i] >= 0); add_site(c[i], p[i], s[i]); } // Read MAP score motin.getline(line, 200); strtok(line, ":"); set_map(atof(strtok(NULL, "\0"))); // Read specificity motin.getline(line, 200); strtok(line, ":"); set_spec(atof(strtok(NULL, "\0"))); // Read sequence cutoff motin.getline(line, 200); strtok(line, ":"); set_seq_cutoff(atof(strtok(NULL, "\0"))); // Read expression cutoff motin.getline(line, 200); strtok(line, ":"); set_expr_cutoff(atof(strtok(NULL, "\0"))); // Read iteration found motin.getline(line, 200); strtok(line, ":"); set_iter(strtok(NULL, "\0")); // Read dejavu motin.getline(line, 200); strtok(line, ":"); set_dejavu(atoi(strtok(NULL, "\0"))); }
static void populate_resources(struct resmgr *r) { int i, j; struct tile *tile; struct tile_type *tile_type; char *site_name; int site_index; for(i=0;i<r->db->chip.w*r->db->chip.h;i++) { tile = &r->db->chip.tiles[i]; tile_type = &r->db->tile_types[tile->type]; tile->user = alloc_size(tile_type->n_sites*sizeof(struct resmgr_site *)); for(j=0;j<tile_type->n_sites;j++) { site_name = r->db->site_types[tile_type->sites[j]].name; site_index = resmgr_get_site_index(site_name); switch(site_index) { case RESMGR_SITE_SLICEX: add_site(r, r->slices.slicex[0], i, j, RESMGR_BEL_COUNT_SLICEX); break; case RESMGR_SITE_SLICEL: add_site(r, r->slices.slicel[0], i, j, RESMGR_BEL_COUNT_SLICELM); break; case RESMGR_SITE_SLICEM: add_site(r, r->slices.slicem[0], i, j, RESMGR_BEL_COUNT_SLICELM); break; case RESMGR_SITE_IOBM: add_site(r, r->free_iobm, i, j, 1); break; case RESMGR_SITE_IOBS: add_site(r, r->free_iobs, i, j, 1); break; default: break; } } } }
int read_config(const char *path, int type) { char line[1024]; FILE *fp; char *s, *key, *val, *end_of_key; const char *error; char *cp, *cp2; int i; int lineno = 0; int got_transport = 0; int min_timeout = 0; struct ticket_config defaults = { { 0 } }; struct ticket_config *current_tk = NULL; fp = fopen(path, "r"); if (!fp) { log_error("failed to open %s: %s", path, strerror(errno)); return -1; } booth_conf = malloc(sizeof(struct booth_config) + TICKET_ALLOC * sizeof(struct ticket_config)); if (!booth_conf) { fclose(fp); log_error("failed to alloc memory for booth config"); return -ENOMEM; } memset(booth_conf, 0, sizeof(struct booth_config) + TICKET_ALLOC * sizeof(struct ticket_config)); ticket_size = TICKET_ALLOC; booth_conf->proto = UDP; booth_conf->port = BOOTH_DEFAULT_PORT; booth_conf->maxtimeskew = BOOTH_DEFAULT_MAX_TIME_SKEW; booth_conf->authkey[0] = '\0'; /* Provide safe defaults. -1 is reserved, though. */ booth_conf->uid = -2; booth_conf->gid = -2; strcpy(booth_conf->site_user, "hacluster"); strcpy(booth_conf->site_group, "haclient"); strcpy(booth_conf->arb_user, "nobody"); strcpy(booth_conf->arb_group, "nobody"); parse_weights("", defaults.weight); defaults.clu_test.path = NULL; defaults.clu_test.pid = 0; defaults.clu_test.status = 0; defaults.clu_test.progstate = EXTPROG_IDLE; defaults.term_duration = DEFAULT_TICKET_EXPIRY; defaults.timeout = DEFAULT_TICKET_TIMEOUT; defaults.retries = DEFAULT_RETRIES; defaults.acquire_after = 0; defaults.mode = TICKET_MODE_AUTO; error = ""; log_debug("reading config file %s", path); while (fgets(line, sizeof(line), fp)) { lineno++; s = skip_while(line, isspace); if (is_end_of_line(s) || *s == '#') continue; key = s; /* Key */ end_of_key = skip_while_in(key, isalnum, "-_"); if (end_of_key == key) { error = "No key"; goto err; } if (!*end_of_key) goto exp_equal; /* whitespace, and something else but nothing more? */ s = skip_while(end_of_key, isspace); if (*s != '=') { exp_equal: error = "Expected '=' after key"; goto err; } s++; /* It's my buffer, and I terminate if I want to. */ /* But not earlier than that, because we had to check for = */ *end_of_key = 0; /* Value tokenizing */ s = skip_while(s, isspace); switch (*s) { case '"': case '\'': val = s+1; s = skip_until(val, *s); /* Terminate value */ if (!*s) { error = "Unterminated quoted string"; goto err; } /* Remove and skip quote */ *s = 0; s++; if (*(s = skip_while(s, isspace)) && *s != '#') { error = "Surplus data after value"; goto err; } *s = 0; break; case 0: no_value: error = "No value"; goto err; break; default: val = s; /* Rest of line. */ i = strlen(s); /* i > 0 because of "case 0" above. */ while (i > 0 && isspace(s[i-1])) i--; s += i; *s = 0; } if (val == s) goto no_value; if (strlen(key) > BOOTH_NAME_LEN || strlen(val) > BOOTH_NAME_LEN) { error = "key/value too long"; goto err; } if (strcmp(key, "transport") == 0) { if (got_transport) { error = "config file has multiple transport lines"; goto err; } if (strcasecmp(val, "UDP") == 0) booth_conf->proto = UDP; else if (strcasecmp(val, "SCTP") == 0) booth_conf->proto = SCTP; else { error = "invalid transport protocol"; goto err; } got_transport = 1; continue; } if (strcmp(key, "port") == 0) { booth_conf->port = atoi(val); continue; } if (strcmp(key, "name") == 0) { safe_copy(booth_conf->name, val, BOOTH_NAME_LEN, "name"); continue; } #if HAVE_LIBGCRYPT || HAVE_LIBMHASH if (strcmp(key, "authfile") == 0) { safe_copy(booth_conf->authfile, val, BOOTH_PATH_LEN, "authfile"); continue; } if (strcmp(key, "maxtimeskew") == 0) { booth_conf->maxtimeskew = atoi(val); continue; } #endif if (strcmp(key, "site") == 0) { if (add_site(val, SITE)) goto err; continue; } if (strcmp(key, "arbitrator") == 0) { if (add_site(val, ARBITRATOR)) goto err; continue; } if (strcmp(key, "site-user") == 0) { safe_copy(booth_conf->site_user, optarg, BOOTH_NAME_LEN, "site-user"); continue; } if (strcmp(key, "site-group") == 0) { safe_copy(booth_conf->site_group, optarg, BOOTH_NAME_LEN, "site-group"); continue; } if (strcmp(key, "arbitrator-user") == 0) { safe_copy(booth_conf->arb_user, optarg, BOOTH_NAME_LEN, "arbitrator-user"); continue; } if (strcmp(key, "arbitrator-group") == 0) { safe_copy(booth_conf->arb_group, optarg, BOOTH_NAME_LEN, "arbitrator-group"); continue; } if (strcmp(key, "debug") == 0) { if (type != CLIENT && type != GEOSTORE) debug_level = max(debug_level, atoi(val)); continue; } if (strcmp(key, "ticket") == 0) { if (current_tk && strcmp(current_tk->name, "__defaults__")) { if (!postproc_ticket(current_tk)) { goto err; } } if (!strcmp(val, "__defaults__")) { current_tk = &defaults; } else if (add_ticket(val, ¤t_tk, &defaults)) { goto err; } continue; } /* current_tk must be allocated at this point, otherwise * we don't know to which ticket the key refers */ if (!current_tk) { error = "Unexpected keyword"; goto err; } if (strcmp(key, "expire") == 0) { current_tk->term_duration = read_time(val); if (current_tk->term_duration <= 0) { error = "Expected time >0 for expire"; goto err; } continue; } if (strcmp(key, "timeout") == 0) { current_tk->timeout = read_time(val); if (current_tk->timeout <= 0) { error = "Expected time >0 for timeout"; goto err; } if (!min_timeout) { min_timeout = current_tk->timeout; } else { min_timeout = min(min_timeout, current_tk->timeout); } continue; } if (strcmp(key, "retries") == 0) { current_tk->retries = strtol(val, &s, 0); if (*s || s == val || current_tk->retries<3 || current_tk->retries > 100) { error = "Expected plain integer value in the range [3, 100] for retries"; goto err; } continue; } if (strcmp(key, "renewal-freq") == 0) { current_tk->renewal_freq = read_time(val); if (current_tk->renewal_freq <= 0) { error = "Expected time >0 for renewal-freq"; goto err; } continue; } if (strcmp(key, "acquire-after") == 0) { current_tk->acquire_after = read_time(val); if (current_tk->acquire_after < 0) { error = "Expected time >=0 for acquire-after"; goto err; } continue; } if (strcmp(key, "before-acquire-handler") == 0) { if (parse_extprog(val, current_tk)) { goto err; } continue; } if (strcmp(key, "attr-prereq") == 0) { if (parse_attr_prereq(val, current_tk)) { goto err; } continue; } if (strcmp(key, "mode") == 0) { current_tk->mode = retrieve_ticket_mode(val); continue; } if (strcmp(key, "weights") == 0) { if (parse_weights(val, current_tk->weight) < 0) goto err; continue; } error = "Unknown keyword"; goto err; } fclose(fp); if ((booth_conf->site_count % 2) == 0) { log_warn("Odd number of nodes is strongly recommended!"); } /* Default: make config name match config filename. */ if (!booth_conf->name[0]) { cp = strrchr(path, '/'); cp = cp ? cp+1 : (char *)path; cp2 = strrchr(cp, '.'); if (!cp2) cp2 = cp + strlen(cp); if (cp2-cp >= BOOTH_NAME_LEN) { log_error("booth config file name too long"); goto out; } strncpy(booth_conf->name, cp, cp2-cp); *(booth_conf->name+(cp2-cp)) = '\0'; } if (!postproc_ticket(current_tk)) { goto out; } poll_timeout = min(POLL_TIMEOUT, min_timeout/10); if (!poll_timeout) poll_timeout = POLL_TIMEOUT; return 0; err: fclose(fp); out: log_error("%s in config file line %d", error, lineno); free(booth_conf); booth_conf = NULL; return -1; }
//--------- Begin of function SiteArray::create_raw_site ----------// // // <int> regionId - if this parameter is given, the raw site // will be built on this region. // [int] townRecno - if this parameter is given, the raw site // will be built near this town. // int SiteArray::create_raw_site(int regionId, int townRecno) { //-------- count the no. of each raw material -------// Site* sitePtr; short rawCountArray[MAX_RAW]; memset( rawCountArray, 0, sizeof(rawCountArray) ); int i; for( i=size(); i>0 ; i-- ) { if( site_array.is_deleted(i) ) continue; sitePtr = site_array[i]; if( sitePtr->site_type == SITE_RAW ) { err_when( sitePtr->object_id < 1 || sitePtr->object_id > MAX_RAW ); rawCountArray[ sitePtr->object_id-1 ]++; } } //---- find the minimum raw count ----// int minCount=0xFFFF; for( i=0 ; i<MAX_RAW ; i++ ) { if( rawCountArray[i] < minCount ) minCount = rawCountArray[i]; } //----- pick a raw material type -----// int rawId = m.random(MAX_RAW)+1; for( i=0 ; i<MAX_RAW ; i++ ) { if( ++rawId > MAX_RAW ) rawId = 1; if( rawCountArray[rawId-1] == minCount ) // don't use this raw type unless it is one of the less available ones. break; } //--------- create the raw site now ------// int locX1, locY1, locX2, locY2; int maxTries; if( townRecno ) { #define MAX_TOWN_SITE_DISTANCE 10 Town* townPtr = town_array[townRecno]; locX1 = townPtr->center_x - MAX_TOWN_SITE_DISTANCE; locX2 = townPtr->center_x + MAX_TOWN_SITE_DISTANCE; locY1 = townPtr->center_y - MAX_TOWN_SITE_DISTANCE; locY2 = townPtr->center_y + MAX_TOWN_SITE_DISTANCE; if(locX1<0) locX1 = 0; else if(locX2>=MAX_WORLD_X_LOC) locX2 = MAX_WORLD_X_LOC-1; if(locY1<0) locY1 = 0; else if(locY2>=MAX_WORLD_Y_LOC) locY2 = MAX_WORLD_Y_LOC-1; maxTries = (locX2-locX1+1)*(locY2-locY1+1); regionId = townPtr->region_id; } else { locX1 = 0; locY1 = 0; locX2 = MAX_WORLD_X_LOC-1; locY2 = MAX_WORLD_Y_LOC-1; maxTries = 10000; } //----- randomly locate a space to add the site -----// if( world.locate_space_random(locX1, locY1, locX2, locY2, 5, 5, maxTries, regionId, 1) ) // 5,5 are the size of the raw site, it must be large enough for a mine to build and 1 location for the edges. The last para 1 = site building mode { int reserveQty = MAX_RAW_RESERVE_QTY * (50 + m.random(50)) / 100; add_site( locX1+2, locY1+2, SITE_RAW, rawId, reserveQty ); // xLoc+1 & yLoc+1 as the located size is 3x3, the raw site is at the center of it return 1; } else { return 0; } }
//--------- Begin of function SiteArray::create_raw_site ----------// // // <int> regionId - if this parameter is given, the raw site // will be built on this region. // [int] townRecno - if this parameter is given, the raw site // will be built near this town. // // return: <int> >0 - the recno of the site // 0 - no site is added // int SiteArray::create_raw_site(int regionId, int townRecno) { //-------- count the no. of each raw material -------// Site* sitePtr; short rawCountArray[MAX_RAW]; memset( rawCountArray, 0, sizeof(rawCountArray) ); int i; for( i=size(); i>0 ; i-- ) { if( site_array.is_deleted(i) ) continue; sitePtr = site_array[i]; if( sitePtr->site_type == SITE_RAW ) { err_when( sitePtr->object_id < 1 || sitePtr->object_id > MAX_RAW ); rawCountArray[ sitePtr->object_id-1 ]++; } } //---- find the minimum raw count ----// int minCount=0xFFFF; for( i=0 ; i<MAX_RAW ; i++ ) { if( rawCountArray[i] < minCount ) minCount = rawCountArray[i]; } //----- pick a raw material type -----// int rawId = misc.random(MAX_RAW)+1; for( i=0 ; i<MAX_RAW ; i++ ) { if( ++rawId > MAX_RAW ) rawId = 1; if( rawCountArray[rawId-1] == minCount ) // don't use this raw type unless it is one of the less available ones. break; } //--------- create the raw site now ------// int locX1, locY1, locX2, locY2; int maxTries; int siteWidth = raw_res[rawId]->map_loc_width; int siteHeight = raw_res[rawId]->map_loc_height; if( townRecno ) { // ####### begin Gilbert 26/5 ########// // #define MAX_TOWN_SITE_DISTANCE (MAX_LINKED_FIRM_TOWN-STD_TOWN_LOC_WIDTH) const int maxTownSiteDistance = world.effective_distance(FIRM_MINE, 0); Town* townPtr = town_array[townRecno]; locX1 = townPtr->center_x - maxTownSiteDistance - (siteWidth-1)/2; // (siteWidth-1)/2 is the distance from mine center_x to mine loc_x1 locX2 = townPtr->center_x + maxTownSiteDistance + (siteWidth+1)/2; locY1 = townPtr->center_y - maxTownSiteDistance - (siteHeight-1)/2; locY2 = townPtr->center_y + maxTownSiteDistance + (siteHeight+1)/2; // ####### end Gilbert 26/5 ########// if(locX1<0) locX1 = 0; else if(locX2>=MAX_WORLD_X_LOC) locX2 = MAX_WORLD_X_LOC-1; if(locY1<0) locY1 = 0; else if(locY2>=MAX_WORLD_Y_LOC) locY2 = MAX_WORLD_Y_LOC-1; maxTries = (locX2-locX1+1)*(locY2-locY1+1); regionId = townPtr->region_id; } else { locX1 = 0; locY1 = 0; locX2 = MAX_WORLD_X_LOC-1; locY2 = MAX_WORLD_Y_LOC-1; maxTries = 10000; } //----- randomly locate a space to add the site -----// int mineSize = firm_res[FIRM_MINE]->loc_width; err_when( firm_res[FIRM_MINE]->loc_height > mineSize ); // assume square size if( world.locate_space_random(locX1, locY1, locX2, locY2, mineSize+INTER_PLACE_SPACE*2, mineSize+INTER_PLACE_SPACE*2, maxTries, regionId, 1) ) { if( world.can_build_firm( locX1+INTER_PLACE_SPACE, locY1+INTER_PLACE_SPACE, FIRM_MINE) ) { int reserveQty = MAX_RAW_RESERVE_QTY * (60 + misc.random(40)) / 100; return add_site( locX1+INTER_PLACE_SPACE, locY1+INTER_PLACE_SPACE, SITE_RAW, rawId, reserveQty ); // xLoc+1 & yLoc+1 as the located size is 3x3, the raw site is at the center of it } } return 0; }