FILEDOOM.CPP
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "rayfile.h"
  4. #include "doom.h"
  5. #include "fixed.h"
  6. #include "blockbsp.h"
  7. #include "blockmap.h"
  8. #include <stdlib.h>
  9. inline ptexture Get_Rand_Texture()
  10. {
  11. short tex_result=1;
  12. #ifdef RAND_TEX
  13. short intermediate=rand();
  14. tex_result+=((intermediate*(Number_Of_Textures-1))/32767);
  15. if (tex_result>=Number_Of_Textures)
  16.   tex_result=Number_Of_Textures-1;
  17. #endif
  18. return (ptexture)(tex_result);
  19. }
  20. void F_Doom_Load(char * filename)
  21. {
  22. F_D_Setup(filename);
  23. base_index=0;
  24. F_D_Load_World();
  25. F_Close();
  26. }
  27. void F_D_Setup(char * filename)
  28. {
  29. binary_mode=TRUE;
  30. fp.open(filename, ios::binary | ios::in | ios::out);
  31. F_Seek(0);
  32. fp.read(header.header, 4);
  33. F_Get_Long(header.dir_entries);
  34. F_Get_Long(header.dir_location);
  35. F_Seek(header.dir_location);
  36. directory=(dir_entry *)NewPtr(header.dir_entries * sizeof(dir_entry));
  37. short counter;
  38. dir_entry * cur_dir;
  39. for (counter=0; counter < header.dir_entries; counter++) {
  40.    cur_dir=directory+counter;
  41.    F_Get_Long(cur_dir->start);
  42.    F_Get_Long(cur_dir->length);
  43.    F_Get_String(cur_dir->name, 8);
  44. } /* endfor */
  45. }
  46. void F_D_Load_World()
  47. {
  48. if (world_loaded)
  49.    F_Clear_World();
  50. world_loaded=TRUE;
  51. F_D_Get_Sectors();
  52. F_D_Get_Vectors();
  53. F_D_Get_SDs();
  54. F_D_Get_LDs();
  55. F_D_Get_Segs();
  56. F_D_Get_SSectors();
  57. F_D_Get_BSP();
  58. F_D_Get_Player_Loc();
  59. }
  60. short F_D_Find_And_Setup(char * string, void * * table)
  61. {
  62.    short temp_index;
  63.    if ((temp_index=F_Find_Dir(string))==-1)
  64.       return temp_index;
  65.    (*table)=(void *)NewPtr(directory[temp_index].length);
  66.    F_Seek(directory[temp_index].start);
  67.    fp.read((char*)(*table), directory[temp_index].length);
  68.    return temp_index;
  69. }
  70. void F_D_Get_Sectors()
  71. {
  72. dm_sector * temp_sectors;
  73. short dir_index=F_D_Find_And_Setup("SECTORS", (void * *)&temp_sectors);
  74. if (dir_index==-1)
  75.    return;
  76. Number_Of_Sectors=(directory[dir_index].length/sizeof(dm_sector));
  77. Sector_List=(sector *)NewPtr(Number_Of_Sectors * sizeof(sector));
  78. short counter;
  79. sector * cur_sec;
  80. dm_sector * cur_dm_sec;
  81. for (counter=0; counter<Number_Of_Sectors; counter++) {
  82.    cur_sec=Sector_List+counter;
  83.    cur_dm_sec=temp_sectors+counter;
  84.    cur_sec->floor_height=cur_dm_sec->floor_ht;
  85.    cur_sec->ceil_height=cur_dm_sec->ceiling_ht;
  86.    cur_sec->tag=(USHORT)cur_dm_sec->trigger;
  87.    cur_sec->light=((UCHAR) cur_dm_sec->light >> 4) + 
  88.         ((UCHAR) cur_dm_sec->light & 0xf0);
  89.    cur_sec->floor_tex=0;
  90.    cur_sec->ceil_tex=0;
  91.    cur_sec->flags=0;
  92. }
  93. DelPtr(temp_sectors);
  94. }
  95. void F_D_Get_Vectors()
  96. {
  97. dm_vertex * temp_vectors;
  98. short dir_index=F_D_Find_And_Setup("VERTEXES",(void * *)&temp_vectors);
  99. if (dir_index==-1)
  100.    return;
  101. Number_Of_Vectors=(directory[dir_index].length / sizeof(dm_vertex));
  102. Vector_List=(vector2 *)NewPtr(Number_Of_Vectors * sizeof(vector2));
  103. short counter;
  104. vector2 * cur_vec;
  105. dm_vertex * cur_dm_vec;
  106. for (counter=0; counter < Number_Of_Vectors; counter++) {
  107.    cur_vec=Vector_List+counter;
  108.    cur_dm_vec=temp_vectors+counter;
  109.    cur_vec->x=(long)cur_dm_vec->x;
  110.    cur_vec->y=(long)cur_dm_vec->y;
  111. }
  112. DelPtr(temp_vectors);
  113. }
  114. void F_D_Get_SDs()
  115. {
  116. srand(rand());
  117. dm_side * temp_sidedefs;
  118. short dir_index=F_D_Find_And_Setup("SIDEDEFS",(void * *)&temp_sidedefs);
  119. if (dir_index==-1)
  120.    return;
  121. Number_Of_Sidedefs=(directory[dir_index].length / sizeof(dm_side));
  122. Sd_List=(sidedef *)NewPtr(Number_Of_Sidedefs * sizeof(sidedef));
  123. short counter;
  124. sidedef * cur_sd;
  125. dm_side * cur_dm_sd;
  126. for (counter=0; counter < Number_Of_Sidedefs; counter++) {
  127.    cur_sd=Sd_List+counter;
  128.    cur_dm_sd=temp_sidedefs+counter;
  129.    cur_sd->attributes=0;
  130.    cur_sd->x_offset=cur_dm_sd->tm_xoffset;
  131.    cur_sd->y_offset=cur_dm_sd->tm_yoffset;
  132.    cur_sd->sec=Sector_List+cur_dm_sd->sector;
  133.    if (Cmp_Str_N(cur_dm_sd->main_tx, "-", 1))
  134.       cur_sd->texture_normal=TEXTURE_NULL;
  135.    else cur_sd->texture_normal=Get_Rand_Texture();
  136.    if (Cmp_Str_N(cur_dm_sd->upper_tx, "-", 1))
  137.       cur_sd->texture_high=TEXTURE_NULL;
  138.    else cur_sd->texture_high=Get_Rand_Texture();
  139.    if (Cmp_Str_N(cur_dm_sd->lower_tx, "-", 1))
  140.       cur_sd->texture_low=TEXTURE_NULL;
  141.    else cur_sd->texture_low=Get_Rand_Texture();
  142. } /* endfor */
  143. DelPtr(temp_sidedefs);
  144. }
  145. void F_D_Get_LDs()
  146. {
  147. dm_line * temp_linedefs;
  148. short dir_index=F_D_Find_And_Setup("LINEDEFS", (void * *)&temp_linedefs);
  149. if (dir_index==-1)
  150.    return;
  151. Number_Of_Linedefs=(directory[dir_index].length / sizeof(dm_line));
  152. Ld_List=(linedef *)NewPtr(Number_Of_Linedefs * sizeof(linedef));
  153. short counter;
  154. linedef * cur_ld;
  155. dm_line * cur_dm_ld;
  156. for (counter=0; counter < Number_Of_Linedefs; counter++) {
  157.    cur_ld=Ld_List+counter;
  158.    cur_dm_ld=temp_linedefs+counter;
  159.    cur_ld->v[0]=cur_dm_ld->from;
  160.    cur_ld->v[1]=cur_dm_ld->to;
  161.    cur_ld->s[0]=Sd_List+cur_dm_ld->side[0];
  162.    // Check to see if there is a second sidedef at all
  163.    if (cur_dm_ld->side[1]!=-1)
  164.       cur_ld->s[1]=Sd_List+cur_dm_ld->side[1];
  165.    else cur_ld->s[1]=NULL;
  166.    cur_ld->attributes=(USHORT)cur_dm_ld->flags;
  167.    cur_ld->tag_type=(USHORT)cur_dm_ld->special;
  168.    cur_ld->tag=(USHORT)cur_dm_ld->tag;
  169. } /* endfor */
  170. DelPtr(temp_linedefs);
  171. Generate_Block_Map();
  172. }
  173. void F_D_Get_Segs()
  174. {
  175. dm_seg * temp_segs;
  176. short dir_index=F_D_Find_And_Setup("SEGS", (void * *)&temp_segs);
  177. if (dir_index==-1)
  178.    return;
  179. Number_Of_Segs=(directory[dir_index].length / sizeof(dm_seg));
  180. Seg_List=(seg *)NewPtr(Number_Of_Segs * sizeof(seg));
  181. short counter;
  182. seg * cur_seg; dm_seg * cur_dm_seg;
  183. for (counter=0; counter < Number_Of_Segs; counter++) {
  184.    cur_seg=Seg_List+counter;
  185.    cur_dm_seg=temp_segs+counter;
  186.    cur_seg->v[0]=cur_dm_seg->from;
  187.    cur_seg->v[1]=cur_dm_seg->to;
  188.    cur_seg->angle=((long)cur_dm_seg->angle * ANGLE_360) / DM_ANGLE_360;
  189.    cur_seg->ld=Ld_List+cur_dm_seg->line;
  190.    cur_seg->left_sidedef=(BOOL)cur_dm_seg->line_side;
  191.    cur_seg->linedef_offset=(LONG)cur_dm_seg->line_offset;
  192. } /* endfor */
  193. DelPtr(temp_segs);
  194. }
  195. void F_D_Get_SSectors()
  196. {
  197. dm_ssector * temp_ssectors;
  198. short dir_index=F_D_Find_And_Setup("SSECTORS", (void * *)&temp_ssectors);
  199. if (dir_index==-1)
  200.    return;
  201. Number_Of_SSectors=(directory[dir_index].length / sizeof(dm_ssector));
  202. SS_List=(ssector *)NewPtr(Number_Of_SSectors * sizeof(ssector));
  203. short counter;
  204. ssector * cur_ss;
  205. dm_ssector * cur_dm_ss;
  206. for (counter=0; counter < Number_Of_SSectors; counter++) {
  207.    cur_ss=SS_List+counter;
  208.    cur_dm_ss=temp_ssectors+counter;
  209.    cur_ss->seg_start=cur_dm_ss->first_seg;
  210.    cur_ss->seg_end=cur_ss->seg_start+cur_dm_ss->num_segs-1;
  211.    Doom_Sort_SSector(cur_ss);
  212.    cur_ss->num_objects=0;
  213.    cur_ss->objects=NULL;
  214.    cur_ss->flags=0;
  215. } /* endfor */
  216. DelPtr(temp_ssectors);
  217. }
  218. void F_D_Get_BSP()
  219. {
  220. dm_node * temp_nodes;
  221. short dir_index=F_D_Find_And_Setup("NODES", (void * *)&temp_nodes);
  222. if (dir_index==-1)
  223.    return;
  224. Number_Of_Nodes=(directory[dir_index].length / sizeof(dm_node));
  225. bsp_tree=(bsp_node *)NewPtr(Number_Of_Nodes * sizeof(bsp_node));
  226. bsp_start_node=Number_Of_Nodes-1;
  227. short counter;
  228. bsp_node * cur_node;
  229. dm_node * cur_dm_node;
  230. prectl cur_rect;
  231. for (counter=0; counter < Number_Of_Nodes; counter++) {
  232.    cur_node=bsp_tree+counter;
  233.    cur_dm_node=temp_nodes+counter;
  234.    cur_node->x1=cur_dm_node->x;
  235.    cur_node->y1=cur_dm_node->y;
  236.    cur_node->x2=cur_node->x1 + cur_dm_node->dx;
  237.    cur_node->y2=cur_node->y1 + cur_dm_node->dy;
  238.    cur_rect=&(cur_node->left_bound);
  239.    cur_rect->top=cur_dm_node->ly2;
  240.    cur_rect->left=cur_dm_node->lx1;
  241.    cur_rect->bottom=cur_dm_node->ly1;
  242.    cur_rect->right=cur_dm_node->lx2;
  243.    cur_rect=&(cur_node->right_bound);
  244.    cur_rect->top=cur_dm_node->ry2;
  245.    cur_rect->left=cur_dm_node->rx1;
  246.    cur_rect->bottom=cur_dm_node->ry1;
  247.    cur_rect->right=cur_dm_node->rx2;
  248.    cur_node->left_child=cur_dm_node->left;
  249.    cur_node->right_child=cur_dm_node->right;
  250. } /* endfor */
  251. DelPtr(temp_nodes);
  252. Generate_BSP_Block();
  253. }
  254. void F_D_Get_Player_Loc()
  255. {
  256. dm_thing * temp_things;
  257. short dir_index=F_D_Find_And_Setup("THINGS", (void * *)&temp_things);
  258. if (dir_index==-1)
  259.    return;
  260. short num_things=(directory[dir_index].length / sizeof(dm_thing));
  261. short counter;
  262. dm_thing * cur_dm_thing;
  263. for (counter=0; counter < num_things; counter++) {
  264.    cur_dm_thing=temp_things+counter;
  265.    if (cur_dm_thing->thing_type==PLAYER_START) {
  266. /*      the_player->x=cur_dm_thing->x;
  267.       the_player->y=cur_dm_thing->y;
  268.       the_player->angle=0; */
  269.    }
  270. } /* endfor */
  271. DelPtr(temp_things);
  272. }
  273. void Doom_Sort_SSector(ssector * cur_ss)
  274. {
  275. pseg cur_low, cur_test, seg_base;
  276. seg_base=Seg_List+cur_ss->seg_start;
  277. USHORT num_segs=cur_ss->seg_end - cur_ss->seg_start+1;
  278. for (short i=0; i<(num_segs-1); i++) {
  279.    cur_low=seg_base+i;
  280.    for (short j=i+1; j<num_segs; j++) {
  281.       cur_test=seg_base+j;
  282.       if (cur_test->angle > cur_low->angle)
  283.          Swap_Segs(cur_test, cur_low);
  284.    } /* endfor */
  285. } /* endfor */
  286. }
  287. void Swap_Segs(seg * base_seg, seg * swap_seg)
  288. {
  289. seg temp_seg;
  290. memcpy(&temp_seg, base_seg, sizeof(seg));
  291. memcpy(base_seg, swap_seg, sizeof(seg));
  292. memcpy(swap_seg, &temp_seg, sizeof(seg));
  293. }