Пример #1
0
void
pln_mine(struct emp_qelem *list, coord tx, coord ty)
{
    struct emp_qelem *qp;
    struct plist *plp;
    int amt;
    struct sctstr sect;

    amt = 0;
    for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
	plp = (struct plist *)qp;
	amt += plp->load;

    }
    if (amt > 0) {
	getsect(tx, ty, &sect);
	if (sect.sct_type != SCT_WATER) {
	    pr("Your seamines have no effect here.\n");
	    return;
	}
	sect.sct_mines = MIN(sect.sct_mines + amt, MINES_MAX);
	pr("%d mines laid in %s.\n", amt, xyas(tx, ty, player->cnum));
	if (map_set(player->cnum, tx, ty, 'X', 0))
	    writemap(player->cnum);
	putsect(&sect);
    }
}
Пример #2
0
int
do_look(int type)
{
    int i;
    struct nstr_item ni;
    union empobj_storage unit;
    struct sctstr sect;
    int x, y;
    unsigned char *bitmap;
    int changed = 0;

    if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
	type = EF_SHIP;

    if (!snxtitem(&ni, type, player->argp[1], NULL))
	return RET_SYN;
    bitmap = calloc((WORLD_SZ() + 7) / 8, 1);
    if (!bitmap) {
	logerror("malloc failed in do_look\n");
	pr("Memory error.  Tell the deity.\n");
	return RET_FAIL;
    }
    while (nxtitem(&ni, &unit)) {
	if (!player->owner)
	    continue;
	if (type == EF_LAND) {
	    if (unit.land.lnd_ship >= 0)
		continue;
	    if (unit.land.lnd_land >= 0)
		continue;
	    /* Spies don't need military to do a "llook".  Other
	       units do */
	    if ((unit.land.lnd_item[I_MILIT] <= 0) &&
		!(lchr[(int)unit.land.lnd_type].l_flags & L_SPY))
		continue;
	    look_land(&unit.land);
	} else
	    look_ship(&unit.ship);
	for (i = 0; i <= 6; i++) {
	    x = diroff[i][0] + unit.gen.x;
	    y = diroff[i][1] + unit.gen.y;
	    if (emp_getbit(x, y, bitmap))
		continue;
	    emp_setbit(x, y, bitmap);
	    getsect(x, y, &sect);
	    if (sect.sct_type == SCT_WATER)
		continue;
	    look_at_sect(&sect, 10);
	    changed += map_set(player->cnum, x, y,
			       dchr[sect.sct_type].d_mnem, 0);
	    if (opt_HIDDEN) {
		setcont(player->cnum, sect.sct_own, FOUND_LOOK);
	    }
	}
    }
    if (changed)
	writemap(player->cnum);
    free(bitmap);
    return RET_OK;
}
Пример #3
0
int print(int argc, char**argv){
  if(argc==0){
    fprintf(stderr,"print FILE [-r chrName]\n");
    exit(0);
  }
  char *base = *argv;
  char* outnames_bin = append(base,BIN);
  char* outnames_idx = append(base,IDX);
  fprintf(stderr,"Assuming binfile:%s and indexfile:%s\n",outnames_bin,outnames_idx);
  
  myMap mm = getMap(outnames_idx);
  writemap(stderr,mm);
  BGZF *fp = bgzf_open(outnames_bin,"r");

  --argc;++argv;
  //  fprintf(stderr,"argc=%d\n",argc);
  int argP =0;
  char *chr=NULL;

  while(argP<argc){
    //   fprintf(stderr,"args=%s\n",argv[argP]);
    if(argP==argc){
      fprintf(stderr,"incomplete arguments list\n");
      exit(0);
    }
    if(strcmp("-r",argv[argP])==0)
      chr = argv[argP+1];
    else {
      fprintf(stderr,"Unknown argument:%s\n",argv[argP]);
      exit(0);

    }
    argP +=2;
  }
  
  
  if(chr!=NULL){  
    myMap::iterator it = mm.find(chr);
    if(it==mm.end()){
      fprintf(stderr,"Problem finding chr: %s in index\n",chr);
      exit(0);
    }
    datum d = it->second;
    bgzf_seek(fp,d.fpos,SEEK_SET);
  }

  while(1){
    perChr pc = getPerChr(fp);
    if(pc.nSites==0)
      break;
    fprintf(stderr,"pc.chr=%s pc.nSites=%zu firstpos=%d lastpos=%d\n",pc.chr,pc.nSites,pc.posi[0],pc.posi[pc.nSites-1]);
    print_main(pc,stdout);
    if(chr!=NULL)
      break;
    dalloc(pc);
  }

  return 0;
}
Пример #4
0
/*
 * Update @owner's bmap for radar at @cx,@cy.
 * @eff is the radar's efficiency, @tlev its tech level, @spy its power.
 */
