RGB colormap_threecolor(const double x) { RGB pix; pix.r = map_func(x); pix.g = map_func(x + 1.0 / 3.0); pix.b = map_func(x + 2.0 / 3.0); return pix; }
int add_to_board(shape s, board * b){ int i, j; int * map; int * (*map_func)(int i, int j) = get_map_func(s, 0); for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ map = map_func(i , j); //set piece in board if(CHECK_BIT(shape_bytes[s.type][BYTE_MAP(map)], BIT_MAP(map))){ if(s.y == -1) return -1; b->xy[s.x + X_MAP(map)][s.y + Y_MAP(map)] = s.type + 1; } } } return 1; }
int draw_shape(shape s, int board_x, int board_y){ int i, j; int * map; int * (*map_func)(int i, int j) = get_map_func(s, 0); int y_vals[4]; int height = 0; attron(COLOR_PAIR(s.type+1)); for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ map = map_func(i , j); if(CHECK_BIT(shape_bytes[s.type][BYTE_MAP(map)], BIT_MAP(map))){ mvaddch(s.y + Y_MAP(map) + board_y, ((s.x + X_MAP(map))*2) + (board_x) + 1, ' '|A_REVERSE); mvaddch(s.y + Y_MAP(map) + board_y, ((s.x + X_MAP(map))*2) + (board_x) + 2, ' '|A_REVERSE); if(!in_array(s.y + Y_MAP(map), y_vals, 4)){ y_vals[height] = s.y + Y_MAP(map); height++; } }/*else{ mvaddch(s.y + Y_MAP(map) + board_y, ((s.x + X_MAP(map))*2) + (board_x*2) + 1, 'x'); mvaddch(s.y + Y_MAP(map) + board_y, ((s.x + X_MAP(map))*2) + (board_x*2) + 2, 'x'); }*/ } } attroff(COLOR_PAIR(s.type+1)); return height; }
/* Map the function MAP_FUNC(E, DATA) over all innermost extents E containing part of the section of TX specified by START, END. This function can be (and is) safely longjmp'd through. */ void map_section_extents(void (*map_func)(Lisp_Extent *x, void *data), Lisp_Extent *root, Pos *start, Pos *end, void *data) { Lisp_Extent *x = find_extent(root, start); Pos s_copy = *start, e_copy = *end; intptr_t delta = row_delta(x->parent); s_copy.row -= delta; e_copy.row -= delta; while(x != 0 && PPOS_LESS_P(&x->start, &e_copy)) { if(PPOS_GREATER_P(&x->end, &s_copy)) { /* Deleting X in here would screw things up.. Not much that can be done though. And it shouldn't crash (famous last words..) */ map_func(x, data); } /* Try to work downwards and rightwards as much as possible */ if(x->first_child != 0) { /* Map though X's children as well. */ s_copy.row -= x->start.row; e_copy.row -= x->start.row; x = x->first_child; } else if(x->right_sibling != 0) x = x->right_sibling; else if(x->parent != 0) { s_copy.row += x->parent->start.row; e_copy.row += x->parent->start.row; x = x->parent->right_sibling; } else break; assert_invariants (x); } }
int check_move(shape s, board b, int x_direction, int y_direction, int rotation){ shape s_new = s; s_new.rotation += rotation; if(s_new.rotation == 4) s_new.rotation = 0; else if(s_new.rotation == -1) s_new.rotation = 3; int i, j; int * map; int * (*map_func)(int i, int j) = get_map_func(s_new, 0); for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ map = map_func(i , j); if(CHECK_BIT(shape_bytes[s_new.type][BYTE_MAP(map)], BIT_MAP(map))){ //check board boundaries if( (s.x + X_MAP(map) + x_direction) < 0 || (s.x + X_MAP(map) + x_direction) > (b.width - 1) || (s.y + Y_MAP(map) + y_direction) < 0 || (s.y + Y_MAP(map) + y_direction) > (b.height - 1)){ return 0; } //check within board if(b.xy[s.x + X_MAP(map) + x_direction][s_new.y + Y_MAP(map) + y_direction] > 0){ return 0; } } } } return 1; }
void map(int array[], size_t len, int (*map_func)(int)) { size_t i = 0; for(; i < len; ++i) array[i] = map_func(array[i]); }