Ejemplo n.º 1
0
void result_window_refresh(int scrX,int scrY,int ch_num,char * key)
{
    WINDOW *sub_win;
    char *Tags[]={"Show_x0020_All_x0020_ZIP_x0020_Codes","ZIP_x0020_Code","/ZIP_x0020_Code","City","/City","State_x0020_Abbreviation","/State_x0020_Abbreviation","/Show_x0020_All_x0020_ZIP_x0020_Codes"};
	char *string;
	char *token;
	int i;
	int flag=0;

	TOKEN * now_token;
	NODE *now_node;

	char *zipcode;
	char *cityName;
	char *state;

	FILE *file;
	file=fopen("zipcode.xml","rt");
	string=(char*)malloc(sizeof(char)*1024);
	//printf("file open\n");

	i=0;
	while(1)
	{
		//i++;
		if(fgets(string,1024,file)==NULL)
		{
			break;
		}
		//printf("%d %s\n",i++,string);
		while(1)
		{
			if(flag==0)
			{
				token=strtok(string,"<>");
				flag=1;
			}
			else
			{
				token=strtok(NULL,"<>");
			}
			if(token==NULL)
			{
				flag=0;
				break;
			}
			else
			{
				if(strstr(token,key)!=NULL)
				{
					//printf("%d zipcode: %s city: %s\n",i,zipcode,token);
					i++;
					cityName=(char*)malloc(sizeof(char)*1024);
					strcpy(cityName,token);
				}
				else if(strcmp(token,Tags[1])==0)
				{
					token=strtok(NULL,"<>");
					if(token==NULL)
					{
						flag=0;
						break;
					}
					zipcode=(char*)malloc(sizeof(char)*1024);
					strcpy(zipcode,token);

				}

				else if(strcmp(token,Tags[5])==0)
				{
                    token=strtok(NULL,"<>");
					if(token==NULL)
					{
						flag=0;
						break;
					}
					state=(char*)malloc(sizeof(char)*1024);
					strcpy(state,token);
					//i++;
					//printf("%d zipcode: %s city: %s state:%s\n",i,zipcode,cityName,state);
					//insert_NODE(zipcode,cityName,state);
				}

			}
		}
	}
	if(i+2<scrY)
	{
        sub_win=newwin(i+2,scrX,5,0);
    }
    else
    {
        sub_win=newwin(scrY,scrX,5,0);
    }
	refresh();
	box(sub_win,0,0);
	wrefresh(sub_win);
    mvprintw(2,1,"%d",i);
	i=0;
	fseek(file,0,0);
	while(1)
	{
		//i++;
		if(fgets(string,1024,file)==NULL)
		{
			break;
		}
		//printf("%d %s\n",i++,string);
		while(1)
		{
			if(flag==0)
			{
				token=strtok(string,"<>");
				flag=1;
			}
			else
			{
				token=strtok(NULL,"<>");
			}
			if(token==NULL)
			{
				flag=0;
				break;
			}
			else
			{
				if(strstr(token,key)!=NULL)
				{
					//printf("%d zipcode: %s city: %s\n",i,zipcode,token);
					i++;
					cityName=(char*)malloc(sizeof(char)*1024);
					strcpy(cityName,token);
					mvprintw(i+5,1,"%d zipcode: %s /city: %s",i,zipcode,cityName);
				}
				else if(strcmp(token,Tags[1])==0)
				{
					token=strtok(NULL,"<>");
					if(token==NULL)
					{
						flag=0;
						break;
					}
					zipcode=(char*)malloc(sizeof(char)*1024);
					strcpy(zipcode,token);

				}

				else if(strcmp(token,Tags[5])==0)
				{
                    token=strtok(NULL,"<>");
					if(token==NULL)
					{
						flag=0;
						break;
					}
					state=(char*)malloc(sizeof(char)*1024);
					strcpy(state,token);
					//mvprintw(i+6,1,"%d zipcode: %s /city: %s /state:%s",i,zipcode,cityName,state);
					//insert_NODE(zipcode,cityName,state);
				}

			}
		}
	}
	move(crsY,crsX);


	fclose(file);
}
Ejemplo n.º 2
0
void insert_window_refresh(WINDOW *my_win,int scrX,int scrY)
{
	char ch;
	char *string;
	int index=0;
	WINDOW *erase_win;

	string=(char*)malloc(sizeof(char)*1024);
	my_win=newwin(4,scrX,0,0);
	refresh();
	box(my_win,0,0);
	wprintw(my_win,"Search");
	wrefresh(my_win);

	mvprintw(1,1,"insert city name:");
	getyx(stdscr,crsY,crsX);


	while(1)
	{
		ch=getch();
		if (ch!='\n')
		{
			string[index]=ch;
			index++;
			string[index]='\0';
			crsX++;
			clear();

			my_win=newwin(4,scrX,0,0);
			refresh();
			box(my_win,0,0);
			wprintw(my_win,"Search");
			wrefresh(my_win);

			mvprintw(1,1,"insert city name: %s",string);
			getyx(stdscr,crsY,crsX);
			result_window_refresh(scrX,scrY,index,string);
		}
		else
		{
			index=0;
			//wclear(my_win);
			//box(my_win,0,0);
			//wprintw(my_win,"Search");
			//mvprintw(1,1,"insert city name:");
			//getyx(stdscr,crsY,crsX);
			//wrefresh(my_win);

			clear();


			my_win=newwin(4,scrX,0,0);
			refresh();
			box(my_win,0,0);
			wprintw(my_win,"Search");
			wrefresh(my_win);

			mvprintw(1,1,"insert city name:");
			getyx(stdscr,crsY,crsX);
		}

	}
}
Ejemplo n.º 3
0
//note that passing in iteminfo instances with sType == "MENU" or "DESCRIPTION" does special things
//if sType == "MENU", sFmt == "iOffsetY" or "iOffsetX" also do special things
//otherwise if sType == "MENU", dValue can be used to control color
//all this should probably be cleaned up at some point, rather than using a function for things it wasn't meant for
// well frack, half the game uses it so: optional (int)selected argument causes entry highlight, and enter to return entry's key. Also it now returns int
int compare_split_screen_popup(int iLeft, int iWidth, int iHeight, std::string sItemName,
                               std::vector<iteminfo> vItemDisplay, std::vector<iteminfo> vItemCompare, int selected)
{
    WINDOW *w = newwin(iHeight, iWidth, VIEW_OFFSET_Y, iLeft + VIEW_OFFSET_X);

    mvwprintz(w, 1, 2, c_white, sItemName.c_str());
    int line_num = 3;
    int iStartX = 0;
    bool bStartNewLine = true;
    int selected_ret = '\n';
    std::string spaces(iWidth - 2, ' ');
    for (int i = 0; i < vItemDisplay.size(); i++) {
        if (vItemDisplay[i].sType == "MENU") {
            if (vItemDisplay[i].sFmt == "iOffsetY") {
                line_num += int(vItemDisplay[i].dValue);
            } else if (vItemDisplay[i].sFmt == "iOffsetX") {
                iStartX = int(vItemDisplay[i].dValue);
            } else {
                nc_color nameColor = c_ltgreen; //pre-existing behavior, so make it the default
                //patched to allow variable "name" coloring, e.g. for item examining
                nc_color bgColor = c_white;     //yes the name makes no sense
                if (vItemDisplay[i].dValue >= 0) {
                    if (vItemDisplay[i].dValue < .1 && vItemDisplay[i].dValue > -.1) {
                        nameColor = c_ltgray;
                    } else {
                        nameColor = c_ltred;
                    }
                }
                if ( i == selected && vItemDisplay[i].sName != "" ) {
                    bgColor = h_white;
                    selected_ret = (int)vItemDisplay[i].sName.c_str()[0]; // fixme: sanity check(?)
                }
                mvwprintz(w, line_num, 1, bgColor, "%s", spaces.c_str() );
                shortcut_print(w, line_num, iStartX, bgColor, nameColor, vItemDisplay[i].sFmt.c_str());
                line_num++;
            }
        } else if (vItemDisplay[i].sType == "DESCRIPTION") {
            std::string sText = word_rewrap(vItemDisplay[i].sName, iWidth - 4);
            std::stringstream ss(sText);
            std::string l;
            while (std::getline(ss, l, '\n')) {
                line_num++;
                mvwprintz(w, line_num, 2, c_white, l.c_str());
            }
        } else {
            if (bStartNewLine) {
                mvwprintz(w, line_num, 2, c_white, "%s", (vItemDisplay[i].sName).c_str());
                bStartNewLine = false;
            } else {
                wprintz(w, c_white, "%s", (vItemDisplay[i].sName).c_str());
            }

            std::string sPlus = vItemDisplay[i].sPlus;
            std::string sFmt = vItemDisplay[i].sFmt;
            std::string sNum = " ";
            std::string sPost = "";

            //A bit tricky, find %d and split the string
            size_t pos = sFmt.find("<num>");
            if(pos != std::string::npos) {
                wprintz(w, c_white, sFmt.substr(0, pos).c_str());
                sPost = sFmt.substr(pos + 5);
            } else {
                wprintz(w, c_white, sFmt.c_str());
            }

            if (vItemDisplay[i].sValue != "-999") {
                nc_color thisColor = c_white;
                for (int k = 0; k < vItemCompare.size(); k++) {
                    if (vItemCompare[k].sValue != "-999") {
                        if (vItemDisplay[i].sName == vItemCompare[k].sName) {
                            if (vItemDisplay[i].dValue > vItemCompare[k].dValue - .1 &&
                                vItemDisplay[i].dValue < vItemCompare[k].dValue + .1) {
                                thisColor = c_white;
                            } else if (vItemDisplay[i].dValue > vItemCompare[k].dValue) {
                                if (vItemDisplay[i].bLowerIsBetter) {
                                    thisColor = c_ltred;
                                } else {
                                    thisColor = c_ltgreen;
                                }
                            } else if (vItemDisplay[i].dValue < vItemCompare[k].dValue) {
                                if (vItemDisplay[i].bLowerIsBetter) {
                                    thisColor = c_ltgreen;
                                } else {
                                    thisColor = c_ltred;
                                }
                            }
                            break;
                        }
                    }
                }
                if (vItemDisplay[i].is_int == true) {
                    wprintz(w, thisColor, "%s%.0f", sPlus.c_str(), vItemDisplay[i].dValue);
                } else {
                    wprintz(w, thisColor, "%s%.1f", sPlus.c_str(), vItemDisplay[i].dValue);
                }
            }
            wprintz(w, c_white, sPost.c_str());

            if (vItemDisplay[i].bNewLine) {
                line_num++;
                bStartNewLine = true;
            }
        }
    }

    draw_border(w);

    int ch = (int)' ';

    wrefresh(w);
    if (iLeft > 0) {
        ch = (int)getch();
        if ( selected > 0 && ( ch == '\n' || ch == KEY_RIGHT ) && selected_ret != 0 ) {
            ch = selected_ret;
        } else if ( selected == KEY_LEFT ) {
            ch = (int)' ';
        }
        delwin(w);
    }

    return ch;
}
Ejemplo n.º 4
0
int main(){

	WINDOW *my_win, *menu_win, *my_menu_win;
	ITEM **my_items;
	MENU *my_menu;
	list *FileBuffer;
	
	int height, width, startx, starty, exit = 0;
	int highlight = 1;
	int ch, c, choice = 0, i, j;
	char str[81];
	
	FileBuffer = (list *)malloc(sizeof(list));
	
	
	if (FileBuffer == NULL)
		return;
	
	InitialiseBuffer(FileBuffer);
	
	initscr();
	clear();
	noecho();
	cbreak();
	
	start_color();
	
	
	/*Checking whether the terminal supports colors*/
	if (has_colors() == FALSE){
		endwin();
		printf("Your terminal does not support colors\n");
	}
		
	
	keypad(stdscr, TRUE);
	
	height = 3;
	width = 10;
	starty = (LINES - height)/2;
	startx = (COLS - width) / 2;	

	refresh();
	
	my_win = Create_NewWindow(height, width, starty, startx);

	mvwhline(my_win, 5, 1,	ACS_HLINE, width - 1);
	
	init_pair(1, COLOR_RED, COLOR_BLACK);
	init_pair(2, COLOR_CYAN, COLOR_BLACK);
	
	/* Create items */
    my_items = (ITEM **)calloc(nchoices, sizeof(ITEM *));
    for(i = 0; i < nchoices; ++i)
    	my_items[i] = new_item(menu_options[i], menu_options[i]);

	/* Create menu */	
	my_menu = new_menu((ITEM **)my_items);

	/* Set menu option not to show the description */
	menu_opts_off(my_menu, O_SHOWDESC);

	/* Create the window to be associated with the menu */
    my_menu_win = newwin(0, 0, 0, 0);
    keypad(my_menu_win, TRUE);
     
	/* Set main window and sub window */
    set_menu_win(my_menu, my_menu_win);
	set_menu_sub(my_menu, derwin(my_menu_win, 0, 0, 0, 0));
 	set_menu_format(my_menu, 1, 6);
	set_menu_mark(my_menu, " * ");


	/* Post the menu */
	post_menu(my_menu);
	wrefresh(my_menu_win);
	
	i = 0;
		
	mvwhline(my_menu_win, 1, 0, ACS_HLINE, COLS);
	mvwprintw(my_menu_win, LINES - 1, 0, "Press F3 to go to the menu, F6 to exit", c);
	
	
	while(1){
		choice = ToggleMenu(my_menu_win, my_menu, i);
		i = choice;
		switch(choice){
			case 0:
				MenuOpen(FileBuffer, my_menu_win);
				break;
			case 1:
				MenuNew(FileBuffer, my_menu_win, my_menu);
				break;
			case 2:
				MenuSave(FileBuffer, my_menu_win);
				break;
			case 3:
				MenuSaveAs(FileBuffer, my_menu_win);
				break;
			case 4:
				MenuEdit(FileBuffer, my_menu_win);
				break;
			case 5:
				MenuExit(FileBuffer, my_menu_win);
				exit = 1;
				break;
			default:
				break;
		}
		if (exit)
			break;
	}
	/*Assertion: the user wants to exit the program*/
    
		
	/* Unpost and free all the memory taken up */
       unpost_menu(my_menu);
       free_menu(my_menu);
       for(j = 0; j < nchoices; ++j)
               free_item(my_items[j]);
                
	clrtoeol();
	refresh();	
	
	/*Ending curses mode*/
	
	endwin();		
	return 0;
}
Ejemplo n.º 5
0
bool player::create(game *g, character_type type, std::string tempname)
{
 weapon = item(g->itypes["null"], 0);

 g->u.prof = profession::generic();

 WINDOW* w = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH,
                    (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY-FULL_SCREEN_HEIGHT)/2 : 0,
                    (TERMX > FULL_SCREEN_WIDTH) ? (TERMX-FULL_SCREEN_WIDTH)/2 : 0);

 int tab = 0, points = 38, max_trait_points = 12;
 if (type != PLTYPE_CUSTOM) {
  switch (type) {
   case PLTYPE_NOW:
    g->u.male = (rng(1,100)>50);
    g->u.pick_name();
   case PLTYPE_RANDOM:
   {
    str_max = rng(6, 12);
    dex_max = rng(6, 12);
    int_max = rng(6, 12);
    per_max = rng(6, 12);
    points = points - str_max - dex_max - int_max - per_max;
    if (str_max > HIGH_STAT)
     points -= (str_max - HIGH_STAT);
    if (dex_max > HIGH_STAT)
     points -= (dex_max - HIGH_STAT);
    if (int_max > HIGH_STAT)
     points -= (int_max - HIGH_STAT);
    if (per_max > HIGH_STAT)
     points -= (per_max - HIGH_STAT);

    int num_gtraits = 0, num_btraits = 0, rn, tries;
    while (points < 0 || rng(-3, 20) > points) {
     if (num_btraits < max_trait_points && one_in(3)) {
      tries = 0;
      do {
       rn = random_bad_trait();
       tries++;
      } while ((has_trait(rn) ||
              num_btraits - traits[rn].points > max_trait_points) && tries < 5);
      if (tries < 5) {
       toggle_trait(rn);
       points -= traits[rn].points;
       num_btraits -= traits[rn].points;
      }
     } else {
      switch (rng(1, 4)) {
       case 1: if (str_max > 5) { str_max--; points++; } break;
       case 2: if (dex_max > 5) { dex_max--; points++; } break;
       case 3: if (int_max > 5) { int_max--; points++; } break;
       case 4: if (per_max > 5) { per_max--; points++; } break;
      }
     }
    }
    while (points > 0) {
     switch (rng((num_gtraits < max_trait_points ? 1 : 5), 9)) {
     case 1:
     case 2:
     case 3:
     case 4:
      rn = random_good_trait();
      if (!has_trait(rn) && points >= traits[rn].points &&
          num_gtraits + traits[rn].points <= max_trait_points) {
       toggle_trait(rn);
       points -= traits[rn].points;
       num_gtraits += traits[rn].points;
      }
      break;
     case 5:
      switch (rng(1, 4)) {
       case 1: if (str_max < HIGH_STAT) { str_max++; points--; } break;
       case 2: if (dex_max < HIGH_STAT) { dex_max++; points--; } break;
       case 3: if (int_max < HIGH_STAT) { int_max++; points--; } break;
       case 4: if (per_max < HIGH_STAT) { per_max++; points--; } break;
      }
      break;
     case 6:
     case 7:
     case 8:
     case 9:
      rn = random_skill();

      Skill *aSkill = Skill::skill(rn);
      int level = skillLevel(aSkill);

      if (level < points) {
        points -= level + 1;
        skillLevel(aSkill).level(level + 2);
      }
      break;
     }
    }
   } break;
   case PLTYPE_TEMPLATE: {
    std::ifstream fin;
    std::stringstream filename;
    filename << "data/" << tempname << ".template";
    fin.open(filename.str().c_str());
    if (!fin.is_open()) {
     debugmsg("Couldn't open %s!", filename.str().c_str());
     return false;
    }
    std::string(data);
    getline(fin, data);
    load_info(g, data);
    points = 0;
   } break;
  }
  tab = NEWCHAR_TAB_MAX;
 } else
  points = OPTIONS[OPT_INITIAL_POINTS];
  max_trait_points = OPTIONS[OPT_MAX_TRAIT_POINTS];

 do {
  werase(w);
  wrefresh(w);
  switch (tab) {
   case 0: tab += set_stats      (w, g, this, type, points); break;
   case 1: tab += set_traits     (w, g, this, type, points, max_trait_points); break;
   case 2: tab += set_profession (w, g, this, type, points); break;
   case 3: tab += set_skills     (w, g, this, type, points); break;
   case 4: tab += set_description(w, g, this, type, points); break;
  }
 } while (tab >= 0 && tab <= NEWCHAR_TAB_MAX);
 delwin(w);

 if (tab < 0)
  return false;

 // Character is finalized.  Now just set up HP, &c
 for (int i = 0; i < num_hp_parts; i++) {
  hp_max[i] = calc_HP(str_max, has_trait(PF_TOUGH));
  hp_cur[i] = hp_max[i];
 }
 if (has_trait(PF_HARDCORE)) {
  for (int i = 0; i < num_hp_parts; i++) {
   hp_max[i] = int(hp_max[i] * .25);
   hp_cur[i] = hp_max[i];
  }
 } if (has_trait(PF_GLASSJAW)) {
  hp_max[hp_head] = int(hp_max[hp_head] * .80);
  hp_cur[hp_head] = hp_max[hp_head];
 }
 if (has_trait(PF_SMELLY))
  scent = 800;
 if (has_trait(PF_ANDROID)) {
  bionic_id first_bio;
  do {
   first_bio = g->random_good_bionic();
  } while (bionics[first_bio]->power_cost > 10);
  add_bionic(first_bio);
  add_bionic(bionic_id(power_source_bionics[rng(0,power_source_bionics.size()-1)]));	// Power Source
  max_power_level = 10;
  power_level = 10;
 }

 if (has_trait(PF_MARTIAL_ARTS)) {
  itype_id ma_type;
  do {
   int choice = (PLTYPE_NOW==type)? rng(1, 5) : menu(false, _("Pick your style:"),
                     _("Karate"), _("Judo"), _("Aikido"), _("Tai Chi"),
                     _("Taekwondo"), NULL);
   if (choice == 1)
    ma_type = "style_karate";
   if (choice == 2)
    ma_type = "style_judo";
   if (choice == 3)
    ma_type = "style_aikido";
   if (choice == 4)
    ma_type = "style_tai_chi";
   if (choice == 5)
    ma_type = "style_taekwondo";
   item tmpitem = item(g->itypes[ma_type], 0);
   if(PLTYPE_NOW!=type) {
       full_screen_popup(tmpitem.info(true).c_str());
   }
  } while (PLTYPE_NOW!=type && !query_yn(_("Use this style?")));
  styles.push_back(ma_type);
  style_selected=ma_type;
 }

    if (has_trait(PF_MARTIAL_ARTS2)) {
  itype_id ma_type;
  do {
   int choice = (PLTYPE_NOW==type)? rng(1, 5) : menu(false, _("Pick your style:"),
                     _("Capoeira"), _("Krav Maga"), _("Muay Thai"),
                     _("Ninjutsu"), _("Zui Quan"), NULL);
   if (choice == 1)
    ma_type = "style_capoeira";
   if (choice == 2)
    ma_type = "style_krav_maga";
   if (choice == 3)
    ma_type = "style_muay_thai";
   if (choice == 4)
    ma_type = "style_ninjutsu";
   if (choice == 5)
    ma_type = "style_zui_quan";
   item tmpitem = item(g->itypes[ma_type], 0);
   if(PLTYPE_NOW!=type) {
     full_screen_popup(tmpitem.info(true).c_str());
   }
  } while (PLTYPE_NOW!=type && !query_yn(_("Use this style?")));
  styles.push_back(ma_type);
  style_selected=ma_type;
 }
 if (has_trait(PF_MARTIAL_ARTS3)) {
  itype_id ma_type;
  do {
   int choice = (PLTYPE_NOW==type)? rng(1, 5) : menu(false, _("Pick your style:"),
                     _("Tiger"), _("Crane"), _("Leopard"), _("Snake"),
                     _("Dragon"), NULL);
   if (choice == 1)
    ma_type = "style_tiger";
   if (choice == 2)
    ma_type = "style_crane";
   if (choice == 3)
    ma_type = "style_leopard";
   if (choice == 4)
    ma_type = "style_snake";
   if (choice == 5)
    ma_type = "style_dragon";
   item tmpitem = item(g->itypes[ma_type], 0);
   if(PLTYPE_NOW!=type) {
     full_screen_popup(tmpitem.info(true).c_str());
   }
  } while (PLTYPE_NOW!=type && !query_yn(_("Use this style?")));
  styles.push_back(ma_type);
  style_selected=ma_type;
 }
 if (has_trait(PF_MARTIAL_ARTS4)) {
  itype_id ma_type;
  do {
   int choice = (PLTYPE_NOW==type)? rng(1, 5) : menu(false, _("Pick your style:"),
                     _("Centipede"), _("Viper"), _("Scorpion"), _("Lizard"),
                     _("Toad"), NULL);
   if (choice == 1)
    ma_type = "style_centipede";
   if (choice == 2)
    ma_type = "style_venom_snake";
   if (choice == 3)
    ma_type = "style_scorpion";
   if (choice == 4)
    ma_type = "style_lizard";
   if (choice == 5)
    ma_type = "style_toad";
   item tmpitem = item(g->itypes[ma_type], 0);
   if(PLTYPE_NOW!=type) {
     full_screen_popup(tmpitem.info(true).c_str());
   }
  } while (PLTYPE_NOW!=type && !query_yn(_("Use this style?")));
  styles.push_back(ma_type);
  style_selected=ma_type;
 }
 ret_null = item(g->itypes["null"], 0);
 weapon = get_combat_style();


 item tmp; //gets used several times

 std::vector<std::string> prof_items = g->u.prof->items();
 for (std::vector<std::string>::const_iterator iter = prof_items.begin(); iter != prof_items.end(); ++iter)
 {
    tmp = item(item_controller->find_template(*iter), 0);
    inv.push_back(tmp);
 }

 std::vector<addiction> prof_addictions = g->u.prof->addictions();
 for (std::vector<addiction>::const_iterator iter = prof_addictions.begin(); iter != prof_addictions.end(); ++iter)
 {
     g->u.addictions.push_back(*iter);
 }

 // Grab the skills from the profession, if there are any
 profession::StartingSkillList prof_skills = g->u.prof->skills();
 for (profession::StartingSkillList::const_iterator iter = prof_skills.begin();
      iter != prof_skills.end(); ++iter)
 {
     assert(Skill::skill(iter->first));
     if (Skill::skill(iter->first))
     {
        g->u.boost_skill_level(iter->first, iter->second);
     }
 }

 // Those who are both near-sighted and far-sighted start with bifocal glasses.
 if (has_trait(PF_HYPEROPIC) && has_trait(PF_MYOPIC))
 {
    tmp = item(g->itypes["glasses_bifocal"], 0);
    inv.push_back(tmp);
 }
 // The near-sighted start with eyeglasses.
 else if (has_trait(PF_MYOPIC))
 {
    tmp = item(g->itypes["glasses_eye"], 0);
    inv.push_back(tmp);
 }
 // The far-sighted start with reading glasses.
 else if (has_trait(PF_HYPEROPIC))
 {
    tmp = item(g->itypes["glasses_reading"], 0);
    inv.push_back(tmp);
 }