void
rad_map_set(natid owner, int cx, int cy, int eff, double tlev, int spy)
{
    struct nstr_sect ns;
    struct sctstr sect;
    int range = rad_range(eff, tlev, spy);
    int changed = 0;
    char ch;

    snxtsct_dist(&ns, cx, cy, range);
    while (nxtsct(&ns, &sect)) {
	ch = rad_char(&sect, ns.curdist, range, owner);
	changed += map_set(owner, ns.x, ns.y, ch, 0);
    }
    if (changed)
	writemap(owner);
}
Пример #5
0
/*ARGSUSED*/
static int
move_map(coord curx, coord cury, char *arg)
{
    struct nstr_sect ns;
    struct sctstr sect;
    char view[7];
    int i;
    int changed = 0;

    snxtsct_dist(&ns, curx, cury, 1);
    i = 0;
    while (i < 7 && nxtsct(&ns, &sect)) {
	/* Nasty: this relies on the iteration order */
	view[i] = dchr[sect.sct_type].d_mnem;
	switch (sect.sct_type) {
	case SCT_WATER:
	case SCT_RURAL:
	case SCT_MOUNT:
	case SCT_WASTE:
	case SCT_PLAINS:
	    break;
	default:
	    if (sect.sct_own != player->cnum && !player->god)
		view[i] = '?';
	    break;
	}
	changed += map_set(player->cnum, ns.x, ns.y, view[i], 0);
	i++;
    }
    if (changed)
	writemap(player->cnum);
    if (!getsect(curx, cury, &sect))
	return RET_FAIL;
    pr("    %c %c      eff   mob   civ  mil   uw food  work  avail\n",
       view[0], view[1]);
    pr("   %c %c %c     %3d   %3d  %4d %4d %4d %4d   %3d   %3d\n",
       view[2], view[3], view[4],
       sect.sct_effic, sect.sct_mobil,
       sect.sct_item[I_CIVIL], sect.sct_item[I_MILIT], sect.sct_item[I_UW],
       sect.sct_item[I_FOOD], sect.sct_work, sect.sct_avail);
    pr("    %c %c\n", view[5], view[6]);
    return RET_OK;
}
Пример #6
0
void
plane_sweep(struct emp_qelem *plane_list, coord x, coord y)
{
    struct plnstr *pp;
    struct plchrstr *pcp;
    struct emp_qelem *qp;
    struct emp_qelem *next;
    struct plist *ip;
    struct sctstr sect;
    int mines_there;
    int found = 0;

    getsect(x, y, &sect);
    mines_there = sect.sct_mines;

    if (mines_there == 0)
	return;

    if (sect.sct_type != SCT_WATER)
	return;

    for (qp = plane_list->q_forw; ((qp != plane_list) && (mines_there));
	 qp = next) {
	next = qp->q_forw;
	ip = (struct plist *)qp;
	pp = &ip->plane;
	pcp = ip->pcp;
	if (!(pcp->pl_flags & P_SWEEP))	/* if it isn't an sweep plane */
	    continue;

	if (chance((100.0 - pln_acc(pp)) / 100.0)) {
	    pr("Sweep! in %s\n",
	       xyas(sect.sct_x, sect.sct_y, player->cnum));
	    mines_there--;
	    found = 1;
	}
    }

    if (found && map_set(player->cnum, sect.sct_x, sect.sct_y, 'X', 0))
	writemap(player->cnum);
    sect.sct_mines = mines_there;
    putsect(&sect);
}
Пример #7
0
int
main()
{
	copyright = ecalloc(1, BUFSIZ);
	strbuf = ecalloc(1, BUFSIZ);
	firstglyph = ecalloc(1, sizeof(Glyph)); /* TODO dummies use memory */
	lastglyph = firstglyph;

	while (!feof(stdin)) {
		fgets(strbuf, BUFSIZ, stdin);
		parse(strbuf);
	}

	writeheader();
	writemap();
	writeglyphs();

	/* TODO does one free buffers before return? */
	return 0;
}
Пример #8
0
int main(int argc, char* argv[])
{
	initscr();
	scrollok(stdscr, TRUE);
	stufffilename();
	wprintw(stdscr, "\n");
	if(middleman() != 0)
	{
		wprintw(stdscr, "Failed to open input or temp file!\n");
		refresh();
		hang(2);
		endwin();
		return 1;
	}
	if(readmap() != 0)
	{
		wprintw(stdscr, "Failed to open temporary map file!\n");
		refresh();
		hang(2);
		endwin();
		return 1;
	}
	stufffilename();
	wprintw(stdscr, "\n");
	if(writemap() != 0)
	{
		wprintw(stdscr, "Failed to write map file!\n");
		refresh();
		hang(2);
		endwin();
		return 1;
	}
	wprintw(stdscr, "Happily finished!\n");
	refresh();
	hang(2);
	endwin();
	return 0;
}
Пример #9
0
int
sona(void)
{
    struct nstr_item ni, nit;
    struct sctstr sect;
    struct shpstr ship;
    struct shpstr targ;
    struct natstr *natp;
    struct mchrstr *mcp;
    struct mchrstr *tmcp;
    struct nstr_sect ns;
    int range;
    int visib, pingrange;
    int srange;
    int vrange;
    int dist;
    int x, y;
    int cx, cy;
    int changed = 0;
    int row;
    /* Where these are used are non-re-entrant, so we keep 'em around */
    static char **rad = NULL;
    static char *radbuf = NULL;
    static signed char **vis = NULL;
    static signed char *visbuf = NULL;

    if (!snxtitem(&ni, EF_SHIP, player->argp[1], NULL))
	return RET_SYN;
    if (!radbuf)
	radbuf = malloc(WORLD_Y * MAPWIDTH(1));
    if (!visbuf)
	visbuf = malloc(WORLD_Y * MAPWIDTH(1));
    if (!rad && radbuf) {
	rad = malloc(WORLD_Y * sizeof(char *));
	if (rad) {
	    for (x = 0; x < WORLD_Y; x++) {
		rad[x] = &radbuf[(WORLD_X + 1) * x];
	    }
	}
    }
    if (!vis && visbuf) {
	vis = malloc(WORLD_Y * sizeof(signed char *));
	if (vis) {
	    for (x = 0; x < WORLD_Y; x++) {
		vis[x] = &visbuf[(WORLD_X + 1) * x];
	    }
	}
    }
    if (!radbuf || !visbuf || !rad || !vis) {
	pr("Memory error, tell the deity.\n");
	logerror("malloc failed in sona\n");
	return RET_FAIL;
    }
    while (nxtitem(&ni, &ship)) {
	if (!player->owner)
	    continue;
	mcp = &mchr[(int)ship.shp_type];
	if (!(mcp->m_flags & M_SONAR))
	    continue;
	getsect(ship.shp_x, ship.shp_y, &sect);
	if (sect.sct_type != SCT_WATER)
	    continue;
	range = (int)techfact(ship.shp_tech, mcp->m_vrnge);
	srange = MIN(7, 7 * range * ship.shp_effic / 200);
	pr("%s at %s efficiency %d%%, max range %d\n",
	   prship(&ship),
	   xyas(ship.shp_x, ship.shp_y, player->cnum),
	   ship.shp_effic, srange);
	snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
	blankfill(radbuf, &ns.range, 1);
	while (nxtsct(&ns, &sect)) {
	    if (player->owner || sect.sct_type == SCT_WATER)
		rad[ns.dy][ns.dx] = dchr[sect.sct_type].d_mnem;
	    else {
		rad[ns.dy][ns.dx] = '?';
	    }
	}
	snxtsct_dist(&ns, ship.shp_x, ship.shp_y, srange);
	cx = deltx(&ns.range, ship.shp_x);
	cy = delty(&ns.range, ship.shp_y);
	while (nxtsct(&ns, &sect)) {
	    if (!line_of_sight(rad, cx, cy, ns.dx, ns.dy)) {
		rad[ns.dy][ns.dx] = ' ';
		continue;
	    }
	    if (ship.shp_tech >= 310 && sect.sct_type == SCT_WATER) {
		if (sect.sct_mines) {
		    pr("Sonar detects %d mines in %s!\n",
		       sect.sct_mines,
		       xyas(sect.sct_x, sect.sct_y, player->cnum));
		    rad[ns.dy][ns.dx] = 'X';
		}
	    }
	    changed |= map_set(player->cnum, sect.sct_x, sect.sct_y,
			       rad[ns.dy][ns.dx], 0);

	}
	memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
	snxtitem_dist(&nit, EF_SHIP, ship.shp_x, ship.shp_y, range);
	while (nxtitem(&nit, &targ)) {
	    if (targ.shp_own == player->cnum || targ.shp_own == 0)
		continue;
	    tmcp = &mchr[(int)targ.shp_type];
	    visib = shp_visib(&targ);
	    pingrange = MIN(7, MAX(visib, 10) * range / 10);
	    vrange = pingrange * ship.shp_effic / 200;
	    dist = mapdist(targ.shp_x, targ.shp_y, ship.shp_x, ship.shp_y);
	    pingrange = (MAX(pingrange, 2) * targ.shp_effic) / 100;
	    if (dist > pingrange)
		continue;
	    if (tmcp->m_flags & M_SONAR && targ.shp_own) {
		natp = getnatp(targ.shp_own);
		if (natp->nat_flags & NF_SONAR)
		    wu(0, targ.shp_own,
		       "Sonar ping from %s detected by %s!\n",
		       xyas(ship.shp_x, ship.shp_y,
			    targ.shp_own), prship(&targ));
		if (targ.shp_rflags & RET_SONARED)
		    retreat_ship(&targ, targ.shp_own, 's');
	    }
	    if (dist > vrange)
		continue;
	    x = deltx(&ns.range, (int)targ.shp_x);
	    y = delty(&ns.range, (int)targ.shp_y);
	    if (rad[y][x] != dchr[SCT_WATER].d_mnem && rad[y][x] != 'X')
		continue;
	    if (tmcp->m_flags & M_SUB &&
		relations_with(targ.shp_own, player->cnum) < FRIENDLY) {
		if (mcp->m_vrnge + visib < 8)
		    pr("Sonar detects sub #%d @ %s\n",
		       targ.shp_uid,
		       xyas(targ.shp_x, targ.shp_y, player->cnum));
		else if (mcp->m_vrnge + visib < 10)
		    pr("Sonar detects %s @ %s\n",
		       prship(&targ),
		       xyas(targ.shp_x, targ.shp_y, player->cnum));
		else
		    pr("Sonar detects %s %s @ %s\n",
		       cname(targ.shp_own), prship(&targ),
		       xyas(targ.shp_x, targ.shp_y, player->cnum));
	    } else
		pr("Sonar detects %s %s @ %s\n",
		   cname(targ.shp_own), prship(&targ),
		   xyas(targ.shp_x, targ.shp_y, player->cnum));

	    if (visib > vis[y][x]) {
		vis[y][x] = visib;
		/* &~0x20 makes it a cap letter */
		rad[y][x] = (*mchr[(int)targ.shp_type].m_name) & ~0x20;
	    }
	}
	if (!player->argp[2]) {
	    rad[cy][cx] = '0';
	    for (row = 0; row < ns.range.height; row++)
		if (!blankrow(rad[row]))
		    pr("%s\n", rad[row]);
	}
	pr("\n");

    }
    if (changed)
	writemap(player->cnum);
    return RET_OK;
}
Пример #10
0
int do_stat(int argc, char**argv){
  if(argc==0){
    fprintf(stderr,"do_stat FILE -win -step -nChr [-r chrName -type [0,1,2]]\n");
    exit(0);
  }
  char *base = *argv;
  char* outnames_bin = append(base,BIN);
  char* outnames_idx = append(base,IDX);
  fprintf(stderr,"\tAssuming binfile:%s and indexfile:%s\n",outnames_bin,outnames_idx);
  
  myMap mm = getMap(outnames_idx);
  writemap(stderr,mm);
  BGZF *fp = bgzf_open(outnames_bin,"r");

  --argc;++argv;
  //  fprintf(stderr,"argc=%d\n",argc);
  int argP =0;
  char *chr=NULL;
  char *outnames = NULL;
  int nChr =0;
  int win =0;
  int step =0;
  int type =0;
  while(argP<argc){
    //   fprintf(stderr,"args=%s\n",argv[argP]);
    if(argP==argc){
      fprintf(stderr,"incomplete arguments list\n");
      exit(0);
    }
    if(strcmp("-r",argv[argP])==0)
      chr = argv[argP+1];
    else if(strcmp("-outnames",argv[argP])==0)
      outnames = argv[argP+1];
    else if(strcmp("-step",argv[argP])==0)
      step = atoi(argv[argP+1]);
    else if(strcmp("-win",argv[argP])==0)
      win = atoi(argv[argP+1]);
    else if(strcmp("-nChr",argv[argP])==0)
      nChr = atoi(argv[argP+1]);
    else if(strcmp("-type",argv[argP])==0)
      type = atoi(argv[argP+1]);
    
    else {
      fprintf(stderr,"Unknown argument:%s\n",argv[argP]);
      exit(0);
    }
    argP +=2;
  }

  fprintf(stderr,"\t -r=%s outnames=%s step: %d win: %d nChr:%d\n",chr,outnames,step,win,nChr);
  if(nChr==0){
    fprintf(stderr,"nChr must be different from zero\n");
    exit(0);
  }
  if(win==0||step==0){
    fprintf(stderr,"\tWinsize equals zero or step size equals zero. Will use entire chromosome as window\n");
    win=step=0;
  }  
  
  if(chr!=NULL){  
    myMap::iterator it = mm.find(chr);
    if(it==mm.end()){
      fprintf(stderr,"\tProblem finding chr: %s in index\n",chr);
      exit(0);
    }
    datum d = it->second;
    bgzf_seek(fp,d.fpos,SEEK_SET);
  }
  if(outnames==NULL)
    outnames = base;

  char *resname = append(outnames,RES);
  FILE *fpres = fopen(resname,"w");
  //fprintf(fpres,"## thetaStat VERSION: %s build:(%s,%s)\n",VERSION,__DATE__,__TIME__);
  fprintf(fpres,"#(indexStart,indexStop)(firstPos_withData,lastPos_withData)(WinStart,WinStop)\t");
  fprintf(fpres,"Chr\tWinCenter\t");
  fprintf(fpres,"tW\ttP\ttF\ttH\ttL\t");
  fprintf(fpres,"Tajima\tfuf\tfud\tfayh\tzeng\tnSites\n");
  while(1){
    perChr pc = getPerChr(fp);
    if(pc.nSites==0)
      break;
    fprintf(stderr,"\tpc.chr=%s pc.nSites=%zu firstpos=%d lastpos=%d\n",pc.chr,pc.nSites,pc.posi[0],pc.posi[pc.nSites-1]);
    kstring_t str = do_stat_main(pc,step,win,nChr,type);
    fwrite(str.s,1,str.l,fpres);//should clean up str, doesn't matter for this program;
    fflush(fpres);
    if(chr!=NULL)
      break;
    dalloc(pc);
  }
  fclose(fpres);
  fprintf(stderr,"\tDumping file: \"%s\"\n",resname);
  return 0;
}
Пример #11
0
/*
 * Draw a radar map for radar at @cx,@cy.
 * @eff is the radar's efficiency, @tlev its tech level, @spy its power.
 * Submarines are detected at fraction @seesub of the range.
 */
