hschumann2/TempleOS-Source-Code
0838
1 2U8 movement_costs[16];3movement_costs[PLAINS]=2;4movement_costs[TREES]=6;5movement_costs[MOUNTAINS]=10;6 7I64 HexMoveOneCost(Unit *tmpu,I64 r,I64 c,I64 facing)8{9 I64 res;10 if (tmpu->infantry)11 res=0;12 else {13 res=FacingChg(facing,tmpu->facing);14 if (res>0) res--;15 }16 if (roads[r][c] && roads[tmpu->row][tmpu->col])17 res+=1;18 else {19 if (tmpu->infantry)20 res+=2;21 else {22 res+=movement_costs[terrain[r][c]];23 if (rivers[r][c])24 res=tmpu->movement;25 }26 }27 return res;28}29 30I64 HexMoveOne(I64 *_row,I64 *_col,F64 x,F64 y)31{32 I64 direction,best_direction=-1,r,c;33 F64 dd,best_dd,x1,y1;34 RowCol2XY(&x1,&y1,*_row,*_col);35 best_dd=Sqr(x1-x)+Sqr(y1-y);36 for (direction=0;direction<6;direction++) {37 r=*_row; c=*_col;38 Toward(&r,&c,direction);39 RowCol2XY(&x1,&y1,r,c);40 dd=Sqr(x1-x)+Sqr(y1-y);41 if (0<=r<map_rows && 0<=c<map_cols && dd<best_dd) {42 best_dd=dd;43 best_direction=direction;44 }45 }46 if (best_direction>=0) {47 Toward(_row,_col,best_direction);48 return best_direction;49 } else50 return -1;51}52 53Bool UnitMovePlot(U0,I64 x,I64 y,I64)54{55 move_x=x; move_y=y;56 Sleep(5*animation_delay);57 return TRUE;58}59 60U0 UnitMoveAnimation(Unit *tmpu,I64 r,I64 c,I64 facing)61{62 F64 x1,y1,x2,y2,f=facing*60.0*pi/180.0;63 moving_unit=tmpu;64 RowCol2XY(&x1,&y1,tmpu->row,tmpu->col);65 move_x=x1; move_y=y1;66 moving=TRUE;67 if (tmpu->infantry)68 Snd(53);69 else {70 move_facing=tmpu->facing*60.0*pi/180.0;71 Snd(41);72 while (Wrap(f-move_facing,-pi)<=0) {73 move_facing-=0.03;74 Sleep(5*animation_delay);75 }76 while (Wrap(f-move_facing,-pi)>0) {77 move_facing+=0.03;78 Sleep(5*animation_delay);79 }80 Snd(34);81 }82 move_facing=f;83 RowCol2XY(&x2,&y2,r,c);84 Line(NULL,x1,y1,0,x2,y2,0,&UnitMovePlot);85 Snd;86 moving_unit=NULL;87 moving=FALSE;88}89 90Bool UnitMove(Unit *tmpu,I64 x,I64 y)91{92 Unit *target;93 I64 r,c,r0=tmpu->row,c0=tmpu->col,i,facing;94 while (tmpu->remaining_movement>0) {95 r=tmpu->row;96 c=tmpu->col;97 if ((facing=HexMoveOne(&r,&c,x,y))<0)98 break;99 else {100 i=HexMoveOneCost(tmpu,r,c,facing);101 if (i>tmpu->movement)102 i=tmpu->movement;103 if (!tmpu->fired && tmpu->remaining_movement>=i &&104 tmpu->remaining_movement>=tmpu->movement>>1 &&105 (target=UnitFind(r,c)) && target->player!=tmpu->player &&106 tmpu->infantry!=target->infantry) {107 if (!HexOccupy(ToBool(target->infantry),tmpu,target)) {108 tmpu=NULL;109 break;110 }111 i=tmpu->remaining_movement;112 }113 if (tmpu->remaining_movement>=i && !UnitFind(r,c)) {114 UnitMoveAnimation(tmpu,r,c,facing);115 tmpu->facing=facing;116 tmpu->remaining_movement-=i;117 tmpu->row=r;118 tmpu->col=c;119 VisRecalc(VR_UPDATE_FRIENDLY_UNIT,tmpu);120 LBEqu(&tmpu->vis[enemy_player],0,121 VisRecalc(VR_ONE_ENEMY_UNIT,tmpu));122 } else123 break;124 }125 }126 if (!tmpu || tmpu->row!=r0 || tmpu->col!=c0)127 return TRUE;128 else129 return FALSE;130}131 