// Likewise, the asthmatic start with their medication.
 if (has_trait(PF_ASTHMA)) {
  tmp = item(g->itypes["inhaler"], 0);
  inv.push_back(tmp);
 }
// Basic starter gear, added independently of profession.
 tmp = item(g->itypes["pockknife"], 0);
  inv.push_back(tmp);
 tmp = item(g->itypes["matches"], 0);
  inv.push_back(tmp);
// make sure we have no mutations
 for (int i = 0; i < PF_MAX2; i++)
  if (!has_base_trait(i))
	my_mutations[i] = false;

	// Equip any armor from our inventory. If we are unable to wear some of it due to encumberance, it will silently fail.
    std::vector<item*> tmp_inv;
    inv.dump(tmp_inv);

    for(std::vector<item*>::iterator i = tmp_inv.begin(); i != tmp_inv.end(); ++i)
    {
        if( (*i)->is_armor())
        {
            if( (*i)->has_flag("VARSIZE"))
            {
                (*i)->item_tags.insert("FIT");
            }
            // It might be more elegant to use player::wear_item, but then we have to implement our own inventory removal.
            wear(g, (*i)->invlet, false);
        }
    }

 // Ensure that persistent morale effects (e.g. Optimist) are present at the start.
 apply_persistent_morale();
 return true;
}
Ejemplo n.º 6
0
static void
test_inserts(int level)
{
    static bool first = TRUE;

    wint_t ch;
    int code;
    int limit;
    int row = 1;
    int col;
    int row2, col2;
    int length;
    wchar_t buffer[BUFSIZ];
    WINDOW *look = 0;
    WINDOW *work = 0;
    WINDOW *show = 0;
    int margin = (2 * TABSIZE) - 1;
    Options option = ((m_opt ? oMove : oDefault)
		      | ((w_opt || (level > 0)) ? oWindow : oDefault));

    if (first) {
	static char cmd[80];
	setlocale(LC_ALL, "");

	putenv(strcpy(cmd, "TABSIZE=8"));

	initscr();
	(void) cbreak();	/* take input chars one at a time, no wait for \n */
	(void) noecho();	/* don't echo input */
	keypad(stdscr, TRUE);
    }

    limit = LINES - 5;
    if (level > 0) {
	look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
	work = newwin(limit - 2, COLS - (2 * level), 1, level);
	show = newwin(4, COLS, limit + 1, 0);
	box(look, 0, 0);
	wnoutrefresh(look);
	limit -= 2;
    } else {
	work = stdscr;
	show = derwin(stdscr, 4, COLS, limit + 1, 0);
    }
    keypad(work, TRUE);

    for (col = margin + 1; col < COLS; col += TABSIZE)
	mvwvline(work, row, col, '.', limit - 2);

    mvwvline(work, row, margin, ACS_VLINE, limit - 2);
    mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
    limit /= 2;

    mvwaddstr(work, 1, 2, "String");
    mvwaddstr(work, limit + 1, 2, "Chars");
    wnoutrefresh(work);

    buffer[length = 0] = '\0';
    legend(show, level, option, buffer, length);
    wnoutrefresh(show);

    doupdate();

    /*
     * Show the characters inserted in color, to distinguish from those that
     * are shifted.
     */
    if (has_colors()) {
	start_color();
	init_pair(1, COLOR_WHITE, COLOR_BLUE);
	wbkgdset(work, COLOR_PAIR(1) | ' ');
    }

    while ((code = wget_wch(work, &ch)) != ERR) {

	if (code == KEY_CODE_YES) {
	    switch (ch) {
	    case KEY_DOWN:
		ch = CTRL('N');
		break;
	    case KEY_BACKSPACE:
		ch = '\b';
		break;
	    default:
		beep();
		continue;
	    }
	} else if (code == ERR) {
	    beep();
	    break;
	}
	if (ch == 'q')
	    break;

	wmove(work, row, margin + 1);
	switch (ch) {
	case 'w':
	    test_inserts(level + 1);

	    touchwin(look);
	    touchwin(work);
	    touchwin(show);

	    wnoutrefresh(look);
	    wnoutrefresh(work);
	    wnoutrefresh(show);

	    doupdate();
	    break;
	case CTRL('N'):
	    if (row < limit) {
		++row;
		/* put the whole string in, all at once */
		col2 = margin + 1;
		switch (option) {
		case oDefault:
		    if (n_opt > 1) {
			for (col = 0; col < length; col += n_opt) {
			    col2 = ColOf(buffer, col, margin);
			    if (move(row, col2) != ERR) {
				InsNStr(buffer + col, LEN(col));
			    }
			}
		    } else {
			if (move(row, col2) != ERR) {
			    InsStr(buffer);
			}
		    }
		    break;
		case oMove:
		    if (n_opt > 1) {
			for (col = 0; col < length; col += n_opt) {
			    col2 = ColOf(buffer, col, margin);
			    MvInsNStr(row, col2, buffer + col, LEN(col));
			}
		    } else {
			MvInsStr(row, col2, buffer);
		    }
		    break;
		case oWindow:
		    if (n_opt > 1) {
			for (col = 0; col < length; col += n_opt) {
			    col2 = ColOf(buffer, col, margin);
			    if (wmove(work, row, col2) != ERR) {
				WInsNStr(work, buffer + col, LEN(col));
			    }
			}
		    } else {
			if (wmove(work, row, col2) != ERR) {
			    WInsStr(work, buffer);
			}
		    }
		    break;
		case oMoveWindow:
		    if (n_opt > 1) {
			for (col = 0; col < length; col += n_opt) {
			    col2 = ColOf(buffer, col, margin);
			    MvWInsNStr(work, row, col2, buffer + col, LEN(col));
			}
		    } else {
			MvWInsStr(work, row, col2, buffer);
		    }
		    break;
		}

		/* do the corresponding single-character insertion */
		row2 = limit + row;
		for (col = 0; col < length; ++col) {
		    col2 = ColOf(buffer, col, margin);
		    switch (option) {
		    case oDefault:
			if (move(row2, col2) != ERR) {
			    InsCh((chtype) buffer[col]);
			}
			break;
		    case oMove:
			MvInsCh(row2, col2, (chtype) buffer[col]);
			break;
		    case oWindow:
			if (wmove(work, row2, col2) != ERR) {
			    WInsCh(work, (chtype) buffer[col]);
			}
			break;
		    case oMoveWindow:
			MvWInsCh(work, row2, col2, (chtype) buffer[col]);
			break;
		    }
		}
	    } else {
		beep();
	    }
	    break;
	case KEY_BACKSPACE:
	    ch = '\b';
	    /* FALLTHRU */
	default:
	    buffer[length++] = ch;
	    buffer[length] = '\0';

	    /* put the string in, one character at a time */
	    col = ColOf(buffer, length - 1, margin);
	    switch (option) {
	    case oDefault:
		if (move(row, col) != ERR) {
		    InsStr(buffer + length - 1);
		}
		break;
	    case oMove:
		MvInsStr(row, col, buffer + length - 1);
		break;
	    case oWindow:
		if (wmove(work, row, col) != ERR) {
		    WInsStr(work, buffer + length - 1);
		}
		break;
	    case oMoveWindow:
		MvWInsStr(work, row, col, buffer + length - 1);
		break;
	    }

	    /* do the corresponding single-character insertion */
	    switch (option) {
	    case oDefault:
		if (move(limit + row, col) != ERR) {
		    InsCh(ch);
		}
		break;
	    case oMove:
		MvInsCh(limit + row, col, ch);
		break;
	    case oWindow:
		if (wmove(work, limit + row, col) != ERR) {
		    WInsCh(work, ch);
		}
		break;
	    case oMoveWindow:
		MvWInsCh(work, limit + row, col, ch);
		break;
	    }

	    wnoutrefresh(work);

	    legend(show, level, option, buffer, length);
	    wnoutrefresh(show);

	    doupdate();
	    break;
	}
    }
    if (level > 0) {
	delwin(show);
	delwin(work);
	delwin(look);
    }
}
Ejemplo n.º 7
0
int proto_forward()
{
	WINDOW *w;
	int p = 1;
	int key;
	int done = 0;
	struct ruleset r;
	char src[128];
	char dst[128];
	char forw[128];
	char src_port[5];
	char dst_port[5];
	char forw_port[5];
	int ret;
 
	r.action = FWD;
	r.proto  = TCP;

	// where is the methlab 
	bzero(r.src, sizeof(r.src));
	bzero(r.src_port, sizeof(r.src_port));
	bzero(r.dst, sizeof(r.dst));
	bzero(r.dst_port, sizeof(r.dst_port));
	bzero(src, sizeof(src));
	bzero(dst, sizeof(dst));
	bzero(forw, sizeof(forw));
	bzero(src_port, sizeof(src_port));
	bzero(dst_port, sizeof(dst_port));
	bzero(forw_port, sizeof(forw_port));
	
	// now initialize the layout 	
	dialog_clear();
	w = newwin(22, 76, 1, 1);
	_std_window(w, "ClosedBSD: Insert a TCP/UDP port forward");
	wrefresh(w);

	draw_protocol_forward_box(w, 2, 13, 1, r);
	draw_host_inputbox(w, 7, 19);
	wattrset(w, dialog_attr);
	mvwprintw(w, 8, 54, ":");
	draw_port_inputbox(w, 7, 55);
	draw_host_inputbox(w, 10, 19);
	wattrset(w, dialog_attr);	
	mvwprintw(w, 11, 54, ":");
	draw_port_inputbox(w, 10, 55); 
	draw_host_inputbox(w, 13, 19);
	wattrset(w, dialog_attr);
	mvwprintw(w, 14, 54, ":"); 
	draw_port_inputbox(w, 13, 55);
	buttons(w, 0); 

	mvwprintw(w, 8, 7, "packet src");
	mvwprintw(w, 11, 7, "packet dst");
	mvwprintw(w, 14, 7, "forward to");
	wmove(w, 0, 0);
	help(w, 31, r);	
	wrefresh(w);

	keypad(w, TRUE);
	while(!done) {
		switch(key = wgetch(w)) {
		case KEY_DOWN:
			down:
			switch(p) {
			case 1:
				p++;
				help(w, 32, r);
				draw_protocol_forward_box(w, 2, 13, p, r);
				break;

			case 2:
				p++;
				help(w, 8, r);
				draw_protocol_forward_box(w, 2, 13, 0, r);	
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 1;
					goto down;
				}	
				else {
					p = 3;
					goto down;
				}
				break;

			case 3:
				p++;
				help(w, 9, r);
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 2;
					goto down;
				}
				else {
					p = 4;	
					goto down;
				}
				break;

			case 4:
				p++;
				help(w, 10, r);
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 3;
					goto down;
				}
				else {
					p = 5;
					goto down;
				}
				break;

			case 5:
				p++;
				help(w, 9, r);
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 4;
					goto down;
				}
				else {
					p = 6;
					goto down;
				}
				break;

			case 6:
				p++;
				help(w, 12, r);
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 5;
					goto down;
				}
				else {
					p = 7;
					goto down;
				}
				break;

			case 7:
				p++;
				buttons(w, 0);
				help(w, 9, r);
				ret = forw_input(w, p, src, dst, forw, src_port, dst_port, forw_port);
				if (ret < p) {
					p = 6;
					goto down;
				}
				else {
					p = 8;
					goto down;
				}
				break;
			
			case 8:
				p++;
				help(w, 33, r);
				buttons(w, 1);
				break;

			case 9:
				p++;
				help(w, 34, r);
				buttons(w, 2);
				break;
			}
			break;
		
		case KEY_RIGHT:
			switch(p) {
			case 9:
				p++;
				buttons(w, 2);
				break;
			}
			break;

		case KEY_LEFT:
			switch(p) {
			case 10:
				p--;
				buttons(w, 1);
				break;
			}
			break;
	
		case KEY_UP:
			switch(p) {
			case 1:
				p = 1;
				help(w, 31, r);	
				draw_protocol_forward_box(w, 2, 13, p, r);
				break;	
			case 2:
				p--;
				help(w, 31, r);
				draw_protocol_forward_box(w, 2, 13, p, r);
				break;
			
			case 9:
				p = 7;
				buttons(w, 0);
				goto down;
			
			case 10:
				p = 8;
				buttons(w, 1);
				goto down;
			}
			break;
	
		case 10:
			switch(p) {
			case 9:
				snprintf(r.src, sizeof(r.src), "%s", src);
				snprintf(r.src_port, sizeof(r.src_port), "%s", src_port);
				snprintf(r.dst, sizeof(r.dst), "%s", dst);
				snprintf(r.dst_port, sizeof(r.dst_port), "%s", dst_port);
				snprintf(r.other_a, sizeof(r.other_a),"%s", forw);
				snprintf(r.other_b, sizeof(r.other_b), "%s", forw_port);
				
				ret = __insert_rule(r);
				if (ret == 0) { 
					dialog_msgbox("ClosedBSD: Notice", "Rule has been added", -1, -1, 1);
					return DITEM_SUCCESS | DITEM_RESTORE | DITEM_CONTINUE;
				}
				
				else {
					dialog_msgbox("ClosedBSD: Notice", "There was an error adding the rule. Verify proper information!", -1, -1, 1);
					break;
				}	
			
			case 10:
				return DITEM_SUCCESS | DITEM_RESTORE | DITEM_CONTINUE;
			}

			break;

		case 32:
			switch(p) {
			case 1:
				r.proto = TCP;
				draw_protocol_forward_box(w, 2, 13, p, r);
			 	break;
			case 2:
				r.proto = UDP;
				draw_protocol_forward_box(w, 2, 13, p, r);
				break;
			}
			break;
		}	
	}
	wmove(w, 0, 0);
	wrefresh(w);
	return 0;
}
Ejemplo n.º 8
0
/*
 * Display a menu for choosing among a number of options
 */
