hschumann2/TempleOS-Source-Code
0838
1 2#define VR_ONE_FRIENDLY_UNIT 03#define VR_UPDATE_FRIENDLY_UNIT 14#define VR_FRIENDLY_UNIT_DIED 35#define VR_ONE_ENEMY_UNIT 46#define VR_ALL_UNITS 57 8class MPCtrl19{10 I64 mode,lo,hi;11 Unit *tmpu;12};13 14class MPCtrl215{16 I64 lo,hi,row,col;17};18 19 20U0 VRSetUp(I64 player)21{22 I64 i;23 Unit *ut0,*ut1;24 ut0=&units[player][0];25 ut1=&units[player^1][0];26 for (i=0;i<UNITS_NUM;i++,ut0++,ut1++) {27 LBtr(&ut1->vis[player],0);28 LBEqu(&ut0->vis[player],0,ut0->life>0);29 }30}31 32U0 VRMerge(I64 player)33{34 I64 i,j;35 Unit *ut1;36 U8 *dst,*src,*mask=CAlloc((UNITS_NUM+7)>>3);37 for (j=0;j<UNITS_NUM;j++) {//p038 src=&vis_unit_bitmap[player][(((UNITS_NUM+7)&~7)*j)>>3];39 dst=mask;40 for (i=0;i<(UNITS_NUM+7)>>3;i++) //player141 *dst++|=*src++;42 }43 ut1=&units[player^1][0];44 for (j=0;j<UNITS_NUM;j++,ut1++)45 LBEqu(&ut1->vis[player],0,Bt(mask,j) && ut1->life>0);46 Free(mask);47}48 49Bool MPVisRecalc(MPCtrl1 *job)50{51 Bool res=FALSE,seen;52 I64 i,j,row,col;53 F64 x1,y1,x2,y2,dd,range;54 Unit *ut0,*ut1;55 ut0=&units[cur_player][job->lo];56 ut1=&units[enemy_player][job->lo];57 if (job->tmpu) {58 row=job->tmpu->row;59 col=job->tmpu->col;60 range=job->tmpu->range*2*HEX_RADIUS;61 range*=range;62 }63 switch (job->mode) {64 case VR_UPDATE_FRIENDLY_UNIT:65 case VR_ONE_FRIENDLY_UNIT:66 if (job->mode==VR_UPDATE_FRIENDLY_UNIT)67 range=F64_MAX;68 RowCol2XY(&x1,&y1,row,col);69 for (i=job->lo;i<job->hi;i++,ut1++) {70 seen=FALSE;71 if (ut1->life>0 &&72 LOS(row,col,ut1->row,ut1->col)) {73 RowCol2XY(&x2,&y2,ut1->row,ut1->col);74 dd=Sqr(x2-x1)+Sqr(y2-y1);75 if (dd<range) {76 seen=TRUE;77 LBts(&ut1->vis[cur_player],0);78 }79 }80 if (job->mode==VR_UPDATE_FRIENDLY_UNIT)81 LBEqu(&vis_unit_bitmap[cur_player],82 i+job->tmpu->num*((UNITS_NUM+7)&~7),seen);83 }84 break;85 case VR_ONE_ENEMY_UNIT:86 RowCol2XY(&x1,&y1,row,col);87 for (i=job->lo;i<job->hi;i++,ut1++)88 if (ut1->life>0 &&89 LOS(row,col,ut1->row,ut1->col)) {90 LBts(&vis_unit_bitmap[enemy_player],91 job->tmpu->num+i*((UNITS_NUM+7)&~7));92 res=TRUE;93 } else94 LBtr(&vis_unit_bitmap[enemy_player],95 job->tmpu->num+i*((UNITS_NUM+7)&~7));96 break;97 case VR_ALL_UNITS:98 ut0=&units[cur_player][0];99 for (i=0;i<UNITS_NUM;i++,ut0++)100 if (ut0->life>0) {101 RowCol2XY(&x1,&y1,ut0->row,ut0->col);102 ut1=&units[enemy_player][job->lo];103 for (j=job->lo;j<job->hi;j++,ut1++) {104 if (ut1->life>0 &&105 LOS(ut0->row,ut0->col,ut1->row,ut1->col)) {106 LBts(&ut1->vis[cur_player],0);107 LBts(&vis_unit_bitmap[cur_player],j+i*((UNITS_NUM+7)&~7));108 } else109 LBtr(&vis_unit_bitmap[cur_player],j+i*((UNITS_NUM+7)&~7));110 }111 } else112 for (j=job->lo;j<job->hi;j++)113 LBtr(&vis_unit_bitmap[cur_player],j+i*((UNITS_NUM+7)&~7));114 ut0=&units[enemy_player][0];115 for (i=0;i<UNITS_NUM;i++,ut0++)116 if (ut0->life>0) {117 RowCol2XY(&x1,&y1,ut0->row,ut0->col);118 ut1=&units[cur_player][job->lo];119 for (j=job->lo;j<job->hi;j++,ut1++) {120 if (ut1->life>0 &&121 LOS(ut0->row,ut0->col,ut1->row,ut1->col)) {122 LBts(&ut1->vis[enemy_player],0);123 LBts(&vis_unit_bitmap[enemy_player],j+i*((UNITS_NUM+7)&~7));124 } else125 LBtr(&vis_unit_bitmap[enemy_player],j+i*((UNITS_NUM+7)&~7));126 }127 } else128 for (j=job->lo;j<job->hi;j++)129 LBtr(&vis_unit_bitmap[enemy_player],j+i*((UNITS_NUM+7)&~7));130 break;131 }132 return res;133}134 135Bool VisRecalc(I64 mode,Unit *tmpu=NULL)136{137 I64 i,hi,k,cnt;138 Bool res;139/*The compiler doesn't go out of it's way140to know if something is const.;-) This141just compiles with the val at compile142time, an advantage of just-in-time over143AOT binaries. TempleOS has a limited144stk size, so don't get in the habit.145MAlloc() would probably be the better choice.146*/147 MPCtrl1 job[mp_cnt];148 CJob *cmd[mp_cnt];149 150 if (mode==VR_FRIENDLY_UNIT_DIED) {151 MemSet((&vis_unit_bitmap[enemy_player])(U8 *)+152 (tmpu->num*((UNITS_NUM+7)&~7))>>3,0,(UNITS_NUM+7)>>3);153 VRMerge(enemy_player);154 return 0; //Return any value--don't care155 }156 157 cnt=mp_cnt; //Cores158 hi=UNITS_NUM;159 if (mode==VR_ONE_ENEMY_UNIT) {160 for (hi--;hi>=0;hi--)161 if (units[enemy_player][hi].life>0)162 break;163 hi++;164 }165 k=hi;166 if (hi/mp_cnt<2)167 cnt=1;168 for (i=0;i<cnt;i++) {169 job[i].mode=mode;170 job[i].tmpu=tmpu;171 job[i].hi=k;172 k-=hi/cnt;173 if (k<0) k=0;174 if (i==cnt-1) k=0;175 job[i].lo=k;176 }177 178 res=FALSE;179 for (i=0;i<cnt;i++)180 cmd[i]=JobQue(&MPVisRecalc,&job[i],i,0);181 for (i=0;i<cnt;i++)182 if (JobResGet(cmd[i]))183 res=TRUE;184 if (mode==VR_UPDATE_FRIENDLY_UNIT)185 VRMerge(cur_player);186 return res;187}188 189U0 MPVisRecalcMap(MPCtrl2 *job)190{191 I64 i,j;192 for (j=job->lo;j<job->hi;j++)193 for (i=0;i<map_cols;i++)194 if (LOS(job->row,job->col,j,i))195 vis_map[j][i]=TRUE;196 else197 vis_map[j][i]=FALSE;198}199 200U0 VisRecalcMap(I64 row,I64 col)201{202 I64 i,hi,k,cnt;203 MPCtrl2 job[mp_cnt];204 CJob *cmd[mp_cnt];205 206 cnt=mp_cnt; //Cores207 hi=map_rows;208 k=hi;209 if (hi/mp_cnt<2)210 cnt=1;211 for (i=0;i<cnt;i++) {212 job[i].row=row;213 job[i].col=col;214 job[i].hi=k;215 k-=hi/cnt;216 if (k<0) k=0;217 if (i==cnt-1) k=0;218 job[i].lo=k;219 }220 for (i=0;i<cnt;i++)221 cmd[i]=JobQue(&MPVisRecalcMap,&job[i],i,0);222 for (i=0;i<cnt;i++)223 JobResGet(cmd[i]);224}225 