示例#1
0
int doc_display_aux(doc_ptr doc, cptr caption, int top, rect_t display)
{
    int     rc = _OK;
    int     i;
    char    finder_str[81];
    char    back_str[81];
    int     page_size;
    bool    done = FALSE;

    strcpy(finder_str, "");

    page_size = display.cy - 4;

    if (top < 0)
        top = 0;
    if (top > doc->cursor.y - page_size)
        top = MAX(0, doc->cursor.y - page_size);

    for (i = 0; i < display.cy; i++)
        Term_erase(display.x, display.y + i, display.cx);

    while (!done)
    {
        int cmd;

        Term_erase(display.x, display.y, display.cx);
        put_str(format("[%s, Line %d/%d]", caption, top, doc->cursor.y), display.y, display.x);
        doc_sync_term(doc, doc_region_create(0, top, doc->width, top + page_size - 1), doc_pos_create(display.x, display.y + 2));
        Term_erase(display.x, display.y + display.cy - 1, display.cx);
        put_str("[Press ESC to exit. Press ? for help]", display.y + display.cy - 1, display.x);

        cmd = inkey_special(TRUE);

        if ('a' <= cmd && cmd <= 'z')
        {
            doc_link_ptr link = int_map_find(doc->links, cmd);
            if (link)
            {
                rc = doc_display_help_aux(string_buffer(link->file), string_buffer(link->topic), display);
                if (rc == _UNWIND)
                    done = TRUE;
                continue;
            }
        }

        switch (cmd)
        {
        case '?':
            if (!strstr(caption, "helpinfo.txt"))
            {
                rc = doc_display_help_aux("helpinfo.txt", NULL, display);
                if (rc == _UNWIND)
                    done = TRUE;
            }
            break;
        case ESCAPE:
            done = TRUE;
            break;
        case 'q':
            done = TRUE;
            rc = _UNWIND;
            break;
        case SKEY_TOP:
        case '7':
            top = 0;
            break;
        case SKEY_BOTTOM:
        case '1':
            top = MAX(0, doc->cursor.y - page_size);
            break;
        case SKEY_PGUP:
        case '9':
            top -= page_size;
            if (top < 0) top = 0;
            break;
        case SKEY_PGDOWN:
        case '3':
            top += page_size;
            if (top > doc->cursor.y - page_size)
                top = MAX(0, doc->cursor.y - page_size);
            break;
        case SKEY_UP:
        case '8':
            top--;
            if (top < 0) top = 0;
            break;
        case SKEY_DOWN:
        case '2':
            top++;
            if (top > doc->cursor.y - page_size)
                top = MAX(0, doc->cursor.y - page_size);
            break;
        case '>':
        {
            doc_pos_t pos = doc_next_bookmark(doc, doc_pos_create(doc->width - 1, top));
            if (doc_pos_is_valid(pos))
            {
                top = pos.y;
                if (top > doc->cursor.y - page_size)
                    top = MAX(0, doc->cursor.y - page_size);
            }
            break;
        }
        case '<':
        {
            doc_pos_t pos = doc_prev_bookmark(doc, doc_pos_create(0, top));
            if (doc_pos_is_valid(pos))
                top = pos.y;
            else
                top = 0;
            break;
        }
        case '|':
        {
            FILE *fp2;
            char buf[1024];
            char name[82];
            int  cb;
            int  format = DOC_FORMAT_TEXT;

            strcpy(name, string_buffer(doc->name));

            if (!get_string("File name: ", name, 80)) break;
            path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
            fp2 = my_fopen(buf, "w");
            if (!fp2)
            {
                msg_format("Failed to open file: %s", buf);
                break;
            }

            cb = strlen(buf);
            if (cb > 5 && strcmp(buf + cb - 5, ".html") == 0)
                format = DOC_FORMAT_HTML;
            else if (cb > 4 && strcmp(buf + cb - 4, ".htm") == 0)
                format = DOC_FORMAT_HTML;

            doc_write_file(doc, fp2, format);
            my_fclose(fp2);
            msg_format("Created file: %s", buf);
            msg_print(NULL);
            break;
        }
        case '/':
            Term_erase(display.x, display.y + display.cy - 1, display.cx);
            put_str("Find: ", display.y + display.cy - 1, display.x);
            strcpy(back_str, finder_str);
            if (askfor(finder_str, 80))
            {
                if (finder_str[0])
                {
                    doc_pos_t pos = doc->selection.stop;
                    if (!doc_pos_is_valid(pos))
                        pos = doc_pos_create(0, top);
                    pos = doc_find_next(doc, finder_str, pos);
                    if (doc_pos_is_valid(pos))
                    {
                        top = pos.y;
                        if (top > doc->cursor.y - page_size)
                            top = MAX(0, doc->cursor.y - page_size);
                    }
                }
            }
            else strcpy(finder_str, back_str);
            break;
        case '\\':
            Term_erase(display.x, display.y + display.cy - 1, display.cx);
            put_str("Find: ", display.y + display.cy - 1, display.x);
            strcpy(back_str, finder_str);
            if (askfor(finder_str, 80))
            {
                if (finder_str[0])
                {
                    doc_pos_t pos = doc->selection.start;
                    if (!doc_pos_is_valid(pos))
                        pos = doc_pos_create(doc->width, top + page_size);
                    pos = doc_find_prev(doc, finder_str, pos);
                    if (doc_pos_is_valid(pos))
                    {
                        top = pos.y;
                        if (top > doc->cursor.y - page_size)
                            top = MAX(0, doc->cursor.y - page_size);
                    }
                }
            }
            else strcpy(finder_str, back_str);
            break;
        default:
        {   /* BETA: Any unhandled keystroke will navigate to the next topic based
                     upon a comparison of the first letter. This is nice, say, for
                     viewing the Character Sheet and navigating to the various sections */
            doc_pos_t pos = doc_next_bookmark_char(doc, doc_pos_create(1, top), cmd);
            if (!doc_pos_is_valid(pos)) /* wrap */
                pos = doc_next_bookmark_char(doc, doc_pos_create(0, 0), cmd);
            if (doc_pos_is_valid(pos))
            {
                top = pos.y;
                if (top > doc->cursor.y - page_size)
                    top = MAX(0, doc->cursor.y - page_size);
            }
        }
        }
    }
    return rc;
}
示例#2
0
/* Idea borrowed from Vanilla 3.5, but recoded from scratch ... */
void do_cmd_list_monsters(void)
{
    int         i, ct_types, ct_total = 0;
    int_map_ptr info = int_map_alloc(free);
    
    /* Collect */
    for (i = 0; i < max_m_idx; i++)
    {
        const monster_type *m_ptr = &m_list[i];
        _mon_list_info_ptr  info_ptr;
        
        if (!m_ptr->ap_r_idx) continue;
        if (!m_ptr->ml) continue;

        info_ptr = int_map_find(info, m_ptr->ap_r_idx);
        if (!info_ptr)
        {
            info_ptr = malloc(sizeof(_mon_list_info_t));
            info_ptr->r_idx = m_ptr->ap_r_idx;
            info_ptr->ct_total = 0;
            info_ptr->ct_awake = 0;
            info_ptr->ct_los = 0;
            
            int_map_add(info, m_ptr->ap_r_idx, info_ptr);
        }

        assert(info_ptr);
        info_ptr->ct_total++;
        ct_total++;

        if (!MON_CSLEEP(m_ptr)) info_ptr->ct_awake++;
        if (projectable(py, px, m_ptr->fy, m_ptr->fx)) info_ptr->ct_los++;
    }
    
    ct_types = int_map_count(info);
    if (ct_types)
    {
        int_map_iter_ptr  iter;
        int              *order;
        int               cx, cy, row = 1, col;

        /* Sort */
        C_MAKE(order, ct_types, int);

        i = 0;
        for (iter = int_map_iter_alloc(info); 
                int_map_iter_is_valid(iter); 
                int_map_iter_next(iter))
        {
            _mon_list_info_ptr info_ptr = int_map_iter_current(iter);
            order[i++] = info_ptr->r_idx;
        }
        int_map_iter_free(iter);

        ang_sort_comp = _compare_r_level;
        ang_sort_swap = _swap_int;
        ang_sort(order, NULL, ct_types);

        /* Display */
        Term_get_size(&cx, &cy);
        col = cx - 52;
        screen_save();
        c_prt(TERM_WHITE, format("You see %d monster%s", ct_total, ct_total != 1 ? "s" : ""), 0, col);
        for (i = 0; i < ct_types; i++)
        {
            int                 r_idx = order[i];
            const monster_race *r_ptr = &r_info[r_idx];
            byte                attr = TERM_WHITE;
            _mon_list_info_ptr  info_ptr = int_map_find(info, r_idx);
            char                buf[100];

            assert(info_ptr);

            Term_erase(col - 1, row, 53);

            if (row >= cy - 2) 
            {
                c_prt(TERM_YELLOW, "...", row++, col+2);
                break;
            }

            if (r_ptr->flags1 & RF1_UNIQUE)
                attr = TERM_VIOLET;
            else if (r_ptr->level > base_level)
                attr = TERM_RED;                
            else if (!info_ptr->ct_awake)
                attr = TERM_L_UMBER;

            if (info_ptr->ct_total == 1)
                sprintf(buf, "%s", r_name + r_ptr->name);
            else if (!info_ptr->ct_awake)
                sprintf(buf, "%s (%d sleeping)", r_name + r_ptr->name, info_ptr->ct_total);
            else if (info_ptr->ct_awake == info_ptr->ct_total)
                sprintf(buf, "%s (%d awake)", r_name + r_ptr->name, info_ptr->ct_total);
            else
                sprintf(buf, "%s (%d awake, %d sleeping)", r_name + r_ptr->name, 
                    info_ptr->ct_awake, info_ptr->ct_total - info_ptr->ct_awake);

            Term_queue_bigchar(col, row, r_ptr->x_attr, r_ptr->x_char, 0, 0);
            c_put_str(attr, format(" %-50.50s", buf), row++, col+1);
        }
        Term_erase(col - 1, row, 53);
        c_prt(TERM_YELLOW, "Hit any key.", row, col+2);
        inkey();
        prt("", 0, 0);

        screen_load();

        C_KILL(order, ct_types, int);
    }
    else
        msg_print("You see no visible monsters.");

    int_map_free(info);
}
示例#3
0
/** Self-test start function.
 *
 * Run all self-tests.
 *
 * @returns EOK on success.
 * @returns The first error occurred.
 *
 */