int
dialog_menu (const char *title, const char *prompt, int height, int width,
		int menu_height, const char *current, int item_no,
		const char * const * items)

{
    int i, j, x, y, box_x, box_y;
    int key = 0, button = 0, scroll = 0, choice = 0, first_item = 0, max_choice;
    WINDOW *dialog, *menu;
    FILE *f;

    max_choice = MIN (menu_height, item_no);

    /* center dialog box on screen */
    x = (COLS - width) / 2;
    y = (LINES - height) / 2;

    draw_shadow (stdscr, y, x, height, width);

    dialog = newwin (height, width, y, x);
    keypad (dialog, TRUE);

    draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
    wattrset (dialog, border_attr);
    mvwaddch (dialog, height - 3, 0, ACS_LTEE);
    for (i = 0; i < width - 2; i++)
	waddch (dialog, ACS_HLINE);
    wattrset (dialog, dialog_attr);
    wbkgdset (dialog, dialog_attr & A_COLOR);
    waddch (dialog, ACS_RTEE);

    if (title != NULL && strlen(title) >= width-2 ) {
	/* truncate long title -- mec */
	char * title2 = malloc(width-2+1);
	memcpy( title2, title, width-2 );
	title2[width-2] = '\0';
	title = title2;
    }

    if (title != NULL) {
	wattrset (dialog, title_attr);
	mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
	waddstr (dialog, (char *)title);
	waddch (dialog, ' ');
    }

    wattrset (dialog, dialog_attr);
    print_autowrap (dialog, prompt, width - 2, 1, 3);

    menu_width = width - 6;
    box_y = height - menu_height - 5;
    box_x = (width - menu_width) / 2 - 1;

    /* create new window for the menu */
    menu = subwin (dialog, menu_height, menu_width,
		y + box_y + 1, x + box_x + 1);
    keypad (menu, TRUE);

    /* draw a box around the menu items */
    draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2,
	      menubox_border_attr, menubox_attr);

    /*
     * Find length of longest item in order to center menu.
     * Set 'choice' to default item. 
     */
    item_x = 0;
    for (i = 0; i < item_no; i++) {
	item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2));
	if (strcmp(current, items[i*2]) == 0) choice = i;
    }

    item_x = (menu_width - item_x) / 2;

    /* get the scroll info from the temp file */
    if ( (f=fopen("lxdialog.scrltmp","r")) != NULL ) {
	if ( (fscanf(f,"%d\n",&scroll) == 1) && (scroll <= choice) &&
	     (scroll+max_choice > choice) && (scroll >= 0) &&
	     (scroll+max_choice <= item_no) ) {
	    first_item = scroll;
	    choice = choice - scroll;
	    fclose(f);
	} else {
	    scroll=0;
	    remove("lxdialog.scrltmp");
	    fclose(f);
	    f=NULL;
	}
    }
    if ( (choice >= max_choice) || (f==NULL && choice >= max_choice/2) ) {
	if (choice >= item_no-max_choice/2)
	    scroll = first_item = item_no-max_choice;
	else
	    scroll = first_item = choice - max_choice/2;
	choice = choice - scroll;
    }

    /* Print the menu */
    for (i=0; i < max_choice; i++) {
	print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice,
                    (items[(first_item + i)*2][0] != ':'));
    }

    wnoutrefresh (menu);

    print_arrows(dialog, item_no, scroll,
		 box_y, box_x+item_x+1, menu_height);

    print_buttons (dialog, height, width, 0);

    while (key != ESC) {
	key = wgetch(dialog);

	if (key < 256 && isalpha(key)) key = tolower(key);

	if (strchr("ynm", key))
		i = max_choice;
	else {
        for (i = choice+1; i < max_choice; i++) {
		j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
		if (key == tolower(items[(scroll+i)*2+1][j]))
                	break;
	}
	if (i == max_choice)
       		for (i = 0; i < max_choice; i++) {
			j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
			if (key == tolower(items[(scroll+i)*2+1][j]))
                		break;
		}
	}

	if (i < max_choice || 
            key == KEY_UP || key == KEY_DOWN ||
            key == '-' || key == '+' ||
            key == KEY_PPAGE || key == KEY_NPAGE) {

            print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
                       (items[(scroll+choice)*2][0] != ':'));

	    if (key == KEY_UP || key == '-') {
                if (choice < 2 && scroll) {
	            /* Scroll menu down */
                    scrollok (menu, TRUE);
                    wscrl (menu, -1);
                    scrollok (menu, FALSE);

                    scroll--;

                    print_item (menu, items[scroll * 2 + 1], 0, FALSE,
                               (items[scroll*2][0] != ':'));
		} else
		    choice = MAX(choice - 1, 0);

	    } else if (key == KEY_DOWN || key == '+')  {

		print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
                                (items[(scroll+choice)*2][0] != ':'));

                if ((choice > max_choice-3) &&
                    (scroll + max_choice < item_no)
                   ) {
		    /* Scroll menu up */
		    scrollok (menu, TRUE);
                    scroll (menu);
                    scrollok (menu, FALSE);

                    scroll++;

                    print_item (menu, items[(scroll+max_choice-1)*2+1],
                               max_choice-1, FALSE,
                               (items[(scroll+max_choice-1)*2][0] != ':'));
                } else
                    choice = MIN(choice+1, max_choice-1);

	    } else if (key == KEY_PPAGE) {
	        scrollok (menu, TRUE);
                for (i=0; (i < max_choice); i++) {
                    if (scroll > 0) {
                	wscrl (menu, -1);
                	scroll--;
                	print_item (menu, items[scroll * 2 + 1], 0, FALSE,
                	(items[scroll*2][0] != ':'));
                    } else {
                        if (choice > 0)
                            choice--;
                    }
                }
                scrollok (menu, FALSE);

            } else if (key == KEY_NPAGE) {
                for (i=0; (i < max_choice); i++) {
                    if (scroll+max_choice < item_no) {
			scrollok (menu, TRUE);
			scroll(menu);
			scrollok (menu, FALSE);
                	scroll++;
                	print_item (menu, items[(scroll+max_choice-1)*2+1],
			            max_choice-1, FALSE,
			            (items[(scroll+max_choice-1)*2][0] != ':'));
		    } else {
			if (choice+1 < max_choice)
			    choice++;
		    }
                }

            } else
                choice = i;

            print_item (menu, items[(scroll+choice)*2+1], choice, TRUE,
                       (items[(scroll+choice)*2][0] != ':'));

            print_arrows(dialog, item_no, scroll,
                         box_y, box_x+item_x+1, menu_height);

            wnoutrefresh (menu);
            wrefresh (dialog);

	    continue;		/* wait for another key press */
        }

	switch (key) {
	case KEY_LEFT:
	case TAB:
	case KEY_RIGHT:
	    button = ((key == KEY_LEFT ? --button : ++button) < 0)
			? 2 : (button > 2 ? 0 : button);

	    print_buttons(dialog, height, width, button);
	    wrefresh (dialog);
	    break;
	case ' ':
	case 's':
	case 'y':
	case 'n':
	case 'm':
	    /* save scroll info */
	    if ( (f=fopen("lxdialog.scrltmp","w")) != NULL ) {
		fprintf(f,"%d\n",scroll);
		fclose(f);
	    }
	    delwin (dialog);
            fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
            switch (key) {
            case 's': return 3;
            case 'y': return 3;
            case 'n': return 4;
            case 'm': return 5;
            case ' ': return 6;
            }
	    return 0;
	case 'h':
	case '?':
	    button = 2;
	case '\n':
	    delwin (dialog);
	    if (button == 2) 
            	fprintf(stderr, "%s \"%s\"\n", 
			items[(scroll + choice) * 2],
			items[(scroll + choice) * 2 + 1] +
			first_alpha(items[(scroll + choice) * 2 + 1],""));
	    else
            	fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);

	    remove("lxdialog.scrltmp");
	    return button;
	case 'e':
	case 'x':
	    key = ESC;
	case ESC:
	    break;
	}
    }

    delwin (dialog);
    remove("lxdialog.scrltmp");
    return -1;			/* ESC pressed */
}
Ejemplo n.º 9
0
/*
 * Display a dialog box with a list of options that can be turned on or off
 * in the style of radiolist (only one option turned on at a time).
 */
