Example #1
0
File: nj.c Project: stplaydog/DCJUC
/********************************************************
 * this is the main entry for the neighbor joining method
 * ******************************************************/
int 
run_nj(char *file){
	int i, j;
	po o = (po)malloc(sizeof(to));	
	init_orders(file, o);
	pdism d_mat = (pdism)malloc(sizeof(tdism));
	init_dis_mat(d_mat, o->num_input_genome);
	compute_distance_matrix(o, d_mat); // we only need to compute distance once
	compute_q_matrix(d_mat);
	edge_count = 0;
	while(d_mat->num_remain_spc > 2){
#ifdef SIMP_NJ
		join_two_simp(d_mat);
#else
		join_two_car(d_mat, o);
#endif
		compute_q_matrix(d_mat);
	}
	//join the last two
	d_mat->score += d_mat->dis_mat[0][1];
	pnje tmp = d_mat->tree;
	int result = d_mat->score;
	free_d_mat(d_mat);
	free_orders(o);
	return result;
}
Example #2
0
File: nj.c Project: stplaydog/DCJUC
/****************************************************
 *this is to compute the upper bound of the tree
 * after performing some maximum parsimony operations
 * *************************************************/
int 
run_upper_bound(po o){
	int i, j;
	po oo = (po)malloc(sizeof(to));
	int num_remain = 0;
	for(i=0; i<o->num_genome; i++){
		if(o->valid[i] == FALSE)
			continue;
		num_remain++;
	}
	if(num_remain==0)
		return;
	oo->num_genome = o->num_genome;
	oo->num_med_genome = o->num_med_genome;
	oo->num_input_genome = o->num_input_genome;
	oo->med_genome_idx = o->med_genome_idx;
	oo->v_size = o->v_size;
	oo->v_idx = (int**)malloc(sizeof(int*)*o->num_genome);
	oo->e_idx = (int**)malloc(sizeof(int*)*o->num_genome);
	oo->e_size = (int*)malloc(sizeof(int)*o->num_genome);
	oo->valid = (int*)malloc(sizeof(int)*o->num_genome);
	for(i=0; i<o->num_genome; i++){
		oo->valid[i] = o->valid[i];
		oo->e_size[i] = o->e_size[i];
		oo->v_idx[i] = (int*)malloc(sizeof(int)*o->v_size);
		if(o->e_size[i] <= 0)
			continue;
		oo->e_idx[i] = (int*)malloc(sizeof(int)*o->e_size[i]);
		for(j=0;j<o->v_size; j++)
			oo->v_idx[i][j] = o->v_idx[i][j];
		for(j=0; j<o->e_size[i]; j++)
			oo->e_idx[i][j] = o->e_idx[i][j];
	}
	//
	pdism d_mat = (pdism)malloc(sizeof(tdism));
	init_dis_mat_by_interm_orders(d_mat, oo);
	compute_q_matrix(d_mat);
	while(d_mat->num_remain_spc > 2){
#ifdef SIMP_NJ
		join_two_simp(d_mat);
#else
		join_two_car(d_mat, oo);
		//printf("%d %d %p %p\n", oo->v_idx[i][0], oo->e_size[i], oo->v_idx[i], oo->e_idx[i]);
#endif
		compute_q_matrix(d_mat);
	}
	//join the last two
	d_mat->score += d_mat->dis_mat[0][1];
	pnje tmp = d_mat->tree;
	free_orders(oo);
	int result = d_mat->score;
	free_d_mat(d_mat);
	return result;
}
Example #3
0
void u_setfaction(unit * u, faction * f)
{
  int cnt = u->number;

  if (u->faction == f)
    return;
  if (u->faction) {
    set_number(u, 0);
    if (count_unit(u))
      --u->faction->no_units;
    join_group(u, NULL);
    free_orders(&u->orders);
    set_order(&u->thisorder, NULL);

    if (u->nextF)
      u->nextF->prevF = u->prevF;
    if (u->prevF)
      u->prevF->nextF = u->nextF;
    else
      u->faction->units = u->nextF;
  }

  if (f != NULL) {
    if (f->units)
      f->units->prevF = u;
    u->prevF = NULL;
    u->nextF = f->units;
    f->units = u;
  } else
    u->nextF = NULL;

  u->faction = f;
  if (u->region)
    update_interval(f, u->region);
  if (cnt && f) {
    set_number(u, cnt);
    if (count_unit(u))
      ++f->no_units;
  }
}
Example #4
0
/** remove the unit from memory.
 * this frees all memory that's only accessible through the unit,
 * and you should already have called uunhash and removed the unit from the
 * region.
 */
