home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / shareware / crystalppc / world.cpp < prev    next >
C/C++ Source or Header  |  1998-06-08  |  9KB  |  454 lines

  1. #include <math.h>
  2. #include <time.h>
  3. #include "system.h"
  4.  
  5. #ifndef DEF_H
  6. #include "def.h"
  7. #endif
  8.  
  9. #ifndef WORLD_H
  10. #include "world.h"
  11. #endif
  12.  
  13. #ifndef CAMERA_H
  14. #include "camera.h"
  15. #endif
  16.  
  17. #ifndef LIGHT_H
  18. #include "light.h"
  19. #endif
  20.  
  21. #ifndef POLYPLANE_H
  22. #include "polyplan.h"
  23. #endif
  24.  
  25. #ifndef POLYGON_H
  26. #include "polygon.h"
  27. #endif
  28.  
  29. #ifndef THING_H
  30. #include "thing.h"
  31. #endif
  32.  
  33. #ifndef CSOBJECT_H
  34. #include "csobject.h"
  35. #endif
  36.  
  37. #ifndef SECTOR_H
  38. #include "sector.h"
  39. #endif
  40.  
  41. #ifndef SCRIPT_H
  42. #include "script.h"
  43. #endif
  44.  
  45. #ifndef TEXTURE_H
  46. #include "texture.h"
  47. #endif
  48.  
  49. #ifndef SCAN_H
  50. #include "scan.h"
  51. #endif
  52.  
  53. #ifndef TOKEN_H
  54. #include "token.h"
  55. #endif
  56.  
  57. #ifndef LIGHTMAP_H
  58. #include "lightmap.h"
  59. #endif
  60.  
  61. //---------------------------------------------------------------------------
  62.  
  63. extern TextureCache cache;
  64.  
  65. World::World ()
  66. {
  67.   first_sector = NULL;
  68.   first_plane = NULL;
  69.   first_script = NULL;
  70.   first_run = NULL;
  71.   first_collection = NULL;
  72. }
  73.  
  74. World::~World ()
  75. {
  76.   clear ();
  77. }
  78.  
  79. void World::clear ()
  80. {
  81.   while (first_sector)
  82.   {
  83.     Sector* s = (Sector*)(first_sector->get_next ());
  84.     delete first_sector;
  85.     first_sector = s;
  86.   }
  87.   while (first_plane)
  88.   {
  89.     PolyPlane* p = first_plane->get_next ();
  90.     delete first_plane;
  91.     first_plane = p;
  92.   }
  93.   while (first_run)
  94.   {
  95.     ScriptRun* r = first_run->get_next ();
  96.     delete first_run;
  97.     first_run = r;
  98.   }
  99.   while (first_script)
  100.   {
  101.     Script* s = first_script->get_next ();
  102.     delete first_script;
  103.     first_script = s;
  104.   }
  105.   while (first_collection)
  106.   {
  107.     Collection* c = first_collection->get_next ();
  108.     delete first_collection;
  109.     first_collection = c;
  110.   }
  111.   if (textures) { delete textures; textures = NULL; }
  112. }
  113.  
  114. Textures* World::new_textures (int max)
  115. {
  116.   textures = new Textures (max);
  117.   return textures;
  118. }
  119.  
  120. Sector* World::new_sector (char* name, int max_v, int max_p)
  121. {
  122.   Sector* s = new Sector (name, max_v, max_p);
  123.   s->set_next (first_sector);
  124.   first_sector = s;
  125.   return s;
  126. }
  127.  
  128. Sector* World::get_sector (char* name)
  129. {
  130.   Sector* s = first_sector;
  131.   while (s)
  132.   {
  133.     if (!strcmp (name, s->get_name ())) return s;
  134.     s = (Sector*)(s->get_next ());
  135.   }
  136.   return NULL;
  137. }
  138.  
  139. PolyPlane* World::new_plane (char* name)
  140. {
  141.   PolyPlane* p = new PolyPlane (name);
  142.   p->set_next (first_plane);
  143.   p->set_prev (NULL);
  144.   if (first_plane) first_plane->set_prev (p);
  145.   first_plane = p;
  146.   return p;
  147. }
  148.  
  149. PolyPlane* World::get_plane (char* name)
  150. {
  151.   PolyPlane* p = first_plane;
  152.   while (p)
  153.   {
  154.     if (!strcmp (name, p->get_name ())) return p;
  155.     p = p->get_next ();
  156.   }
  157.   return NULL;
  158. }
  159.  
  160. Script* World::new_script (char* name)
  161. {
  162.   Script* s = new Script (name);
  163.   s->set_next (first_script);
  164.   first_script = s;
  165.   return s;
  166. }
  167.  
  168. Script* World::get_script (char* name)
  169. {
  170.   Script* s = first_script;
  171.   while (s)
  172.   {
  173.     if (!strcmp (name, s->get_name ())) return s;
  174.     s = s->get_next ();
  175.   }
  176.   return NULL;
  177. }
  178.  
  179. Collection* World::new_collection (char* name)
  180. {
  181.   Collection* s = new Collection (name);
  182.   s->set_next (first_collection);
  183.   first_collection = s;
  184.   return s;
  185. }
  186.  
  187. Collection* World::get_collection (char* name)
  188. {
  189.   Collection* s = first_collection;
  190.   while (s)
  191.   {
  192.     if (!strcmp (name, s->get_name ())) return s;
  193.     s = s->get_next ();
  194.   }
  195.   return NULL;
  196. }
  197.  
  198. Thing* World::get_thing (char* name)
  199. {
  200.   Sector* s = first_sector;
  201.   while (s)
  202.   {
  203.     Thing* t = s->get_thing (name);
  204.     if (t) return t;
  205.     s = (Sector*)(s->get_next ());
  206.   }
  207.   return NULL;
  208. }
  209.  
  210. ScriptRun* World::run_script (Script* s, CsObject* ps)
  211. {
  212.   ScriptRun* r = new ScriptRun (s, ps);
  213.   r->set_next (first_run);
  214.   r->set_prev (NULL);
  215.   if (first_run) first_run->set_prev (r);
  216.   first_run = r;
  217.   return r;
  218. }
  219.  
  220. void World::step_scripts ()
  221. {
  222.   ScriptRun* r = first_run;
  223.   while (r)
  224.   {
  225.     ScriptRun* n = r->get_next ();
  226.     if (r->step ())
  227.     {
  228.       // Finish run.
  229.       if (r->get_prev ()) r->get_prev ()->set_next (r->get_next ());
  230.       else first_run = r->get_next ();
  231.       if (r->get_next ()) r->get_next ()->set_prev (r->get_prev ());
  232.       delete r;
  233.     }
  234.     r = n;
  235.   }
  236. }
  237.  
  238. void World::trigger_activate (Camera& c)
  239. {
  240.   Vector3 where;
  241.   c.get_forward_position (3, where);
  242.   Polygon3D* p = c.get_hit (where);
  243.   where.dump ("where");
  244.   if (p)
  245.   {
  246.     printf ("Activate polygon '%s' ", p->get_name ());
  247.     PolygonSet* ob = p->get_poly_set ();
  248.     printf ("in set '%s'\n", ob->get_name ());
  249.     ob->do_activate_triggers (this);
  250.   }
  251. }
  252.  
  253. void World::save (FILE* fp, int indent)
  254. {
  255.   char sp[100]; strcpy (sp, spaces); sp[indent] = 0;
  256.   fprintf (fp, "%sWORLD (\n", sp);
  257.   textures->save (fp, indent+2);
  258.  
  259.   PolyPlane* p = first_plane;
  260.   while (p)
  261.   {
  262.     p->save (fp, indent+2);
  263.     p = p->get_next ();
  264.   }
  265.  
  266.   Script* sc = first_script;
  267.   while (sc)
  268.   {
  269.     sc->save (fp, indent+2);
  270.     sc = sc->get_next ();
  271.   }
  272.  
  273.   Sector* s = first_sector;
  274.   while (s)
  275.   {
  276.     s->save (fp, indent+2, textures);
  277.     s = (Sector*)(s->get_next ());
  278.   }
  279.  
  280.   Collection* c = first_collection;
  281.   while (c)
  282.   {
  283.     c->save (fp, indent+2);
  284.     c = c->get_next ();
  285.   }
  286.  
  287.   fprintf (fp, "%s)\n", sp);
  288. }
  289.  
  290. void World::load (char** buf)
  291. {
  292.   char* t;
  293.   char* old_buf;
  294.  
  295.   clear ();
  296.  
  297.   skip_token (buf, "WORLD");
  298.   skip_token (buf, "(", "Expected '%s' instead of '%s' after WORLD!\n");
  299.   while (TRUE)
  300.   {
  301.     old_buf = *buf;
  302.     t = get_token (buf);
  303.     if (*t == ')' || *t == 0) break;
  304.     if (!strcmp (t, "SECTOR"))
  305.     {
  306.       t = get_token (buf);
  307.       Sector* s = get_sector (t);
  308.       if (!s) s = new_sector (t, 10, 10);
  309.       *buf = old_buf;
  310.       s->load (this, buf, textures);
  311.     }
  312.     else if (!strcmp (t, "PLANE"))
  313.     {
  314.       t = get_token (buf);
  315.       PolyPlane* p = new_plane (t);
  316.       *buf = old_buf;
  317.       p->load (buf);
  318.     }
  319.     else if (!strcmp (t, "SCRIPT"))
  320.     {
  321.       t = get_token (buf);
  322.       Script* s = new_script (t);
  323.       *buf = old_buf;
  324.       s->load (buf);
  325.     }
  326.     else if (!strcmp (t, "COLLECTION"))
  327.     {
  328.       t = get_token (buf);
  329.       Collection* c = new_collection (t);
  330.       *buf = old_buf;
  331.       c->load (this, buf);
  332.     }
  333.     else if (!strcmp (t, "TEXTURES"))
  334.     {
  335.       *buf = old_buf;
  336.       if (textures) delete textures;
  337.       textures = new Textures (50);
  338.       textures->load (buf);
  339.       Filter::init_filters (textures);
  340.     }
  341.     else if (!strcmp (t, "ROOM"))
  342.     {
  343.       // Not an object but it is translated to a special sector.
  344.       t = get_token (buf);
  345.       Sector* s = get_sector (t);
  346.       if (!s) s = new_sector (t, 100, 50);
  347.       *buf = old_buf;
  348.       s->load_room (this, buf, textures);
  349.     }
  350.     else
  351.     {
  352.       printf ("What is '%s' doing in a WORLD statement?\n", t);
  353.     }
  354.   }
  355.  
  356.   cache.clear ();
  357.   shine_lights ();
  358.   mipmap_settings (LightMap::setting);
  359. }
  360.  
  361. void World::shine_lights ()
  362. {
  363.   Sector* s = first_sector;
  364.   while (s)
  365.   {
  366.     s->shine_lights ();
  367.     s = (Sector*)(s->get_next ());
  368.   }
  369. }
  370.  
  371. void World::mipmap_settings (int setting)
  372. {
  373.   Sector* s = first_sector;
  374.   while (s)
  375.   {
  376.     s->mipmap_settings (setting);
  377.     s = (Sector*)(s->get_next ());
  378.   }
  379. }
  380.  
  381. void World::save (char* file)
  382. {
  383.   FILE* fp = fopen (file, "w");
  384.   save (fp, 0);
  385.   fclose (fp);
  386. }
  387.  
  388. void World::load (char* file)
  389. {
  390.   FILE* fp = fopen (file, "rb");
  391.   fseek (fp, 0, SEEK_END);
  392.   long off = ftell (fp);
  393.   fseek (fp, 0, SEEK_SET);
  394.   char* buf = new char [off];
  395.   char* b = buf;
  396.   fread (b, 1, off, fp);
  397.  
  398.   load (&b);
  399.  
  400.   delete[] buf;
  401.  
  402.   fclose (fp);
  403. }
  404.  
  405. void World::draw (Graphics* g, Camera* c, ViewPolygon* view)
  406. {
  407.   Scan::g = g;
  408.   Scan::c = c;
  409.   Scan::textures = textures;
  410.  
  411.   if (!Scan::z_buffer)
  412.   {
  413.     Scan::z_buffer = new unsigned long [FRAME_WIDTH*FRAME_HEIGHT];
  414.     // @@@ This is never freed!!!
  415.   }
  416.   //@@@ WE SHOULD USE A WAY SO THAT WE DON'T NEED TO CLEAR THIS BUFFER.
  417.   // -> IF WE DO A Z-FILL OF EVERYTHING WHILE DRAWING THE SECTORS THIS
  418.   // STEP IS NOT NEEDED ANYMORE!
  419.   memset (Scan::z_buffer, 0, sizeof (unsigned long)*FRAME_WIDTH*FRAME_HEIGHT);
  420.  
  421.   c->sector->draw (view, Scan::c->m_world2cam, Scan::c->m_cam2world, Scan::c->v_viewpos);
  422. }
  423.  
  424. Polygon3D* World::select_polygon (Camera* c, ViewPolygon* view, int xx, int yy)
  425. {
  426.   yy = FRAME_HEIGHT-yy;
  427.   return c->sector->select_polygon (c, view, xx, yy);
  428. }
  429.  
  430. Vertex* World::select_vertex (Camera* c, ViewPolygon* view, int xx, int yy)
  431. {
  432.   yy = FRAME_HEIGHT-yy;
  433.   return c->sector->select_vertex (c, view, xx, yy);
  434. }
  435.  
  436. void World::edit_split_poly (Camera* c)
  437. {
  438.   if (!c->sel_polygon)
  439.   {
  440.     printf ("Please select a polygon!\n");
  441.     return;
  442.   }
  443.   if (c->num_sel_verts < 3)
  444.   {
  445.     printf ("Please select at least three vertices adjacent to the polygon!\n");
  446.     return;
  447.   }
  448.  
  449.   PolygonSet* pset = c->sel_polygon->get_poly_set ();
  450.   pset->edit_split_poly (c, textures);
  451. }
  452.  
  453. //---------------------------------------------------------------------------
  454.