int self_test(void)
{
	printf("Running networking self-tests\n");
	
	printf("\nChar map test");
	char_map_t cm;
	
	TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL);
	TEST(char_map_initialize(&cm), EOK);
	TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL);
	TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL);
	TEST(char_map_add(&cm, "bla", 0, 1), EOK);
	TEST(char_map_find(&cm, "bla", 0), 1);
	TEST(char_map_add(&cm, "bla", 0, 10), EEXISTS);
	TEST(char_map_update(&cm, "bla", 0, 2), EOK);
	TEST(char_map_find(&cm, "bla", 0), 2);
	TEST(char_map_update(&cm, "ucho", 0, 2), EOK);
	TEST(char_map_exclude(&cm, "bla", 0), 2);
	TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL);
	TEST(char_map_find(&cm, "ucho", 0), 2);
	TEST(char_map_update(&cm, "ucho", 0, 3), EOK);
	TEST(char_map_find(&cm, "ucho", 0), 3);
	TEST(char_map_add(&cm, "blabla", 0, 5), EOK);
	TEST(char_map_find(&cm, "blabla", 0), 5);
	TEST(char_map_add(&cm, "bla", 0, 6), EOK);
	TEST(char_map_find(&cm, "bla", 0), 6);
	TEST(char_map_exclude(&cm, "bla", 0), 6);
	TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL);
	TEST(char_map_find(&cm, "blabla", 0), 5);
	TEST(char_map_add(&cm, "auto", 0, 7), EOK);
	TEST(char_map_find(&cm, "auto", 0), 7);
	TEST(char_map_add(&cm, "kara", 0, 8), EOK);
	TEST(char_map_find(&cm, "kara", 0), 8);
	TEST(char_map_add(&cm, "nic", 0, 9), EOK);
	TEST(char_map_find(&cm, "nic", 0), 9);
	TEST(char_map_find(&cm, "blabla", 0), 5);
	TEST(char_map_add(&cm, "micnicnic", 5, 9), EOK);
	TEST(char_map_find(&cm, "micni", 0), 9);
	TEST(char_map_find(&cm, "micnicn", 5), 9);
	TEST(char_map_add(&cm, "\x10\x0\x2\x2", 4, 15), EOK);
	TEST(char_map_find(&cm, "\x10\x0\x2\x2", 4), 15);
	
	TEST((char_map_destroy(&cm), EOK), EOK);
	TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL);
	
	printf("\nCRC computation test");
	uint32_t value;
	
	TEST(value = ~compute_crc32(~0, "123456789", 8 * 9), 0xcbf43926);
	TEST(value = ~compute_crc32(~0, "1", 8), 0x83dcefb7);
	TEST(value = ~compute_crc32(~0, "12", 8 * 2), 0x4f5344cd);
	TEST(value = ~compute_crc32(~0, "123", 8 * 3), 0x884863d2);
	TEST(value = ~compute_crc32(~0, "1234", 8 * 4), 0x9be3e0a3);
	TEST(value = ~compute_crc32(~0, "12345678", 8 * 8), 0x9ae0daaf);
	TEST(value = ~compute_crc32(~0, "ahoj pane", 8 * 9), 0x5fc3d706);
	
	printf("\nDynamic fifo test");
	dyn_fifo_t fifo;
	
	TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL);
	TEST(dyn_fifo_initialize(&fifo, 1), EOK);
	TEST(dyn_fifo_push(&fifo, 1, 0), EOK);
	TEST(dyn_fifo_pop(&fifo), 1);
	TEST(dyn_fifo_pop(&fifo), ENOENT);
	TEST(dyn_fifo_push(&fifo, 2, 1), EOK);
	TEST(dyn_fifo_push(&fifo, 3, 1), ENOMEM);
	TEST(dyn_fifo_push(&fifo, 3, 0), EOK);
	TEST(dyn_fifo_pop(&fifo), 2);
	TEST(dyn_fifo_pop(&fifo), 3);
	TEST(dyn_fifo_push(&fifo, 4, 2), EOK);
	TEST(dyn_fifo_push(&fifo, 5, 2), EOK);
	TEST(dyn_fifo_push(&fifo, 6, 2), ENOMEM);
	TEST(dyn_fifo_push(&fifo, 6, 5), EOK);
	TEST(dyn_fifo_push(&fifo, 7, 5), EOK);
	TEST(dyn_fifo_pop(&fifo), 4);
	TEST(dyn_fifo_pop(&fifo), 5);
	TEST(dyn_fifo_push(&fifo, 8, 5), EOK);
	TEST(dyn_fifo_push(&fifo, 9, 5), EOK);
	TEST(dyn_fifo_push(&fifo, 10, 6), EOK);
	TEST(dyn_fifo_push(&fifo, 11, 6), EOK);
	TEST(dyn_fifo_pop(&fifo), 6);
	TEST(dyn_fifo_pop(&fifo), 7);
	TEST(dyn_fifo_push(&fifo, 12, 6), EOK);
	TEST(dyn_fifo_push(&fifo, 13, 6), EOK);
	TEST(dyn_fifo_push(&fifo, 14, 6), ENOMEM);
	TEST(dyn_fifo_push(&fifo, 14, 8), EOK);
	TEST(dyn_fifo_pop(&fifo), 8);
	TEST(dyn_fifo_pop(&fifo), 9);
	TEST(dyn_fifo_pop(&fifo), 10);
	TEST(dyn_fifo_pop(&fifo), 11);
	TEST(dyn_fifo_pop(&fifo), 12);
	TEST(dyn_fifo_pop(&fifo), 13);
	TEST(dyn_fifo_pop(&fifo), 14);
	TEST(dyn_fifo_destroy(&fifo), EOK);
	TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL);
	
	printf("\nGeneric char map test");
	
	int *x;
	int *y;
	int *z;
	int *u;
	int *v;
	int *w;
	
	XMALLOC(x, int);
	XMALLOC(y, int);
	XMALLOC(z, int);
	XMALLOC(u, int);
	XMALLOC(v, int);
	XMALLOC(w, int);
	
	int_char_map_t icm;
	icm.magic = 0;
	
	TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL);
	TEST(int_char_map_initialize(&icm), EOK);
	TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
	TEST(int_char_map_find(&icm, "bla", 0), NULL);
	TEST(int_char_map_add(&icm, "bla", 0, x), EOK);
	TEST(int_char_map_find(&icm, "bla", 0), x);
	TEST(int_char_map_add(&icm, "bla", 0, y), EEXISTS);
	TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
	TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
	TEST(int_char_map_add(&icm, "blabla", 0, v), EOK);
	TEST(int_char_map_find(&icm, "blabla", 0), v);
	TEST(int_char_map_add(&icm, "bla", 0, w), EOK);
	TEST(int_char_map_find(&icm, "bla", 0), w);
	TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
	TEST(int_char_map_find(&icm, "bla", 0), NULL);
	TEST(int_char_map_find(&icm, "blabla", 0), v);
	TEST(int_char_map_add(&icm, "auto", 0, u), EOK);
	TEST(int_char_map_find(&icm, "auto", 0), u);
	TEST((int_char_map_destroy(&icm), EOK), EOK);
	TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL);
	
	printf("\nGeneric field test");
	
	XMALLOC(x, int);
	XMALLOC(y, int);
	XMALLOC(z, int);
	XMALLOC(u, int);
	XMALLOC(v, int);
	XMALLOC(w, int);
	
	int_field_t gf;
	gf.magic = 0;
	
	TEST(int_field_add(&gf, x), EINVAL);
	TEST(int_field_count(&gf), -1);
	TEST(int_field_initialize(&gf), EOK);
	TEST(int_field_count(&gf), 0);
	TEST(int_field_get_index(&gf, 1), NULL);
	TEST(int_field_add(&gf, x), 0);
	TEST(int_field_get_index(&gf, 0), x);
	TEST((int_field_exclude_index(&gf, 0), EOK), EOK);
	TEST(int_field_get_index(&gf, 0), NULL);
	TEST(int_field_add(&gf, y), 1);
	TEST(int_field_get_index(&gf, 1), y);
	TEST(int_field_add(&gf, z), 2);
	TEST(int_field_get_index(&gf, 2), z);
	TEST(int_field_get_index(&gf, 1), y);
	TEST(int_field_count(&gf), 3);
	TEST(int_field_add(&gf, u), 3);
	TEST(int_field_get_index(&gf, 3), u);
	TEST(int_field_add(&gf, v), 4);
	TEST(int_field_get_index(&gf, 4), v);
	TEST(int_field_add(&gf, w), 5);
	TEST(int_field_get_index(&gf, 5), w);
	TEST(int_field_count(&gf), 6);
	TEST((int_field_exclude_index(&gf, 1), EOK), EOK);
	TEST(int_field_get_index(&gf, 1), NULL);
	TEST(int_field_get_index(&gf, 3), u);
	TEST((int_field_exclude_index(&gf, 7), EOK), EOK);
	TEST(int_field_get_index(&gf, 3), u);
	TEST(int_field_get_index(&gf, 5), w);
	TEST((int_field_exclude_index(&gf, 4), EOK), EOK);
	TEST(int_field_get_index(&gf, 4), NULL);
	TEST((int_field_destroy(&gf), EOK), EOK);
	TEST(int_field_count(&gf), -1);
	
	printf("\nInt map test");
	
	XMALLOC(x, int);
	XMALLOC(y, int);
	XMALLOC(z, int);
	XMALLOC(u, int);
	XMALLOC(v, int);
	XMALLOC(w, int);
	
	int_map_t im;
	im.magic = 0;
	
	TEST(int_map_add(&im, 1, x), EINVAL);
	TEST(int_map_count(&im), -1);
	TEST(int_map_initialize(&im), EOK);
	TEST(int_map_count(&im), 0);
	TEST(int_map_find(&im, 1), NULL);
	TEST(int_map_add(&im, 1, x), 0);
	TEST(int_map_find(&im, 1), x);
	TEST((int_map_exclude(&im, 1), EOK), EOK);
	TEST(int_map_find(&im, 1), NULL);
	TEST(int_map_add(&im, 1, y), 1);
	TEST(int_map_find(&im, 1), y);
	TEST(int_map_add(&im, 4, z), 2);
	TEST(int_map_get_index(&im, 2), z);
	TEST(int_map_find(&im, 4), z);
	TEST(int_map_find(&im, 1), y);
	TEST(int_map_count(&im), 3);
	TEST(int_map_add(&im, 2, u), 3);
	TEST(int_map_find(&im, 2), u);
	TEST(int_map_add(&im, 3, v), 4);
	TEST(int_map_find(&im, 3), v);
	TEST(int_map_get_index(&im, 4), v);
	TEST(int_map_add(&im, 6, w), 5);
	TEST(int_map_find(&im, 6), w);
	TEST(int_map_count(&im), 6);
	TEST((int_map_exclude(&im, 1), EOK), EOK);
	TEST(int_map_find(&im, 1), NULL);
	TEST(int_map_find(&im, 2), u);
	TEST((int_map_exclude(&im, 7), EOK), EOK);
	TEST(int_map_find(&im, 2), u);
	TEST(int_map_find(&im, 6), w);
	TEST((int_map_exclude_index(&im, 4), EOK), EOK);
	TEST(int_map_get_index(&im, 4), NULL);
	TEST(int_map_find(&im, 3), NULL);
	TEST((int_map_destroy(&im), EOK), EOK);
	TEST(int_map_count(&im), -1);
	
	printf("\nMeasured strings test");
	
	measured_string_ref string =
	    measured_string_create_bulk("I am a measured string!", 0);
	printf("\n%x, %s at %x of %d\n", string, string->value, string->value,
	    string->length);
	
	return EOK;
}