00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "obj_actnpts.h"
00022
00023 #include "globals.h"
00024 #include "lev_data.h"
00025 #include "obj_slabs.h"
00026
00027 unsigned char get_actnpt_subtile_x(unsigned char *actnpt)
00028 {
00029 if (actnpt==NULL) return 0;
00030 return actnpt[1];
00031 }
00032
00033 short set_actnpt_subtile_x(unsigned char *actnpt,unsigned char pos_x)
00034 {
00035 if (actnpt==NULL) return 0;
00036 actnpt[1]=pos_x;
00037 }
00038
00039 unsigned char get_actnpt_subtile_y(unsigned char *actnpt)
00040 {
00041 if (actnpt==NULL) return 0;
00042 return actnpt[3];
00043 }
00044
00045 short set_actnpt_subtile_y(unsigned char *actnpt,unsigned char pos_y)
00046 {
00047 if (actnpt==NULL) return 0;
00048 actnpt[3]=pos_y;
00049 }
00050
00051 short set_actnpt_subtile(unsigned char *actnpt,unsigned char pos_x,unsigned char pos_y)
00052 {
00053 if (actnpt==NULL) return 0;
00054 actnpt[1]=pos_x;
00055 actnpt[3]=pos_y;
00056 }
00057
00058 unsigned char get_actnpt_range_subtile(unsigned char *actnpt)
00059 {
00060 if (actnpt==NULL) return 0;
00061 return actnpt[5];
00062 }
00063
00064 short set_actnpt_range_subtile(unsigned char *actnpt,unsigned char rng_tl)
00065 {
00066 if (actnpt==NULL) return 0;
00067 actnpt[5]=rng_tl;
00068 }
00069
00070
00071
00072
00073 unsigned int get_actnpt_range_adv(unsigned char *actnpt)
00074 {
00075 return ((unsigned int)get_actnpt_range_subtile(actnpt)<<8)+
00076 get_actnpt_range_subtpos(actnpt);
00077 }
00078
00079 unsigned char get_actnpt_subtpos_x(unsigned char *actnpt)
00080 {
00081 if (actnpt==NULL) return 0;
00082 return actnpt[0];
00083 }
00084
00085 unsigned short get_actnpt_pos_x_adv(const unsigned char *actnpt)
00086 {
00087 if (actnpt==NULL) return 0;
00088 return (((unsigned short)actnpt[1])<<8) + actnpt[0];
00089 }
00090
00091 short set_actnpt_subtpos_x(unsigned char *actnpt,unsigned char pos_x)
00092 {
00093 if (actnpt==NULL) return 0;
00094 actnpt[0]=pos_x;
00095 }
00096
00097 unsigned char get_actnpt_subtpos_y(unsigned char *actnpt)
00098 {
00099 if (actnpt==NULL) return 0;
00100 return actnpt[2];
00101 }
00102
00103 unsigned short get_actnpt_pos_y_adv(const unsigned char *actnpt)
00104 {
00105 if (actnpt==NULL) return 0;
00106 return (((unsigned short)actnpt[3])<<8) + actnpt[2];
00107 }
00108
00109 short set_actnpt_subtpos_y(unsigned char *actnpt,unsigned char pos_y)
00110 {
00111 if (actnpt==NULL) return 0;
00112 actnpt[2]=pos_y;
00113 }
00114
00115 short set_actnpt_subtpos(unsigned char *actnpt,unsigned char pos_x,unsigned char pos_y)
00116 {
00117 if (actnpt==NULL) return 0;
00118 actnpt[0]=pos_x;
00119 actnpt[2]=pos_y;
00120 }
00121
00122 unsigned char get_actnpt_range_subtpos(unsigned char *actnpt)
00123 {
00124 if (actnpt==NULL) return 0;
00125 return actnpt[4];
00126 }
00127
00128 short set_actnpt_range_subtpos(unsigned char *actnpt,unsigned char rng_st)
00129 {
00130 if (actnpt==NULL) return 0;
00131 actnpt[4]=rng_st;
00132 }
00133
00134 unsigned short get_actnpt_number(unsigned char *actnpt)
00135 {
00136 if (actnpt==NULL) return 0;
00137 return (unsigned short)((actnpt[7]<<8)+actnpt[6]);
00138 }
00139
00140 short set_actnpt_number(unsigned char *actnpt,unsigned short apt_num)
00141 {
00142 if (actnpt==NULL) return 0;
00143 actnpt[7]=(apt_num>>8);
00144 actnpt[6]=apt_num&255;
00145 }
00146
00147
00148
00149
00150
00151
00152 unsigned char *create_actnpt(struct LEVEL *lvl, unsigned int sx, unsigned int sy)
00153 {
00154
00155 const int arr_entries_x=lvl->tlsize.x*MAP_SUBNUM_X;
00156 const int arr_entries_y=lvl->tlsize.y*MAP_SUBNUM_Y;
00157 sx%=arr_entries_x;
00158 sy%=arr_entries_y;
00159
00160 unsigned char *actnpt;
00161 actnpt = (unsigned char *)malloc(SIZEOF_DK_APT_REC);
00162 if (actnpt==NULL)
00163 {
00164 message_error("create_actnpt: Out of memory");
00165 return NULL;
00166 }
00167 int i;
00168 for (i=0; i < SIZEOF_DK_APT_REC; i++)
00169 actnpt[i]=0;
00170 set_actnpt_subtpos(actnpt,0x80,0x80);
00171 set_actnpt_subtile(actnpt,(unsigned char)sx,(unsigned char)sy);
00172 set_actnpt_number(actnpt,get_free_actnpt_number(lvl));
00173 set_actnpt_range_subtile(actnpt,5);
00174 return actnpt;
00175 }
00176
00177
00178
00179
00180
00181
00182 unsigned char *create_actnpt_copy(unsigned int sx, unsigned int sy,unsigned char *src)
00183 {
00184 unsigned char *actnpt;
00185 actnpt = (unsigned char *)malloc(SIZEOF_DK_APT_REC);
00186 if (actnpt==NULL)
00187 {
00188 message_error("create_actnpt_copy: Out of memory");
00189 return NULL;
00190 }
00191 memcpy(actnpt,src,SIZEOF_DK_APT_REC);
00192 set_actnpt_subtpos(actnpt,0x80,0x80);
00193 set_actnpt_subtile(actnpt,(unsigned char)sx,(unsigned char)sy);
00194 return actnpt;
00195 }
00196
00197
00198
00199
00200 unsigned short get_free_actnpt_number(const struct LEVEL *lvl)
00201 {
00202 get_free_actnpt_number_next(lvl,1);
00203 }
00204
00205
00206
00207
00208
00209 unsigned short get_free_actnpt_number_next(const struct LEVEL *lvl,const unsigned short start)
00210 {
00211 unsigned int used_size=start+16;
00212 unsigned char *used=NULL;
00213 if (!create_actnpt_number_used_arr(lvl,&used,&used_size))
00214 return start;
00215 int new_num=start;
00216 while (new_num<used_size)
00217 {
00218 if (used[new_num]==0) break;
00219 new_num++;
00220 }
00221 free(used);
00222 return new_num;
00223 }
00224
00225
00226
00227
00228
00229 unsigned short get_free_actnpt_number_prev(const struct LEVEL *lvl,const unsigned short start)
00230 {
00231 unsigned int used_size=start+2;
00232 unsigned char *used=NULL;
00233 if (!create_actnpt_number_used_arr(lvl,&used,&used_size))
00234 return start;
00235 int new_num=start;
00236 while (new_num>1)
00237 {
00238 if (used[new_num]==0) break;
00239 new_num--;
00240 }
00241 free(used);
00242 return new_num;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 short create_actnpt_number_used_arr(const struct LEVEL *lvl,unsigned char **used,unsigned int *used_size)
00252 {
00253
00254 const int arr_entries_x=lvl->tlsize.x*MAP_SUBNUM_X;
00255 const int arr_entries_y=lvl->tlsize.y*MAP_SUBNUM_Y;
00256 int k;
00257 *used_size=max(lvl->apt_total_count+16,*used_size);
00258 *used=malloc((*used_size)*sizeof(unsigned char));
00259 if (*used==NULL) return false;
00260 for (k=0;k<(*used_size);k++)
00261 (*used)[k]=0;
00262 int cy, cx;
00263 for (cy=0; cy<arr_entries_y; cy++)
00264 {
00265 for (cx=0; cx<arr_entries_x; cx++)
00266 {
00267 int num_subs=get_actnpt_subnums(lvl,cx,cy);
00268 for (k=0; k<num_subs; k++)
00269 {
00270 char *actnpt=get_actnpt(lvl,cx,cy,k);
00271 unsigned short cnum=get_actnpt_number(actnpt);
00272 if (cnum<(*used_size))
00273 (*used)[cnum]++;
00274 else
00275 (*used)[0]++;
00276 }
00277 }
00278 }
00279 return true;
00280 }
00281
00282 unsigned char get_stlight_subtile_x(unsigned char *stlight)
00283 {
00284 if (stlight==NULL) return 0;
00285 return (unsigned char)stlight[11];
00286 }
00287
00288 short set_stlight_subtile_x(unsigned char *stlight,unsigned char pos_x)
00289 {
00290 if (stlight==NULL) return 0;
00291 stlight[11]=pos_x;
00292 }
00293
00294 unsigned char get_stlight_subtile_y(unsigned char *stlight)
00295 {
00296 if (stlight==NULL) return 0;
00297 return (unsigned char)stlight[13];
00298 }
00299
00300 short set_stlight_subtile_y(unsigned char *stlight,unsigned char pos_y)
00301 {
00302 if (stlight==NULL) return 0;
00303 stlight[13]=pos_y;
00304 }
00305
00306 short set_stlight_subtile(unsigned char *stlight,unsigned char pos_x,unsigned char pos_y)
00307 {
00308 if (stlight==NULL) return 0;
00309 stlight[11]=pos_x;
00310 stlight[13]=pos_y;
00311 }
00312
00313 unsigned char get_stlight_subtile_h(unsigned char *stlight)
00314 {
00315 if (stlight==NULL) return 0;
00316 return stlight[15];
00317 }
00318
00319 short set_stlight_subtile_h(unsigned char *stlight,unsigned char pos_h)
00320 {
00321 if (stlight==NULL) return 0;
00322 stlight[15]=pos_h;
00323 }
00324
00325 unsigned char get_stlight_subtpos_x(unsigned char *stlight)
00326 {
00327 if (stlight==NULL) return 0;
00328 return stlight[10];
00329 }
00330
00331 unsigned short get_stlight_pos_x_adv(const unsigned char *stlight)
00332 {
00333 if (stlight==NULL) return 0;
00334 return (((unsigned short)stlight[11])<<8) + stlight[10];
00335 }
00336
00337 short set_stlight_subtpos_x(unsigned char *stlight,unsigned char pos_x)
00338 {
00339 if (stlight==NULL) return 0;
00340 stlight[10]=pos_x;
00341 }
00342
00343 unsigned char get_stlight_subtpos_y(unsigned char *stlight)
00344 {
00345 if (stlight==NULL) return 0;
00346 return stlight[12];
00347 }
00348
00349 unsigned short get_stlight_pos_y_adv(const unsigned char *stlight)
00350 {
00351 if (stlight==NULL) return 0;
00352 return (((unsigned short)stlight[13])<<8) + stlight[12];
00353 }
00354
00355 short set_stlight_subtpos_y(unsigned char *stlight,unsigned char pos_y)
00356 {
00357 if (stlight==NULL) return 0;
00358 stlight[12]=pos_y;
00359 }
00360
00361 short set_stlight_subtpos(unsigned char *stlight,unsigned char pos_x,unsigned char pos_y)
00362 {
00363 if (stlight==NULL) return 0;
00364 stlight[10]=pos_x;
00365 stlight[12]=pos_y;
00366 }
00367
00368 unsigned char get_stlight_subtpos_h(unsigned char *stlight)
00369 {
00370 if (stlight==NULL) return 0;
00371 return stlight[14];
00372 }
00373
00374 short set_stlight_subtpos_h(unsigned char *stlight,unsigned char pos_h)
00375 {
00376 if (stlight==NULL) return 0;
00377 stlight[14]=pos_h;
00378 }
00379
00380 unsigned char get_stlight_range_subtile(unsigned char *stlight)
00381 {
00382 if (stlight==NULL) return 0;
00383 return stlight[1];
00384 }
00385
00386 short set_stlight_range_subtile(unsigned char *stlight,unsigned char rng_tl)
00387 {
00388 if (stlight==NULL) return 0;
00389 stlight[1]=rng_tl;
00390 }
00391
00392 unsigned char get_stlight_range_subtpos(unsigned char *stlight)
00393 {
00394 if (stlight==NULL) return 0;
00395 return stlight[0];
00396 }
00397
00398 short set_stlight_range_subtpos(unsigned char *stlight,unsigned char rng_st)
00399 {
00400 if (stlight==NULL) return 0;
00401 stlight[0]=rng_st;
00402 }
00403
00404
00405
00406
00407 unsigned int get_stlight_range_adv(unsigned char *stlight)
00408 {
00409 return ((unsigned int)get_stlight_range_subtile(stlight)<<8)+
00410 get_stlight_range_subtpos(stlight);
00411 }
00412
00413 unsigned char get_stlight_intensivity(unsigned char *stlight)
00414 {
00415 if (stlight==NULL) return 0;
00416 return stlight[2];
00417 }
00418
00419 short set_stlight_intensivity(unsigned char *stlight,unsigned char intens)
00420 {
00421 if (stlight==NULL) return 0;
00422 stlight[2]=intens;
00423 return 1;
00424 }
00425
00426
00427
00428
00429
00430 unsigned char *create_stlight(unsigned int sx, unsigned int sy)
00431 {
00432 unsigned char *stlight;
00433 stlight = (unsigned char *)malloc(SIZEOF_DK_LGT_REC);
00434 if (stlight==NULL)
00435 {
00436 message_error("create_stlight: Out of memory");
00437 return NULL;
00438 }
00439 int i;
00440 for (i=0; i < SIZEOF_DK_LGT_REC; i++)
00441 stlight[i]=0;
00442 set_stlight_subtpos(stlight,((sx%MAP_SUBNUM_X)*0x40+0x40),((sy%MAP_SUBNUM_Y)*0x40+0x40));
00443 set_stlight_subtile(stlight,(unsigned char)sx,(unsigned char)sy);
00444 set_stlight_subtile_h(stlight,5);
00445 set_stlight_range_subtile(stlight,16);
00446 set_stlight_range_subtpos(stlight,0);
00447 set_stlight_intensivity(stlight,32);
00448
00449 stlight[18]=0x0ff;
00450 stlight[19]=0x0ff;
00451 return stlight;
00452 }
00453
00454
00455
00456
00457
00458
00459 unsigned char *create_stlight_copy(unsigned int sx, unsigned int sy,unsigned char *src)
00460 {
00461 unsigned char *stlight;
00462 stlight = (unsigned char *)malloc(SIZEOF_DK_LGT_REC);
00463 if (stlight==NULL)
00464 {
00465 message_error("create_stlight_copy: Out of memory");
00466 return NULL;
00467 }
00468 int i;
00469 memcpy(stlight,src,SIZEOF_DK_LGT_REC);
00470 set_stlight_subtpos(stlight,((sx%MAP_SUBNUM_X)*0x40+0x40),((sy%MAP_SUBNUM_Y)*0x40+0x40));
00471 set_stlight_subtile(stlight,(unsigned char)sx,(unsigned char)sy);
00472 return stlight;
00473 }
00474
00481 unsigned long get_actnpt_distance_adv(const unsigned char *actnpt,const int ssx,const int ssy)
00482 {
00483 int dx=get_actnpt_pos_x_adv(actnpt)-ssx;
00484 int dy=get_actnpt_pos_y_adv(actnpt)-ssy;
00485 float dist_sqr=((float)dx*dx)+((float)dy*dy);
00486 return sqrt(dist_sqr);
00487 }
00488
00495 unsigned long get_stlight_distance_adv(const unsigned char *stlight,const int ssx,const int ssy)
00496 {
00497 int dx=get_stlight_pos_x_adv(stlight)-ssx;
00498 int dy=get_stlight_pos_y_adv(stlight)-ssy;
00499 float dist_sqr=((float)dx*dx)+((float)dy*dy);
00500 return sqrt(dist_sqr);
00501 }
00502