示例#1
0
文件: otto.c 项目: a565109863/src
static void
face_and_move_direction(int rel_dir, int distance)
{
	int	old_facing;
	char	cmd;

	old_facing = facing;
	cmd = DIRKEYS[facing = direction(facing, rel_dir)];

	if (rel_dir != FRONT) {
		int	i;
		struct	item	items[NUMDIRECTIONS];

		command[comlen++] = toupper((unsigned char)cmd);
		if (distance == 0) {
			/* rotate ottolook's to be in right position */
			for (i = 0; i < NUMDIRECTIONS; i++)
				items[i] =
					flbr[(i + old_facing) % NUMDIRECTIONS];
			memcpy(flbr, items, sizeof flbr);
		}
	}
	while (distance--) {
		command[comlen++] = cmd;
		switch (facing) {

		case NORTH:	row--; break;
		case WEST:	col--; break;
		case SOUTH:	row++; break;
		case EAST:	col++; break;
		}
		if (distance == 0)
			look_around();
	}
}
示例#2
0
文件: otto.c 项目: a565109863/src
int
otto(int y, int x, char face, char *buf, size_t buflen)
{
	int		i;

	if (usleep(Otto_pause) < 0)
		panic("usleep");

	/* save away parameters so other functions may use/update info */
	switch (face) {
	case '^':	facing = NORTH; break;
	case '<':	facing = WEST; break;
	case 'v':	facing = SOUTH; break;
	case '>':	facing = EAST; break;
	default:	panic("unknown face");
	}
	row = y; col = x;
	been_there[row][col] |= 1 << facing;

	/* initially no commands to be sent */
	comlen = 0;

	/* find something to do */
	look_around();
	for (i = 0; i < NUMDIRECTIONS; i++) {
		if (strchr(OPPONENT, flbr[i].what) != NULL) {
			attack(i, &flbr[i]);
			memset(been_there, 0, sizeof been_there);
			goto done;
		}
	}

	if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) {
		duck(BACK);
		memset(been_there, 0, sizeof been_there);
	} else if (go_for_ammo(BOOT_PAIR)) {
		memset(been_there, 0, sizeof been_there);
	} else if (go_for_ammo(BOOT)) {
		memset(been_there, 0, sizeof been_there);
	} else if (go_for_ammo(GMINE))
		memset(been_there, 0, sizeof been_there);
	else if (go_for_ammo(MINE))
		memset(been_there, 0, sizeof been_there);
	else
		wander();