void
radmap(int cx, int cy, int eff, double tlev, int spy, double seesub)
{
    int visib, rng;
    struct sctstr sect;
    struct shpstr ship;
    struct plnstr plane;
    struct nstr_sect ns;
    struct nstr_item ni;
    int x, y;
    int row;
    int n;
    int range = rad_range(eff, tlev, spy);
    int changed = 0;

    if (!radbuf)
	radbuf = malloc(WORLD_Y * MAPWIDTH(1));
    if (!visbuf)
	visbuf = malloc(WORLD_Y * MAPWIDTH(1));
    if (!rad) {
	rad = malloc(WORLD_Y * sizeof(char *));
	if (rad && radbuf) {
	    for (x = 0; x < WORLD_Y; x++)
		rad[x] = &radbuf[(WORLD_X + 1) * x];
	}
    }
    if (!vis) {
	vis = malloc(WORLD_Y * sizeof(signed char *));
	if (vis && visbuf) {
	    for (x = 0; x < WORLD_Y; x++)
		vis[x] = &visbuf[(WORLD_X + 1) * x];
	}
    }
    if (!radbuf || !visbuf || !rad || !vis) {
	pr("Memory error in radmap2, tell the deity.\n");
	return;
    }

    memset(visbuf, 0, (WORLD_Y * (WORLD_X + 1)));
    pr("%s efficiency %d%%, max range %d\n",
       xyas(cx, cy, player->cnum), eff, range);
    snxtsct_dist(&ns, cx, cy, range);
    blankfill(radbuf, &ns.range, 1);
    while (nxtsct(&ns, &sect)) {
	rad[ns.dy][ns.dx] = rad_char(&sect, ns.curdist, range,
				     player->cnum);
	changed += map_set(player->cnum, ns.x, ns.y, rad[ns.dy][ns.dx], 0);
    }
    if (changed)
	writemap(player->cnum);
    snxtitem_dist(&ni, EF_PLANE, cx, cy, range);
    while (nxtitem(&ni, &plane)) {
	if (plane.pln_own == 0)
	    continue;
	/* Used to have 'ghosts' when scanning whole world --ts */
	x = deltx(&ns.range, (int)plane.pln_x);
	y = delty(&ns.range, (int)plane.pln_y);

	if (pln_is_in_orbit(&plane) && plane.pln_own != player->cnum) {
	    vis[y][x] = 100;
	    rad[y][x] = '$';
	}
    }
    snxtitem_dist(&ni, EF_SHIP, cx, cy, range);
    while (nxtitem(&ni, &ship)) {
	if (ship.shp_own == 0)
	    continue;
	/* Used to have 'ghosts' when scanning whole world --ts */
	x = deltx(&ns.range, (int)ship.shp_x);
	y = delty(&ns.range, (int)ship.shp_y);

	visib = shp_visib(&ship);
	rng = (int)(range * visib / 20.0);
	if (ni.curdist > rng)
	    continue;
	if ((mchr[(int)ship.shp_type].m_flags & M_SUB) &&
	    ni.curdist > rng * seesub)
	    continue;
	if (visib > vis[y][x]) {
	    vis[y][x] = visib;
	    /* &~0x20 makes it a cap letter */
	    rad[y][x] = (*mchr[(int)ship.shp_type].m_name) & ~0x20;
	}
    }
    /*
     * make the center of the display 0
     * so ve et al can find it.
     */
    rad[delty(&ns.range, cy)][deltx(&ns.range, cx)] = '0';

    n = ns.range.height;
    for (row = 0; row < n; row++)
	pr("%s\n", rad[row]);
    pr("\n");
}
Пример #12
0
void localservertoclient(uchar *buf, int len)   // processes any updates from the server
{
	if(ENET_NET_TO_HOST_16(*(ushort *)buf)!=len) neterr("packet length");
	incomingdemodata(buf, len);

	uchar *end = buf+len;
	uchar *p = buf+2;
	char text[MAXTRANS];
	int cn = -1, type;
	dynent *d = NULL;
	bool mapchanged = false;

	while(p<end) switch(type = getint(p))
	{
		case SV_INITS2C:                    // welcome messsage from the server
			{
				cn = getint(p);
				int prot = getint(p);
				if(prot!=PROTOCOL_VERSION)
				{
					conoutf("you are using a different game protocol (you: %d, server: %d)", PROTOCOL_VERSION, prot);
					disconnect();
					return;
				};
				toservermap[0] = 0;
				clientnum = cn;                 // we are now fully connected
				if(!getint(p)) strcpy_s(toservermap, getclientmap());   // we are the first client on this server, set map
				sgetstr();
				if(text[0] && strcmp(text, clientpassword))
				{
					conoutf("you need to set the correct password to join this server!");
					disconnect();
					return;
				};
				if(getint(p)==1)
				{
					conoutf("server is FULL, disconnecting..");
				};
				break;
			};

		case SV_POS:                        // position of another client
			{
				cn = getint(p);
				d = getclient(cn);
				if(!d) return;
				d->o.x   = getint(p)/DMF;
				d->o.y   = getint(p)/DMF;
				d->o.z   = getint(p)/DMF;
				d->yaw   = getint(p)/DAF;
				d->pitch = getint(p)/DAF;
				d->roll  = getint(p)/DAF;
				d->vel.x = getint(p)/DVF;
				d->vel.y = getint(p)/DVF;
				d->vel.z = getint(p)/DVF;
				int f = getint(p);
				d->strafe = (f&3)==3 ? -1 : f&3;
				f >>= 2;
				d->move = (f&3)==3 ? -1 : f&3;
				d->onfloor = (f>>2)&1;
				int state = f>>3;
				if(state==CS_DEAD && d->state!=CS_DEAD) d->lastaction = lastmillis;
				d->state = state;
				if(!demoplayback) updatepos(d);
				break;
			};

		case SV_SOUND:
			playsound(getint(p), &d->o);
			break;

		case SV_TEXT:
			sgetstr();
			conoutf("%s:\f %s", d->name, text);
			break;

		case SV_MAPCHANGE:
			sgetstr();
			changemapserv(text, getint(p));
			mapchanged = true;
			break;

		case SV_ITEMLIST:
			{
				int n;
				if(mapchanged) { senditemstoserver = false; resetspawns(); };
				while((n = getint(p))!=-1) if(mapchanged) setspawn(n, true);
				break;
			};

		case SV_MAPRELOAD:          // server requests next map
			{
				getint(p);
				sprintf_sd(nextmapalias)("nextmap_%s", getclientmap());
				char *map = getalias(nextmapalias);     // look up map in the cycle
				changemap(map ? map : getclientmap());
				break;
			};

		case SV_INITC2S:            // another client either connected or changed name/team
			{
				sgetstr();
				if(d->name[0])          // already connected
				{
					if(strcmp(d->name, text))
						conoutf("%s is now known as %s", d->name, text);
				}
				else                    // new client
				{
					c2sinit = false;    // send new players my info again
					conoutf("connected: %s", text);
				};
				strcpy_s(d->name, text);
				sgetstr();
				strcpy_s(d->team, text);
				d->lifesequence = getint(p);
				break;
			};

		case SV_CDIS:
			cn = getint(p);
			if(!(d = getclient(cn))) break;
			conoutf("player %s disconnected", d->name[0] ? d->name : "[incompatible client]");
			zapdynent(players[cn]);
			break;

		case SV_SHOT:
			{
				int gun = getint(p);
				vec s, e;
				s.x = getint(p)/DMF;
				s.y = getint(p)/DMF;
				s.z = getint(p)/DMF;
				e.x = getint(p)/DMF;
				e.y = getint(p)/DMF;
				e.z = getint(p)/DMF;
				if(gun==GUN_SG) createrays(s, e);
				shootv(gun, s, e, d);
				break;
			};

		case SV_DAMAGE:
			{
				int target = getint(p);
				int damage = getint(p);
				int ls = getint(p);
				if(target==clientnum) { if(ls==player1->lifesequence) selfdamage(damage, cn, d); }
				else playsound(S_PAIN1+rnd(5), &getclient(target)->o);
				break;
			};

		case SV_DIED:
			{
				int actor = getint(p);
				if(actor==cn)
				{
					conoutf("%s suicided", d->name);
				}
				else if(actor==clientnum)
				{
					int frags;
					if(isteam(player1->team, d->team))
					{
						frags = -1;
						conoutf("you fragged a teammate (%s)", d->name);
					}
					else
					{
						frags = 1;
						conoutf("you fragged %s", d->name);
					};
					addmsg(1, 2, SV_FRAGS, player1->frags += frags);
				}
				else
				{
					dynent *a = getclient(actor);
					if(a)
					{
						if(isteam(a->team, d->name))
						{
							conoutf("%s fragged his teammate (%s)", a->name, d->name);
						}
						else
						{
							conoutf("%s fragged %s", a->name, d->name);
						};
					};
				};
				playsound(S_DIE1+rnd(2), &d->o);
				d->lifesequence++;
				break;
			};

		case SV_FRAGS:
			players[cn]->frags = getint(p);
			break;

		case SV_ITEMPICKUP:
			setspawn(getint(p), false);
			getint(p);
			break;

		case SV_ITEMSPAWN:
			{
				uint i = getint(p);
				setspawn(i, true);
				if(i>=(uint)ents.length()) break;
				vec v = { ents[i].x, ents[i].y, ents[i].z };
				playsound(S_ITEMSPAWN, &v);
				break;
			};

		case SV_ITEMACC:            // server acknowledges that I picked up this item
			realpickup(getint(p), player1);
			break;

		case SV_PING:
			getint(p);
			break;

		case SV_PONG:
			addmsg(0, 2, SV_CLIENTPING, player1->ping = (player1->ping*5+lastmillis-getint(p))/6);
			break;

		case SV_CLIENTPING:
			players[cn]->ping = getint(p);
			break;

		case SV_GAMEMODE:
			nextmode = getint(p);
			break;

		case SV_TIMEUP:
			timeupdate(getint(p));
			break;

		case SV_RECVMAP:
			{
				sgetstr();
				conoutf("received map \"%s\" from server, reloading..", text);
				int mapsize = getint(p);
				writemap(text, mapsize, p);
				p += mapsize;
				changemapserv(text, gamemode);
				break;
			};

		case SV_SERVMSG:
			sgetstr();
			conoutf("%s", text);
			break;

		case SV_EXT:        // so we can messages without breaking previous clients/servers, if necessary
			{
				for(int n = getint(p); n; n--) getint(p);
				break;
			};

		default:
			neterr("type");
			return;
	};
};
Пример #13
0
int
desi(void)
{
    int n;
    char *p;
    int des;
    struct nstr_sect nstr;
    struct sctstr sect;
    struct natstr *natp;
    char prompt[128];
    char buf[1024];
    int changed = 0;
    int rc = RET_OK;

    if (!snxtsct(&nstr, player->argp[1]))
	return RET_SYN;
    natp = getnatp(player->cnum);
    while (nxtsct(&nstr, &sect)) {
	if (!player->owner)
	    continue;
	if (!player->god && dchr[sect.sct_type].d_cost < 0)
	    continue;
	sprintf(prompt, "%s %d%% %s  desig? ",
		xyas(sect.sct_x, sect.sct_y, player->cnum),
		sect.sct_effic, dchr[sect.sct_type].d_name);
	if (!(p = getstarg(player->argp[2], prompt, buf))) {
	    rc = RET_FAIL;
	    break;
	}

	if (!check_sect_ok(&sect))
	    continue;

	des = sct_typematch(p);
	if (des < 0) {
	    pr("No such designation\n"
	       "See \"info Sector-types\" for possible designations\n");
	    rc = RET_FAIL;
	    break;
	}
	if (!player->god) {
	    if (des == SCT_WASTE) {
		pr("Only a nuclear device (or %s) can make a %s!\n",
		   cname(0), dchr[des].d_name);
		rc = RET_FAIL;
		break;
	    }
	    if (dchr[des].d_cost < 0) {
		pr("Only %s can designate a %s!\n",
		   cname(0), dchr[des].d_name);
		rc = RET_FAIL;
		break;
	    }
	    if (dchr[des].d_terrain != dchr[sect.sct_type].d_terrain) {
		pr("You can't change a %s into a %s\n",
		   dchr[sect.sct_type].d_name, dchr[des].d_name);
		continue;
	    }
	}
	if (sect.sct_type == des && sect.sct_newtype == des)
	    continue;
	if ((des == SCT_HARBR || des == SCT_BHEAD) && !sect.sct_coastal) {
	    pr("%s does not border on water.\n",
	       xyas(nstr.x, nstr.y, player->cnum));
	    if (player->god)
		pr("But if it's what you want ...\n");
	    else
		continue;
	}
	if (sect.sct_type == SCT_SANCT && !player->god)
	    continue;
	n = sect.sct_type;
	if ((sect.sct_newtype != des) && (sect.sct_type != des)
	    && dchr[des].d_cost > 0) {
	    if (natp->nat_money < player->dolcost + dchr[des].d_cost) {
		pr("You can't afford a %s!\n", dchr[des].d_name);
		rc = RET_FAIL;
		break;
	    }
	    player->dolcost += dchr[des].d_cost;
	}
	if (sect.sct_type != des && (sect.sct_effic < 5 || player->god)) {
	    if (player->god)
		set_coastal(&sect, sect.sct_type, des);
	    sect.sct_type = des;
	    sect.sct_effic = 0;
	    changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
			       dchr[des].d_mnem, 0);
	}
	sect.sct_newtype = des;
	putsect(&sect);
	if (!player->god
	    && sect.sct_x == natp->nat_xcap && sect.sct_y == natp->nat_ycap
	    && des != SCT_CAPIT && des != SCT_SANCT && des != SCT_MOUNT)
	    pr("You have redesignated your capital!\n");
	if (opt_EASY_BRIDGES == 0) {	/* may cause a bridge fall */
	    if (n != SCT_BHEAD)
		continue;
	    bridgefall(&sect);
	}
    }
    if (changed)
	writemap(player->cnum);
    return rc;
}
Пример #14
0
int main(int argc, char **argv) {
  char *gidmap = NULL, *inside = NULL, *outside = NULL, *uidmap = NULL;
  char *bind = NULL;
  int hostnet = 0, master, option, stdio = 0;
  pid_t child, parent;

  while ((option = getopt(argc, argv, "+:b:cg:i:no:u:")) > 0)
    switch (option) {
      case 'b':
        bind = optarg;
        break;
      case 'c':
        stdio++;
        break;
      case 'g':
        gidmap = optarg;
        break;
      case 'i':
        inside = optarg;
        break;
      case 'n':
        hostnet++;
        break;
      case 'o':
        outside = optarg;
        break;
      case 'u':
        uidmap = optarg;
        break;
      default:
        usage(argv[0]);
    }

  if (argc <= optind)
    usage(argv[0]);

  parent = getpid();
  switch (child = fork()) {
    case -1:
      error(1, errno, "fork");
    case 0:
      raise(SIGSTOP);
//      if (geteuid() != 0)
//        denysetgroups(parent);
      writemap(parent, GID, gidmap);
      writemap(parent, UID, uidmap);

      if (outside) {
        if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
          error(1, 0, "Failed to drop privileges");
        execlp(SHELL, SHELL, "-c", outside, NULL);
        error(1, errno, "exec %s", outside);
      }

      exit(EXIT_SUCCESS);
  }

  if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
    error(1, 0, "Failed to drop privileges");

  if (unshare(CLONE_NEWIPC | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWUTS) < 0)
    error(1, 0, "Failed to unshare namespaces");

  if (!hostnet && unshare(CLONE_NEWNET) < 0)
      error(1, 0, "Failed to unshare network namespace");

  waitforstop(child);
  kill(child, SIGCONT);
  waitforexit(child);

  setgid(0);
  setgroups(0, NULL);
  setuid(0);

  master = stdio ? -1 : getconsole();
  createroot(argv[optind], master, inside, bind);

  unshare(CLONE_NEWPID);
  switch (child = fork()) {
    case -1:
      error(1, errno, "fork");
    case 0:
      mountproc();
      if (!hostnet)
        mountsys();
      enterroot();

      if (master >= 0) {
        close(master);
        setconsole("/dev/console");
      }

      clearenv();
      putenv("container=contain");

      if (argv[optind + 1])
        execv(argv[optind + 1], argv + optind + 1);
      else
        execl(SHELL, SHELL, NULL);
      error(1, errno, "exec");
  }

  return supervise(child, master);
}
Пример #15
0
int main(int argc, char **argv)
{
    int i;
    char buf[12];
    const char *arg, *orders = 0, *cfgfile = 0;

    rnd_seed((unsigned int) time(0));

    puts("Atlantis v1.0 " __DATE__ "\n"
         "Copyright 1993 by Russell Wallace.\n"
         "Type ? for list of commands.");

    turn = -1;
    for (i = 1; i != argc; ++i) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
            case 'c':
                cfgfile = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
                break;
            case 'i':
                ignore_password = 1;
                break;
            case 'p': /* process */
                orders = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
                break;
            case 't': /* turn */
                arg = (argv[i][2]) ? (argv[i] + 2) : argv[++i];
                turn = atoi(arg);
                break;
            default:
                fprintf(stderr, "invalid argument %d: '%s'\n", i, argv[i]);
                return -1;
            }
        }
    }

    if (cfgfile) {
        FILE * F = fopen(cfgfile, "r");
        if (F) {
            cJSON *json;
            char *data;
            size_t len;
            fseek(F, 0,SEEK_END);
            len = ftell(F);
            fseek(F,0,SEEK_SET);
            data = (char *)malloc(len+1);
            if (data) {
                fread(data,1,len,F);
            }
            json = cJSON_Parse(data);
            if (json) {
                read_config_json(json);
                cJSON_Delete(json);
            } else {
                fprintf(stderr, "could not parse configuration file '%s'\n", cfgfile);
            }
            free(data);
        } else {
            fprintf(stderr, "could not open configuration file '%s'\n", cfgfile);
            return errno ? errno : -1;
        }
    }
    initgame();
    if (orders) {
        return processturn(orders);
    }

    for (;;) {
        printf("> ");
        fgets(buf, sizeof(buf), stdin);

        switch (tolower(buf[0])) {
        case 'm':
            writemap(stdout);
            break;

        case 'g':
            turn = 0;
            cleargame(false);
            autoworld("players");
            writemap(stdout);
            break;

        case 'r':
            reports();
            break;
        case 'f':
            fixme();
            break;

        case 'w':
            writesummary();
            writegame();
            break;

        case 'p':
            printf("Name of orders file? ");
            fgets(buf, sizeof(buf), stdin);
            if (!buf[0]) return -1;
            return processturn(buf);

        case 'q':
            return 0;

        default:
            puts("C - Create New Continent.\n"
                 "A - Add New Players.\n"
                 "M - Draw Map.\n"
                 "P - Process Game Turn.\n"
                 "R - Write Reports.\n"
                 "G - Generate New World.\n"
                 "Q - Quit.\n"
                 "W - Write Game.\n");
        }
    }
}
Пример #16
0
void localservertoclient(uchar *buf, int len) // processes any updates from the server
{
	if (ENET_NET_TO_HOST_16(*(ushort *)buf) != len)
		neterr("packet length");
	incomingdemodata(buf, len);

	uchar *end = buf + len;
	uchar *p = buf + 2;
	char text[MAXTRANS];
	int cn = -1, type;
	Sprite *spr = NULL;
	bool mapchanged = false;

	while (p < end)
		switch (type = getint(p)) {
		case SV_INITS2C:                    // welcome messsage from the server
		{
			cn = getint(p);
			int prot = getint(p);
			if (prot != PROTOCOL_VERSION) {
				conoutf( "you are using a different game protocol (you: %d, server: %d)", PROTOCOL_VERSION, prot);
				disconnect();
				return;
			};
			toservermap = "";
			clientnum = cn;                 // we are now fully connected
			if (!getint(p)) {
				toservermap = getclientmap(); // we are the first client on this server, set map
			}
			sgetstr();

			if (text[0] && strcmp(text, clientpassword)) {
				conoutf( "you need to set the correct password to join this server!");
				disconnect();
				return;
			};
			if (getint(p) == 1) {
				conoutf("server is FULL, disconnecting..");
			};
			break;
		}
		case SV_POS:                        // position of another client
		{
			cn = getint(p);
			spr = getclient(cn);
			if (!spr)
				return;
			spr->o.x = getint(p) / DMF;
			spr->o.y = getint(p) / DMF;
			spr->o.z = getint(p) / DMF;
			spr->yaw = getint(p) / DAF;
			spr->pitch = getint(p) / DAF;
			spr->roll = getint(p) / DAF;
			spr->vel.x = getint(p) / DVF;
			spr->vel.y = getint(p) / DVF;
			spr->vel.z = getint(p) / DVF;
			int f = getint(p);
			spr->strafe = (f & 3) == 3 ? -1 : f & 3;
			f >>= 2;
			spr->move = (f & 3) == 3 ? -1 : f & 3;
			spr->onfloor = (f >> 2) & 1;
			int state = f >> 3;
			if (state == CS_DEAD && spr->state != CS_DEAD)
				spr->lastaction = lastmillis;
			spr->state = state;
			if (!demoplayback)
				updatepos(spr);
			break;
		}
		case SV_SOUND:
			playsound(getint(p), &spr->o);
			break;
		case SV_TEXT:
			sgetstr();
			conoutf("%s:\f %s", spr->name, text);
			break;
		case SV_MAPCHANGE:
			sgetstr();
			changemapserv(text, getint(p));
			mapchanged = true;
			break;
		case SV_ITEMLIST: {
			int n;
			if (mapchanged) {
				senditemstoserver = false;
				resetspawns();
			};
			while ((n = getint(p)) != -1) {
				if (mapchanged)
					setspawn(n, true);
			}
			break;
		}

		case SV_MAPRELOAD:          // server requests next map
		{
			getint(p);
			std::string nextmapalias = std::string("nextmap_") + getclientmap();
			std::string map = getalias(nextmapalias);     // look up map in the cycle
			changemap(map.empty() ? getclientmap() : map);
			break;
		}

		case SV_INITC2S: // another client either connected or changed name/team
		{
			sgetstr();

			if (spr->name[0]) {         // already connected
				if (strcmp(spr->name, text))
					conoutf("%s is now known as %s", spr->name, text);
			} else {                   // new client
				c2sinit = false;    // send new players my info again 
				conoutf("connected: %s", text);
			};
			strcpy_s(spr->name, text);
			sgetstr();

			strcpy_s(spr->team, text);
			spr->lifesequence = getint(p);
			break;
		}

		case SV_CDIS:
			cn = getint(p);
			if (!(spr = getclient(cn)))
				break;
			conoutf("player %s disconnected", spr->name[0] ? spr->name : "[incompatible client]");
			zapSprite(players[cn]);
			break;

		case SV_SHOT: {
			int gun = getint(p);
			Vec3 s, e;
			s.x = getint(p) / DMF;
			s.y = getint(p) / DMF;
			s.z = getint(p) / DMF;
			e.x = getint(p) / DMF;
			e.y = getint(p) / DMF;
			e.z = getint(p) / DMF;
			if (gun == GUN_SG)
				createrays(s, e);
			shootv(gun, s, e, spr);
			break;
		}

		case SV_DAMAGE: {
			int target = getint(p);
			int damage = getint(p);
			int ls = getint(p);
			if (target == clientnum) {
				if (ls == player1->lifesequence)
					selfdamage(damage, cn, spr);
			} else
				playsound(S_PAIN1 + rnd(5), &getclient(target)->o);
			break;
		}

		case SV_DIED: {
			int actor = getint(p);
			if (actor == cn) {
				conoutf("%s suicided", spr->name);
			} else if (actor == clientnum) {
				int frags;
				if (isteam(player1->team, spr->team)) {
					frags = -1;
					conoutf("you fragged a teammate (%s)", spr->name);
				} else {
					frags = 1;
					conoutf("you fragged %s", spr->name);
				};
				addmsg(1, 2, SV_FRAGS, player1->frags += frags);
			} else {
				Sprite *a = getclient(actor);
				if (a) {
					if (isteam(a->team, spr->name)) {
						conoutf("%s fragged his teammate (%s)", a->name, spr->name);
					} else {
						conoutf("%s fragged %s", a->name, spr->name);
					};
				};
			};
			playsound(S_DIE1 + rnd(2), &spr->o);
			spr->lifesequence++;
			break;
		}
		case SV_FRAGS:
			players[cn]->frags = getint(p);
			break;
		case SV_ITEMPICKUP:
			setspawn(getint(p), false);
			getint(p);
			break;
		case SV_ITEMSPAWN: {
			int i = getint(p);
			setspawn(i, true);
			if (i >= entityList.size())
				break;
			Vec3 v = { entityList[i].x, entityList[i].y, entityList[i].z };
			playsound(S_ITEMSPAWN, &v);
			break;
		}
		case SV_ITEMACC:       // server acknowledges that I picked up this item
			realpickup(getint(p), player1);
			break;
		case SV_EDITH: // coop editing messages, should be extended to include all possible editing ops
		case SV_EDITT:
		case SV_EDITS:
		case SV_EDITD:
		case SV_EDITE: {
			int x = getint(p);
			int y = getint(p);
			int xs = getint(p);
			int ys = getint(p);
			int v = getint(p);
			Rect b = { x, y, xs, ys };
			switch (type) {
			case SV_EDITH:
				editheightxy(v != 0, getint(p), b);
				break;
			case SV_EDITT:
				edittexxy(v, getint(p), b);
				break;
			case SV_EDITS:
				edittypexy(v, b);
				break;
			case SV_EDITD:
				setvdeltaxy(v, b);
				break;
			case SV_EDITE:
				editequalisexy(v != 0, b);
				break;
			};
			break;
		}

		case SV_EDITENT:            // coop edit of ent
		{
			int i = getint(p);
			while (entityList.size() <= i) {
				entityList.emplace_back(Entity());
				entityList.back().type = NOTUSED;
			}
			int to = entityList[i].type;
			entityList[i].type = getint(p);
			entityList[i].x = getint(p);
			entityList[i].y = getint(p);
			entityList[i].z = getint(p);
			entityList[i].attr1 = getint(p);
			entityList[i].attr2 = getint(p);
			entityList[i].attr3 = getint(p);
			entityList[i].attr4 = getint(p);
			entityList[i].spawned = false;
			if (entityList[i].type == LIGHT || to == LIGHT)
				calclight();
			break;
		}
		case SV_PING:
			getint(p);
			break;
		case SV_PONG:
			addmsg(0, 2, SV_CLIENTPING, player1->ping = (player1->ping * 5 + lastmillis - getint(p)) / 6);
			break;
		case SV_CLIENTPING:
			players[cn]->ping = getint(p);
			break;
		case SV_GAMEMODE:
			nextmode = getint(p);
			break;
		case SV_TIMEUP:
			timeupdate(getint(p));
			break;
		case SV_RECVMAP: {
			sgetstr();

			conoutf("received map \"%s\" from server, reloading..", text);
			int mapsize = getint(p);
			writemap(text, mapsize, p);
			p += mapsize;
			changemapserv(text, gamemode);
			break;
		}
		case SV_SERVMSG:
			sgetstr();
			conoutf("%s", text);
			break;
		case SV_EXT: // so we can messages without breaking previous clients/servers, if necessary
		{
			for (int n = getint(p); n; n--)
				getint(p);
			break;
		}
		default:
			neterr("type");
			return;
		}
}