int dialog_checklist(const char *title, const char *prompt, int height,
		     int width, int list_height)
{
	int i, x, y, box_x, box_y;
	int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
	WINDOW *dialog, *list;

	/* which item to highlight */
	item_foreach() {
		if (item_is_tag('X'))
			choice = item_n();
		if (item_is_selected()) {
			choice = item_n();
			break;
		}
	}

do_resize:
	if (getmaxy(stdscr) < (height + 6))
		return -ERRDISPLAYTOOSMALL;
	if (getmaxx(stdscr) < (width + 6))
		return -ERRDISPLAYTOOSMALL;

	max_choice = MIN(list_height, item_count());

	/* center dialog box on screen */
	x = (COLS - width) / 2;
	y = (LINES - height) / 2;

	draw_shadow(stdscr, y, x, height, width);

	dialog = newwin(height, width, y, x);
	keypad(dialog, TRUE);

	draw_box(dialog, 0, 0, height, width,
		 dlg.dialog.atr, dlg.border.atr);
	wattrset(dialog, dlg.border.atr);
	mvwaddch(dialog, height - 3, 0, ACS_LTEE);
	for (i = 0; i < width - 2; i++)
		waddch(dialog, ACS_HLINE);
	wattrset(dialog, dlg.dialog.atr);
	waddch(dialog, ACS_RTEE);

	print_title(dialog, title, width);

	wattrset(dialog, dlg.dialog.atr);
	print_autowrap(dialog, prompt, width - 2, 1, 3);

	list_width = width - 6;
	box_y = height - list_height - 5;
	box_x = (width - list_width) / 2 - 1;

	/* create new window for the list */
	list = subwin(dialog, list_height, list_width, y + box_y + 1,
	              x + box_x + 1);

	keypad(list, TRUE);

	/* draw a box around the list items */
	draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2,
	         dlg.menubox_border.atr, dlg.menubox.atr);

	/* Find length of longest item in order to center checklist */
	check_x = 0;
	item_foreach()
		check_x = MAX(check_x, strlen(item_str()) + 4);

	check_x = (list_width - check_x) / 2;
	item_x = check_x + 4;

	if (choice >= list_height) {
		scroll = choice - list_height + 1;
		choice -= scroll;
	}

	/* Print the list */
	for (i = 0; i < max_choice; i++) {
		item_set(scroll + i);
		print_item(list, i, i == choice);
	}

	print_arrows(dialog, choice, item_count(), scroll,
		     box_y, box_x + check_x + 5, list_height);

	print_buttons(dialog, height, width, 0);

	wnoutrefresh(dialog);
	wnoutrefresh(list);
	doupdate();

	while (key != KEY_ESC) {
		key = wgetch(dialog);

		for (i = 0; i < max_choice; i++) {
			item_set(i + scroll);
			if (toupper(key) == toupper(item_str()[0]))
				break;
		}

		if (i < max_choice || key == KEY_UP || key == KEY_DOWN ||
		    key == '+' || key == '-') {
			if (key == KEY_UP || key == '-') {
				if (!choice) {
					if (!scroll)
						continue;
					/* Scroll list down */
					if (list_height > 1) {
						/* De-highlight current first item */
						item_set(scroll);
						print_item(list, 0, FALSE);
						scrollok(list, TRUE);
						wscrl(list, -1);
						scrollok(list, FALSE);
					}
					scroll--;
					item_set(scroll);
					print_item(list, 0, TRUE);
					print_arrows(dialog, choice, item_count(),
						     scroll, box_y, box_x + check_x + 5, list_height);

					wnoutrefresh(dialog);
					wrefresh(list);

					continue;	/* wait for another key press */
				} else
					i = choice - 1;
			} else if (key == KEY_DOWN || key == '+') {
				if (choice == max_choice - 1) {
					if (scroll + choice >= item_count() - 1)
						continue;
					/* Scroll list up */
					if (list_height > 1) {
						/* De-highlight current last item before scrolling up */
						item_set(scroll + max_choice - 1);
						print_item(list,
							    max_choice - 1,
							    FALSE);
						scrollok(list, TRUE);
						wscrl(list, 1);
						scrollok(list, FALSE);
					}
					scroll++;
					item_set(scroll + max_choice - 1);
					print_item(list, max_choice - 1, TRUE);

					print_arrows(dialog, choice, item_count(),
						     scroll, box_y, box_x + check_x + 5, list_height);

					wnoutrefresh(dialog);
					wrefresh(list);

					continue;	/* wait for another key press */
				} else
					i = choice + 1;
			}
			if (i != choice) {
				/* De-highlight current item */
				item_set(scroll + choice);
				print_item(list, choice, FALSE);
				/* Highlight new item */
				choice = i;
				item_set(scroll + choice);
				print_item(list, choice, TRUE);
				wnoutrefresh(dialog);
				wrefresh(list);
			}
			continue;	/* wait for another key press */
		}
		switch (key) {
		case 'H':
		case 'h':
		case '?':
			button = 1;
			/* fall-through */
		case 'S':
		case 's':
		case ' ':
		case '\n':
			item_foreach()
				item_set_selected(0);
			item_set(scroll + choice);
			item_set_selected(1);
			delwin(list);
			delwin(dialog);
			return button;
		case TAB:
		case KEY_LEFT:
		case KEY_RIGHT:
			button = ((key == KEY_LEFT ? --button : ++button) < 0)
			    ? 1 : (button > 1 ? 0 : button);

			print_buttons(dialog, height, width, button);
			wrefresh(dialog);
			break;
		case 'X':
		case 'x':
			key = KEY_ESC;
			break;
		case KEY_ESC:
			key = on_key_esc(dialog);
			break;
		case KEY_RESIZE:
			delwin(list);
			delwin(dialog);
			on_key_resize();
			goto do_resize;
		}

		/* Now, update everything... */
		doupdate();
	}
	delwin(list);
	delwin(dialog);
	return key;		/* ESC pressed */
}
Ejemplo n.º 10
0
//
// Affiche le tableau des high-scores dans une fenêtre modale.
//
void displayLeaderboard() {
    int i, c;
    
    const int menuWidth = 10;
    
    const int winHeight = MAX_SAVED_SCORES_COUNT + 6;
    const int winWidth = POPUP_WINDOW_WIDTH + 20;
    
    const int colRank = 2;
    const int colScore = 20;
    const int colName = 35;
    const int colBoard = 55;
    
    ITEM* menuItems[2];
    MENU* menu;
    
    Score scores[MAX_SAVED_SCORES_COUNT];
    
    int n = loadScoreBoard(scores);
    
    while (true) {
        clear();
        refresh();
        
        WINDOW* win = newwin(winHeight, winWidth, (LINES - winHeight) / 2, (COLS - winWidth) / 2);
        box(win, 0, 0);
        
        mvwaddch(win, 2, 0, ACS_LTEE);
        mvwhline(win, 2, 1, ACS_HLINE, winWidth - 1);
        mvwaddch(win, 2, winWidth - 1, ACS_RTEE);
        
        mvwprintw(win, 1, colRank, "CLASSEMENT");
        mvwprintw(win, 1, colScore, "SCORE");
        mvwprintw(win, 1, colName, "PSEUDO");
        mvwprintw(win, 1, colBoard, "PLATEAU");
        
        mvwaddch(win, 0, colScore - 2, ACS_TTEE);
        mvwvline(win, 1, colScore - 2, ACS_VLINE, winHeight - 2);
        mvwaddch(win, winHeight - 1, colScore - 2, ACS_BTEE);
        
        mvwaddch(win, 0, colName - 2, ACS_TTEE);
        mvwvline(win, 1, colName - 2, ACS_VLINE, winHeight - 2);
        mvwaddch(win, winHeight - 1, colName - 2, ACS_BTEE);
        
        mvwaddch(win, 0, colBoard - 2, ACS_TTEE);
        mvwvline(win, 1, colBoard - 2, ACS_VLINE, winHeight - 2);
        mvwaddch(win, winHeight - 1, colBoard - 2, ACS_BTEE);
        
        for(i = 0; i < n; i++) {
            mvwprintw(win, i + 3, colRank, "#%d", i+1);
            mvwprintw(win, i + 3, colScore, "%d", scores[i].score);
            mvwprintw(win, i + 3, colName, "%s", scores[i].username);
            mvwprintw(win, i + 3, colBoard, "%s", scores[i].boardName);
        }
        
        menuItems[0] = new_item("<Retour>", NULL);
        menuItems[1] = (ITEM *) NULL;
        
        //on initialise le menu
        menu = new_menu((ITEM **) menuItems);
        
        //on lui précise bien que le menu fait 1 ligne et 1 colonne
        set_menu_format(menu, 1, 1);
        
        //on associe le menu à une fenêtre et une sous-fenêtre
        set_menu_win(menu, win);
        //fenêtre hauteur largeur x y
        set_menu_sub(menu, derwin(win, 1, menuWidth, winHeight - 2, (winWidth - menuWidth) / 2));
        set_menu_mark(menu, "");
        
        noecho();
        curs_set(0);
        
        //et hop, on affiche le menu et on rafraîchit.
        post_menu(menu);
        
        wrefresh(win);
        refresh();
        
        //boucle pour le menu
        while((c = getch())) {
            bool resized = false;
            
            switch(c) {
                case KEY_RESIZE:
                    //si le terminal a été redimensionné, on ré-exécute la boucle d'affichage
                    resized = true;
                    break;
                case KEY_MENU_ENTER: {
                    unpost_menu(menu);
                    free_menu(menu);
                    
                    for(i = 0; i < 2; ++i)
                        free_item(menuItems[i]);
                    
                    clear();
                    refresh();
                    
                    delwin(win);
                    
                    curs_set(1);
                    echo();
                    return;
                }
            }
            
            if(resized) break;
            wrefresh(win);
        }
    }
}
Ejemplo n.º 11
0
//
// Affiche le plateau de jeu.
//
void displayGameBoard(GameState *state) {
    if(state == NULL) return;
    
    int i, j;
    int width = 4 * BOARD_SIZE + 1;
    int height = 2 * BOARD_SIZE + 1;
    
    WINDOW *boardWin = newwin(height, width, (LINES - height) / 2, (FIRST_COL_WIDTH - width) / 2);
    box(boardWin, 0, 0);
    
    for (i = 0; i < BOARD_SIZE; i++) {
        //début de ligne
        for (j = 0; j < BOARD_SIZE; j++) {
            int charLine = i * 2 + 1;
            int charCol = j * 4;
            
            //on affiche les murs verticaux
            //si il y a quelque-chose dans la case actuelle
            int k;
            bool hasContent = false;
            
            //cases contenant des robots
            for (k = 0; k < ROBOTS_COUNT; k++) {
                //si la case courante contient un robot
                if(state->robots[k].position.y == i && state->robots[k].position.x == j) {
                    int l;
                    bool hasObjective = false;
                    
                    hasContent = true;
                    
                    COL_ON_BOT_BG(boardWin, state->robots[k].robotColor);
                    
                    for (l = 0; l < ROBOTS_COUNT; l++) {
                        //si on a un objectif dans la même case que le robot
                        if(state->gameBoard->objectivesPos[l].y == i && state->gameBoard->objectivesPos[l].x == j) {
                            hasObjective = true;
                            break;
                        }
                    }
                    
                    //on affiche [X] s'il y a un objectif, et simplement trois espaces s'il y a juste un robot
                    if(hasObjective) {
                        mvwprintw(boardWin, charLine, charCol + 1, "[X]");
                    } else {
                        mvwprintw(boardWin, charLine, charCol + 1, "   ");
                    }
                    
                    COL_OFF_BOT_BG(boardWin, state->robots[k].robotColor);
                }
            }
            
            //si la case n'a pas encore de contenu (et donc pas de robot) et que c'est une case objectif
            for (k = 0; k < ROBOTS_COUNT; k++) {
                if(!hasContent && state->gameBoard->objectivesPos[k].y == i && state->gameBoard->objectivesPos[k].x == j) {
                    hasContent = true;
                    
                    COL_ON_BOT(boardWin, k);
                    mvwprintw(boardWin, charLine, charCol + 1, "[X]");
                    COL_OFF_BOT(boardWin, k);
                    
                    break;
                }
            }
            
            //si on doit afficher un mur à droite : si il y a un mur à droite dans la case actuelle ou un mur à gauche dans la case directement à droite
            //on n'oublie pas de prendre en compte les murs en L
            if(state->gameBoard->obstacles[i][j] == CELL_WALL_RIGHT
               || state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM_RIGHT
               || state->gameBoard->obstacles[i][j] == CELL_WALL_TOP_RIGHT
               || (j < BOARD_SIZE && state->gameBoard->obstacles[i][j+1] == CELL_WALL_LEFT)
               || (j < BOARD_SIZE && state->gameBoard->obstacles[i][j+1] == CELL_WALL_BOTTOM_LEFT)
               || (j < BOARD_SIZE && state->gameBoard->obstacles[i][j+1] == CELL_WALL_TOP_LEFT)
               || j == BOARD_SIZE - 1) {
                mvwaddch(boardWin, charLine, charCol + 4, ACS_VLINE);
            }
            
            //affichage des T spéciaux pour relier les traits verticaux aux traits horizontaux
            if(j < BOARD_SIZE - 1 && (i == 0 || state->gameBoard->obstacles[i-1][j] == CELL_WALL_BOTTOM)
                && (state->gameBoard->obstacles[i][j+1] == CELL_WALL_LEFT || state->gameBoard->obstacles[i][j] == CELL_WALL_RIGHT)) {
                mvwaddch(boardWin, charLine - 1, charCol + 4, ACS_TTEE);
            } else if(j < BOARD_SIZE - 1 && (i == BOARD_SIZE - 1 || state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP)
                && (state->gameBoard->obstacles[i][j+1] == CELL_WALL_LEFT || state->gameBoard->obstacles[i][j] == CELL_WALL_RIGHT)) {
                mvwaddch(boardWin, charLine + 1, charCol + 4, ACS_BTEE);
            }
            
            charLine = i * 2 + 2;
            
            //on affiche les murs horizontaux, d'une façon analogue aux murs verticaux
            if(i < BOARD_SIZE - 1 &&
               (state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM
                || state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM_LEFT
                || state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM_RIGHT
                || state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP
                || state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP_LEFT
                || state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP_RIGHT)) {
                   mvwaddch(boardWin, charLine, charCol + 1, ACS_HLINE);
                   mvwaddch(boardWin, charLine, charCol + 2, ACS_HLINE);
                   mvwaddch(boardWin, charLine, charCol + 3, ACS_HLINE);
            }
            
            //affichage d'un point à chaque intersection de cases
            if(i < BOARD_SIZE - 1 && j < BOARD_SIZE - 1 && (i != BOARD_SIZE/2-1 || j != BOARD_SIZE/2-1)) {
                mvwaddch(boardWin, charLine, charCol + 4, ACS_BULLET);
            }
            
            //affichage des coins en L spéciaux entre les traits
            if(state->gameBoard->obstacles[i][j] == CELL_WALL_TOP_LEFT) {
                mvwaddch(boardWin, charLine - 2, charCol, ACS_ULCORNER);
            } else if(state->gameBoard->obstacles[i][j] == CELL_WALL_TOP_RIGHT) {
                mvwaddch(boardWin, charLine - 2, charCol + 4, ACS_URCORNER);
            } else if(state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM_RIGHT) {
                mvwaddch(boardWin, charLine, charCol + 4, ACS_LRCORNER);
            } else if(state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM_LEFT) {
                mvwaddch(boardWin, charLine, charCol, ACS_LLCORNER);
            }
            
            //affichage des T spéciaux pour relier les traits verticaux aux traits horizontaux
            if(i < BOARD_SIZE - 1 && (j == 0 || state->gameBoard->obstacles[i][j-1] == CELL_WALL_RIGHT)
               && (state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP || state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM)) {
                mvwaddch(boardWin, charLine, charCol, ACS_LTEE);
            } else if(i < BOARD_SIZE - 1 && (j == BOARD_SIZE - 1 || state->gameBoard->obstacles[i][j+1] == CELL_WALL_LEFT)
                      && (state->gameBoard->obstacles[i+1][j] == CELL_WALL_TOP || state->gameBoard->obstacles[i][j] == CELL_WALL_BOTTOM)) {
                mvwaddch(boardWin, charLine, charCol + 4, ACS_RTEE);
            }
        }
    }
    
    refresh();
    wrefresh(boardWin);
    delwin(boardWin);
}