done:
	if (comlen) {
		if (comlen > buflen)
			panic("not enough buffer space");
		memcpy(buf, command, comlen);
	}
	return comlen;
}
示例#3
0
void game::wishmonster( const tripoint &p )
{
    std::vector<const mtype*> mtypes;

    uimenu wmenu;
    wmenu.w_x = 0;
    wmenu.w_width = TERMX;
    // disabled due to foldstring crash //( TERMX - getmaxx(w_terrain) - 30 > 24 ? getmaxx(w_terrain) : TERMX );
    wmenu.pad_right = ( wmenu.w_width - 30 );
    wmenu.return_invalid = true;
    wmenu.selected = uistate.wishmonster_selected;
    wish_monster_callback *cb = new wish_monster_callback( mtypes );
    wmenu.callback = cb;

    int i = 0;
    for( const auto &montype : MonsterGenerator::generator().get_all_mtypes() ) {
        wmenu.addentry( i, true, 0, "%s", montype->nname().c_str() );
        wmenu.entries[i].extratxt.txt = montype->sym;
        wmenu.entries[i].extratxt.color = montype->color;
        wmenu.entries[i].extratxt.left = 1;
        ++i;
        mtypes.push_back( montype );
    }

    do {
        wmenu.query();
        if ( wmenu.ret >= 0 ) {
            monster mon = monster( mtypes[ wmenu.ret ]->id );
            if (cb->friendly) {
                mon.friendly = -1;
            }
            if (cb->hallucination) {
                mon.hallucination = true;
            }
            tripoint spawn = ( p == tripoint_min ? look_around() : p );
            if( spawn != tripoint_min ) {
                std::vector<tripoint> spawn_points = closest_tripoints_first( cb->group, spawn );
                for( auto spawn_point : spawn_points ) {
                    mon.spawn( spawn_point );
                    add_zombie(mon, true);
                }
                cb->msg = _("Monster spawned, choose another or 'q' to quit.");
                uistate.wishmonster_selected = wmenu.ret;
                wmenu.redraw();
            }
        }
    } while ( wmenu.keypress != 'q' && wmenu.keypress != KEY_ESCAPE && wmenu.keypress != ' ' );
    delete cb;
    cb = NULL;
    return;
}
示例#4
0
void game::wishmonster(int x, int y)
{
    const std::map<std::string, mtype *> montypes = MonsterGenerator::generator().get_all_mtypes();

    uimenu wmenu;
    wmenu.w_x = 0;
    wmenu.w_width = TERMX;
    // disabled due to foldstring crash //( TERMX - getmaxx(w_terrain) - 30 > 24 ? getmaxx(w_terrain) : TERMX );
    wmenu.pad_right = ( wmenu.w_width - 30 );
    wmenu.return_invalid = true;
    wmenu.selected = uistate.wishmonster_selected;
    wish_monster_callback *cb = new wish_monster_callback();
    wmenu.callback = cb;

    int i = 0;
    for (std::map<std::string, mtype *>::const_iterator mon = montypes.begin();
         mon != montypes.end(); ++mon) {
        wmenu.addentry( i, true, 0, "%s", mon->second->nname().c_str() );
        wmenu.entries[i].extratxt.txt = mon->second->sym;
        wmenu.entries[i].extratxt.color = mon->second->color;
        wmenu.entries[i].extratxt.left = 1;
        ++i;
    }

    do {
        wmenu.query();
        if ( wmenu.ret >= 0 ) {
            monster mon = monster(GetMType(wmenu.ret));
            if (cb->friendly) {
                mon.friendly = -1;
            }
            point spawn = ( x == -1 && y == -1 ? look_around() : point ( x, y ) );
            if (spawn.x != -1) {
                std::vector<point> spawn_points = closest_points_first( cb->group, spawn );
                for( auto spawn_point : spawn_points ) {
                    mon.spawn(spawn_point.x, spawn_point.y);
                    add_zombie(mon);
                }
                cb->msg = _("Monster spawned, choose another or 'q' to quit.");
                uistate.wishmonster_selected = wmenu.ret;
                wmenu.redraw();
            }
        }
    } while ( wmenu.keypress != 'q' && wmenu.keypress != KEY_ESCAPE && wmenu.keypress != ' ' );
    delete cb;
    cb = NULL;
    return;
}
off_t * split(const char * filename, int16_t num_parts){
	int cur_part=0;
	off_t * slices = (off_t *)malloc(num_parts*2*sizeof(off_t));
	off_t file_size = get_filesize(filename);
	off_t slice_size = file_size / num_parts;
	FILE *fp = fopen(filename, "r");
	slices[0] = 0;
	for(cur_part=1; cur_part < num_parts; cur_part++){
		off_t pos_init_seq = look_around(fp, slice_size * cur_part);
		// Set end position of previous slice
		slices[2*cur_part - 1] = pos_init_seq - 1;
		// Set start position of current slice
		slices[2*cur_part] = pos_init_seq;
	}
	slices[2*num_parts - 1] = file_size;
	fclose(fp);
	return slices;
}
示例#6
0
void game::monster_wish()
{
    WINDOW* w_list = newwin(25, 30, 0,  0);
    WINDOW* w_info = newwin(25, 50, 0, 30);
    int a = 0, shift = 1, result_selected = 0;
    int ch = '.';
    bool search = false, friendly = false;
    std::string pattern;
    std::string info;
    std::vector<int> search_results;
    monster tmp;
    do {
        werase(w_info);
        werase(w_list);
        mvwprintw(w_list, 0, 0, _("Spawn a: "));
        if (search) {
            if (ch == '\n') {
                search = false;
                ch = '.';
            } else if (ch == KEY_BACKSPACE || ch == 127) {
                if (pattern.length() > 0)
                    pattern.erase(pattern.end() - 1);
            } else if (ch == '>' || ch == KEY_NPAGE) {
                search = false;
                if (!search_results.empty()) {
                    result_selected++;
                    if (result_selected > search_results.size())
                        result_selected = 0;
                    shift = search_results[result_selected];
                    a = 0;
                    if (shift + 23 > mtypes.size()) {
                        a = shift + 23 - mtypes.size();
                        shift = mtypes.size() - 23;
                    }
                }
            } else if (ch == '<' || ch == KEY_PPAGE) {
                search = false;
                if (!search_results.empty()) {
                    result_selected--;
                    if (result_selected < 0)
                        result_selected = search_results.size() - 1;
                    shift = search_results[result_selected];
                    a = 0;
                    if (shift + 23 > mtypes.size()) {
                        a = shift + 23 - mtypes.size();
                        shift = mtypes.size() - 23;
                    }
                }
            } else {
                pattern += ch;
                search_results.clear();
            }

            if (search) {
                for (int i = 1; i < mtypes.size(); i++) {
                    if (mtypes[i]->name.find(pattern) != std::string::npos) {
                        shift = i;
                        a = 0;
                        result_selected = 0;
                        if (shift + 23 > mtypes.size()) {
                            a = shift + 23 - mtypes.size();
                            shift = mtypes.size() - 23;
                        }
                        search_results.push_back(i);
                    }
                }
            }

        } else {	// Not searching; scroll by keys
            if (ch == 'j') a++;
            if (ch == 'k') a--;
            if (ch == 'f') friendly = !friendly;
            if (ch == '/') {
                search = true;
                pattern =  "";
                search_results.clear();
            }
            if (( ch == '>' || ch == KEY_NPAGE ) && !search_results.empty()) {
                result_selected++;
                if (result_selected > search_results.size())
                    result_selected = 0;
                shift = search_results[result_selected];
                a = 0;
                if (shift + 23 > mtypes.size()) {
                    a = shift + 23 - mtypes.size();
                    shift = mtypes.size() - 23;
                }
            } else if (( ch == '<' || ch == KEY_PPAGE ) && !search_results.empty()) {
                result_selected--;
                if (result_selected < 0)
                    result_selected = search_results.size() - 1;
                shift = search_results[result_selected];
                a = 0;
                if (shift + 23 > mtypes.size()) {
                    a = shift + 23 - mtypes.size();
                    shift = mtypes.size() - 23;
                }
            }
        }
        if (!search_results.empty())
            mvwprintz(w_list, 0, 11, c_green, "%s               ", pattern.c_str());
        else if (pattern.length() > 0)
            mvwprintz(w_list, 0, 11, c_red, _("%s not found!            "),pattern.c_str());
        if (a < 0) {
            a = 0;
            shift--;
            if (shift < 1) shift = 1;
        }
        if (a > 22) {
            a = 22;
            shift++;
            if (shift + 23 > mtypes.size()) shift = mtypes.size() - 23;
        }
        for (int i = 1; i < 24; i++) {
            nc_color col = c_white;
            if (i == a + 1)
                col = h_white;
            mvwprintz(w_list, i, 0, col, mtypes[i-1+shift]->name.c_str());
            wprintz(w_list, mtypes[i-1+shift]->color, " %c%", mtypes[i-1+shift]->sym);
        }
        tmp = monster(mtypes[a + shift]);
        if (friendly)
            tmp.friendly = -1;
        tmp.print_info(this, w_info);
        wrefresh(w_info);
        wrefresh(w_list);
        if (search)
            ch = getch();
        else
            ch = input();
    } while (ch != '\n');
    clear();
    delwin(w_info);
    delwin(w_list);
    refresh_all();
    wrefresh(w_terrain);
    point spawn = look_around();
    if (spawn.x == -1)
        return;
    tmp.spawn(spawn.x, spawn.y);
    z.push_back(tmp);
}
示例#7
0
void process_AI(void) {

	printf("processing AI ... %i \n", temeraire_state.state);

	switch(temeraire_state.state) {

		case NOTHING : 
			printf("State doing nothing \n");
			break;

		case CHECKING_ENV :
			       switch(check_sensors()) {
				       case 0 :
					       // Nothing
						printf("Rien a faire \n");
					       break;
				       case 1 :
					       speed_saveZ = TravelLengthZ;
						TravelLengthZ = 0;
					       look_around();
					       temeraire_state.state = AVOIDING_WALL_FRONT;
						printf("Go to avoiding wall front \n");
						system("espeak -a 200 \"Wall detected in front\" &");
					       //find_where_to_turn(1);
					       //gettimeofday( &timebeforenextcheck, NULL );
					       break;
				       case 2 : // front item > go left
					       speed_saveZ = TravelLengthZ;
						TravelLengthX = - TravelLengthZ;
					       temeraire_state.state = AVOIDING_ITEM_LEFT;
						printf("Goto Avoiding item left \n");
						system("espeak -a 200 \"Object detected in front! Avoiding it by the left\" &");
					       break;
				       case 3 :
						speed_saveZ = TravelLengthZ;
					       TravelLengthX = TravelLengthZ;
					       temeraire_state.state = AVOIDING_ITEM_RIGHT;
						printf("Goto Avoiding item right \n");
					       	system("espeak -a 200 \"Object detected in front! Avoiding it by the right\" &");
						break;
				       case 4 :
						speed_saveZ = TravelLengthZ;
					       TravelLengthX = TravelLengthZ;
						TravelRotationY = 5;
					       temeraire_state.state = AVOIDING_WALL_SIDE;
						printf("Goto avoiding wall side \n");
						system("espeak -a 200 \"Wall detected on my left\" &");
					       break;
				       case 5 :
						speed_saveZ = TravelLengthZ;
					       TravelLengthX = - TravelLengthZ;
						TravelRotationY = -5;
					       temeraire_state.state = AVOIDING_WALL_SIDE;
						printf("Goto avoiding wall side \n");
						system("espeak -a 200 \"Wall detected on my right\" &");
					       break;
				       default :
						printf("Default State ?? \n");
					       break;
			       }
			       break;

		case AVOIDING_ITEM_LEFT :
				switch(check_sensors()) {
                                       case 0 :
                                               // Nothing
						printf("Stop avoiding item left \n");
						system("espeak -a 200 \"Object avoided successfully\" &");
						temeraire_state.state = CHECKING_ENV;
						TravelRotationY = 0;
                                		TravelLengthX = 0;
						TravelLengthZ = speed_saveZ;
                                               break;
                                       case 1 :
                                               TravelLengthZ = 0;
                                               look_around();
                                               temeraire_state.state = AVOIDING_WALL_FRONT;
						printf("Goto avoiding wall front \n");
                                               //find_where_to_turn(1);
                                               //gettimeofday( &timebeforenextcheck, NULL );
                                               break;
                                       case 2 : // front item > go left
						TravelLengthZ = speed_saveZ;
						if((us_sensor_distance_front < US_FRONT_LIMIT))
							TravelLengthZ = 0;
						else
							TravelLengthX += 3;
                                               break;
                                       case 3 :
                                               break;
                                       case 4 :
						TravelLengthZ = speed_saveZ;
                                                if((us_sensor_distance_right < US_SIDE_LIMIT))
                                                        TravelLengthZ = 0;
                                                else
                                                        TravelLengthX += 2;
                                               break;
                                       case 5 :
                                               break;
                                       default :
                                               break;
                               }
			       break;


		case AVOIDING_WALL_FRONT :
				
				switch(check_sensors()) {
                                       case 0 :
                                               // Nothing
                                               printf("Stop avoiding wall front \n");
                                                system("espeak -a 200 \"Wall avoided successfully\" &");
                                                temeraire_state.state = CHECKING_ENV;
                                                TravelRotationY = 0;
                                                TravelLengthX = 0;
                                                TravelLengthZ = speed_saveZ;
                                               break;
                                       case 1 :
                                               TravelLengthZ = 10; // Go back
                                               //look_around();
                                               //find_where_to_turn(1);
                                               //gettimeofday( &timebeforenextcheck, NULL );
                                               break;
                                       case 2 : // front item > go left
						TravelRotationY = 10;
                                               break;
                                       case 3 :
						TravelRotationY = -10;
                                               break;
                                       case 4 :
						TravelRotationY = 10;
                                               break;
                                       case 5 :
						TravelRotationY = -10;
                                               break;
                                       default :

                                               break;
                               }



				break;

		case AVOIDING_ITEM_RIGHT :

                                switch(check_sensors()) {
                                       case 0 :
                                               // Nothing
                                                printf("Stop avoiding item right \n");
						system("espeak -a 200 \"Object avoided successfully\" &");
						temeraire_state.state = CHECKING_ENV;
						TravelRotationY = 0;
                                		TravelLengthX = 0;
						TravelLengthZ = speed_saveZ;
                                               break;
                                       case 1 :
                                               TravelLengthZ = 0;
                                               look_around();
                                               temeraire_state.state = AVOIDING_WALL_FRONT;
                                                printf("Goto avoiding wall front \n");
                                               //find_where_to_turn(1);
                                               //gettimeofday( &timebeforenextcheck, NULL );
                                               break;
                                       case 2 : // front item > go left
                                               break;
                                       case 3 :
						TravelLengthZ = speed_saveZ;
                                                if((us_sensor_distance_front < US_FRONT_LIMIT))
                                                        TravelLengthZ = 0;
                                                else
                                                        TravelLengthX -= 3;
                                               break;
                                       case 4 :
                                               break;
                                       case 5 :
						TravelLengthZ = speed_saveZ;
                                                if((us_sensor_distance_left < US_SIDE_LIMIT))
                                                        TravelLengthZ = 0;
                                                else
                                                        TravelLengthX -= 2;
                                               break;
                                       default :

                                               break;
                               }

                               break;

		case AVOIDING_WALL_SIDE :
                                switch(check_sensors()) {
                                       case 0 :
                                               // Nothing
                                                printf("Stop avoiding wall \n");
                                                system("espeak -a 200 \"Wall avoided successfully\" &");
                                                temeraire_state.state = CHECKING_ENV;
                                                TravelRotationY = 0;
                                                TravelLengthX = 0;
                                                TravelLengthZ = speed_saveZ;
                                               break;
                                       case 1 :
                                               TravelLengthZ = 0;
                                               look_around();
                                               temeraire_state.state = AVOIDING_WALL_FRONT;
                                                printf("Goto avoiding wall front \n");
                                               break;
                                       case 2 : // front item > go left
                                                TravelLengthZ = speed_saveZ;
                                                if((us_sensor_distance_front < US_FRONT_LIMIT))
                                                        TravelLengthZ = 0;
                                                else
                                                        TravelLengthX = - speed_saveZ;
                                               break;
						temeraire_state.state = AVOIDING_ITEM_LEFT;
                                       case 3 :
						TravelLengthZ = speed_saveZ;
                                                if((us_sensor_distance_front < US_FRONT_LIMIT))
                                                        TravelLengthZ = 0;
                                                else
                                                        TravelLengthX = speed_saveZ;
                                               break;
                                                temeraire_state.state = AVOIDING_ITEM_RIGHT;
                                               break;
                                       case 4 :
                                               break;
                                       case 5 :
                                               break;
                                       default :
						break;		
				}
				break;


		default : 
			printf("Default state AI \n");
			break;
	}

}
示例#8
0
文件: otto.c 项目: PSR-hunt/hunt
/**
 * Implements the game logic of a otto-matic player located at a
 * given coordinate with a given orientation.
 * @param[in] y A coordinate.
 * @param[in] x A coordinate.
 * @param[in] face The orientation of the player.
 * [PSR]
 */
