LANDSCPE.CPP
上传用户:abcdshs
上传日期:2007-01-07
资源大小:1858k
文件大小:10k
源码类别:

游戏

开发平台:

Visual C++

  1. // (C) Copyright 1996 by Anthony J. Carin.  All Rights Reserved.
  2. #include "stdafx.h"
  3. #include "gateway.h"
  4. #include "dudehold.h"
  5. #include "weaphold.h"
  6. #include "weapon.h"
  7. #include "antagon.h"
  8. #include "levels.h"
  9. #include "teleport.h"
  10. extern CString& getpath(CString& filename);
  11. landscape::landscape(CString& filename) : baseobject(filename)
  12. {
  13.     m_type = LANDSCAPE;
  14.     m_bmpfile = filename;
  15.     m_LSEntry = 0;
  16.     m_DeleteEntryAlso = FALSE;
  17.     PrevLandScpe = 0;
  18.     NextLandScpe = Game->Landscapes()->m_First;
  19.     if (Game->Landscapes()->m_First)
  20.         Game->Landscapes()->m_First->PrevLandScpe = this;
  21.     Game->Landscapes()->m_First = this;
  22. }
  23. landscape::~landscape()
  24. {
  25.     if (PrevLandScpe)
  26.         PrevLandScpe->NextLandScpe = NextLandScpe;
  27.     if (NextLandScpe)
  28.         NextLandScpe->PrevLandScpe = PrevLandScpe;
  29.     if (Game->Landscapes()->m_First == this)
  30.         Game->Landscapes()->m_First = NextLandScpe;
  31.     if (Game->Landscapes()->m_Curr == this)
  32.         Game->Landscapes()->m_Curr = Game->Landscapes()->m_First;
  33.     if (m_DeleteEntryAlso)
  34.     {
  35.         if (m_LSEntry == 0)
  36.             m_LSEntry = Game->Landscapes()->GetLSEntry(location());
  37.         delete m_LSEntry;
  38.     }
  39. }
  40. void landscapelist::Purge()
  41. {
  42.     landscape *TmpFirst = m_First;
  43.     landscape *TmpCurr;
  44.     while (TmpFirst)
  45.     {
  46.         TmpCurr = TmpFirst;
  47.         TmpFirst = TmpFirst->NextLandScpe;
  48.         delete m_Curr;
  49.     }
  50.     m_First = m_Curr = 0;
  51. }
  52. void landscapelist::load(CString& str)
  53. {
  54.     Game->LevelPtr()->landscapemap = str;
  55.     PurgeLandscape();
  56.     FILE *fp;
  57.     char tmpbuf[100];
  58.     CString lobj;
  59.     view tmpview;
  60.     tccolor tmpcolor;
  61.     fp = fopen((LPCSTR)Game->LevelPtr()->landscapemap, "r");
  62.     if (fp <= 0)
  63.         return;
  64.     while (feof(fp) == 0)
  65.     {
  66.         fscanf(fp, "%s", tmpbuf);
  67.         tmpview.setx((float)atof(tmpbuf));
  68.         fscanf(fp, "%s", tmpbuf);
  69.         tmpview.sety((float)atof(tmpbuf));
  70.         fscanf(fp, "%s", tmpbuf);
  71.         tmpview.setz((float)atof(tmpbuf));
  72.         fscanf(fp, "%s", tmpbuf);
  73.         tmpview.setxdir((short)atoi(tmpbuf));
  74.         fscanf(fp, "%s", tmpbuf);
  75.         tmpview.setydir((short)atoi(tmpbuf));
  76.         fscanf(fp, "%s", tmpbuf);
  77.         tmpview.setzdir((short)atoi(tmpbuf));
  78.         fscanf(fp, "%s", tmpbuf);
  79.         lobj = tmpbuf;
  80.         fscanf(fp, "%s", tmpbuf);
  81.         tmpcolor = (long)atol(tmpbuf);
  82.         if (feof(fp) == 0)
  83.             AddLSEntry(tmpview, lobj, tmpcolor);
  84.     }
  85.     fclose(fp);
  86.     GotMap = TRUE;
  87.     PauseCounter = 0;
  88.     CreateCloseLandscapes();
  89. }
  90. void landscapelist::CreateCloseLandscapes()
  91. {
  92.     if (!GotMap)
  93.         return;
  94.     if (--PauseCounter < 0)
  95.         PauseCounter = 10;
  96.     else
  97.         return;
  98.     landscape *tmpobj, *dtmpobj;
  99.     LSEntryType *tmp = LSEntries;
  100.     float dist;
  101.     while (tmp)
  102.     {
  103.         tmpobj = 0;
  104.         coordinate c = Game->Aim();
  105.         dist = tmp->Location.distanceto(c);
  106.         if (tmp->Created == FALSE &&
  107.             dist <= VIEWRANGE)
  108.         {
  109.             tmp->Created = TRUE;
  110.             CString Right = tmp->Image.Right(3);
  111.             Right.MakeUpper();
  112.             if (Right == (CString)"DUD")
  113.             {
  114.                 long itmp = tmp->Color;
  115. #ifdef EDITOR
  116.                 if (itmp > 0)
  117.                     tmpobj = new dudeholder(tmp->Image);
  118.                 else
  119.                 {
  120.                     LSEntryType *savetmp = tmp;
  121.                     tmp = tmp->Next;
  122.                     delete savetmp;
  123.                     continue;
  124.                 }
  125. #else
  126.                 if (itmp > 0)
  127.                 {
  128.                     antagonist *anttmp;
  129.                     if (itmp > 10)
  130.                     {
  131.                         anttmp = new antagonist(tmp->Image, MALE);
  132.                         itmp -= 10;
  133.                     }
  134.                     else
  135.                         anttmp = new antagonist(tmp->Image, FEMALE);
  136.                     anttmp->setto(tmp->Location);
  137.                     anttmp->sethesitation((short) itmp);
  138.                     anttmp->shirtcolor(RGB(random(256), random(256), random(256)));
  139.                     anttmp->pantscolor(RGB(random(256), random(256), random(256)));
  140.                     Game->DrawQueue()->add(anttmp);
  141.                     LSEntryType *savetmp = tmp;
  142.                     tmp = tmp->Next;
  143.                     delete savetmp;
  144.                     continue;
  145.                 }
  146.                 else
  147.                 {
  148.                     tmpobj = new dudeholder(tmp->Image);
  149.                     tmpobj->setto(tmp->Location);
  150.                     tmpobj->setcolor(40);
  151.                     tmpobj->AssociatedLSEntry(tmp);
  152.                     Game->DrawQueue()->add(tmpobj);
  153.                     tmp = tmp->Next;
  154.                     continue;
  155.                 }
  156. #endif
  157.             }
  158.             if (Right == (CString)"MAP")
  159.                 tmpobj = new gateway(tmp->Image);
  160.             else if (Right == (CString)"IMG")
  161.                 tmpobj = new landscape(tmp->Image);
  162.             else if (Right == (CString)"TER") // Teleporter
  163.                 tmpobj = new Teleporter((short) tmp->Color);
  164.             else if (Right == (CString)"PON") // Weapon
  165.             {
  166. #ifdef EDITOR
  167.                 tmpobj = new weaponholder((WeaponType)(long)tmp->Color);
  168. #else
  169.                 WeaponType tmptype = (WeaponType)(long)tmp->Color;
  170.                 if (tmptype == SWORD)
  171.                     tmpobj = new weapon((CString)"sword.dat");
  172.                 else if (tmptype == SHIELD)
  173.                     tmpobj = new weapon((CString)"shield.dat");
  174.                 else
  175.                     tmpobj = new weapon((CString)"knife.dat");
  176.                 if (tmpobj->NoBody())
  177.                 {
  178.                     delete tmpobj;
  179.                     tmpobj = 0;
  180.                 }
  181. #endif
  182.             }
  183.             if (tmpobj)
  184.             {
  185.                 tmpobj->setto(tmp->Location);
  186.                 if (Right == (CString)"PON") // Weapon
  187.                     tmpobj->setcolor(RGB(random(256), random(256), random(256)));
  188.                 else
  189.                     tmpobj->setcolor(tmp->Color);
  190.                 tmpobj->AssociatedLSEntry(tmp);
  191.                 Game->DrawQueue()->add(tmpobj);
  192.             }
  193.         }
  194.         else if (tmp->Created == TRUE &&
  195.                  dist > VIEWRANGE)
  196.             tmp->Created = FALSE;
  197.         tmp = tmp->Next;
  198.     }
  199.     tmpobj = First();
  200.     while (tmpobj)
  201.     {
  202.         dtmpobj = tmpobj;
  203.         tmpobj = Next();
  204.         coordinate c = Game->Aim();
  205.         if (dtmpobj->location().distanceto(c) > VIEWRANGE)
  206.             delete dtmpobj;
  207.     }
  208. }
  209. LSEntryType * landscapelist::GetLSEntry(view& tmploc)
  210. {
  211.     LSEntryType *tmp = LSEntries;
  212.     while (tmp)
  213.     {
  214.         if (tmp->Location == tmploc)
  215.             return tmp;
  216.         tmp = tmp->Next;
  217.     }
  218.     return NULL;
  219. }
  220. void landscapelist::PurgeLandscape()
  221. {
  222.     GotMap = FALSE;
  223.     LSEntryType *tmp;
  224.     while (LSEntries)
  225.     {
  226.         tmp = LSEntries;
  227.         LSEntries = tmp->Next;
  228.         delete tmp;
  229.     }
  230. }
  231. void landscapelist::removeduplicates()
  232. {
  233.     LSEntryType *tmp = LSEntries->Next;
  234.     while (tmp)
  235.     {
  236.         if (*LSEntries == *tmp)
  237.         {
  238.             delete tmp;
  239.             break;
  240.         }
  241.         tmp = tmp->Next;
  242.     }
  243. }
  244. void landscapelist::AddLSEntry(view& v, CString& str, tccolor color)
  245. {
  246.     LSEntryType *newls;
  247.     newls = new LSEntryType(v, str, color);
  248.     newls->Next = LSEntries;
  249.     if (LSEntries)
  250.         LSEntries->Prev = newls;
  251.     LSEntries = newls;
  252.     removeduplicates();
  253. }
  254. void landscapelist::AddLSEntry(baseobject *lnd)
  255. {
  256.     if (lnd->IsGATEWAY())
  257.         AddLSEntry(lnd->location(), ((gateway *)lnd)->mapfile(), lnd->getcolor());
  258.     else if (lnd->IsLANDSCAPE())
  259.         AddLSEntry(lnd->location(), ((landscape *)lnd)->bmpfile(), lnd->getcolor());
  260.     else if (lnd->IsDUDEHOLDER())
  261.         AddLSEntry(lnd->location(), ((dudeholder *)lnd)->dudename(), ((dudeholder *)lnd)->speed());
  262.     else if (lnd->IsWEAPONHOLDER())
  263.         AddLSEntry(lnd->location(), (CString)"WEAPON", ((weaponholder *)lnd)->NumType());
  264.     else if (lnd->IsTELEPORTER())
  265.         AddLSEntry(lnd->location(), (CString)"TELEPORTER", ((Teleporter *)lnd)->ToLevel());
  266.     ((landscape *)lnd)->AssociatedLSEntry(LSEntries);
  267. }
  268. landscapelist::landscapelist()
  269. {
  270.     m_First         = 0;
  271.     m_Curr          = 0;
  272.     GotMap          = FALSE;
  273.     LSEntries       = 0;
  274.     PauseCounter    = 0;
  275. }
  276. LSEntryType::LSEntryType(view& v, CString& str, tccolor color)
  277. {
  278.     Location = v;
  279.     Image    = str;
  280.     Color    = color;
  281.     Created  = FALSE;
  282.     Prev = 0;
  283. }
  284. LSEntryType::~LSEntryType()
  285. {
  286.     if (Next)
  287.         Next->Prev = Prev;
  288.     if (Prev)
  289.         Prev->Next = Next;
  290.     else
  291.         Game->Landscapes()->LSEntries = Next;
  292. }
  293. char LSEntryType::operator ==(LSEntryType& other)
  294. {
  295.     if (Location != other.Location)
  296.         return FALSE;
  297.     if (Image != other.Image)
  298.         return FALSE;
  299.     return TRUE;
  300. }
  301. void landscapelist::save()
  302. {
  303.     if (Game->LevelPtr()->landscapemap.IsEmpty())
  304.         return;
  305.     FILE *fp;
  306.     fp = fopen((LPCSTR)Game->LevelPtr()->landscapemap, "w");
  307.     if (fp <= 0)
  308.         return;
  309.     view    tmploc;
  310.     LSEntryType *LStmp = LSEntries;
  311.     while (LStmp)
  312.     {
  313.         tmploc = LStmp->Location;
  314.         fprintf(fp, "%1.2f ", tmploc.x());
  315.         fprintf(fp, "%1.2f ", tmploc.y());
  316.         fprintf(fp, "%1.2f ", tmploc.z());
  317.         fprintf(fp, "%d ", (short)tmploc.xdirection());
  318.         fprintf(fp, "%d ", (short)tmploc.ydirection());
  319.         fprintf(fp, "%d ", (short)tmploc.zdirection());
  320.         fprintf(fp, "%s ", LStmp->Image);
  321.         fprintf(fp, "%ldn", LStmp->Color);
  322.         LStmp = LStmp->Next;
  323.     }
  324.     antagonist *atmp;
  325.     atmp = (antagonist *)Game->DrawQueue()->first(ANTAGONIST);
  326.     while (atmp)
  327.     {
  328.         tmploc = atmp->location();
  329.         fprintf(fp, "%1.2f ", tmploc.x());
  330.         fprintf(fp, "%1.2f ", tmploc.y());
  331.         fprintf(fp, "%1.2f ", tmploc.z());
  332.         fprintf(fp, "%d ", (short)tmploc.xdirection());
  333.         fprintf(fp, "%d ", (short)tmploc.ydirection());
  334.         fprintf(fp, "%d ", (short)tmploc.zdirection());
  335.         fprintf(fp, "%s ", atmp->filepath());
  336.         if (atmp->ismale() && atmp->skill() > 0)
  337.             fprintf(fp, "%ldn", atmp->skill()+10);
  338.         else
  339.             fprintf(fp, "%ldn", atmp->skill());
  340.         atmp = (antagonist *)Game->DrawQueue()->next(ANTAGONIST);
  341.     }
  342.     fclose(fp);
  343. }