Esempio n. 1
0
void norm2(double *n1, double *n2){
  //n1=0; n2=0;
  static bool first=true;       
  if (first){
    srand(time(NULL));		// initialize the rand() function
    first=false;
  }
  double u1,u2;
  u1 = rand0_1();
  u2 = rand0_1();
  *n1 = xnorm(u1,u2);
  *n2 = ynorm(u1,u2);
}
Esempio n. 2
0
/*
 * setup the nstr_sect structure for sector selection.
 * can select on either NS_ALL, NS_AREA, or NS_DIST
 * iterate thru the "condarg" string looking
 * for arguments to compile into the nstr.
 * Using this function for anything but command arguments is usually
 * incorrect, because it respects conditionals.  Use the snxtsct_FOO()
 * instead.
 */
int
snxtsct(struct nstr_sect *np, char *str)
{
    struct range range;
    struct natstr *natp;
    coord cx, cy;
    int dist;
    char buf[1024];

    if (!str || !*str) {
	if (!(str = getstring("(sects)? ", buf)))
	    return 0;
    } else
	make_stale_if_command_arg(str);
    switch (sarg_type(str)) {
    case NS_AREA:
	if (!sarg_area(str, &range))
	    return 0;
	snxtsct_area(np, &range);
	break;
    case NS_DIST:
	if (!sarg_range(str, &cx, &cy, &dist))
	    return 0;
	snxtsct_dist(np, cx, cy, dist);
	break;
    case NS_ALL:
	/*
	 * Can't use snxtsct_all(), as it would disclose the real
	 * origin.  Use a world-sized area instead.
	 */
	natp = getnatp(player->cnum);
	range.lx = xabs(natp, -WORLD_X / 2);
	range.ly = yabs(natp, -WORLD_Y / 2);
	range.hx = xnorm(range.lx + WORLD_X - 1);
	range.hy = ynorm(range.ly + WORLD_Y - 1);
	xysize_range(&range);
	snxtsct_area(np, &range);
	break;
    default:
	return 0;
    }
    return snxtsct_use_condarg(np);
}
Esempio n. 3
0
void
move_sat(struct plnstr *pp)
{
    coord x1, y1, x2, y2;
    coord dx, dy;
    float newtheta;
    struct sctstr sect;

    newtheta = pp->pln_theta + .05;

    if (newtheta >= 1.0) {
	newtheta -= 1.0;
    }

    x1 = (coord)(2 * pp->pln_theta * WORLD_X);
    x1 = xnorm(x1);
    y1 = (coord)(sin(6 * PI * pp->pln_theta) * (WORLD_Y / 4));
    x2 = (coord)(2 * newtheta * WORLD_X);
    x2 = xnorm(x2);
    y2 = (coord)(sin(6 * PI * newtheta) * (WORLD_Y / 4));
    dx = x1 - pp->pln_x;
    dy = y1 - pp->pln_y;
    x2 -= dx;
    y2 -= dy;

    if ((x2 + y2) & 1) {
	x2++;
    }

    pp->pln_x = xnorm(x2);
    pp->pln_y = ynorm(y2);
    pp->pln_theta = newtheta;
    getsect(pp->pln_x, pp->pln_y, &sect);
    if (sect.sct_own)
	if (pp->pln_own != sect.sct_own)
	    wu(0, sect.sct_own, "%s satellite spotted over %s\n",
	       cname(pp->pln_own), xyas(pp->pln_x, pp->pln_y,
					sect.sct_own));
    return;
}
Esempio n. 4
0
int
nxtitem(struct nstr_item *np, void *ptr)
{
    struct empobj *gp;
    int selected;

    if (np->sel == NS_UNDEF)
	return 0;
    gp = (struct empobj *)ptr;
    do {
	if (np->sel == NS_LIST) {
	    np->index++;
	    if (np->index >= np->size)
		return 0;
	    np->cur = np->list[np->index];
	} else if (np->sel == NS_CARGO) {
	    if (np->next < 0)
		return 0;
	    np->cur = np->next;
	    np->next = unit_cargo_next(np->type, np->next);
	} else {
	    np->cur++;
	}
	if (!ef_read(np->type, np->cur, ptr))
	    return 0;
	selected = 1;
	switch (np->sel) {
	case NS_LIST:
	case NS_CARGO:
	case NS_ALL:
	    break;
	case NS_DIST:
	    if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
		return 0;
	    if (!xyinrange(gp->x, gp->y, &np->range)) {
		selected = 0;
		break;
	    }
	    np->curdist = mapdist(gp->x, gp->y, np->cx, np->cy);
	    if (np->curdist > np->dist)
		selected = 0;
	    break;
	case NS_AREA:
	    if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
		return 0;
	    if (!xyinrange(gp->x, gp->y, &np->range))
		selected = 0;
	    break;
	case NS_XY:
	    if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_XY)))
		return 0;
	    if (xnorm(gp->x) != np->cx || ynorm(gp->y) != np->cy)
		selected = 0;
	    break;
	case NS_GROUP:
	    if (CANT_HAPPEN(!(ef_flags(np->type) & EFF_GROUP)))
		return 0;
	    if (np->group != gp->group)
		selected = 0;
	    break;
	default:
	    CANT_REACH();
	    return 0;
	}
	if (selected && np->ncond) {
	    /* nstr_exec is expensive, so we do it last */
	    if (!nstr_exec(np->cond, np->ncond, ptr))
		selected = 0;
	}
    } while (!selected);
    return 1;
}
Esempio n. 5
0
/*
 * format: skywatch [<SECTS>]
 */
