Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

source/drawing.c

Go to the documentation of this file.
00001 
00007 #include <locking.h>
00008 #include <ui.h>
00009 #include <zakdef.h>
00010 #include <globals.h>
00011 #include <drawing.h>
00012 #include <simulation.h>
00013 
00014 void
00015 InitGraphic(void)
00016 {
00017         UIInitGraphic();
00018 }
00019 
00023 void
00024 Goto(UInt16 x, UInt16 y, goto_code center)
00025 {
00026         Int16 nx = (Int16)x;
00027         Int16 ny = (Int16)y;
00028 
00029         if (center) {
00030                 nx -= (Int16)(getVisibleX() / 2);
00031                 ny -= (Int16)(getVisibleY() / 2);
00032         }
00033 
00034         if (nx < 0)
00035                 nx = 0;
00036         if (nx > (Int16)(getMapWidth() - getVisibleX()))
00037                 nx = (Int16)(getMapWidth() - getVisibleX());
00038         if (ny < 0)
00039                 ny = 0;
00040         if (ny > (Int16)(getMapHeight() - getVisibleY()))
00041                 ny = (Int16)(getMapHeight() - getVisibleY());
00042         setMapXPos((Int8)nx);
00043         setMapYPos((Int8)ny);
00044         RedrawAllFields();
00045 }
00046 
00047 void
00048 RedrawAllFields(void)
00049 {
00050         addGraphicUpdate(gu_all);
00051 }
00052 
00053 void
00054 ScrollDisplay(dirType direction)
00055 {
00056         int moved = 1;
00057 
00058         switch (direction) {
00059         case dtUp:
00060                 if (getMapYPos() > 0)
00061                         setMapYPos((Int8)(getMapYPos() - 1));
00062                 else
00063                         moved = 0;
00064                 break;
00065         case dtRight:
00066                 if (getMapXPos() <=
00067                     (Int16)(getMapWidth() - 1 - getVisibleX()))
00068                         setMapXPos((Int8)(getMapXPos() + 1));
00069                 else
00070                         moved = 0;
00071                 break;
00072         case dtDown:
00073                 if (getMapYPos() <=
00074                     (Int16)(getMapHeight() - 1 - getVisibleY()))
00075                         setMapYPos((Int8)(getMapYPos() + 1));
00076                 else
00077                         moved = 0;
00078                 break;
00079         case dtLeft:
00080                 if (getMapXPos() > 0)
00081                         setMapXPos((Int8)(getMapXPos() - 1));
00082                 else
00083                         moved = 0;
00084                 break;
00085         default:
00086                 return;
00087         }
00088         if (moved)
00089                 UIScrollDisplay(direction);
00090 }
00091 
00092 void
00093 MoveCursor(dirType direction)
00094 {
00095         UInt16 old_x = getCursorX();
00096         UInt16 old_y = getCursorY();
00097 
00098         LockZone(lz_world);
00099         LockZone(lz_flags);
00100 
00101         switch (direction) {
00102         case dtUp:
00103                 if (getCursorY() > 0)
00104                         getCursorY()--;
00105                 if ((Int16)getCursorY() < getMapYPos())
00106                         ScrollDisplay(direction);
00107                 break;
00108         case dtRight:
00109                 if ((Int16)getCursorX() < (getMapWidth() - 1))
00110                         getCursorX()++;
00111                 if (((Int16)getCursorX() >
00112                     (Int16)(getMapXPos() + getVisibleX()-1)) &&
00113                         getCursorX() < getMapWidth())
00114                         ScrollDisplay(direction);
00115                 break;
00116         case dtDown:
00117                 if ((Int16)getCursorY() < (Int16)(getMapHeight() - 1))
00118                         getCursorY()++;
00119                 if ((getCursorY() > getMapYPos() + getVisibleY()-1) &&
00120                         getCursorY() < getMapHeight())
00121                         ScrollDisplay(direction);
00122                 break;
00123         case dtLeft:
00124                 if (getCursorX() > 0)
00125                         getCursorX()--;
00126                 if ((Int16)getCursorX() < getMapXPos())
00127                         ScrollDisplay(direction);
00128                 break;
00129         }
00130 
00131         DrawField(old_x, old_y);
00132         DrawField(getCursorX(), getCursorY());
00133 
00134         UnlockZone(lz_flags);
00135         UnlockZone(lz_world);
00136 }
00137 
00138 void
00139 DrawField(UInt16 xpos, UInt16 ypos)
00140 {
00141         UIInitDrawing();
00142 
00143         DrawFieldWithoutInit(xpos, ypos);
00144 
00145         UIFinishDrawing();
00146 }
00147 
00148 void
00149 DrawCross(UInt16 xpos, UInt16 ypos, UInt16 xsize, UInt16 ysize)
00150 {
00151         UInt16 tx, ty;
00152         tx = xpos;
00153         ty = ypos;
00154         xpos -= 1;
00155         UIInitDrawing();
00156         while (xpos <= tx + xsize) {
00157                 ypos = ty - 1;
00158                 while (ypos <= ty + ysize) {
00159                         if (((ypos == ty - 1) && (xpos == tx - 1)) ||
00160                             ((ypos == ty - 1) && (xpos == tx + xsize)) ||
00161                             ((ypos == ty + ysize) && (xpos == tx - 1)) ||
00162                             ((ypos == ty + ysize) && (xpos == tx + xsize)))
00163                                 goto next;
00164                         if ((xpos < getMapWidth()) &&
00165                             (ypos < getMapHeight())) {
00166                                 DrawFieldWithoutInit(xpos, ypos);
00167                         }
00168 next:
00169                         ypos += 1;
00170                 }
00171                 xpos += 1;
00172         }
00173         UIFinishDrawing();
00174 }
00175 
00176 void
00177 DrawFieldWithoutInit(UInt16 xpos, UInt16 ypos)
00178 {
00179         UInt16 i;
00180         selem_t flag;
00181         welem_t content, special;
00182         UInt32 worldpos;
00183 
00184         if (xpos >= getMapWidth() ||
00185             ypos >= getMapHeight() || UIClipped(xpos, ypos))
00186                 return;
00187 
00188         worldpos = WORLDPOS(xpos, ypos);
00189         getWorldAndFlag(worldpos, &content, &flag);
00190         special = GetGraphicNumber(worldpos);
00191 
00192         orWorldFlags(worldpos, PAINTEDBIT);
00193 
00194         UIPaintField(xpos, ypos, special);
00195         UIPaintMapField(xpos, ypos, special);
00196         UIPaintMapStatus(xpos, ypos, special, flag);
00197 
00198         if ((flag & POWEREDBIT) == 0 && CarryPower(content)) {
00199                 UIPaintPowerLoss(xpos, ypos);
00200         }
00201 
00202         if ((flag & WATEREDBIT) == 0 && CarryWater(content)) {
00203                 UIPaintWaterLoss(xpos, ypos);
00204         }
00205 
00206         if (xpos == getCursorX() && ypos == getCursorY()) {
00207                 UIPaintCursor(getCursorX(), getCursorY());
00208         }
00209 
00210         /* draw monster */
00211         for (i = 0; i < NUM_OF_OBJECTS; i++) {
00212                 if ((UInt16)xpos == game.objects[i].x &&
00213                         (UInt16)ypos == game.objects[i].y &&
00214                         game.objects[i].active != 0) {
00215                         UIPaintSpecialObject(xpos, ypos, (Int8)i);
00216                 }
00217         }
00218         /* draw extra units */
00219         for (i = 0; i < NUM_OF_UNITS; i++) {
00220                 if (xpos == game.units[i].x &&
00221                         ypos == game.units[i].y &&
00222                         game.units[i].active != 0) {
00223                         UIPaintSpecialUnit(xpos, ypos, (Int8)i);
00224                 }
00225         }
00226 }
00227 
00228 welem_t
00229 GetGraphicNumber(UInt32 pos)
00230 {
00231         welem_t retval = 0;
00232 
00233         retval = getWorld(pos);
00234         if (IsRoad(retval)) {
00235                 retval = GetSpecialGraphicNumber(pos);
00236         } else if (IsPowerLine(retval)) {
00237                 retval = GetSpecialGraphicNumber(pos);
00238         } else if (IsWaterPipe(retval)) {
00239                 retval = GetSpecialGraphicNumber(pos);
00240         } else if (IsRail(retval)) {
00241                 retval = GetSpecialGraphicNumber(pos);
00242         }
00243         return (retval);
00244 }
00245 
00246 welem_t
00247 GetSpecialGraphicNumber(UInt32 pos)
00248 {
00249         int a = 0; /* Above me */
00250         int b = 0; /* To the left of me */
00251         int c = 0; /* Below me */
00252         int d = 0; /* to the right of me */
00253         welem_t nAddMe = 0;
00254         welem_t elt = 0;
00255         welem_t wpe = getWorld(pos);
00256 
00257         if (IsRoad(wpe)) {
00258                 if (pos >= getMapWidth()) {
00259                         elt = getWorld(pos - getMapWidth());
00260                         a = IsRoad(elt) || IsRoadBridge(elt) ||
00261                             IsRoadPower(elt) || IsRoadPipe(elt);
00262                 }
00263                 if (pos < (unsigned long)(MapMul() - getMapWidth()))
00264                         elt = getWorld(pos + getMapWidth());
00265                         c = IsRoad(elt) || IsRoadBridge(elt) ||
00266                             IsRoadPower(elt) || IsRoadPipe(elt);
00267                 if ((unsigned long)(pos % getMapWidth()) <
00268                     (unsigned long)(getMapWidth() - 1)) {
00269                         elt = getWorld(pos + 1);
00270                         b = IsRoad(elt) || IsRoadBridge(elt) ||
00271                             IsRoadPower(elt) || IsRoadPipe(elt);
00272                 }
00273                 if (pos % getMapWidth() > 0) {
00274                         elt = getWorld(pos - 1);
00275                         d = IsRoad(elt) || IsRoadBridge(elt) ||
00276                             IsRoadPower(elt) || IsRoadPipe(elt);
00277                 }
00278                 nAddMe = Z_ROAD_START;
00279         } else if (IsWaterPipe(wpe)) {
00280                 if (pos >= getMapWidth())
00281                         a = CarryWater(getWorld(pos - getMapWidth()));
00282                 if (pos < (unsigned long)(MapMul() - getMapWidth()))
00283                         c = CarryWater(getWorld(pos + getMapWidth()));
00284                 if (pos % getMapWidth() < (unsigned long)(getMapWidth() - 1))
00285                         b = CarryWater(getWorld(pos + 1));
00286                 if (pos % getMapWidth() > 0) d = CarryWater(getWorld(pos - 1));
00287                 nAddMe = Z_PIPE_START;
00288         } else if (IsPowerLine(wpe)) {
00289                 if (pos >= getMapWidth())
00290                         a = CarryPower(getWorld(pos - getMapWidth()));
00291                 if (pos < (unsigned long)(MapMul() - getMapWidth()))
00292                         c = CarryPower(getWorld(pos + getMapWidth()));
00293                 if (pos % getMapWidth() < (unsigned long)(getMapWidth() - 1))
00294                         b = CarryPower(getWorld(pos+1));
00295                 if (pos % getMapWidth() > 0) d = CarryPower(getWorld(pos - 1));
00296                 nAddMe = Z_POWERLINE;
00297         } else if (IsRail(wpe)) {
00298                 if (pos >= getMapWidth()) {
00299                         elt = getWorld(pos - getMapWidth());
00300                         a = IsRail(elt) || IsRailTunnel(elt) ||
00301                             IsRailPower(elt) || IsRailPipe(elt);
00302                 }
00303                 if (pos < (unsigned long)(MapMul() - getMapWidth()))
00304                         elt = getWorld(pos + getMapWidth());
00305                         c = IsRail(elt) || IsRailTunnel(elt) ||
00306                             IsRailPower(elt) || IsRailPipe(elt);
00307                 if ((unsigned long)(pos % getMapWidth()) <
00308                     (unsigned long)(getMapWidth() - 1)) {
00309                         elt = getWorld(pos + 1);
00310                         b = IsRail(elt) || IsRailTunnel(elt) ||
00311                             IsRailPower(elt) || IsRailPipe(elt);
00312                 }
00313                 if (pos % getMapWidth() > 0) {
00314                         elt = getWorld(pos - 1);
00315                         d = IsRail(elt) || IsRailTunnel(elt) ||
00316                             IsRailPower(elt) || IsRailPipe(elt);
00317                 }
00318                 nAddMe = Z_RAIL_START;
00319         }
00320 
00321         if ((a && b && c && d) == 1)
00322                 return ((UInt8)(10 + nAddMe));
00323         if ((a && b && d) == 1)
00324                 return ((UInt8)(9 + nAddMe));
00325         if ((b && c && d) == 1)
00326                 return ((UInt8)(8 + nAddMe));
00327         if ((a && b && c) == 1)
00328                 return ((UInt8)(7 + nAddMe));
00329         if ((a && c && d) == 1)
00330                 return ((UInt8)(6 + nAddMe));
00331         if ((a && b) == 1)
00332                 return ((UInt8)(5 + nAddMe));
00333         if ((a && d) == 1)
00334                 return ((UInt8)(4 + nAddMe));
00335         if ((c && d) == 1)
00336                 return ((UInt8)(3 + nAddMe));
00337         if ((c && b) == 1)
00338                 return ((UInt8)(2 + nAddMe));
00339         if ((a || c) == 1)
00340                 return ((UInt8)(1 + nAddMe));
00341 
00342         return (nAddMe);
00343 }
00344 

Generated on Mon Aug 30 19:44:16 2004 for pocketcity by doxygen1.2.18