map.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "player.hh"
  9. #include "error/error.hh"
  10. #include "g1_limits.hh"
  11. #include "image/color.hh"
  12. #include "map.hh"
  13. #include "window/win_evt.hh"
  14. #include "device/kernel.hh"
  15. #include "objs/model_id.hh"
  16. #include "image/image.hh"
  17. #include "loaders/load.hh"
  18. #include "init/init.hh"
  19. #include "resources.hh"
  20. #include "g1_object.hh"
  21. #include "objs/map_piece.hh"
  22. #include "sound_man.hh"
  23. #include "tile.hh"
  24. #include "resources.hh"
  25. #include "remove_man.hh"
  26. #include "math/pi.hh"
  27. #include "g1_speed.hh"
  28. #include "light.hh"
  29. #include "m_flow.hh"
  30. #include "statistics.hh"
  31. #include "input.hh"
  32. #include "time/profile.hh"
  33. #include "g1_render.hh"
  34. #include "height_info.hh"
  35. #include "checksum/checksum.hh"
  36. #include "solvemap_astar.hh"
  37. #include "cwin_man.hh"
  38. #include "map_light.hh"
  39. #include "map_man.hh"
  40. #include "lisp/li_class.hh"
  41. #include "objs/path_object.hh"
  42. #include "objs/vehic_sounds.hh"
  43. #include "map_cell.hh"
  44. #include "map_vert.hh"
  45. #include "tick_count.hh"
  46. #include "map_view.hh"
  47. #include "map_data.hh"
  48. g1_height_info::g1_height_info()
  49. {
  50. floor_height = 0.f;
  51. ceiling_height = 1.f;
  52. flags = BELOW_GROUND;
  53. }
  54. i4_profile_class pf_map_think("map.cc think objects");
  55. i4_profile_class pf_map_post_think("map.cc post_think objects");
  56. i4_profile_class pf_notify_target_position_change("notify_target_pos_change");
  57. g1_statistics_counter_class g1_stat_counter;
  58. w32 g1_map_class::get_tick()
  59. {
  60. return g1_tick_counter;
  61. }
  62. g1_map_cell_class *g1_map_class::cell(w16 x, w16 y) const { return cells + y*w + x; }
  63. g1_map_cell_class *g1_map_class::cell(w32 offset) const { return cells + offset; }
  64. g1_map_vertex_class *g1_map_class::vertex(w16 x, w16 y) const { return verts + y*(w+1) + x; }
  65. void g1_map_class::add_object(g1_object_chain_class &c, w32 x, w32 y)
  66. {
  67. cells[c.offset = y*w+x].add_object(c);
  68. }
  69. void g1_map_class::remove_object(g1_object_chain_class &c)
  70. {
  71. cells[c.offset].remove_object(c);
  72. }
  73. i4_float g1_map_class::min_terrain_height(w16 x, w16 y)
  74. {
  75. g1_map_vertex_class *v1,*v2,*v3,*v4;
  76. v1 = verts+ x + y * (w+1);
  77. v2 = v1+1;
  78. v3 = v1+w+1;
  79. v4 = v3+1;
  80. return g1_vertex_min(v1, g1_vertex_min(v2, g1_vertex_min(v3, v4)))->get_height();
  81. }
  82. // only use this if you know what you are doing
  83. void g1_map_class::change_map(int _w, int _h,
  84. g1_map_cell_class *_cells, g1_map_vertex_class *_vertex)
  85. {
  86. if (cells)
  87. i4_free(cells);
  88. if (verts)
  89. i4_free(verts);
  90. cells=_cells;
  91. verts=_vertex;
  92. w=_w;
  93. h=_h;
  94. }
  95. void g1_map_class::remove_object_type(g1_object_type type)
  96. {
  97. int i,j,k;
  98. g1_map_cell_class *c=cell(0,0);
  99. g1_object_chain_class *cell, *l;
  100. g1_object_class *obj;
  101. for (j=0; j<height(); j++)
  102. for (i=0; i<width(); i++, c++)
  103. {
  104. for (cell=c->get_obj_list(); cell;)
  105. {
  106. l=cell;
  107. cell=cell->next;
  108. obj=l->object;
  109. if (obj->id==type)
  110. {
  111. obj->unoccupy_location();
  112. obj->request_remove();
  113. }
  114. }
  115. }
  116. }
  117. g1_map_class::g1_map_class(const i4_const_str &fname)
  118. {
  119. recalc=0xffffffff; // recalculate everything until further notice
  120. sky_name=0;
  121. // lod=0;
  122. // lod=(g1_map_lod_cell *) i4_malloc(sizeof(g1_map_lod_cell) * MAX_MAP_LOD, "lod");
  123. filename=0;
  124. set_filename(fname);
  125. post_cell_draw=0;
  126. w=0;
  127. h=0;
  128. think_head=think_tail=0;
  129. current_movie=0;
  130. cells=0;
  131. verts=0;
  132. // map=(g1_map_cell_class *)i4_malloc(w*h*sizeof(g1_map_cell_class),"map");
  133. // vert=(g1_map_vertex_class *)i4_malloc((w+1)*(h+1)*sizeof(g1_map_vertex_class),"map vert");
  134. w32 x,count=w*h,y;
  135. solver = new g1_astar_map_solver_class;
  136. // critical_graph=0;
  137. movie_in_progress=i4_F;
  138. }
  139. g1_map_class::~g1_map_class()
  140. {
  141. g1_stop_sound_averages();
  142. g1_map_class *old_current=g1_current_map_PRIVATE;
  143. g1_set_map(this);
  144. for (g1_map_data_class *md=g1_map_data_class::first; md; md=md->next)
  145. md->free();
  146. if (filename)
  147. delete filename;
  148. if (sky_name)
  149. delete sky_name;
  150. if (cells)
  151. {
  152. g1_object_class *olist[G1_MAX_OBJECTS];
  153. w32 ids[G1_MAX_OBJECTS];
  154. sw32 t=make_object_list(olist, G1_MAX_OBJECTS), i;
  155. // can't use object pointers directly because one
  156. // object might delete another
  157. for (i=0; i<t; i++)
  158. ids[i]=olist[i]->global_id;
  159. for (i=0; i<t; i++)
  160. {
  161. if (g1_global_id.check_id(ids[i]))
  162. {
  163. g1_object_class *o=g1_global_id.get(ids[i]);
  164. o->unoccupy_location();
  165. o->request_remove();
  166. g1_remove_man.process_requests();
  167. }
  168. }
  169. i4_free(cells);
  170. }
  171. if (verts)
  172. i4_free(verts);
  173. if (current_movie)
  174. delete current_movie;
  175. if (solver)
  176. delete solver;
  177. if (old_current==this)
  178. g1_set_map(0);
  179. else
  180. g1_set_map(old_current);
  181. }
  182. void check_olist()
  183. {
  184. g1_object_class *olist[G1_MAX_OBJECTS];
  185. sw32 t=g1_get_map()->make_object_list(olist, G1_MAX_OBJECTS);
  186. }
  187. // returns total added
  188. sw32 g1_map_class::make_object_list(g1_object_class **buffer, sw32 buf_size)
  189. {
  190. int x=width()*height(), i;
  191. sw32 t=0;
  192. g1_map_cell_class *c=cells;
  193. if (buf_size==0) return 0;
  194. for (i=0; i<x; i++, c++)
  195. if (c->object_list)
  196. {
  197. for (g1_object_chain_class *obj=c->get_obj_list(); obj; obj=obj->next)
  198. {
  199. g1_object_class *o=obj->object;
  200. if (!o->get_flag(g1_object_class::SCRATCH_BIT)) /// make sure object only added once
  201. {
  202. o->set_flag(g1_object_class::SCRATCH_BIT, 1);
  203. buffer[t++]=o;
  204. if (t==buf_size)
  205. {
  206. i=x;
  207. obj=0;
  208. }
  209. }
  210. }
  211. }
  212. for (i=0; i<t; i++)
  213. buffer[i]->set_flag(g1_object_class::SCRATCH_BIT, 0);
  214. return t;
  215. }
  216. sw32 g1_map_class::make_selected_objects_list(w32 *buffer, sw32 buf_size)
  217. {
  218. int x=width()*height(), i;
  219. sw32 t=0;
  220. g1_map_cell_class *c=cells;
  221. if (buf_size==0) return 0;
  222. for (i=0; i<x; i++, c++)
  223. if (c->object_list)
  224. {
  225. for (g1_object_chain_class *obj=c->get_obj_list(); obj; obj=obj->next)
  226. {
  227. g1_object_class *o=obj->object;
  228. // make sure object only added once
  229. if (o->selected() && !o->get_flag(g1_object_class::SCRATCH_BIT))
  230. {
  231. o->set_flag(g1_object_class::SCRATCH_BIT, 1);
  232. buffer[t++]=o->global_id;
  233. if (t==buf_size)
  234. {
  235. i=x;
  236. obj=0;
  237. }
  238. }
  239. }
  240. }
  241. for (i=0; i<t; i++)
  242. g1_global_id.get(buffer[i])->set_flag(g1_object_class::SCRATCH_BIT, 0);
  243. return t;
  244. }
  245. void g1_map_class::think_objects()
  246. {
  247. li_class *old_this=li_this;
  248. recalc_static_stuff();
  249. pf_map_think.start();
  250. g1_input.que_keys(tick_time);
  251. w32 i,h;
  252. w32 start_tail = think_tail;
  253. w32 start_head = think_head;
  254. //do the think()'s
  255. i = start_tail;
  256. h = start_head;
  257. g1_reset_sound_averages();
  258. pf_map_think.start();
  259. for (; i!=h; )
  260. {
  261. g1_object_class *o=think_que[i];
  262. //always check to make sure the pointer
  263. //is good. objects might have removed themselves
  264. //from the map while still in the que, and left
  265. //a null pointer in their place
  266. if (o)
  267. {
  268. li_this=o->vars;
  269. o->grab_old();
  270. }
  271. i++;
  272. if (i>=THINK_QUE_SIZE)
  273. i=0;
  274. }
  275. i = start_tail;
  276. h = start_head;
  277. for (; i!=h; )
  278. {
  279. g1_object_class *o=think_que[i];
  280. //always check to make sure the pointer
  281. //is good. objects might have removed themselves
  282. //from the map while still in the que, and left
  283. //a null pointer in their place
  284. if (o)
  285. {
  286. li_this=o->vars;
  287. o->set_flag(g1_object_class::THINKING, 0);
  288. o->think();
  289. }
  290. i++;
  291. if (i>=THINK_QUE_SIZE)
  292. i=0;
  293. }
  294. pf_map_think.stop();
  295. pf_map_post_think.start();
  296. //do the post_think()'s
  297. i = start_tail;
  298. h = start_head;
  299. for (; i!=h; )
  300. {
  301. g1_object_class *o=think_que[i];
  302. think_tail++;
  303. if (think_tail>=THINK_QUE_SIZE)
  304. think_tail=0;
  305. //always check to make sure the pointer
  306. //is good. objects might have removed themselves
  307. //from the map while still in the que, and left
  308. //a null pointer in their place
  309. if (o)
  310. {
  311. li_this=o->vars;
  312. o->post_think();
  313. }
  314. i++;
  315. if (i>=THINK_QUE_SIZE)
  316. i=0;
  317. }
  318. pf_map_post_think.stop();
  319. g1_recalc_sound_averages();
  320. g1_tick_counter++;
  321. tick_time.add_milli((1000/G1_HZ));
  322. li_this=old_this;
  323. pf_map_think.stop();
  324. }
  325. void g1_map_class::damage_range(g1_object_class *obj,
  326. i4_float x, i4_float y, i4_float z,
  327. i4_float range, w16 damage, i4_float falloff)
  328. // damage to vehicles centers at x,y,z and falls off by falloff*damage at range distance
  329. // from the center of the damage range
  330. {
  331. sw32 ix,iy,
  332. sx = sw32(x-range),
  333. ex = sw32(x+range)+1,
  334. sy = sw32(y-range),
  335. ey = sw32(y+range)+1;
  336. // clip region
  337. if (sx<0) sx=0;
  338. if (ex>width()) ex=width();
  339. if (sy<0) sy=0;
  340. if (ey>height()) ey=height();
  341. // get first one
  342. g1_map_cell_class *c;
  343. g1_object_chain_class *objlist;
  344. i4_float dist,dx,dy,dz;
  345. range *= range;
  346. falloff *= i4_float(damage)/range;
  347. for (iy=sy; iy<ey; iy++)
  348. {
  349. c = cell(sx,iy);
  350. for (ix=sx; ix<ex; ix++, c++)
  351. {
  352. for (objlist=c->get_obj_list(); objlist; objlist=objlist->next)
  353. {
  354. g1_object_class *hurt_obj=objlist->object;
  355. if (objlist==&hurt_obj->occupied_squares[0]) // make sure object is only hurt once
  356. {
  357. if (hurt_obj->get_flag(g1_object_class::TARGETABLE))
  358. // && hurt_obj->player_num!=obj->player_num)
  359. {
  360. dx=x-hurt_obj->x;
  361. dy=y-hurt_obj->y;
  362. dz=z-hurt_obj->h;
  363. dist = dx*dx+dy*dy+dz*dz;
  364. if (dist<range)
  365. hurt_obj->damage(obj, damage - (int)(falloff*range), i4_3d_vector (0,0,g1_resources.gravity));
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. g1_object_class *g1_map_class::find_object_by_id(w32 object_id,
  373. g1_player_type prefered_team)
  374. {
  375. sw32 i,j=width()*height();
  376. g1_map_cell_class *c=cells;
  377. g1_object_chain_class *o;
  378. g1_object_class *best=0;
  379. for (i=0; i<j; i++, c++)
  380. {
  381. for (o=c->get_obj_list(); o; o=o->next)
  382. {
  383. if (o->object->id==object_id)
  384. {
  385. if (o->object->player_num==prefered_team)
  386. return o->object;
  387. else
  388. best=o->object;
  389. }
  390. }
  391. }
  392. return best;
  393. }
  394. void g1_map_class::remove_from_think_list(g1_object_class *obj)
  395. {
  396. w32 i = think_tail,
  397. h = think_head;
  398. for (; i!=h;)
  399. {
  400. if (think_que[i]==obj) think_que[i]=0;
  401. i++;
  402. if (i>=THINK_QUE_SIZE)
  403. i=0;
  404. }
  405. }
  406. void g1_map_class::request_remove(g1_object_class *obj)
  407. {
  408. obj->flags |= g1_object_class::DELETED;
  409. if (obj->flags & g1_object_class::THINKING)
  410. {
  411. obj->stop_thinking();
  412. }
  413. g1_remove_man.request_remove(obj);
  414. }
  415. i4_const_str g1_map_class::get_filename()
  416. {
  417. return *filename;
  418. }
  419. void g1_map_class::set_filename(const i4_const_str &fname)
  420. {
  421. if (filename)
  422. delete filename;
  423. filename=new i4_str(fname);
  424. }
  425. void g1_map_class::recalc_static_stuff()
  426. {
  427. i4_bool reset_time=i4_F;
  428. // if (recalc & (G1_RECALC_BLOCK_MAPS | G1_RECALC_CRITICAL_DATA))
  429. // li_call("add_undo", li_make_list(new li_int(G1_MAP_CRITICAL_DATA), 0));
  430. if (recalc & G1_RECALC_STATIC_LIGHT)
  431. g1_calc_static_lighting(); // defined in light.cc
  432. // if (recalc & G1_RECALC_BLOCK_MAPS)
  433. // {
  434. // make_block_maps();
  435. // reset_time=i4_T;
  436. // }
  437. if (recalc & G1_RECALC_RADAR_VIEW)
  438. {
  439. // init_lod(); //(OLI) need to put this in the proper place
  440. g1_radar_recalculate_backgrounds();
  441. reset_time=i4_T;
  442. }
  443. if (recalc & (G1_RECALC_WATER_VERTS))
  444. {
  445. g1_map_vertex_class *v=verts+(w+1)+1;
  446. g1_map_cell_class *c=cells+(w)+1;
  447. for (int y=1; y<h-1; y++)
  448. {
  449. for (int x=1; x<w-1; x++, c++, v++)
  450. {
  451. if ((g1_tile_man.get(c[0].type)->flags & g1_tile_class::WAVE) &&
  452. (g1_tile_man.get(c[-1].type)->flags & g1_tile_class::WAVE) &&
  453. (g1_tile_man.get(c[-w].type)->flags & g1_tile_class::WAVE) &&
  454. (g1_tile_man.get(c[-w-1].type)->flags & g1_tile_class::WAVE))
  455. v->flags |= g1_map_vertex_class::APPLY_WAVE_FUNCTION;
  456. }
  457. v+=2;
  458. c+=2;
  459. v++;
  460. }
  461. reset_time=i4_T;
  462. }
  463. recalc=0;
  464. if (reset_time)
  465. tick_time.get(); // don't simulate ticks for the calculation stuff
  466. }
  467. void g1_map_class::range_iterator::begin(float x, float y, float range)
  468. //{{{
  469. {
  470. g1_map_class *map = g1_get_map();
  471. int wx = map->width(), wy = map->height();
  472. left = i4_f_to_i(x - range); if (left<0) left=0;
  473. right = i4_f_to_i(x + range); if (right>wx-1) right=wx-1;
  474. top = i4_f_to_i(y - range); if (top<0) top=0;
  475. bottom = i4_f_to_i(y + range); if (bottom>wy-1) bottom=wy-1;
  476. ix = right;
  477. iy = top-1;
  478. cell = 0;
  479. chain = 0;
  480. object_mask_flags=0xffffffff;
  481. type_mask_flags=0xffffffff;
  482. }
  483. //}}}
  484. void g1_map_class::range_iterator::safe_restart()
  485. //{{{
  486. {
  487. chain=0;
  488. }
  489. //}}}
  490. i4_bool g1_map_class::range_iterator::end()
  491. //{{{
  492. {
  493. return (iy>=bottom);
  494. }
  495. //}}}
  496. void g1_map_class::range_iterator::next()
  497. //{{{
  498. {
  499. return;
  500. /*
  501. g1_object_class *o;
  502. if (chain) chain = chain->next;
  503. while (iy<bottom)
  504. {
  505. if (!chain)
  506. {
  507. ++ix;
  508. ++cell;
  509. if (ix<=right)
  510. chain = cell->get_obj_list();
  511. else
  512. {
  513. ix = left;
  514. iy++;
  515. cell = &g1_get_map()->cell(left, iy);
  516. }
  517. }
  518. else
  519. {
  520. o = chain->object;
  521. if (&o->occupied_squares[0]!=chain ||
  522. (o->flags & object_mask_flags)==0 ||
  523. (g1_object_type_array[o->id]->flags & type_mask_flags)==0)
  524. chain = chain->next;
  525. else
  526. return;
  527. }
  528. }
  529. */
  530. }
  531. //}}}
  532. sw32 g1_map_class::get_objects_in_range(float x, float y, float range,
  533. g1_object_class *dest_array[], w32 array_size,
  534. w32 object_mask_flags, w32 type_mask_flags)
  535. {
  536. sw32 x_left,x_right,y_top,y_bottom;
  537. x_left = i4_f_to_i(x - range); if (x_left<0) x_left=0;
  538. x_right = i4_f_to_i(x + range); if (x_right>w-1) x_right=w-1;
  539. y_top = i4_f_to_i(y - range); if (y_top<0) y_top=0;
  540. y_bottom = i4_f_to_i(y + range); if (y_bottom>h-1) y_bottom=h-1;
  541. sw32 ix,iy;
  542. sw32 num_found=0;
  543. for (iy=y_top; iy<=y_bottom; iy++)
  544. {
  545. g1_map_cell_class *cell = g1_get_map()->cell(x_left, iy);
  546. for (ix=x_left; ix<=x_right; ix++, cell++)
  547. {
  548. g1_object_chain_class *p = cell->get_solid_list();
  549. while (p && num_found<array_size)
  550. {
  551. g1_object_class *o = p->object;
  552. if (o && (&o->occupied_squares[0]==p) && (o->flags & object_mask_flags))
  553. {
  554. if (g1_object_type_array[o->id]->flags & type_mask_flags)
  555. {
  556. dest_array[num_found] = p->object;
  557. num_found++;
  558. }
  559. }
  560. p = p->next_solid();
  561. }
  562. if (num_found >= array_size)
  563. break;
  564. }
  565. if (num_found >= array_size)
  566. break;
  567. }
  568. return num_found;
  569. }