MiniMapGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2005  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "MiniMapGump.h"
00021 #include "GUIApp.h"
00022 #include "World.h"
00023 #include "CurrentMap.h"
00024 #include "Shape.h"
00025 #include "ShapeFrame.h"
00026 #include "MainActor.h"
00027 #include "RenderSurface.h"
00028 #include "ShapeInfo.h"
00029 #include "Palette.h"
00030 #include "getObject.h"
00031 
00032 DEFINE_RUNTIME_CLASSTYPE_CODE(MiniMapGump,Gump);
00033 
00034 MiniMapGump::MiniMapGump(int x, int y) : 
00035         Gump(x,y,MAP_NUM_CHUNKS*2+2,MAP_NUM_CHUNKS*2+2,0,
00036                  FLAG_DRAGGABLE,LAYER_NORMAL), minimap(), lastMapNum(0)
00037 {
00038         minimap.format = TEX_FMT_NATIVE;
00039         minimap.width = minimap.height = MAP_NUM_CHUNKS*MINMAPGUMP_SCALE;
00040         minimap.buffer = texbuffer[0];
00041 
00042         con.AddConsoleCommand("MiniMapGump::generateWholeMap",
00043                                                   MiniMapGump::ConCmd_generateWholeMap);
00044 }
00045 
00046 MiniMapGump::MiniMapGump() : Gump()
00047 {
00048         con.RemoveConsoleCommand(MiniMapGump::ConCmd_generateWholeMap);
00049 }
00050 
00051 MiniMapGump::~MiniMapGump(void)
00052 {
00053         con.RemoveConsoleCommand(MiniMapGump::ConCmd_generateWholeMap);
00054 }
00055 
00056 void MiniMapGump::PaintThis(RenderSurface* surf, sint32 lerp_factor, bool scaled)
00057 {
00058         World *world = World::get_instance();
00059         CurrentMap *currentmap = world->getCurrentMap();
00060         int mapChunkSize = currentmap->getChunkSize();
00061 
00062         if (currentmap->getNum() != lastMapNum) {
00063                 std::memset(texbuffer,0,sizeof(texbuffer));
00064                 lastMapNum = currentmap->getNum();
00065         }
00066 
00067         surf->Fill32(0xFFFFAF00,0,0,MAP_NUM_CHUNKS*2+1,1);
00068         surf->Fill32(0xFFFFAF00,0,1,1,MAP_NUM_CHUNKS*2+1);
00069         surf->Fill32(0xFFFFAF00,1,MAP_NUM_CHUNKS*2+1,MAP_NUM_CHUNKS*2+1,1);
00070         surf->Fill32(0xFFFFAF00,MAP_NUM_CHUNKS*2+1,1,1,MAP_NUM_CHUNKS*2+1);
00071 
00072         for (int y = 0; y < MAP_NUM_CHUNKS; y++) for (int x = 0; x < MAP_NUM_CHUNKS; x++)
00073         {
00074                 if (currentmap->isChunkFast(x,y)) {
00075 
00076                         for (int j = 0; j < MINMAPGUMP_SCALE; j++) for (int i = 0; i < MINMAPGUMP_SCALE; i++)
00077                         {
00078                                 if (texbuffer[y*MINMAPGUMP_SCALE+j][x*MINMAPGUMP_SCALE+i] == 0)
00079                                         texbuffer[y*MINMAPGUMP_SCALE+j][x*MINMAPGUMP_SCALE+i] = sampleAtPoint(
00080                                                 x*mapChunkSize + mapChunkSize/(MINMAPGUMP_SCALE*2) + (mapChunkSize*i)/MINMAPGUMP_SCALE, 
00081                                                 y*mapChunkSize + mapChunkSize/(MINMAPGUMP_SCALE*2) + (mapChunkSize*j)/MINMAPGUMP_SCALE,
00082                                                 currentmap);
00083                         }
00084                 }
00085         }
00086 
00087         // Center on avatar
00088         int sx = 0, sy = 0, ox = 0, oy = 0, lx = 0, ly = 0;
00089 
00090         MainActor *av = getMainActor();
00091         sint32 ax,ay,az;
00092         av->getLocation(ax,ay,az);
00093 
00094         ax = ax/(mapChunkSize/MINMAPGUMP_SCALE);
00095         ay = ay/(mapChunkSize/MINMAPGUMP_SCALE);
00096 
00097         sx = ax - (mapChunkSize/(4*2));
00098         sy = ay - (mapChunkSize/(4*2));
00099         ax = ax - sx;
00100         ay = ay - sy;
00101 
00102         if (sx < 0) {
00103                 ox = -sx;
00104                 surf->Fill32(0,1,1,ox,MAP_NUM_CHUNKS*2);
00105         }
00106         else if ((sx+MAP_NUM_CHUNKS*2) > (MAP_NUM_CHUNKS*MINMAPGUMP_SCALE)) {
00107                 lx = (sx+MAP_NUM_CHUNKS*2) - (MAP_NUM_CHUNKS*MINMAPGUMP_SCALE);
00108                 surf->Fill32(0,1+(MAP_NUM_CHUNKS*2)-lx,1,lx,MAP_NUM_CHUNKS*2);
00109         }
00110 
00111         if (sy < 0) {
00112                 oy = -sy;
00113                 surf->Fill32(0,1,1,MAP_NUM_CHUNKS*2,oy);
00114         }
00115         else if ((sy+MAP_NUM_CHUNKS*2) > (MAP_NUM_CHUNKS*MINMAPGUMP_SCALE)) {
00116                 ly = (sy+MAP_NUM_CHUNKS*2) - (MAP_NUM_CHUNKS*MINMAPGUMP_SCALE);
00117                 surf->Fill32(0,1,1+(MAP_NUM_CHUNKS*2)-ly,MAP_NUM_CHUNKS*2,ly);
00118         }
00119 
00120         surf->Blit(&minimap,sx+ox,sy+oy,MAP_NUM_CHUNKS*2-(ox+lx),MAP_NUM_CHUNKS*2-(oy+ly),1+ox,1+oy);
00121 
00122         surf->Fill32(0xFFFFFF00,1+ax-2,1+ay+0,2,1);
00123         surf->Fill32(0xFFFFFF00,1+ax+0,1+ay-2,1,2);
00124         surf->Fill32(0xFFFFFF00,1+ax+1,1+ay+0,2,1);
00125         surf->Fill32(0xFFFFFF00,1+ax+0,1+ay+1,1,2);
00126 }
00127 
00128 uint32 MiniMapGump::sampleAtPoint(int x, int y, CurrentMap *currentmap)
00129 {
00130         Item *item = currentmap->traceTopItem(x,y,1<<15,-1,0,ShapeInfo::SI_ROOF|ShapeInfo::SI_OCCL|ShapeInfo::SI_LAND|ShapeInfo::SI_SEA);
00131 
00132         if (item) 
00133         {
00134                 sint32 ix,iy,iz,idx,idy,idz;
00135                 item->getLocation(ix,iy,iz);
00136                 item->getFootpadWorld(idx,idy,idz);
00137 
00138                 ix -= x;
00139                 iy -= y;
00140 
00141                 Shape *sh = item->getShapeObject();
00142                 if (!sh) return 0;
00143 
00144                 ShapeFrame *frame = sh->getFrame(item->getFrame());
00145                 if (!frame) return 0;
00146 
00147                 const Pentagram::Palette *pal = sh->getPalette();
00148                 if (!pal) return 0;
00149 
00150                 // Screenspace bounding box bottom x coord (RNB x coord)
00151                 int sx = (ix - iy)/4;
00152                 // Screenspace bounding box bottom extent  (RNB y coord)
00153                 int sy = (ix + iy)/8 + idz;
00154 
00155                 uint16 r=0, g=0, b=0, c=0;
00156 
00157                 for (int j = 0; j < 2; j++) for (int i = 0; i < 2; i++)
00158                 {
00159                         if (!frame->hasPoint(i-sx,j-sy)) continue;
00160 
00161                         uint16 r2, g2, b2;
00162                         UNPACK_RGB8(pal->native_untransformed[frame->getPixelAtPoint(i-sx,j-sy)], r2, g2, b2);
00163                         r += RenderSurface::Gamma22toGamma10[r2]; g += RenderSurface::Gamma22toGamma10[g2]; b += RenderSurface::Gamma22toGamma10[b2];
00164                         c++;
00165                 }
00166                 if (!c) return 0;
00167 
00168                 return PACK_RGB8(RenderSurface::Gamma10toGamma22[r/c],RenderSurface::Gamma10toGamma22[g/c],RenderSurface::Gamma10toGamma22[b/c]);
00169         }
00170         else return 0;
00171 }
00172 
00173 void MiniMapGump::ConCmd_toggle(const Console::ArgvType &argv)
00174 {
00175         GUIApp *app = GUIApp::get_instance();
00176         Gump *desktop = app->getDesktopGump();
00177         Gump *mmg = desktop->FindGump(MiniMapGump::ClassType);
00178 
00179         if (!mmg) {
00180                 mmg = new MiniMapGump(4,4);
00181                 mmg->InitGump(0);
00182                 mmg->setRelativePosition(TOP_LEFT, 4, 4);
00183 
00184         }
00185         else {
00186                 mmg->Close();
00187         }
00188 }
00189 
00190 void MiniMapGump::ConCmd_generateWholeMap(const Console::ArgvType &argv)
00191 {
00192         World *world = World::get_instance();
00193         CurrentMap *currentmap = world->getCurrentMap();
00194         currentmap->setWholeMapFast();
00195 }
00196 
00197 uint16 MiniMapGump::TraceObjId(int mx, int my)
00198 {
00199         uint16 objid = Gump::TraceObjId(mx,my);
00200 
00201         if (!objid || objid == 65535)
00202                 if (PointOnGump(mx,my))
00203                         objid = getObjId();
00204 
00205         return objid;
00206 }
00207 
00208 void MiniMapGump::saveData(ODataSource* ods)
00209 {
00210         Gump::saveData(ods);
00211 }
00212 
00213 bool MiniMapGump::loadData(IDataSource* ids, uint32 version)
00214 {
00215         if (!Gump::loadData(ids, version)) return false;
00216 
00217         lastMapNum = 0;
00218         minimap.format = TEX_FMT_NATIVE;
00219         minimap.width = minimap.height = MAP_NUM_CHUNKS*MINMAPGUMP_SCALE;
00220         minimap.buffer = texbuffer[0];
00221 
00222         con.AddConsoleCommand("MiniMapGump::generateWholeMap",
00223                                                   MiniMapGump::ConCmd_generateWholeMap);
00224         return true;
00225 }

Generated on Fri Jul 27 22:27:25 2007 for pentagram by  doxygen 1.4.7