hschumann2/TempleOS-Source-Code
0838
1 2Unit *UnitNearestFind(I64 row,I64 col,I64 player,Bool in_LOS,F64 range=-1)3{4 I64 i;5 F64 dd,best_dd=F64_MAX,x1,y1,x2,y2;6 Unit *best=NULL;7//Sqrt() is slow, so work with squared distances.8 if (range<0)9 range=F64_MAX;10 else11 range*=range;12 RowCol2XY(&x1,&y1,row,col);13 for (i=0;i<UNITS_NUM;i++)14 if (units[player][i].life>0) {15 if (!in_LOS || LOS(row,col,units[player][i].row,units[player][i].col)) {16 RowCol2XY(&x2,&y2,units[player][i].row,units[player][i].col);17 dd=Sqr(x2-x1)+Sqr(y2-y1);18 if (dd<=range && dd<best_dd) {19 best=&units[player][i];20 best_dd=dd;21 }22 }23 }24 return best;25}26 27U0 PlayerIndirect()28{29 Unit *target,*tmpu;30 I64 i;31 for (i=0;i<UNITS_NUM;i++) {32 UserChk;33 tmpu=&units[cur_player][i];34 if (tmpu->life>0 && tmpu->indirect_fire &&35 (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,36 tmpu->range*2*HEX_RADIUS)))37 IndirectAdd(tmpu,target->row,target->col);38 }39 throw('PhaseOvr',TRUE);40}41 42U0 PlayerMove()43{44 Unit *target,*tmpu;45 I64 i;46 F64 x,y;47 for (i=0;i<UNITS_NUM;i++) {48 UserChk;49 tmpu=&units[cur_player][i];50 if (tmpu->life>0) {51//Cheats because it violates Line-of-Sight52 if (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,FALSE)) {53 RowCol2XY(&x,&y,target->row,target->col);54 if (!UnitMove(tmpu,x,y)) {55 RowCol2XY(&x,&y,tmpu->row,tmpu->col);56 UnitMove(tmpu,x+RandI16,y+RandI16);57 }58 }59 }60 }61 throw('PhaseOvr',TRUE);62}63 64U0 PlayerDirect()65{66 Unit *target,*tmpu;67 I64 i;68 for (i=0;i<UNITS_NUM;i++) {69 UserChk;70 tmpu=&units[cur_player][i];71 if (tmpu->life>0 && !tmpu->indirect_fire &&72 (target=UnitNearestFind(tmpu->row,tmpu->col,enemy_player,TRUE,73 tmpu->range*2*HEX_RADIUS))) {74 UnitDirectFire(tmpu,target);75 Sleep(250*animation_delay);76 }77 }78 throw('PhaseOvr',TRUE);79}80 81 