00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00032
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
00106 GetForm() -> EnableRaw( true );
00107
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
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
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
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
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," Editing Virtual zone#%ld",m_edit);
00250 else
00251 sprintf(header," Virtual zones (%ld vzones displayed)",q.num_rows());
00252 Table tbl("#000000", 3, header);
00253 printf("<tr bgcolor=#ffffff>");
00254 printf("<th align=left> Virtual | Vnum</th>");
00255 printf("<th align=left> Links to</th>");
00256 printf("<th></th>");
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> %ld </td>",q2.get_count(sql));
00271 printf("<td> ");
00272 printf("<a href=%s?virtualform=edit&edit=%ld&preview=1>preview</a>",
00273 GetCgiName().c_str(),vzone.num);
00274 printf(" </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
00286 {
00287 Table tbl("#000000", 3, " 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
00296 {
00297
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
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: ",y);
00329 printf("</td><td> ");
00330 printf("<input class=map type=text size=20 name=row%d value=\"%s\">",y,slask);
00331
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
00342 {
00343 }
00344
00345
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
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
00378
00379 printf("<font face=courier style=\"background-color: #808080\">");
00380
00381 for (int y = 0; y < vzone.sizey; y++)
00382 {
00383 for (int x = 0; x < vzone.sizex; x++)
00384 {
00385
00386 long num = map.GetAt(x,y) - 96;
00387 db::Room *tmp = m_prototype[num];
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 if (tmp)
00402 {
00403 printf("%s",htmlcolor(tmp -> virtual_char));
00404 }
00405 else
00406 {
00407 printf("X");
00408 }
00409 }
00410 printf("</font><br><font face=courier style=\"background-color: #808080\">");
00411 }
00412 printf("</font>");
00413 q.free_result();
00414
00415 printf("</td></tr>");
00416 }
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, " Real room links");
00427 printf("<tr bgcolor=#ffffff>");
00428 printf("<th align=left> From</th>");
00429 printf("<th align=left> Dir</th>");
00430 printf("<th align=left> To</th>");
00431 printf("<th align=left> 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> %s </td>",dir.name);
00441 printf("<td align=right> %ld </td>",x.toroom);
00442 printf("<td align=middle> %ld,%ld </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 }
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521