static int get_discrete_uv( Context ctx, int time, int r, int c, int l, float *uptr, float *vptr, float *wptr ) { float u = get_grid_value( ctx, time, ctx->dpy_ctx->TrajU, r, c, l ); float v = get_grid_value( ctx, time, ctx->dpy_ctx->TrajV, r, c, l ); if (IS_MISSING(u) || IS_MISSING(v)) { /* missing */ return 0; } else { /* return u,v,w in boxes/sec */ *uptr = u * ctx->Uscale[r][c]; *vptr = v * ctx->Vscale[r][c]; *wptr = 0; return 1; } }
ShotResult TakeShot(Ocean &ocean, const Point &coordinate) { // Check edges. if (coordinate.x < 0 || coordinate.x > ocean.x_quadrants || coordinate.y < 0 || coordinate.y > ocean.y_quadrants) { return(srILLEGAL); } // Get the value for easy access. int gridvalue = *get_grid_value(ocean, coordinate.x, coordinate.y); // Miss test. if (gridvalue == 0) { ocean.stats.misses++; *get_grid_value(ocean, coordinate.x, coordinate.y) = -1; return(srMISS); } // Duplicate test. if (gridvalue == -1 || gridvalue > HIT_OFFSET) { ocean.stats.duplicates++; return(srDUPLICATE); } // If we're still here, it must've been a hit. Is it sunk? ocean.stats.hits++; ocean.boats[gridvalue].hits++; *get_grid_value(ocean, coordinate.x, coordinate.y) += HIT_OFFSET; if (ocean.boats[gridvalue].hits >= BOAT_LENGTH) { ocean.stats.sunk++; return(srSUNK); } return(srHIT); }
BoatPlacement PlaceBoat(Ocean &ocean, const Boat& boat) { // Perform edge checks. if (boat.position.x < 0 || boat.position.x > ocean.x_quadrants || boat.position.y < 0 || boat.position.y > ocean.y_quadrants) { return(bpREJECTED); } if ((boat.orientation == oHORIZONTAL && ((boat.position.x + BOAT_LENGTH) > ocean.x_quadrants))) { return(bpREJECTED); } if ((boat.orientation == oVERTICAL && ((boat.position.y + BOAT_LENGTH) > ocean.y_quadrants))) { return(bpREJECTED); } // Perform overlap checks. if (boat.orientation == oHORIZONTAL) { for (int i = 0; i < BOAT_LENGTH; i++) { if (*get_grid_value(ocean, boat.position.x + i, boat.position.y) != 0) { return(bpREJECTED); } } } else { for (int i = 0; i < BOAT_LENGTH; i++) { if (*get_grid_value(ocean, boat.position.x, boat.position.y + i) != 0) { return(bpREJECTED); } } } // All clear if it got here. if (boat.orientation == oHORIZONTAL) { for (int i = 0; i < BOAT_LENGTH; i++) { *get_grid_value(ocean, boat.position.x + i, boat.position.y) = boat.ID; } } else { for (int i = 0; i < BOAT_LENGTH; i++) { *get_grid_value(ocean, boat.position.x, boat.position.y + i) = boat.ID; } } ocean.boats[boat.ID].hits = 0; return(bpACCEPTED); }