int
skyw(void)
{
    struct sctstr sect;
    struct nstr_sect nstr;
    struct sky *skyp;
    struct sky *list[TSIZE];
    int i, n;
    int vrange, see;
    int x, y, dx, dy, dxmax;
    int nsat = 0;
    double tech;
    struct nstr_item ni;

    if (!snxtsct(&nstr, player->argp[1]))
	return RET_SYN;
    for (i = 0; i < TSIZE; i++)
	list[i] = NULL;
    skyp = malloc(sizeof(*skyp));
    snxtitem_all(&ni, EF_PLANE);
    while (nxtitem(&ni, &skyp->s_sat)) {
	if (!skyp->s_sat.pln_own)
	    continue;
	if (!pln_is_in_orbit(&skyp->s_sat))
	    continue;
	getsect(skyp->s_sat.pln_x, skyp->s_sat.pln_y, &sect);
	n = scthash(skyp->s_sat.pln_x, skyp->s_sat.pln_y, TSIZE);
	skyp->s_spotted = 0;
	skyp->s_next = list[n];
	list[n] = skyp;
	skyp = malloc(sizeof(*skyp));
	nsat++;
    }
    /* get that last one! */
    free(skyp);
    pr("- = [ Skywatch report for %s ] = -\n", cname(player->cnum));
    pr("  Country            Satellite     Location\n");
    tech = tfact(player->cnum, 1.0);
    while (nxtsct(&nstr, &sect) && nsat) {
	if (sect.sct_own != player->cnum)
	    continue;
	see = sect.sct_type == SCT_RADAR ? 14 : 4;
	vrange = (int)(sect.sct_effic / 100.0 * see * tech);
	if (vrange < 1)
	    vrange = 1;
	for (dy = -vrange; dy <= vrange; dy++) {
	    y = ynorm(sect.sct_y + dy);
	    dxmax = 2 * vrange - abs(dy);
	    for (dx = -dxmax; dx <= dxmax; dx += 2) {
		x = xnorm(sect.sct_x + dx);
		n = scthash(x, y, TSIZE);
		if (!list[n])
		    continue;
		nsat -= showsat(&list[n], x, y);
	    }
	}
    }
    /* free up the sky structs calloc'ed above */
    for (i = 0; i < TSIZE; i++) {
	while (NULL != (skyp = list[i])) {
	    list[i] = skyp->s_next;
	    free(skyp);
	}
    }
    return RET_OK;
}
Esempio n. 6
0
/*
 * format: coastwatch [<SECTS>]
 */
int
coas(void)
{
    struct sctstr sect;
    struct nstr_sect nstr;
    struct coast *cp;
    struct coast *list[TSIZE];
    int i, n;
    int vrange, see;
    int x, y, dx, dy, dxmax;
    int nship = 0;
    double tech;
    struct nstr_item ni;

    if (!snxtsct(&nstr, player->argp[1]))
	return RET_SYN;
    for (i = 0; i < TSIZE; i++)
	list[i] = NULL;
    cp = malloc(sizeof(*cp));
    snxtitem_all(&ni, EF_SHIP);
    while (nxtitem(&ni, &cp->c_shp)) {
	if (cp->c_shp.shp_own == 0 || cp->c_shp.shp_own == player->cnum)
	    continue;
	/*
	 * don't bother putting subs in the table...
	 * unless they're in a sector you own (harbor or such)
	 */
	getsect(cp->c_shp.shp_x, cp->c_shp.shp_y, &sect);
	if ((mchr[(int)cp->c_shp.shp_type].m_flags & M_SUB) &&
	    (sect.sct_own != player->cnum))
	    continue;
	n = scthash(cp->c_shp.shp_x, cp->c_shp.shp_y, TSIZE);
	cp->c_spotted = 0;
	cp->c_number = i;
	cp->c_next = list[n];
	list[n] = cp;
	cp = malloc(sizeof(*cp));
	nship++;
    }
    /* get that last one! */
    free(cp);
    pr("- = [ Coastwatch report for %s ] = -\n", cname(player->cnum));
    pr("  Country            Ship          Location\n");
    tech = tfact(player->cnum, 1.0);
    while (nxtsct(&nstr, &sect) && nship) {
	if (sect.sct_own != player->cnum)
	    continue;
	see = sect.sct_type == SCT_RADAR ? 14 : 4;
	vrange = (int)(sect.sct_effic / 100.0 * see * tech);
	if (vrange < 1)
	    vrange = 1;
	for (dy = -vrange; dy <= vrange; dy++) {
	    y = ynorm(sect.sct_y + dy);
	    dxmax = 2 * vrange - abs(dy);
	    for (dx = -dxmax; dx <= dxmax; dx += 2) {
		x = xnorm(sect.sct_x + dx);
		n = scthash(x, y, TSIZE);
		if (!list[n])
		    continue;
		nship -= showship(&list[n], x, y);
	    }
	}
    }
    /* free up the coast structs calloc'ed above */
    for (i = 0; i < TSIZE; i++) {
	while (NULL != (cp = list[i])) {
	    list[i] = cp->c_next;
	    free(cp);
	}
    }
    return RET_OK;
}