home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / source / project.m < prev    next >
Encoding:
Text File  |  1996-08-08  |  8.7 KB  |  527 lines

  1. //======================================
  2. //
  3. // QuakeEd Project Management
  4. //
  5. //======================================
  6.  
  7. #import "qedefs.h"
  8.  
  9.  
  10. id    project_i;
  11.  
  12. @implementation Project
  13.  
  14. - init
  15. {
  16.     project_i = self;
  17.  
  18.     return self;
  19. }
  20.  
  21. //===========================================================
  22. //
  23. //    Project code
  24. //
  25. //===========================================================
  26. - initVars
  27. {
  28.     char        *s;
  29.     
  30.     s = [preferences_i getProjectPath];
  31.     StripFilename(s);
  32.     strcpy(path_basepath,s);
  33.     
  34.     strcpy(path_progdir,s);
  35.     strcat(path_progdir,"/"SUBDIR_ENT);
  36.     
  37.     strcpy(path_mapdirectory,s);
  38.     strcat(path_mapdirectory,"/"SUBDIR_MAPS);    // source dir
  39.  
  40.     strcpy(path_finalmapdir,s);
  41.     strcat(path_finalmapdir,"/"SUBDIR_MAPS);    // dest dir
  42.     
  43.     [basepathinfo_i    setStringValue:s];        // in Project Inspector
  44.     
  45.     #if 0
  46.     if ((s = [projectInfo getStringFor:BASEPATHKEY]))
  47.     {
  48.         strcpy(path_basepath,s);
  49.         
  50.         strcpy(path_progdir,s);
  51.         strcat(path_progdir,"/"SUBDIR_ENT);
  52.         
  53.         strcpy(path_mapdirectory,s);
  54.         strcat(path_mapdirectory,"/"SUBDIR_MAPS);    // source dir
  55.  
  56.         strcpy(path_finalmapdir,s);
  57.         strcat(path_finalmapdir,"/"SUBDIR_MAPS);    // dest dir
  58.         
  59.         [basepathinfo_i    setStringValue:s];        // in Project Inspector
  60.     }
  61.     #endif
  62.         
  63.     if ((s = [projectInfo getStringFor:BSPFULLVIS]))
  64.     {
  65.         strcpy(string_fullvis,s);
  66.         changeString('@','\"',string_fullvis);
  67.     }
  68.         
  69.     if ((s = [projectInfo getStringFor:BSPFASTVIS]))
  70.     {
  71.         strcpy(string_fastvis,s);
  72.         changeString('@','\"',string_fastvis);
  73.     }
  74.         
  75.     if ((s = [projectInfo getStringFor:BSPNOVIS]))
  76.     {
  77.         strcpy(string_novis,s);
  78.         changeString('@','\"',string_novis);
  79.     }
  80.         
  81.     if ((s = [projectInfo getStringFor:BSPRELIGHT]))
  82.     {
  83.         strcpy(string_relight,s);
  84.         changeString('@','\"',string_relight);
  85.     }
  86.         
  87.     if ((s = [projectInfo getStringFor:BSPLEAKTEST]))
  88.     {
  89.         strcpy(string_leaktest,s);
  90.         changeString('@','\"',string_leaktest);
  91.     }
  92.  
  93.     if ((s = [projectInfo getStringFor:BSPENTITIES]))
  94.     {
  95.         strcpy(string_entities,s);
  96.         changeString('@','\"', string_entities);
  97.     }
  98.  
  99.     // Build list of wads    
  100.     wadList = [projectInfo parseMultipleFrom:WADSKEY];
  101.  
  102.     //    Build list of maps & descriptions
  103.     mapList = [projectInfo parseMultipleFrom:MAPNAMESKEY];
  104.     descList = [projectInfo parseMultipleFrom:DESCKEY];
  105.     [self changeChar:'_' to:' ' in:descList];
  106.     
  107.     [self initProjSettings];
  108.  
  109.     return self;
  110. }
  111.  
  112. //
  113. //    Init Project Settings fields
  114. //
  115. - initProjSettings
  116. {
  117.     [pis_basepath_i    setStringValue:path_basepath];
  118.     [pis_fullvis_i    setStringValue:string_fullvis];
  119.     [pis_fastvis_i    setStringValue:string_fastvis];
  120.     [pis_novis_i    setStringValue:string_novis];
  121.     [pis_relight_i    setStringValue:string_relight];
  122.     [pis_leaktest_i    setStringValue:string_leaktest];
  123.     
  124.     return self;
  125. }
  126.  
  127. //
  128. //    Add text to the BSP Output window
  129. //
  130. - addToOutput:(char *)string
  131. {
  132.     int    end;
  133.     
  134.     end = [BSPoutput_i textLength];
  135.     [BSPoutput_i setSel:end :end];
  136.     [BSPoutput_i replaceSel:string];
  137.     
  138.     end = [BSPoutput_i textLength];
  139.     [BSPoutput_i setSel:end :end];
  140.     [BSPoutput_i scrollSelToVisible];
  141.     
  142.     return self;
  143. }
  144.  
  145. - clearBspOutput:sender
  146. {
  147.     [BSPoutput_i    selectAll:self];
  148.     [BSPoutput_i    replaceSel:"\0"];
  149.     
  150.     return self;
  151. }
  152.  
  153. - print
  154. {
  155.     [BSPoutput_i    printPSCode:self];
  156.     return self;
  157. }
  158.  
  159.  
  160. - initProject
  161. {
  162.     [self parseProjectFile];
  163.     if (projectInfo == NULL)
  164.         return self;
  165.     [self initVars];
  166.     [mapbrowse_i reuseColumns:YES];
  167.     [mapbrowse_i loadColumnZero];
  168.     [pis_wads_i reuseColumns:YES];
  169.     [pis_wads_i loadColumnZero];
  170.  
  171.     [things_i        initEntities];
  172.     
  173.     return self;
  174. }
  175.  
  176. //
  177. //    Change a character to another in a Storage list of strings
  178. //
  179. - changeChar:(char)f to:(char)t in:(id)obj
  180. {
  181.     int    i;
  182.     int    max;
  183.     char    *string;
  184.  
  185.     max = [obj count];
  186.     for (i = 0;i < max;i++)
  187.     {
  188.         string = [obj elementAt:i];
  189.         changeString(f,t,string);
  190.     }
  191.     return self;
  192. }
  193.  
  194. //
  195. //    Fill the QuakeEd Maps or wads browser
  196. //    (Delegate method - delegated in Interface Builder)
  197. //
  198. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  199. {
  200.     id        cell, list;
  201.     int        max;
  202.     char    *name;
  203.     int        i;
  204.  
  205.     if (sender == mapbrowse_i)
  206.         list = mapList;
  207.     else if (sender == pis_wads_i)
  208.         list = wadList;
  209.     else
  210.     {
  211.         list = nil;
  212.         Error ("Project: unknown browser to fill");
  213.     }
  214.     
  215.     max = [list count];
  216.     for (i = 0 ; i<max ; i++)
  217.     {
  218.         name = [list elementAt:i];
  219.         [matrix addRow];
  220.         cell = [matrix cellAt:i :0];
  221.         [cell setStringValue:name];
  222.         [cell setLeaf:YES];
  223.         [cell setLoaded:YES];
  224.     }
  225.     return i;
  226. }
  227.  
  228. //
  229. //    Clicked on a map name or description!
  230. //
  231. - clickedOnMap:sender
  232. {
  233.     id    matrix;
  234.     int    row;
  235.     char    fname[1024];
  236.     id    panel;
  237.     
  238.     matrix = [sender matrixInColumn:0];
  239.     row = [matrix selectedRow];
  240.     sprintf(fname,"%s/%s.map",path_mapdirectory,
  241.         (char *)[mapList elementAt:row]);
  242.     
  243.     panel = NXGetAlertPanel("Loading...",
  244.         "Loading map. Please wait.",NULL,NULL,NULL);
  245.     [panel orderFront:NULL];
  246.  
  247.     [quakeed_i doOpen:fname];
  248.  
  249.     [panel performClose:NULL];
  250.     NXFreeAlertPanel(panel);
  251.     return self;
  252. }
  253.  
  254.  
  255. - setTextureWad: (char *)wf
  256. {
  257.     int        i, c;
  258.     char    *name;
  259.     
  260.     qprintf ("loading %s", wf);
  261.  
  262. // set the row in the settings inspector wad browser
  263.     c = [wadList count];
  264.     for (i=0 ; i<c ; i++)
  265.     {
  266.         name = (char *)[wadList elementAt:i];
  267.         if (!strcmp(name, wf))
  268.         {
  269.             [[pis_wads_i matrixInColumn:0] selectCellAt: i : 0];
  270.             break;
  271.         }
  272.     }
  273.  
  274. // update the texture inspector
  275.     [texturepalette_i initPaletteFromWadfile:wf ];
  276.     [[map_i objectAt: 0] setKey:"wad" toValue: wf];
  277. //    [inspcontrol_i changeInspectorTo:i_textures];
  278.  
  279.     [quakeed_i updateAll];
  280.  
  281.     return self;
  282. }
  283.  
  284. //
  285. //    Clicked on a wad name
  286. //
  287. - clickedOnWad:sender
  288. {
  289.     id        matrix;
  290.     int        row;
  291.     char    *name;
  292.     
  293.     matrix = [sender matrixInColumn:0];
  294.     row = [matrix selectedRow];
  295.  
  296.     name = (char *)[wadList elementAt:row];
  297.     [self setTextureWad: name];
  298.     
  299.     return self;
  300. }
  301.  
  302.  
  303. //
  304. //    Read in the <name>.QE_Project file
  305. //
  306. - parseProjectFile
  307. {
  308.     char    *path;
  309.     int        rtn;
  310.     
  311.     path = [preferences_i getProjectPath];
  312.     if (!path || !path[0] || access(path,0))
  313.     {
  314.         rtn = NXRunAlertPanel("Project Error!",
  315.             "A default project has not been found.\n"
  316.             , "Open Project", NULL, NULL);
  317.         if ([self openProject] == nil)
  318.             while (1)        // can't run without a project
  319.                 [NXApp terminate: self];
  320.         return self;    
  321.     }
  322.  
  323.     [self openProjectFile:path];
  324.     return self;
  325. }
  326.  
  327. //
  328. //    Loads and parses a project file
  329. //
  330. - openProjectFile:(char *)path
  331. {        
  332.     FILE    *fp;
  333.     struct    stat s;
  334.  
  335.     strcpy(path_projectinfo,path);
  336.  
  337.     projectInfo = NULL;
  338.     fp = fopen(path,"r+t");
  339.     if (fp == NULL)
  340.         return self;
  341.  
  342.     stat(path,&s);
  343.     lastModified = s.st_mtime;
  344.  
  345.     projectInfo = [(Dict *)[Dict alloc] initFromFile:fp];
  346.     fclose(fp);
  347.     
  348.     return self;
  349. }
  350.  
  351. - (char *)currentProjectFile
  352. {
  353.     return path_projectinfo;
  354. }
  355.  
  356. //
  357. //    Open a project file
  358. //
  359. - openProject
  360. {
  361.     char    path[128];
  362.     id        openpanel;
  363.     int        rtn;
  364.     char    *projtypes[2] = {"qpr",NULL};
  365.     char    **filenames;
  366.     char    *dir;
  367.     
  368.     openpanel = [OpenPanel new];
  369.     [openpanel allowMultipleFiles:NO];
  370.     [openpanel chooseDirectories:NO];
  371.     rtn = [openpanel runModalForTypes:projtypes];
  372.     if (rtn == NX_OKTAG)
  373.     {
  374.          (const char *const *)filenames = [openpanel filenames];
  375.          dir = (char *)[openpanel directory];
  376.          sprintf(path,"%s/%s",dir,filenames[0]);
  377.          strcpy(path_projectinfo,path);
  378.          [self openProjectFile:path];
  379.          return self;
  380.     }
  381.     
  382.     return nil;
  383. }
  384.  
  385.  
  386. //
  387. //    Search for a string in a List of strings
  388. //
  389. - (int)searchForString:(char *)str in:(id)obj
  390. {
  391.     int    i;
  392.     int    max;
  393.     char    *s;
  394.  
  395.     max = [obj count];
  396.     for (i = 0;i < max; i++)
  397.     {
  398.         s = (char *)[obj elementAt:i];
  399.         if (!strcmp(s,str))
  400.             return 1;
  401.     }
  402.     return 0;
  403. }
  404.  
  405. - (char *)getMapDirectory
  406. {
  407.     return path_mapdirectory;
  408. }
  409.  
  410. - (char *)getFinalMapDirectory
  411. {
  412.     return path_finalmapdir;
  413. }
  414.  
  415. - (char *)getProgDirectory
  416. {
  417.     return path_progdir;
  418. }
  419.  
  420.  
  421. //
  422. //    Return the WAD name for cmd-8
  423. //
  424. - (char *)getWAD8
  425. {
  426.     if (!path_wad8[0])
  427.         return NULL;
  428.     return path_wad8;
  429. }
  430.  
  431. //
  432. //    Return the WAD name for cmd-9
  433. //
  434. - (char *)getWAD9
  435. {
  436.     if (!path_wad9[0])
  437.         return NULL;
  438.     return path_wad9;
  439. }
  440.  
  441. //
  442. //    Return the WAD name for cmd-0
  443. //
  444. - (char *)getWAD0
  445. {
  446.     if (!path_wad0[0])
  447.         return NULL;
  448.     return path_wad0;
  449. }
  450.  
  451. //
  452. //    Return the FULLVIS cmd string
  453. //
  454. - (char *)getFullVisCmd
  455. {
  456.     if (!string_fullvis[0])
  457.         return NULL;
  458.     return string_fullvis;
  459. }
  460.  
  461. //
  462. //    Return the FASTVIS cmd string
  463. //
  464. - (char *)getFastVisCmd
  465. {
  466.     if (!string_fastvis[0])
  467.         return NULL;
  468.     return string_fastvis;
  469. }
  470.  
  471. //
  472. //    Return the NOVIS cmd string
  473. //
  474. - (char *)getNoVisCmd
  475. {
  476.     if (!string_novis[0])
  477.         return NULL;
  478.     return string_novis;
  479. }
  480.  
  481. //
  482. //    Return the RELIGHT cmd string
  483. //
  484. - (char *)getRelightCmd
  485. {
  486.     if (!string_relight[0])
  487.         return NULL;
  488.     return string_relight;
  489. }
  490.  
  491. //
  492. //    Return the LEAKTEST cmd string
  493. //
  494. - (char *)getLeaktestCmd
  495. {
  496.     if (!string_leaktest[0])
  497.         return NULL;
  498.     return string_leaktest;
  499. }
  500.  
  501. - (char *)getEntitiesCmd
  502. {
  503.     if (!string_entities[0])
  504.         return NULL;
  505.     return string_entities;
  506. }
  507.  
  508. @end
  509.  
  510. //====================================================
  511. // C Functions
  512. //====================================================
  513.  
  514. //
  515. // Change a character to a different char in a string
  516. //
  517. void changeString(char cf,char ct,char *string)
  518. {
  519.     int    j;
  520.  
  521.     for (j = 0;j < strlen(string);j++)
  522.         if (string[j] == cf)
  523.             string[j] = ct;
  524. }
  525.  
  526.  
  527.