int main() { int pa_hora, pa_min, pb_hora, pb_min; int cb_hora, cb_min, ca_hora, ca_min; scanf("%d:%d %d:%d ", &pa_hora, &pa_min, &cb_hora, &cb_min); scanf("%d:%d %d:%d", &pb_hora, &pb_min, &ca_hora, &ca_min); int pa = pa_min + NUM_SEG_PER_HORA * pa_hora; int pb = pb_min + NUM_SEG_PER_HORA * pb_hora; int ca = ca_min + NUM_SEG_PER_HORA * ca_hora; int cb = cb_min + NUM_SEG_PER_HORA * cb_hora; int tempo_ida = cb - pa; int tempo_volta = ca - pb; int tempo_real = (tempo_ida + tempo_volta)/2; tempo_real = pos_mod(tempo_real, MEIO_DIA); int fuso = pos_mod(tempo_ida - tempo_real, UM_DIA); if (fuso > MEIO_DIA) { fuso -= UM_DIA; } fuso /= NUM_SEG_PER_HORA; printf("%d %d\n", tempo_real, fuso) ; return 0; }
void move_player(enum direction direction){ int x, y, ele; int xx, yy; struct square *from, *to; struct tile *tilefrom, *tileto; x = xx = player.x; y = yy = player.y; ele = player.ele; switch(direction){ case(NW): xx = x - 1; case(N): yy = y - 1; break; case(SW): yy = y + 1; case(W): xx = x - 1; break; case(SE): xx = x + 1; case(S): yy = y + 1; break; case(NE): yy = y - 1; case(E): xx = x + 1; break; } tilefrom = get_tile(pos_div(x, TILESIZE), pos_div(y, TILESIZE), 0); if(pos_div(x, TILESIZE) == pos_div(xx, TILESIZE) && pos_div(y, TILESIZE) == pos_div(yy, TILESIZE)) tileto = tilefrom; else tileto = get_tile(pos_div(xx, TILESIZE), pos_div(yy, TILESIZE), 0); from = tilefrom->type == Array? SQUARE(tilefrom->tile.sq, pos_mod(x, TILESIZE), pos_mod(y, TILESIZE)) : tilefrom->tile.fill; to = tileto->type == Array? SQUARE(tileto->tile.sq, pos_mod(xx, TILESIZE), pos_mod(yy, TILESIZE)) : tileto->tile.fill; // TODO handle elevation / going to adjacent layers if(to->c){ // TODO FIGHT or do other things I guess hit(to->c,&player); } else { // try moving switch(to->terrain){ case(TERRAIN_ROCK_WALL): putmsg("You bump into a wall."); break; default: from->c = 0; player.x = xx; player.y = yy; to->c = &player; draw_map(xx, yy, ele); } } }
struct pos * pos2room (struct pos *p, int room, struct pos *pv) { *pv = *p; /* npos (pv, pv); */ if (pv->room == room) return pv; struct pos pb, pa, pl, pr; pb = pa = pl = pr = *pv; int mpb, mpa, mpr, mpl; mpb = mpa = mpr = mpl = INT_MAX; int ra, rb, rl, rr; ra = roomd (pv->l, room, ABOVE); rb = roomd (pv->l, room, BELOW); rl = roomd (pv->l, room, LEFT); rr = roomd (pv->l, room, RIGHT); if (rb == pv->room) { pb.floor += FLOORS; pb.room = room; mpb = pos_mod (&pb, p); } if (ra == pv->room) { pa.floor -= FLOORS; pa.room = room; mpa = pos_mod (&pa, p); } if (rr == pv->room) { pr.place += PLACES; pr.room = room; mpr = pos_mod (&pr, p); } if (rl == pv->room) { pl.place -= PLACES; pl.room = room; mpl = pos_mod (&pl, p); } int lm = mpb; lm = min_int (lm, mpa); lm = min_int (lm, mpr); lm = min_int (lm, mpl); if (lm == mpb) *pv = pb; else if (lm == mpa) *pv = pa; else if (lm == mpr) *pv = pr; else if (lm == mpl) *pv = pl; return pv; }
void hsm_find_local_maxima_circ(int n, const double*f, int*maxima, int*nmaxima) { *nmaxima = 0; for(int i=0;i<n;i++) { double val = f[i]; double left = f[ pos_mod(i-1,n) ]; double right = f[ pos_mod(i+1,n) ]; if( (val>0) && (val>left) && (val>right)) maxima[(*nmaxima)++] = i; } }
struct square* get_square(int x, int y, int ele){ return SQUARE(get_tile(pos_div(x, TILESIZE), pos_div(y, TILESIZE), ele)->tile.sq, pos_mod(x, TILESIZE), pos_mod(y, TILESIZE)); }
void hsm_match(struct hsm_params*p, hsm_buffer b1, hsm_buffer b2) { sm_log_push("hsm_match"); /* Let's measure the time */ clock_t hsm_match_start = clock(); assert(b1->num_angular_cells == b2->num_angular_cells); assert(p->max_translation > 0); assert(b1->linear_cell_size > 0); b1->num_valid_results = 0; /* Compute cross-correlation of spectra */ hsm_circular_cross_corr_stupid(b1->num_angular_cells, b2->hs, b1->hs, b1->hs_cross_corr); /* Find peaks in cross-correlation */ int peaks[p->num_angular_hypotheses], npeaks; hsm_find_peaks_circ(b1->num_angular_cells, b1->hs_cross_corr, p->angular_hyp_min_distance_deg, 0, p->num_angular_hypotheses, peaks, &npeaks); sm_debug("Found %d peaks (max %d) in cross correlation.\n", npeaks, p->num_angular_hypotheses); if(npeaks == 0) { sm_error("Cross correlation of spectra has 0 peaks.\n"); sm_log_pop(); return; } sm_log_push("loop on theta hypotheses"); /* lag e' quanto 2 si sposta a destra rispetto a 1 */ for(int np=0;np<npeaks;np++) { int lag = peaks[np]; double theta_hypothesis = lag * (2*M_PI/b1->num_angular_cells); sm_debug("Theta hyp#%d: lag %d, angle %fdeg\n", np, lag, rad2deg(theta_hypothesis)); /* Superimpose the two spectra */ double mult[b1->num_angular_cells]; for(int r=0;r<b1->num_angular_cells;r++) mult[r] = b1->hs[r] * b2->hs[pos_mod(r-lag, b1->num_angular_cells)]; /* Find directions where both are intense */ int directions[p->xc_ndirections], ndirections; hsm_find_peaks_circ(b1->num_angular_cells, b1->hs_cross_corr, p->xc_directions_min_distance_deg, 1, p->xc_ndirections, directions, &ndirections); if(ndirections<2) { sm_error("Too few directions.\n"); } struct { /* Direction of cross correlation */ double angle; int nhypotheses; struct { double delta; double value; } hypotheses[p->linear_xc_max_npeaks]; } dirs[ndirections]; sm_debug("Using %d (max %d) correlations directions.\n", ndirections, p->xc_ndirections); int max_lag = (int) ceil(p->max_translation / b1->linear_cell_size); int min_lag = -max_lag; sm_debug("Max lag: %d cells (max t: %f, cell size: %f)\n", max_lag, p->max_translation, b1->linear_cell_size); sm_log_push("loop on xc direction"); /* For each correlation direction */ for(int cd=0;cd<ndirections;cd++) { dirs[cd].angle = theta_hypothesis + (directions[cd]) * (2*M_PI/b1->num_angular_cells); printf(" cd %d angle = %d deg\n", cd, (int) rad2deg(dirs[cd].angle)); /* Do correlation */ int lags [2*max_lag + 1]; double xcorr [2*max_lag + 1]; int i1 = pos_mod(directions[cd] , b1->num_angular_cells); int i2 = pos_mod(directions[cd] + lag , b1->num_angular_cells); double *f1 = b1->ht[i1]; double *f2 = b2->ht[i2]; hsm_linear_cross_corr_stupid( b2->num_linear_cells,f2, b1->num_linear_cells,f1, xcorr, lags, min_lag, max_lag); /* Find peaks of cross-correlation */ int linear_peaks[p->linear_xc_max_npeaks], linear_npeaks; hsm_find_peaks_linear( 2*max_lag + 1, xcorr, p->linear_xc_peaks_min_distance/b1->linear_cell_size, p->linear_xc_max_npeaks, linear_peaks, &linear_npeaks); sm_debug("theta hyp #%d: Found %d (max %d) peaks for correlation.\n", cd, linear_npeaks, p->linear_xc_max_npeaks); dirs[cd].nhypotheses = linear_npeaks; sm_log_push("Considering each peak of linear xc"); for(int lp=0;lp<linear_npeaks;lp++) { int linear_xc_lag = lags[linear_peaks[lp]]; double value = xcorr[linear_peaks[lp]]; double linear_xc_lag_m = linear_xc_lag * b1->linear_cell_size; sm_debug("lag: %d delta: %f value: %f \n", linear_xc_lag, linear_xc_lag_m, value); dirs[cd].hypotheses[lp].delta = linear_xc_lag_m; dirs[cd].hypotheses[lp].value = value; } sm_log_pop(); if(p->debug_true_x_valid) { double true_delta = cos(dirs[cd].angle) * p->debug_true_x[0] + sin(dirs[cd].angle) * p->debug_true_x[1]; sm_debug("true_x delta = %f \n", true_delta ); } } /* xc direction */ sm_log_pop(); sm_debug("Now doing all combinations. How many are there?\n"); int possible_choices[ndirections]; int num_combinations = 1; for(int cd=0;cd<ndirections;cd++) { possible_choices[cd] = dirs[cd].nhypotheses; num_combinations *= dirs[cd].nhypotheses; } sm_debug("Total: %d combinations\n", num_combinations); sm_log_push("For each combination.."); for(int comb=0;comb<num_combinations;comb++) { int choices[ndirections]; hsm_generate_combinations(ndirections, possible_choices, comb, choices); /* Linear least squares */ double M[2][2]={{0,0},{0,0}}; double Z[2]={0,0}; /* heuristic quality value */ double sum_values = 0; for(int cd=0;cd<ndirections;cd++) { double angle = dirs[cd].angle; double c = cos(angle), s = sin(angle); double w = dirs[cd].hypotheses[choices[cd]].value; double y = dirs[cd].hypotheses[choices[cd]].delta; M[0][0] += c * c * w; M[1][0] += c * s * w; M[0][1] += c * s * w; M[1][1] += s * s * w; Z[0] += w * c * y; Z[1] += w * s * y; sum_values += w; } double det = M[0][0]*M[1][1]-M[0][1]*M[1][0]; double Minv[2][2]; Minv[0][0] = M[1][1] * (1/det); Minv[1][1] = M[0][0] * (1/det); Minv[0][1] = -M[0][1] * (1/det); Minv[1][0] = -M[1][0] * (1/det); double t[2] = { Minv[0][0]*Z[0] + Minv[0][1]*Z[1], Minv[1][0]*Z[0] + Minv[1][1]*Z[1]}; /* copy result in results slot */ int k = b1->num_valid_results; b1->results[k][0] = t[0]; b1->results[k][1] = t[1]; b1->results[k][2] = theta_hypothesis; b1->results_quality[k] = sum_values; b1->num_valid_results++; } sm_log_pop(); } /* theta hypothesis */ sm_log_pop(); /* for(int i=0;i<b1->num_valid_results;i++) { printf("#%d %.0fdeg %.1fm %.1fm quality %f \n",i, rad2deg(b1->results[i][2]), b1->results[i][0], b1->results[i][1], b1->results_quality[i]); }*/ /* Sorting based on values */ int indexes[b1->num_valid_results]; for(int i=0;i<b1->num_valid_results;i++) indexes[i] = i; qsort_descending(indexes, (size_t) b1->num_valid_results, b1->results_quality); /* copy in the correct order*/ double*results_tmp[b1->num_valid_results]; double results_quality_tmp[b1->num_valid_results]; for(int i=0;i<b1->num_valid_results;i++) { results_tmp[i] = b1->results[i]; results_quality_tmp[i] = b1->results_quality[i]; } for(int i=0;i<b1->num_valid_results;i++) { b1->results[i] = results_tmp[indexes[i]]; b1->results_quality[i] = results_quality_tmp[indexes[i]]; } for(int i=0;i<b1->num_valid_results;i++) { char near[256]=""; double *x = b1->results[i]; if(p->debug_true_x_valid) { double err_th = rad2deg(fabs(angleDiff(p->debug_true_x[2],x[2]))); double err_m = hypot(p->debug_true_x[0]-x[0], p->debug_true_x[1]-x[1]); const char * ast = (i == 0) && (err_th > 2) ? " ***** " : ""; sprintf(near, "th err %4d err_m %5f %s",(int)err_th ,err_m,ast); } if(i<10) printf("after #%d %3.1fm %.1fm %3.0fdeg quality %5.0f \t%s\n",i, x[0], x[1], rad2deg(x[2]), b1->results_quality[i], near); } /* How long did it take? */ clock_t hsm_match_stop = clock(); int ticks = hsm_match_stop-hsm_match_start; double ctime = ((double)ticks) / CLOCKS_PER_SEC; sm_debug("Time: %f sec (%d ticks)\n", ctime, ticks); sm_log_pop(); }