void free_unit(unit * u)
{
  free(u->name);
  free(u->display);
  free_order(u->thisorder);
  free_orders(&u->orders);
  if (u->skills)
    free(u->skills);
  while (u->items) {
    item *it = u->items->next;
    u->items->next = NULL;
    i_free(u->items);
    u->items = it;
  }
  while (u->attribs)
    a_remove(&u->attribs, u->attribs);
  while (u->reservations) {
    struct reservation *res = u->reservations;
    u->reservations = res->next;
    free(res);
  }
}
Example #5
0
void make_undead_unit(unit * u)
{
    free_orders(&u->orders);
    name_unit(u);
    u->flags |= UFL_ISNEW;
}
Example #6
0
void plan_monsters(faction * f)
{
    region *r;
    
    assert(f);
    attack_chance = get_param_flt(global.parameters, "rules.monsters.attack_chance", 0.4);
    f->lastorders = turn;

    for (r = regions; r; r = r->next) {
        unit *u;
        bool attacking = false;

        for (u = r->units; u; u = u->next) {
            attrib *ta;
            order *long_order = NULL;

            /* Ab hier nur noch Befehle für NPC-Einheiten. */
            if (!is_monsters(u->faction))
                continue;

            /* Befehle müssen jede Runde neu gegeben werden: */
            free_orders(&u->orders);
            if (skill_enabled(SK_PERCEPTION)) {
                /* Monster bekommen jede Runde ein paar Tage Wahrnehmung dazu */
                /* TODO: this only works for playerrace */
                produceexp(u, SK_PERCEPTION, u->number);
            }

            if (!attacking) {
                if (chance(attack_chance)) attacking = true;
            }
            if (u->status > ST_BEHIND) {
                setstatus(u, ST_FIGHT);
                /* all monsters fight */
            }
            if (attacking && (!r->land || is_guard(u, GUARD_TAX))) {
                monster_attacks(u);
            }
            /* units with a plan to kill get ATTACK orders: */
            ta = a_find(u->attribs, &at_hate);
            if (ta && !monster_is_waiting(u)) {
                unit *tu = (unit *)ta->data.v;
                if (tu && tu->region == r) {
                    order * ord = monster_attack(u, tu);
                    if (ord) {
                        addlist(&u->orders, ord);
                    }
                }
                else if (tu) {
                    tu = findunitg(ta->data.i, NULL);
                    if (tu != NULL) {
                        long_order = make_movement_order(u, tu->region, 2, allowed_walk);
                    }
                }
                else
                    a_remove(&u->attribs, ta);
            }

            /* All monsters guard the region: */
            if (!monster_is_waiting(u) && r->land) {
                addlist(&u->orders, create_order(K_GUARD, u->faction->locale, NULL));
            }

            /* Einheiten mit Bewegungsplan kriegen ein NACH: */
            if (long_order == NULL) {
                attrib *ta = a_find(u->attribs, &at_targetregion);
                if (ta) {
                    if (u->region == (region *)ta->data.v) {
                        a_remove(&u->attribs, ta);
                    }
                }
                else if (u_race(u)->flags & RCF_MOVERANDOM) {
                    if (rng_int() % 100 < MOVECHANCE || check_overpopulated(u)) {
                        long_order = monster_move(r, u);
                    }
                }
            }

            if (long_order == NULL && unit_can_study(u)) {
                /* Einheiten, die Waffenlosen Kampf lernen könnten, lernen es um
                 * zu bewachen: */
                if (u_race(u)->bonus[SK_WEAPONLESS] != -99) {
                    if (effskill(u, SK_WEAPONLESS, 0) < 1) {
                        long_order =
                            create_order(K_STUDY, f->locale, "'%s'",
                            skillname(SK_WEAPONLESS, f->locale));
                    }
                }
            }

            if (long_order == NULL) {
                /* Ab hier noch nicht generalisierte Spezialbehandlungen. */

                if (!u->orders) {
                    handle_event(u->attribs, "ai_move", u);
                }

                switch (old_race(u_race(u))) {
                case RC_SEASERPENT:
                    long_order = create_order(K_PIRACY, f->locale, NULL);
                    break;
#ifdef TODO_ALP
                case RC_ALP:
                    long_order = monster_seeks_target(r, u);
                    break;
#endif
                case RC_FIREDRAGON:
                case RC_DRAGON:
                case RC_WYRM:
                    long_order = plan_dragon(u);
                    break;
                default:
                    if (u_race(u)->flags & RCF_LEARN) {
                        long_order = monster_learn(u);
                    }
                    break;
                }
            }
            if (long_order) {
                addlist(&u->orders, long_order);
            }
        }
    }
    pathfinder_cleanup();
}