double SheepReadBC::sampleTime(double t, double xcoord, double ycoord) { mooseAssert(_x.size() > 0, "Sampling an empty SheepReadBC."); int posi; int posj; bool found = false; if(t <= _x[0]) { posi = 0; for(unsigned int i(0); i < _nx * _ny; ++i) if( (std::fabs(_px(posi, i) - xcoord) < 0.1) && (std::fabs(_py(posi, i)- ycoord) < 0.1) ) return _pv(posi, i); mooseError("Unreachable? Dirichlet: x="<<xcoord<<" , y="<<ycoord<<" , file="<<_y[posi]<<" !!" ); } else if(t >= _x.back()) { posi = _x.size()-1; for(unsigned int i(0); i < _nx * _ny; ++i) if( (std::fabs(_px(posi, i) - xcoord) < 0.1) && (std::fabs(_py(posi, i)- ycoord) < 0.1) ) return _pv(posi, i); mooseError("Unreachable? Dirichlet: x="<<xcoord<<" , y="<<ycoord<<" , file="<<_y[posi]<<" !!" ); } else { found = false; for(unsigned int i(0); i+1 < _x.size(); ++i) { if( t >= _x[i] && t < _x[i+1]) { posi = i; found = true; break; } } if( !found ) mooseError("Unreachable? sampleTime: x="<<xcoord<<" , y="<<ycoord<<" , posi="<<posi<<" !!" ); found = false; for(unsigned int j(0); j < _nx* _ny; ++j) { if(std::fabs(_px(posi, j) - xcoord) < 0.1 && std::fabs(_py(posi, j) - ycoord) < 0.1) { posj = j; found = true; break; } } if( !found ) mooseError("Unreachable? sampleTime: x="<<xcoord<<" , y="<<ycoord<<" , posj="<<posj<<" !!" ); return _pv(posi,posj) + (_pv(posi+1,posj) - _pv(posi,posj)) * (t - _x[posi])/(_x[posi+1] - _x[posi]); } return 0; }
double SheepReadBC::sampleDerivative(double t, double xcoord, double ycoord) { int posi; int posj; mooseAssert(_x.size() > 0, "Sampling an empty SheepReadBC."); if(t <= _x[0]) return 0.0; if(t >= _x[_x.size()-1]) return 0.0; for(unsigned int i(0); i+1 < _x.size(); ++i) { if( t >= _x[i] && t < _x[i+1]) { posi = i; break; } } if(posi + 1 == _x.size()) mooseError("Unreachable? sampleDerivative: x="<<xcoord<<" , y="<<ycoord<<" , posi="<<posi<<" !!" ); for(unsigned int j(0); j < _nx* _ny; ++j) { if(std::fabs(_px(posi, j) - xcoord) < 0.1 && std::fabs(_py(posi, j) - ycoord) < 0.1) { posj = j; break; } } if(posj + 1 == (_nx*_ny)) mooseError("Unreachable? sampleDerivative: x="<<xcoord<<" , y="<<ycoord<<" , posj="<<posj<<" !!" ); return ( _pv(posi + 1,posj) - _pv(posi,posj) ) / ( _x[posi+1] - _x[posi] ); }
void lock_piece() { int cx,cy; int w=curr_piece.t->mask_size,h=curr_piece.t->mask_size; int fw=f->w, fh=f->h; char *frame = &curr_piece.t->mask[curr_piece.t->mask_size * curr_piece.t->mask_size * curr_piece.rot]; for(cy=h-1;cy>=0;--cy) { for(cx=w-1;cx>=0;--cx) { if(frame[cy*w+cx] != 0) f->field[(cy+_py(curr_piece.y))*fw+curr_piece.x+cx] = frame[cy*w+cx]; } } }
double SheepReadBC::integrate(double xcoord, double ycoord) { double answer(0); int posi; for(unsigned int i(0); i < _nx* _ny; ++i) { if(std::fabs(_px(0, i) - xcoord) < 0.1 && std::fabs(_py(0, i) - ycoord) < 0.1) { posi = i; break; } } for(unsigned int i(1); i < _x.size(); ++i) answer += 0.5*(_pv(i, posi) + _pv(i-1, posi))*(_x[i] - _x[i-1]); return answer; }
void drop_draw(drop d, SDL_Surface *s, int x, int y) { array_draw(d.t->mask+(d.t->mask_size*d.t->mask_size)*d.rot, d.t->mask_size, d.t->mask_size, s, x+BLOCK_SIZE*d.x, y+BLOCK_SIZE*_py(d.y), 0, 0, 0); }
void game_frame(SDL_Surface *screen) { game_handle_keys(); switch(game_state) { case TS_FALLING: curr_piece.y += 4; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.y = _bx(_py(curr_piece.y)-1); ++curr_piece.lock; } if(game_get_key_DAS(TK_DOWN)) curr_piece.y += 0xFF; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.y = _bx(_py(curr_piece.y)-1); curr_piece.lock = T_LOCKDELAY+1; } if(game_get_key(TK_UP)) { while(!collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.y += 0xFF; curr_piece.y -= 0xFF; } if(game_get_key_DAS(TK_RIGHT)) { curr_piece.x += 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.x -= 1; } if(game_get_key_DAS(TK_LEFT)) { curr_piece.x -= 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.x += 1; } int init_rot = curr_piece.rot; if(game_get_key(TK_A)||game_get_key(TK_B)||game_get_key(TK_C)) { if(game_get_key(TK_A)||game_get_key(TK_C)) { curr_piece.rot -= 1; if(curr_piece.rot < 0) curr_piece.rot = curr_piece.t->frames-1; } else if(game_get_key(TK_B)) { curr_piece.rot += 1; if(curr_piece.rot >= curr_piece.t->frames) curr_piece.rot = 0; } if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x -= 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x += 2; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x -= 1; curr_piece.rot = init_rot; } } } } if(curr_piece.lock >= T_LOCKDELAY) { game_state = TS_LINECLEAR; clear_time = T_CLEARDELAY; lock_piece(); } break; case TS_LINECLEAR: if(clear_time == T_CLEARDELAY) { int x,y,numclear=0,empty; for(y=f->h-1; y>=0; --y) { empty = 0; for(x=f->w-1; x>=0; --x) { if(f->field[y*f->w + x] == 0) { empty = 1; break; } } if(empty == 0) { for(x=f->w-1; x>=0; --x) f->field[y*f->w + x] = 0xFE; ++numclear; } } if(numclear == 0) { game_state = TS_ENTRY; break; } } --clear_time; if(clear_time == 0) { int y,cy,cx; for(y=0; y<f->h; ++y) { if((unsigned char)f->field[y*f->w+0] == 0xFE) { printf("clearing line %i\n", y); for(cy=y; cy>0; --cy) for(cx=f->w-1; cx>=0; --cx) f->field[cy*f->w+cx] = f->field[(cy-1)*f->w+cx]; } } game_state = TS_ENTRY; } break; case TS_ENTRY: --are_time; if(are_time == 0) { are_time = T_ENTRYDELAY; game_next_piece(); game_state = TS_FALLING; } break; } SDL_Rect rect = { (SCREEN_WIDTH) / 2 - (10 * BLOCK_SIZE) / 2, (SCREEN_HEIGHT) - (20 * BLOCK_SIZE) - 20, BLOCK_SIZE * 10 + 1, BLOCK_SIZE * 20 + 1}; SDL_Rect next_rect = { (rect.x) + ((BLOCK_SIZE * 10/2) - (BLOCK_SIZE * 4/2)), rect.y - (4 * BLOCK_SIZE) - 10, 4 * BLOCK_SIZE+1, 4 * BLOCK_SIZE+1}; SDL_FillRect(screen, &rect, block_color[0xFF]); field_draw(f, screen, rect.x, rect.y); next_draw(next_piece, screen, next_rect.x, next_rect.y + (4-next_piece->mask_size)*BLOCK_SIZE); if(game_state == TS_FALLING) drop_draw(curr_piece, screen, rect.x, rect.y); }
double SheepReadBC::sample(double t, double xcoord, double ycoord) { mooseAssert(_x.size() > 0, "Sampling an empty SheepReadBC."); int posi; int posj; if(_y.size()>1) { for(unsigned int i(0); i <_y.size() - 1; ++i) { if(t <= _x[0]) { posi = 0; break; } else if(t >= _x.back()) { posi = _x.size()-1; break; } else if(t >= _x[i] && t < _x[i+1]) { posi = i; break; } } } else { if(t != _x[0]) { mooseError("In SheepReadBC::sample : t= "<<t<<" but _x= "<<_x[0]); } posi = 0; } if(!_has_neumann) { for(unsigned int i(0); i < _nx * _ny; ++i) { if( (std::fabs(_px(posi, i) - xcoord) < 0.1) && (std::fabs(_py(posi, i)- ycoord) < 0.1) ) { return _pv(posi, i); } } mooseError("Unreachable? Dirichlet: x="<<xcoord<<" , y="<<ycoord<<" , file="<<_y[posi]<<" !!" ); } else { double value = 0.0; int count = 0; for(unsigned int i(0); i < _ny - 1; ++i) for(unsigned int j(0); j < _nx - 1; ++j) if( (_px(posi,(i * _nx) + j) < xcoord && _px(posi,(i * _nx) + j + 1) > xcoord) && (_py(posi,(i * _nx) + j) < ycoord && _py(posi,(i + 1)*_nx + j) > ycoord)) { value += _pv(posi, (i * _nx) + j) + _pv(posi, (i * _nx) + j + 1) + _pv(posi, (i + 1) * _nx + j) + _pv(posi, (i + 1) * _nx + j + 1); value /= 4; count++; } if(count > 0) value /= count; else if (count == 0) mooseError("Unreachable? Neumann: x="<<xcoord<<" , y="<<ycoord<<" , file="<<_y[posi]<< count <<" !!" ); return value; } return 0; }