void otto(int y,int x,char face) {
	int i;
	struct sigaction handler, old_handler; /* Added to substitute deprecated signals functions. [PSR] */
	sigset_t old_mask, sig_mask; /* Added to substitute deprecated signals functions. [PSR] */

	bool done;

# ifdef	DEBUG
	if (debug == NULL) {
		debug = fopen("bug", "w");
		setbuf(debug, NULL);
	}
	fprintf(debug, "\n%c(%d,%d)", face, y, x);
# endif
	handler.sa_handler=&empty_handler; /* Added to substitute deprecated signals functions. [PSR] */
	sigaction(SIGALRM, &handler, &old_handler);/* Added to substitute deprecated signals functions. [PSR] */
	sigemptyset(&sig_mask);/* Added to substitute deprecated signals functions. [PSR] */
	sigaddset(&sig_mask, SIGALRM);/* Added to substitute deprecated signals functions. [PSR] */
	sigprocmask(SIG_BLOCK, &sig_mask, &old_mask);/* Added to substitute deprecated signals functions. [PSR] */
	setitimer(ITIMER_REAL, &pause_time, NULL);/* Added to substitute deprecated signals functions. [PSR] */
	sigsuspend(&old_mask);/* Added to substitute deprecated signals functions. [PSR] */
	sigprocmask(SIG_SETMASK, &old_mask, NULL);/* Added to substitute deprecated signals functions. [PSR] */

	/* save away parameters so other functions may use/update info */
	switch (face) {
		case '^': facing = NORTH; break;
		case '<': facing = WEST; break;
		case 'v': facing = SOUTH; break;
		case '>': facing = EAST; break;
		default: abort();
	}
	row = y; col = x;
	been_there[row][col] |= 1 << facing;

	/* initially no commands to be sent */
	comlen = 0;

	/* find something to do */
	look_around();

	done=false;

	for (i = 0; i < NUMDIRECTIONS; i++) {
		if (strchr(OPPONENT, flbr[i].what) != NULL) {
			attack(i, &flbr[i]);
			memset(been_there, 0, sizeof been_there);
			done=true;
			break;
		}
	}

	if(!done) {
		if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) {
			duck(BACK);
			memset(been_there, 0, sizeof been_there);
# ifdef BOOTS
		} else if (go_for_ammo(BOOT_PAIR)) {
			memset(been_there, 0, sizeof been_there);
		} else if (go_for_ammo(BOOT)) {
			memset(been_there, 0, sizeof been_there);
# endif
		} else if (go_for_ammo(GMINE)) {
			memset(been_there, 0, sizeof been_there);
		}
		else if (go_for_ammo(MINE)) {
			memset(been_there, 0, sizeof been_there);
		}
		else {
			wander();
		}
	}

	write_and_push(main_socket, command, comlen);
	otto_count += comlen;
# ifdef	DEBUG
	(void) fwrite(command, 1, comlen, debug);
# endif
}