MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | #include "../haus.h" |
| 2 | #include <properties.h> |
| 3 | #include <moving.h> |
| 4 | |
| 5 | inherit "std/thing"; |
| 6 | |
| 7 | static void do_gen(string ow, object env, int nr, int fin, int all); |
| 8 | private string special(string str); |
| 9 | private string dump(int haus, int final); |
| 10 | private void header(string f, int final, object haus, int nr); |
| 11 | private void trailer(string f, int final, object haus, int nr); |
| 12 | private void sProp(string f, string pName, string pStr); |
| 13 | private void mProp(string f, string pName, mapping pMap); |
| 14 | private void cProp(string f, string pName, mapping cMap); |
| 15 | private void prettyString(string f, string *str, int indent); |
| 16 | private void prettyArray(string f, string *arr, int sep); |
| 17 | |
| 18 | create() |
| 19 | { |
| 20 | if (!clonep(this_object())) return; |
| 21 | ::create(); |
| 22 | |
| 23 | SetProp( P_NAME, "Hausmeister"); |
| 24 | SetProp( P_SHORT, "Ein Hausmeister" ); |
| 25 | SetProp( P_LONG, |
| 26 | "Dies ist der Hausmeister aller Seherhaeuser. Er hilft Wurzel bei der\n" |
| 27 | +"Verwaltung der Haeuser.\n" |
| 28 | +"Der Hausmeister hat einen blauen Kittel an und eine Abrissbirne gezueckt.\n" ); |
| 29 | SetProp( P_GENDER,MALE); |
| 30 | SetProp( P_NOGET, 1); |
| 31 | |
| 32 | AddId( "hausmeister" ); |
| 33 | |
| 34 | AddCmd( "generiere", "generieren" ); |
| 35 | AddCmd( ({ "reiss", "reisse" }), "abreissen" ); |
| 36 | AddCmd( "verlege", "verlegen" ); |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | bewege(mixed dest, int flags, string msg1, string msg2) |
| 41 | { |
| 42 | int ret; |
| 43 | |
| 44 | tell_room(environment(), capitalize(name(WER))+" "+msg1+".\n"); |
| 45 | if ((ret = move(dest, flags)) == 1) |
| 46 | tell_room(environment(), capitalize(name(WER))+" "+msg2+".\n"); |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | static int generieren(string str) |
| 52 | { |
| 53 | object env; |
| 54 | int nr, fin, all; |
| 55 | string ow, *parm; |
| 56 | |
| 57 | env = environment(this_player()); |
| 58 | |
| 59 | notify_fail( "Syntax: generiere <name> [<nr>] [soft | ganz]\n" ); |
| 60 | |
| 61 | if (!str || str == "") |
| 62 | return 0; |
| 63 | |
| 64 | parm = old_explode(str, " "); |
| 65 | fin = 1; |
| 66 | |
| 67 | switch (sizeof(parm)) { |
| 68 | case 3: |
| 69 | if (parm[2] == "soft") |
| 70 | fin = 0; |
| 71 | case 2: |
| 72 | if (parm[1] == "soft") |
| 73 | fin = 0; |
| 74 | else if (parm[1] == "ganz") { |
| 75 | ow = parm[0]; |
| 76 | nr = (VERWALTER)->HausProp(ow, HP_ROOMS); |
| 77 | str = ow+"raum"+nr; |
| 78 | all = 1; |
| 79 | break; |
| 80 | } |
| 81 | else { |
| 82 | nr = to_int(parm[1]); |
| 83 | str = parm[0]+"raum"+parm[1]; |
| 84 | ow = parm[0]; |
| 85 | break; |
| 86 | } |
| 87 | case 1: |
| 88 | ow = parm[0]; |
| 89 | nr = -1; |
| 90 | str = ow+"haus"; |
| 91 | break; |
| 92 | default: |
| 93 | return 0; |
| 94 | } |
| 95 | if (file_size(HAUSSAVEPATH+ow+".o")<0) { |
| 96 | if (nr >= 0 && file_size(HAUSSAVEPATH+ow+nr+".o")<0) { |
| 97 | write( "Es gibt kein '"+str+"'!\n"); |
| 98 | return 1; |
| 99 | } |
| 100 | } |
| 101 | do_gen(ow, env, nr, fin, all); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | static void do_gen(string ow, object env, int nr, int fin, int all) |
| 106 | { |
| 107 | string str, out; |
| 108 | |
| 109 | if (nr >= 0) |
| 110 | str = ow+"raum"+nr; |
| 111 | else |
| 112 | str = ow+"haus"; |
| 113 | |
| 114 | bewege(PATH+str, M_NOCHECK, "geht zur Arbeit", "kommt an"); |
| 115 | tell_room(environment(), Name(WER)+" zueckt einen Block, sieht sich alles genau an und macht\n" |
| 116 | +"sich hin und wieder Notizen. Dann nickt er zufrieden und steckt seinen\nBlock wieder weg.\n" ); |
| 117 | out = dump(nr, fin); |
| 118 | bewege(env, M_NOCHECK, "geht zu seinem Auftraggeber zurueck", "kommt an"); |
| 119 | write( "Der Hausmeister sagt: Ich habe '"+str+"' in die Datei\n" |
| 120 | +"Der Hausmeister sagt: '"+out+"' geschrieben, Chef!\n"); |
| 121 | |
| 122 | if (all) { |
| 123 | if (nr > 0) |
| 124 | call_out("do_gen", 1, ow, env, nr-1, fin, all); |
| 125 | else |
| 126 | call_out("do_gen", 1, ow, env, -1, fin); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static int abreissen(string str) |
| 131 | { |
| 132 | object haus, env; |
| 133 | string ich, msg; |
| 134 | |
| 135 | if (!str || str == "" || sscanf(str, "%s ab",str) != 1) { |
| 136 | notify_fail( "Syntax: reisse <name> ab\n" ); |
| 137 | return 0; |
| 138 | } |
| 139 | str = lower_case(str); |
| 140 | if (!(haus = VERWALTER->FindeHaus(str))) { |
| 141 | write( capitalize(str)+" hat kein Haus!\n" ); |
| 142 | return 1; |
| 143 | } |
| 144 | ich = capitalize(name(WER)); |
| 145 | env = environment(); |
| 146 | bewege(environment(haus), M_NOCHECK, "geht zur Arbeit", "kommt an"); |
| 147 | msg = haus->Name(WER); |
| 148 | |
| 149 | if (VERWALTER->LoescheHaus(str) == -1) { |
| 150 | tell_room(environment(), ich+" versucht vergeblich, ein Haus abzureissen.\n" ); |
| 151 | msg = "Der Abrissversuch ist fehlgeschlagen, Chef!\n"; |
| 152 | } |
| 153 | else { |
| 154 | tell_room(environment(), ich+" betaetigt eine Sirene.\n"+msg+" wird von "+name(WEM)+" eingeebnet.\n"); |
| 155 | msg = msg+" ist abgerissen, Chef!\n"; |
| 156 | if (haus) haus->remove(); |
| 157 | } |
| 158 | bewege(env, M_NOCHECK, "geht zu seinem Auftraggeber zurueck", "kommt an"); |
| 159 | write( "Der Hausmeister sagt: "+msg); |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | static int verlegen(string str) |
| 164 | { |
| 165 | string name, von, nach; |
| 166 | object haus; |
| 167 | int ret; |
| 168 | |
| 169 | if (!str || sscanf(str, "%s nach %s", name, nach) != 2) { |
| 170 | notify_fail( "Syntax: verlege <name> nach <ziel>\n" ); |
| 171 | return 0; |
| 172 | } |
| 173 | if (nach == "hier") |
| 174 | nach = object_name(environment(this_player())); |
| 175 | |
| 176 | name = lower_case(name); |
| 177 | |
| 178 | if (!(haus = VERWALTER->FindeHaus(name))) { |
| 179 | write( "Der Hausmeister sagt: "+capitalize(name)+" hat kein Haus!\n" ); |
| 180 | return 1; |
| 181 | } |
| 182 | von = object_name(environment(haus)); |
| 183 | ret = VERWALTER->VerlegeHaus(name, von, nach); |
| 184 | write( "Der Hausmeister sagt: " ); |
| 185 | switch(ret) { |
| 186 | case -111: |
| 187 | write( "Das hast Du nicht zu bestimmen!\n" ); |
| 188 | break; |
| 189 | case -3: |
| 190 | write( "Der Zielraum laesst sich nicht laden! Verlegen abgebrochen!\n" ); |
| 191 | break; |
| 192 | case -4: |
| 193 | write( "Im Zielraum kann nicht gebaut werden!\n" ); |
| 194 | break; |
| 195 | case -5: |
| 196 | write( "Im Haus von "+capitalize(name)+" gibt es noch Ausgaenge in andere Haeuser!\n"); |
| 197 | break; |
| 198 | case 1: |
| 199 | write( "OK, Chef, ich habe das Haus verlegt!\n" ); |
| 200 | break; |
| 201 | } |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | /*** Funktionen zum Generieren einer Datei aus den Properties des Hauses ***/ |
| 206 | |
| 207 | #define EXIT_TMPL "\"/players/%s/seherhaus/%sraum%d\"" |
| 208 | |
| 209 | private void dump_chest(string out, object chest) |
| 210 | { |
| 211 | mixed n; |
| 212 | |
| 213 | catch(rm(out)); |
| 214 | |
| 215 | write_file(out, "#include <properties.h>\n#include <moving.h>\n\n" |
| 216 | "inherit \"/d/wueste/durian/behaelter\";\n\n" |
| 217 | "void create()\n{\n if(!clonep(this_object())) return;\n\n" |
| 218 | " ::create();\n\n" ); |
| 219 | sProp(out, "P_SHORT", chest->QueryProp(P_SHORT)); |
| 220 | sProp(out, "P_LONG", chest->QueryProp(P_LONG)); |
| 221 | |
| 222 | write_file(out, " SetProp(P_NOGET, 1);\n"); |
| 223 | write_file(out, " SetProp(P_GENDER, "+ |
| 224 | ({"NEUTER","MALE","FEMALE"})[chest->QueryProp(P_GENDER)]+ |
| 225 | " );\n SetProp(P_NAME, "); |
| 226 | |
| 227 | n = chest->QueryProp(P_NAME); |
| 228 | if (stringp(n)) |
| 229 | write_file(out, "\""+n+"\""); |
| 230 | else |
| 231 | prettyArray(out, n, 0); |
| 232 | write_file(out, " );\n\n AddId( "); |
| 233 | prettyArray(out, chest->QueryProp(P_IDS)-({ TRUHE }), 0); |
| 234 | write_file(out, " );\n"); |
| 235 | |
| 236 | if ((n=chest->QueryProp(P_ADJECTIVES)) && sizeof(n)) { |
| 237 | write_file(out, "\n AddAdjective( "); |
| 238 | prettyArray(out, n, 0); |
| 239 | write_file(out, " );\n"); |
| 240 | } |
| 241 | if ((n=chest->QueryProp(P_NAME_ADJ)) && sizeof(n)) { |
| 242 | write_file(out, "\n SetProp(P_NAME_ADJ, "); |
| 243 | prettyArray(out, n, 0); |
| 244 | write_file(out, " );\n"); |
| 245 | } |
| 246 | write_file(out, "}\n"); |
| 247 | } |
| 248 | |
| 249 | private string dump(int nr, int final) |
| 250 | { |
| 251 | string out; |
| 252 | object env; |
| 253 | mapping prop; |
| 254 | |
| 255 | env = environment(this_object()); |
| 256 | |
| 257 | if (final && env->QueryProp(H_CHEST)) |
| 258 | dump_chest(PATH+"rep/"+env->QueryOwner()+"truhe.c", present(TRUHE, env)); |
| 259 | |
| 260 | if (final) { |
| 261 | out = PATH+"rep/"+env->QueryOwner(); |
| 262 | if (nr < 0) |
| 263 | out += "haus.c"; |
| 264 | else |
| 265 | out += "raum"+nr+".c"; |
| 266 | } |
| 267 | else |
| 268 | out = PATH+"fixed.c"; |
| 269 | |
| 270 | catch(rm(out)); |
| 271 | |
| 272 | header(out, final, env, nr); |
| 273 | if (nr<0) { |
| 274 | sProp(out, "P_SHORT", env->QueryProp(P_SHORT)); |
| 275 | write_file(out, "\n"); |
| 276 | sProp(out, "P_LONG", env->QueryProp(P_LONG)); |
| 277 | write_file(out, "\n"); |
| 278 | |
| 279 | if (final) { |
| 280 | mixed n; |
| 281 | |
| 282 | write_file(out, " SetProp(P_NOGET, 1);\n"); |
| 283 | write_file(out, " SetProp(P_GENDER, "+ |
| 284 | ({"NEUTER","MALE","FEMALE"})[env->QueryProp(P_GENDER)]+ |
| 285 | " );\n SetProp(P_NAME, "); |
| 286 | n = env->QueryProp(P_NAME); |
| 287 | if (stringp(n)) |
| 288 | write_file(out, "\""+n+"\""); |
| 289 | else |
| 290 | prettyArray(out, n, 0); |
| 291 | write_file(out, " );\n\n AddId( "); |
| 292 | prettyArray(out, env->QueryProp(P_IDS)-({ "sehe\rhaus", "\n"+env->QueryOwner()+"haus" }), 0); |
| 293 | write_file(out, " );\n"); |
| 294 | } |
| 295 | } |
| 296 | else { |
| 297 | sProp(out, "P_INT_SHORT", env->QueryProp(P_INT_SHORT)); |
| 298 | write_file(out, "\n"); |
| 299 | sProp(out, "P_INT_LONG", env->QueryProp(P_INT_LONG)); |
| 300 | write_file(out, "\n"); |
| 301 | if (sizeof(prop = env->QueryProp(P_DETAILS))) { |
| 302 | mProp(out, "AddDetail", prop); |
| 303 | write_file(out, "\n"); |
| 304 | } |
| 305 | if (sizeof(prop = env->QueryProp(P_READ_DETAILS))) { |
| 306 | mProp(out, "AddReadDetail", prop); |
| 307 | write_file(out, "\n"); |
| 308 | } |
| 309 | if (sizeof(prop = env->QueryProp(H_COMMANDS))) |
| 310 | cProp(out, "AddUserCmd", prop); |
| 311 | |
| 312 | write_file(out, "\n"); |
| 313 | if (final) { |
| 314 | string *k, o; |
| 315 | int i, num; |
| 316 | |
| 317 | prop = env->QueryProp(P_EXITS); |
| 318 | k = m_indices(prop); |
| 319 | if (member(k, "raus") >= 0) { |
| 320 | k -= ({ "raus" }); |
| 321 | write_file(out, sprintf(" AddExit( \"raus\", \"%s\");\n", prop["raus"])); |
| 322 | } |
| 323 | for (i=sizeof(k)-1; i>=0; i--) { |
| 324 | if (sscanf(prop[k[i]], PATH+"%sraum%d", o, num) != 2) |
| 325 | printf("Komischer Exit (%O)\n%s -> %s\n", env, k[i], prop[k[i]]); |
| 326 | else { |
| 327 | if (o == env->QueryOwner()) { |
| 328 | o = sprintf(EXIT_TMPL, o, o, num); |
| 329 | write_file(out, sprintf(" AddExit( \"%s\", %s);\n", k[i], o)); |
| 330 | } |
| 331 | else { |
| 332 | write_file(out, sprintf(" AddExit( \"%s\", \"%s\");\n", k[i], prop[k[i]])); |
| 333 | printf("Exit von %O nach %s!\n", env, prop[k[i]]); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | trailer(out, final, env, nr); |
| 340 | |
| 341 | return out; |
| 342 | } |
| 343 | |
| 344 | private void header(string f, int final, object haus, int nr) |
| 345 | { |
| 346 | if (final) { |
| 347 | write_file(f, "#include <properties.h>" ); |
| 348 | if (nr >= 0) { |
| 349 | write_file(f, "\n\ninherit \"std/room\";\ninherit \""+PATH+"modules/usercmd\";"); |
| 350 | write_file(f, "\n\ncreate()\n{\n room::create();\n usercmd::create();\n\n"); |
| 351 | write_file(f, " SetProp(P_LIGHT, 1);\n SetProp(P_INDOORS, 1);\n\n"); |
| 352 | } |
| 353 | else { |
| 354 | write_file(f, "\n#include <moving.h>\n#include \""+PATH+"haus.h\"\n\n" ); |
| 355 | write_file(f, "inherit \"std/thing\";\ninherit HAUSTUER;\n\n" ); |
| 356 | write_file(f, "create()\n{\n thing::create();\n haustuer::create();\n\n"); |
| 357 | } |
| 358 | } |
| 359 | else { |
| 360 | write_file(f, "#include \"haus.h\"\n#include <properties.h>\n\ninherit "); |
| 361 | write_file(f, ((nr < 0) ? "HAUS" : "RAUM" ) ); |
| 362 | write_file(f, ";\ninherit \"/std/thing/moving\";\n\n"); |
| 363 | write_file(f, "create()\n{\n if (!clonep(this_object())) return;\n ::create();\n\n"); |
| 364 | write_file(f, " SetOwner(\""+haus->QueryOwner()+"\""); |
| 365 | if (nr >= 0) |
| 366 | write_file(f, ", "+nr); |
| 367 | write_file(f, ");\n Load();\n\n"); |
| 368 | if (nr >= 0) |
| 369 | write_file(f, " SetProp(P_DETAILS, ([]));\n SetProp(P_READ_DETAILS, ([]));\n\n"); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | private void trailer(string f, int final, object haus, int nr) |
| 374 | { |
| 375 | if (final) { |
| 376 | if (nr >= 0) |
| 377 | write_file(f, "}\n"); |
| 378 | else { |
| 379 | write_file(f, read_file(PATH+"tools/haus.apx")); |
| 380 | write_file(f, " this_player()->move("); |
| 381 | write_file(f, sprintf(EXIT_TMPL, haus->QueryOwner(), haus->QueryOwner(), 0)); |
| 382 | write_file(f, ",\n\t\t\tM_GO, 0, \"betritt \"+name(WEN,1), \"kommt herein\");\n" |
| 383 | " return 1;\n}\n"); |
| 384 | if (!haus->QueryProp(P_SHORT)) |
| 385 | write_file(f, "\nstring short()\n{\n string ret;\n\n" |
| 386 | " ret = ::short();\n" |
| 387 | " if (previous_object() != environment() && !ret)\n" |
| 388 | " ret =\"\";\n\n return ret;\n}\n"); |
| 389 | } |
| 390 | } |
| 391 | else { |
| 392 | write_file(f, " Save(1);\n\n"); |
| 393 | write_file(f, " { object raum;\n if (raum = find_object("); |
| 394 | if (nr >= 0) |
| 395 | write_file(f, "RAUMNAME(\""+haus->QueryOwner()+"\", "+nr+")))\n"); |
| 396 | else |
| 397 | write_file(f, "HAUSNAME(\""+haus->QueryOwner()+"\")))\n"); |
| 398 | write_file(f, " raum->Load();\n }\n\n call_out(\"remove\",0);\n}\n"); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | private string special(string s) |
| 403 | { |
| 404 | s = implode(explode(s, "\\"), "\\\\"); |
| 405 | s = implode(explode(s, "\t"), "\\t"); |
| 406 | s = implode(explode(s, "\""), "\\\""); |
| 407 | |
| 408 | return s; |
| 409 | } |
| 410 | |
| 411 | private void sProp(string f, string pName, string pStr) |
| 412 | { |
| 413 | string *str; |
| 414 | write_file(f, " SetProp( "+pName+","); |
| 415 | if (!pStr) { |
| 416 | write_file(f, "0 );\n"); |
| 417 | return; |
| 418 | } |
| 419 | pStr = special(pStr); |
| 420 | if (sizeof(str=old_explode(pStr,"\n")) > 1) |
| 421 | prettyString(f, str, 4); |
| 422 | else { |
| 423 | if (sizeof(str)==0) |
| 424 | str = ({""}); |
| 425 | write_file(f, "\n \""+str[0]); |
| 426 | if (pStr[<1] == '\n') |
| 427 | write_file(f, "\\n\""); |
| 428 | else |
| 429 | write_file(f, "\""); |
| 430 | } |
| 431 | write_file(f, " );\n"); |
| 432 | } |
| 433 | |
| 434 | private void mProp(string f, string pName, mapping pMap) |
| 435 | { |
| 436 | string *eq, t1; |
| 437 | mixed cmd; |
| 438 | int i,j; |
| 439 | |
| 440 | if (sizeof(pMap) == 0) |
| 441 | return; |
| 442 | |
| 443 | cmd = VERWALTER->PCrunch(pMap); |
| 444 | |
| 445 | for (i=sizeof(cmd)-1; i>=0; i--) { |
| 446 | write_file(f, " "+pName+"(\n "); |
| 447 | eq = cmd[i][0]; |
| 448 | t1 = cmd[i][1]; |
| 449 | |
| 450 | prettyArray(f, eq, 0); |
| 451 | write_file(f, ", "); |
| 452 | |
| 453 | prettyString(f, old_explode(special(t1), "\n"), 6); |
| 454 | write_file(f, " );\n"); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | private void cProp(string f, string pName, mapping cMap) |
| 459 | { |
| 460 | string t1, t2; |
| 461 | mixed cmd, eq; |
| 462 | int i,j; |
| 463 | |
| 464 | cmd = VERWALTER->PCrunch(cMap); |
| 465 | |
| 466 | for( i=sizeof(cmd)-1; i>=0; i--) { |
| 467 | write_file(f, " "+pName+"(\n "); |
| 468 | eq = cmd[i][0]; |
| 469 | t1 = cmd[i][1]; |
| 470 | t2 = cmd[i][2]; |
| 471 | |
| 472 | prettyArray(f, eq, 1); |
| 473 | write_file(f, ", 0, "); |
| 474 | |
| 475 | prettyString(f, old_explode(special(t1), "\n"), 4); |
| 476 | write_file(f, ", "); |
| 477 | if (t2) |
| 478 | prettyString(f, old_explode(special(t2), "\n"), 4); |
| 479 | else |
| 480 | write_file(f, "0"); |
| 481 | write_file(f, " );\n"); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private void prettyString(string f, string *str, int indent) |
| 486 | { |
| 487 | string ind; |
| 488 | int i; |
| 489 | |
| 490 | ind = extract("\n ",0,indent); |
| 491 | |
| 492 | if (!sizeof(str)) { |
| 493 | write_file(f, ind+" \"\\n\""); |
| 494 | return; |
| 495 | } |
| 496 | write_file(f, ind+" \""+str[0]+"\\n\""); |
| 497 | for (i=1; i<sizeof(str); i++) { |
| 498 | write_file(f, ind+"+\""+str[i]+"\\n\""); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | private void prettyArray(string f, string *arr, int sep) |
| 503 | { |
| 504 | int i,j; |
| 505 | string res, t1, t2; |
| 506 | |
| 507 | write_file(f, "({"); |
| 508 | |
| 509 | if (sizeof(arr)) { |
| 510 | t1 = ("\""+arr[0]+"\""); |
| 511 | res = " "+t1; |
| 512 | t2 = ""; |
| 513 | |
| 514 | for (i=1, j=sizeof(arr); i<j; i++) { |
| 515 | t2 = "\""+arr[i]+"\""; |
| 516 | if (!sep) { |
| 517 | if ((sizeof(t1)+sizeof(t2))>69) { |
| 518 | res += (",\n "+t2); |
| 519 | t1 = t2; |
| 520 | t2 = ""; |
| 521 | } |
| 522 | else { |
| 523 | t1 += (", "+t2); |
| 524 | res += (", "+t2); |
| 525 | } |
| 526 | } |
| 527 | else { |
| 528 | res += (",\n "+t2); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | write_file(f, res + " })" ); |
| 533 | } |
| 534 | |