Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

VirtualForm.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003  Anders Hedstrom
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (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 <stdio.h>
00020 #include <stdlib.h>
00021 #include <mysql/mysql.h>
00022 #include <libsql++.h>
00023 #include <string>
00024 using std::string;
00025 #include <libcgi++.h>
00026 #include <libwolfshade.h>
00027 #include <libgd.h>
00028 
00029 #include "BaseForm.h"
00030 #include "WolfshadeWeb.h"
00031 //#include "CMemoryAllocator.h"
00032 //#include "MyString.h"
00033 #include "Map.h"
00034 #include "VirtualForm.h"
00035 
00036 
00037 namespace cgi
00038 {
00039 
00040 
00041 VirtualForm::VirtualForm(Web *web)
00042 :BaseForm(web, "virtualform")
00043 ,m_edit(0)
00044 ,m_error(false)
00045 ,m_preview(false)
00046 ,m_area(0)
00047 ,m_level(0)
00048 ,m_x(0)
00049 ,m_y(0)
00050 {
00051         Query q(GetDatabase());
00052 
00053         q.get_result("select * from room where virtual_room<>0");
00054         while (q.fetch_row())
00055         {
00056                 db::Room *tmp = new db::Room(GetDatabase(),&q);
00057                 m_prototype[tmp -> num] = tmp;
00058         }
00059         q.free_result();
00060 }
00061 
00062 
00063 VirtualForm::~VirtualForm()
00064 {
00065         for (rooms_t::iterator it = m_prototype.begin(); it != m_prototype.end(); it++)
00066         {
00067                 delete (*it).second;
00068         }
00069 }
00070 
00071 
00072 void VirtualForm::Process()
00073 {
00074         char action[200];
00075         
00076         if (GetForm() -> getvalue("virtualform",action,200))
00077         {
00078                 if (!strcmp(action,"edit"))
00079                 {
00080                         char slask[100];
00081                         GetForm() -> getvalue("edit",slask,100);
00082                         m_edit = atol(slask);
00083                         if (GetForm() -> getvalue("preview",slask,100))
00084                         {
00085                                 m_preview = true;
00086                         }
00087                 }
00088                 else
00089                 if (!strcmp(action," Save "))
00090                 {
00091                         char slask[100];
00092                         GetForm() -> getvalue("num",slask,100);
00093                         long num = atol(slask);
00094                         GetForm() -> getvalue("onum",slask,100);
00095                         long onum = atol(slask);
00096                         db::Vzone vzone(GetDatabase(),num);
00097                         bool new_zone = false;
00098 
00099                         if (num != onum && vzone.num)
00100                         {
00101                                 m_error = true;
00102                         }
00103                         else
00104                         {
00105 // alias, name, dpd, descr, longdescr
00106                                 GetForm() -> EnableRaw( true );
00107 //                              vzone.num = num;
00108                                 GetForm() -> getvalue("name",vzone.name,255);
00109                                 if (!vzone.num)
00110                                 {
00111                                         GetForm() -> getvalue("type",slask,100);
00112                                         vzone.type = atol(slask);
00113                                         GetForm() -> getvalue("vnumstart",slask,100);
00114                                         vzone.vnumstart = atol(slask);
00115                                         GetForm() -> getvalue("sizex",slask,100);
00116                                         vzone.sizex = atol(slask);
00117                                         GetForm() -> getvalue("sizey",slask,100);
00118                                         vzone.sizey = atol(slask);
00119                                         GetForm() -> getvalue("sizez",slask,100);
00120                                         vzone.sizez = atol(slask);
00121                                         GetForm() -> getvalue("seed",slask,100);
00122                                         vzone.seed = atol(slask);
00123                                         GetForm() -> getvalue("temporary",slask,100);
00124                                         vzone.temporary = atol(slask);
00125                                         //
00126                                         new_zone = true;
00127                                 }
00128                                 vzone.save();
00129                                 if (new_zone)
00130                                 {
00131                                         Map x(GetDatabase(),vzone.num,0);
00132                                         // create type 1 2d zone
00133                                         x.Save();
00134                                 }
00135                                 //
00136                                 m_edit = vzone.num;
00137                         }
00138                 }
00139                 else
00140                 if (!strcmp(action,"area"))
00141                 {
00142                         char slask[100];
00143                         // area,lvl,x,y
00144                         GetForm() -> getvalue("area",slask,100);
00145                         m_area = atol(slask);
00146                         GetForm() -> getvalue("lvl",slask,100);
00147                         m_level = atoi(slask);
00148                         GetForm() -> getvalue("x",slask,100);
00149                         m_x = atoi(slask);
00150                         GetForm() -> getvalue("y",slask,100);
00151                         m_y = atoi(slask);
00152                         //
00153                         m_edit = m_area;
00154                 }
00155                 else
00156                 if (!strcmp(action," Change "))
00157                 {
00158                         char slask[100];
00159                         char sql[200];
00160                         // area,lvl,x,y
00161                         GetForm() -> getvalue("area",slask,100);
00162                         m_area = atol(slask);
00163                         GetForm() -> getvalue("lvl",slask,100);
00164                         m_level = atoi(slask);
00165                         GetForm() -> getvalue("x",slask,100);
00166                         m_x = atoi(slask);
00167                         GetForm() -> getvalue("y",slask,100);
00168                         m_y = atoi(slask);
00169                         //
00170                         sprintf(sql,"select * from vmap where vzone=%ld and level=%d",m_area,m_level);
00171                         db::Vmap map(GetDatabase(),sql);
00172                         db::Vzone vzone(GetDatabase(),m_area);
00173                         for (int y = m_y; y < m_y + 16; y++)
00174                         {
00175                                 if (y < vzone.sizey)
00176                                 {
00177                                         int i = 0;
00178                                         sprintf(slask,"row%d",y);
00179                                         GetForm() -> getvalue(slask,slask,100);
00180                                         for (int x = m_x; x < m_x + 16; x++,i++)
00181                                         {
00182                                                 if (x < vzone.sizex)
00183                                                 {
00184                                                         map.map[y][x] = slask[i];
00185                                                 }
00186                                         }
00187                                 }
00188                         }
00189                         // save map and create new image
00190                         {
00191                                 Query q(GetDatabase());
00192                                 string query;
00193                                 query = "update vmap set map='";
00194                                 for (vector<string>::iterator it = map.map.begin(); it != map.map.end(); it++)
00195                                 {
00196                                         query += *it;
00197                                         query += '\n';
00198                                 }
00199                                 sprintf(slask,"' where num=%ld",map.num);
00200                                 query += slask;
00201                                 q.execute( (char *)query.c_str());
00202                                 CreatePicture(vzone,map,"/tmp/tmp_pic.gif");
00203                                 map.pic = AddPic(map.pic,"/tmp/tmp_pic.gif");
00204                         }
00205                         //
00206                         m_edit = m_area;
00207                 }
00208         }
00209 }
00210 
00211 
00212 void VirtualForm::Select(const string &)
00213 {
00214 }
00215 
00216 
00217 void VirtualForm::List()
00218 {
00219 }
00220 
00221 
00222 void VirtualForm::Display(long)
00223 {
00224         WolfshadeWeb *web = static_cast<WolfshadeWeb *>(GetWeb());
00225         Database *db = GetDatabase();
00226         Query q(db);
00227         Query q2(db);
00228         char sql[200];
00229 
00230         if (!m_edit)
00231                 web -> DisplayStartAntalForm();
00232 
00233         if (m_error)
00234         {
00235                 printf("<h3>New Virtual# is already taken</h3>");
00236         }
00237 
00238         {
00239                 if (m_edit)
00240                         sprintf(sql,"select * from vzone where num=%ld",m_edit);
00241                 else
00242                 {
00243                         sprintf(sql,"select * from vzone where num>=%ld order by num limit %ld",GetStart(),GetAntal());
00244                 }
00245                 q.get_result(sql);
00246                 char header[100];
00247                 
00248                 if (m_edit)
00249                         sprintf(header,"&nbsp;Editing Virtual zone#%ld",m_edit);
00250                 else
00251                         sprintf(header,"&nbsp;Virtual zones (%ld vzones displayed)",q.num_rows());
00252                 Table tbl("#000000", 3, header);
00253                 printf("<tr bgcolor=#ffffff>");
00254                 printf("<th align=left>&nbsp;Virtual | Vnum</th>");
00255                 printf("<th align=left>&nbsp;Links to</th>");
00256                 printf("<th></th>"); // preview
00257                 printf("</tr>");
00258 
00259                 while (q.fetch_row())
00260                 {
00261                         db::Vzone vzone(db,&q);
00262 
00263                         printf("<tr bgcolor=#808080>");
00264                         printf("<td bgcolor=#808080>");
00265                         ShowVirtual(vzone,true);
00266                         printf("</td>");
00267                         sprintf(sql,"select count(*) from room_door where toroom>=%ld and toroom<%ld",
00268                                 vzone.vnumstart,
00269                                 vzone.vnumstart + vzone.sizex * vzone.sizey);
00270                         printf("<td align=right>&nbsp;%ld&nbsp;</td>",q2.get_count(sql));
00271                         printf("<td>&nbsp;");
00272                         printf("<a href=%s?virtualform=edit&edit=%ld&preview=1>preview</a>",
00273                                 GetCgiName().c_str(),vzone.num);
00274                         printf("&nbsp;</td>");
00275                         printf("</tr>");
00276                 }
00277                 q.free_result();
00278         }
00279         printf("<br>");
00280 
00281         if (m_edit)
00282         {
00283                 db::Vzone vzone(db,m_edit);
00284 
00285                 // link to gif
00286                 {
00287                         Table tbl("#000000", 3, "&nbsp;Map");
00288                         printf("<tr bgcolor=#ffffff>");
00289                         printf("<td>");
00290                         printf("<img border=0 src=%s?mapform=map&map=%ld usemap=mmap>",GetCgiName().c_str(),vzone.num);
00291                         printf("</td>");
00292                         printf("</tr>");
00293                 }
00294                 printf("<br>");
00295                 // the gif map
00296                 {
00297                         // area,lvl,x,y
00298                         printf("<map name=mmap>");
00299                         for (int y = 0; y < vzone.sizey; y += 16)
00300                                 for (int x = 0; x < vzone.sizex; x += 16)
00301                                         printf("<area href=%s?%s=area&area=%ld&lvl=0&x=%d&y=%d alt=\"%d,%d\" shape=rect coords=%d,%d,%d,%d>",
00302                                                 GetCgiName().c_str(),
00303                                                 GetFormName().c_str(),
00304                                                 vzone.num,
00305                                                 x,y,x,y,x,y,x + 16,y + 16);
00306                         printf("</map>");
00307                 }
00308 
00309                 // edit part of this map
00310                 if (m_area)
00311                 {
00312                         sprintf(sql,"select * from vmap where vzone=%ld and level=%d",m_area,m_level);
00313                         db::Vmap map(db,sql);
00314                         char slask[100];
00315                         printf("<form action=%s method=post>",GetCgiName().c_str());
00316                         printf("<input type=hidden name=area value=%ld>",m_area);
00317                         printf("<input type=hidden name=level value=%d>",m_level);
00318                         printf("<input type=hidden name=x value=%d>",m_x);
00319                         printf("<input type=hidden name=y value=%d>",m_y);
00320                         printf("<table>");
00321                         for (int y = m_y; y < m_y + 16; y++)
00322                         {
00323                                 if (y < vzone.sizey)
00324                                 {
00325                                         printf("<tr><td align=right>");
00326                                         strncpy(slask,map.map[y].c_str() + m_x,16);
00327                                         slask[16] = 0;
00328                                         printf("%d:&nbsp;",y);
00329                                         printf("</td><td>&nbsp;");
00330                                         printf("<input class=map type=text size=20 name=row%d value=\"%s\">",y,slask);
00331 //                                      printf("<br>");
00332                                         printf("</td></tr>");
00333                                 }
00334                         }
00335                         printf("</table>");
00336                         printf("<input type=submit name=%s value=\" Change \">",GetFormName().c_str());
00337                         printf("</form>");
00338                         printf("<br>");
00339                 }
00340 
00341                 // references to this vzone
00342                 {
00343                 }
00344 
00345                 // edit
00346                 if (!m_preview)
00347                 {
00348                         Table tbl("#000000", 3);
00349 
00350                         printf("<form action=%s method=post>",GetCgiName().c_str());
00351                         printf("<input type=hidden name=onum value=%ld>",vzone.num);
00352 
00353                         inputnum("Vnum", "num", vzone.num);
00354                         inputstr("VirtualName","name",60,vzone.name,true);
00355                         inputselect(q,"Type","type","select num,name from vzone_type order by num",vzone.type);
00356                         inputnum("VnumStart","vnumstart",vzone.vnumstart);
00357                         inputnum("SizeX","sizex",vzone.sizex);
00358                         inputnum("SizeY","sizey",vzone.sizey);
00359                         inputnum("SizeZ","sizez",vzone.sizez);
00360                         inputnum("Seed","seed",vzone.seed);
00361                         inputcheck("Temporary","temporary",vzone.temporary ? true : false);
00362                         inputsubmit(" Save ");
00363 
00364                         printf("</form>");
00365                 }
00366                 if (!m_preview)
00367                         printf("<br>");
00368 
00369                 // show map
00370                 if (m_preview)
00371                 {
00372                         Table tbl("#000000", 3);
00373                         Map map(GetDatabase(),vzone.num,0);
00374 
00375                         printf("<tr><td bgcolor=#ffffff>");
00376 
00377 //                      sprintf(sql,"select * from vroom where vzone=%ld order by y,x",vzone.num);
00378 //                      q.get_result(sql);
00379                         printf("<font face=courier style=\"background-color: #808080\">");
00380 //                      while (q.fetch_row())
00381                         for (int y = 0; y < vzone.sizey; y++)
00382                         {
00383                         for (int x = 0; x < vzone.sizex; x++)
00384                         {
00385 //                              db::Vroom vr(db,&q);
00386                                 long num = map.GetAt(x,y) - 96;
00387                                 db::Room *tmp = m_prototype[num]; //vr.roomvnum];
00388 /*
00389                                 while (cx != vr.x && cy != vr.y && cy < vzone.sizey)
00390                                 {
00391                                         printf("X");
00392                                         cx++;
00393                                         if (cx >= vzone.sizex)
00394                                         {
00395                                                 printf("</font><br><font face=courier style=\"background-color: #808080\">");
00396                                                 cx = 0;
00397                                                 cy++;
00398                                         }
00399                                 }
00400 */
00401                                 if (tmp)
00402                                 {
00403                                         printf("%s",htmlcolor(tmp -> virtual_char));
00404                                 }
00405                                 else
00406                                 {
00407                                         printf("X");
00408                                 }
00409                         } // for (x)
00410                                         printf("</font><br><font face=courier style=\"background-color: #808080\">");
00411                         } // for (y)
00412                         printf("</font>");
00413                         q.free_result();
00414                         
00415                         printf("</td></tr>");
00416                 } // m_preview
00417                 if (m_preview)
00418                         printf("<br>");
00419 
00420                 sprintf(sql,"select * from room_door where toroom>=%ld and toroom<%ld",
00421                         vzone.vnumstart,
00422                         vzone.vnumstart + vzone.sizex * vzone.sizey);
00423                 q.get_result(sql);
00424                 if (q.num_rows())
00425                 {
00426                         Table tbl("#000000", 3, "&nbsp;Real room links");
00427                         printf("<tr bgcolor=#ffffff>");
00428                         printf("<th align=left>&nbsp;From</th>");
00429                         printf("<th align=left>&nbsp;Dir</th>");
00430                         printf("<th align=left>&nbsp;To</th>");
00431                         printf("<th align=left>&nbsp;x,y</th>");
00432                         printf("</tr>");
00433                         while (q.fetch_row())
00434                         {
00435                                 db::Room_door x(db,&q);
00436                                 db::Room room(db,x.room);
00437                                 db::Room_dir dir(db,x.direction);
00438                                 printf("<tr bgcolor=#808080>");
00439                                 ShowRoom(room,true,true);
00440                                 printf("<td align=middle>&nbsp;%s&nbsp;</td>",dir.name);
00441                                 printf("<td align=right>&nbsp;%ld&nbsp;</td>",x.toroom);
00442                                 printf("<td align=middle>&nbsp;%ld,%ld&nbsp;</td>",
00443                                         (x.toroom - vzone.vnumstart) % vzone.sizex,
00444                                         (x.toroom - vzone.vnumstart) / vzone.sizex);
00445                                 printf("</tr>");
00446                         }
00447                 }
00448                 q.free_result();
00449         }
00450 }
00451 
00452 
00453 void VirtualForm::CreatePicture(db::Vzone &vzone,db::Vmap &map,const string &filename)
00454 {
00455         gd::Image img(vzone.sizex,vzone.sizey,vzone.sizex,vzone.sizey);
00456         int black = img.AddColor(0,0,0);
00457         int colors[256];
00458 
00459         colors[0] = black;
00460         for (int i = 1; i < 256; i++)
00461                 colors[i] = 0;
00462         for (int i = 'a'; i < 'z'; i++)
00463         {
00464                 int tmp = i - 'a';
00465                 int intensity = 4 * tmp / 8;
00466                 int r = (tmp & 1) ? 0x33 : 0;
00467                 int g = (tmp & 2) ? 0x33 : 0;
00468                 int b = (tmp & 4) ? 0x33 : 0;
00469                 r *= intensity;
00470                 g *= intensity;
00471                 b *= intensity;
00472                 colors[i] = img.AddColor(r,g,b);
00473         }
00474         for (int y = 0; y < vzone.sizey; y++)
00475         {
00476                 for (int x = 0; x < vzone.sizex; x++)
00477                 {
00478                         char c = map.map[y][x];
00479                         if (!colors[c])
00480                                 colors[c] = img.AddColor(random()%256,random()%256,random()%256);
00481                         img.Line(x,y,x,y,colors[c]);
00482                 }
00483         }
00484         {
00485                 FILE *fil = fopen(filename.c_str(),"wb");
00486                 if (fil)
00487                 {
00488                         img.SaveTo(fil);
00489                         fclose(fil);
00490                 }
00491         }
00492 }
00493 
00494 
00495 } // namespace cgi
00496 
00497 
00498 /*
00499 CREATE TABLE vzone (
00500   num int(11) NOT NULL auto_increment,
00501   name varchar(255) NOT NULL default '',
00502   vnumstart int(11) NOT NULL default '0',
00503   sizex int(11) NOT NULL default '0',
00504   sizey int(11) NOT NULL default '0',
00505   PRIMARY KEY  (num)
00506 ) TYPE=MyISAM;
00507 */
00508 /*
00509 CREATE TABLE vroom (
00510   num int(11) NOT NULL auto_increment,
00511   vzone int(11) NOT NULL default '0',
00512   x int(11) NOT NULL default '0',
00513   y int(11) NOT NULL default '0',
00514   roomvnum int(11) NOT NULL default '0',
00515   exits int(11) NOT NULL default '0',
00516   PRIMARY KEY  (num),
00517   KEY vzone (vzone,y,x)
00518 ) TYPE=MyISAM;
00519 */
00520 
00521 

Generated for Wolfshade by doxygen 1.3.6

Page, code, and content Copyright (C) 2004 by Anders Hedström