Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 825 lines (672 sloc) 27.152 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
// illarionserver - server for the game Illarion
// Copyright 2011 Illarion e.V.
//
// This file is part of illarionserver.
//
// illarionserver is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// illarionserver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with illarionserver. If not, see <http://www.gnu.org/licenses/>.


#include "World.hpp"
#include "Player.hpp"
#include "Item.hpp"
#include "data/CommonObjectTable.hpp"
#include "Field.hpp"
#include "data/NamesObjectTable.hpp"
#include "data/ArmorObjectTable.hpp"
#include "data/WeaponObjectTable.hpp"
#include "data/NaturalArmorTable.hpp"
#include "netinterface/protocol/ServerCommands.hpp"
#include "netinterface/protocol/BBIWIServerCommands.hpp"
#include "Logger.hpp"
#include "fuse_ptr.hpp"

extern CommonObjectTable *CommonItems;
extern NamesObjectTable *ItemNames;
class Character;

bool World::deleteNPC(unsigned int npcid) {
    /*
int posx,posy,posz;
Field* tempf; //alte NPC's l?chen
NPCVECTOR::iterator npcIterator;
for ( npcIterator = Npc.begin(); npcIterator < Npc.end(); ++npcIterator ) {
if (( *npcIterator )->id == npcid) {
if ( GetPToCFieldAt( tempf, ( *npcIterator )->pos.x, ( *npcIterator )->pos.y, ( *npcIterator )->pos.z ) ) {
//tempf->SetNPCOnField( false );
tempf->removeChar();
}

sendRemoveCharToVisiblePlayers( ( *npcIterator )->id, ( *npcIterator )->pos );
delete( *npcIterator );
Npc.erase( npcIterator );
return true;
}
}
return false;*/
    LostNpcs.push_back(npcid);
    return true;
}

bool World::createDynamicNPC(std::string name, Character::race_type type, position pos, /*CCharacter::face_to dir,*/ Character::sex_type sex, std::string scriptname) {
    try {

        try {
            NPC *newNPC = new NPC(DYNNPC_BASE, name, type, pos, (Character::face_to)4/*dir*/, false, sex, 0, 0, 255, 255, 255, 255, 255, 255);

            // add npc to npc list
            Npc.push_back(newNPC);

            // set field to occupied
            Field *tempf;

            if (GetPToCFieldAt(tempf, pos)) {
                tempf->setChar();
            }

            try {
                // try to load the script
                boost::shared_ptr<LuaNPCScript> script(new LuaNPCScript(scriptname, newNPC));
                newNPC->setScript(script);
            } catch (ScriptException &e) {
                Logger::writeError("scripts", "World::createDynamicNPC: Error while loading dynamic NPC script: " + scriptname + ":\n" + std::string(e.what()) + "\n");
            }
        } catch (NoSpace &s) {
            Logger::writeError("scripts", "World::createDynamicNPC: No space available for dynamic NPC: " + name + ":\n" + std::string(s.what()) + "\n");
        }

        return true;
    } catch (...) {
        std::cerr << "World::createDynamicNPC: Unknown error while loading dynamic NPC: " << name << std::endl;
        return false;
    }
}

luabind::object World::LuaLoS(position startingpos, position endingpos) {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;
    std::list<BlockingObject> objects = LoS(startingpos, endingpos);

    for (std::list<BlockingObject>::iterator boIterator = objects.begin(); boIterator != objects.end(); boIterator++) {
        luabind::object innerlist = luabind::newtable(luaState);

        if (boIterator->blockingType == BlockingObject::BT_CHARACTER) {
            innerlist["TYPE"] = "CHARACTER";
            innerlist["OBJECT"] = fuse_ptr<Character>(boIterator->blockingChar);
        } else if (boIterator->blockingType == BlockingObject::BT_ITEM) {
            innerlist["TYPE"] = "ITEM";
            innerlist["OBJECT"] = boIterator->blockingItem;
        }

        list[index] = innerlist;
        index++;
    }

    return list;
}

luabind::object World::getPlayersOnline() {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;
    PLAYERVECTOR::iterator pIterator;

    for (pIterator = Players.begin(); pIterator != Players.end(); ++pIterator) {
        list[index] = fuse_ptr<Character>(*pIterator);
        index++;
    }

    return list;
}

luabind::object World::getNPCS() {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;
    NPCVECTOR::iterator npcIterator;

    for (npcIterator = Npc.begin(); npcIterator != Npc.end(); ++npcIterator) {
        list[index] = fuse_ptr<Character>(*npcIterator);
        index++;
    }

    return list;

}

luabind::object World::getCharactersInRangeOf(position posi, uint8_t range) {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;

    std::vector < Player * > tempP = Players.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< Player *>::iterator pIterator = tempP.begin(); pIterator != tempP.end() ; ++pIterator) {
        list[index] = fuse_ptr<Character>(*pIterator);
        index++;
    }

    std::vector < Monster * > tempM = Monsters.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< Monster *>::iterator mIterator = tempM.begin(); mIterator != tempM.end() ; ++mIterator) {
        list[index] = fuse_ptr<Character>(*mIterator);
        index++;
    }

    std::vector < NPC * > tempN = Npc.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< NPC *>::iterator nIterator = tempN.begin(); nIterator != tempN.end() ; ++nIterator) {
        list[index] = fuse_ptr<Character>(*nIterator);
        index++;
    }

    return list;
}

luabind::object World::getPlayersInRangeOf(position posi, uint8_t range) {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;

    std::vector < Player * > tempP = Players.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< Player *>::iterator pIterator = tempP.begin(); pIterator != tempP.end() ; ++pIterator) {
        list[index] = fuse_ptr<Character>(*pIterator);
        index++;
    }

    return list;
}

luabind::object World::getMonstersInRangeOf(position posi, uint8_t range) {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;

    std::vector < Monster * > tempM = Monsters.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< Monster *>::iterator mIterator = tempM.begin(); mIterator != tempM.end() ; ++mIterator) {
        list[index] = fuse_ptr<Character>(*mIterator);
        index++;
    }

    return list;
}

luabind::object World::getNPCSInRangeOf(position posi, uint8_t range) {
    lua_State *luaState = getCurrentScript()->getLuaState();
    luabind::object list = luabind::newtable(luaState);
    int index = 1;

    std::vector < NPC * > tempN = Npc.findAllCharactersInRangeOf(posi.x , posi.y, posi.z, range);

    for (std::vector< NPC *>::iterator nIterator = tempN.begin(); nIterator != tempN.end() ; ++nIterator) {
        list[index] = fuse_ptr<Character>(*nIterator);
        index++;
    }

    return list;
}

void World::ItemInform(Character *user, ScriptItem item, std::string message) {
    if (user->character != Character::player) {
        return;
    }

    Player *cp = dynamic_cast<Player *>(user);

    if (item.type == ScriptItem::it_showcase1 ||item.type == ScriptItem::it_showcase2) {
        if (item.owner->character == Character::player) {
            unsigned char showcase;

            if (item.type == ScriptItem::it_showcase1) {
                showcase = 0;
            } else if (item.type == ScriptItem::it_showcase2) {
                showcase = 1;
            }

            boost::shared_ptr<BasicServerCommand>cmd(new NameOfShowCaseItemTC(showcase, item.itempos, message, item.getWorth()));
            cp->Connection->addCommand(cmd);
        }
    } else if (item.type == ScriptItem::it_inventory || item.type == ScriptItem::it_belt) {
        if (item.owner->character == Character::player) {
            boost::shared_ptr<BasicServerCommand>cmd(new NameOfInventoryItemTC(item.itempos, message, item.getWorth()));
            cp->Connection->addCommand(cmd);
        }
    } else if (item.type == ScriptItem::it_field) {
        if (item.owner->character == Character::player) {
            boost::shared_ptr<BasicServerCommand>cmd(new NameOfMapItemTC(item.pos.x, item.pos.y, item.pos.z , message));
            cp->Connection->addCommand(cmd);
        }
    }
}


void World::changeQuality(ScriptItem item, short int amount) {
    short int tmpQuality = ((amount+item.getDurability())<100) ? (amount + item.getQuality()) : (item.getQuality() - item.getDurability() + 99);

    if (tmpQuality%100 > 0) {
        item.setQuality(tmpQuality);
        changeItem(item);
    } else {
        erase(item,MAXITEMS);
    }
}

void World::changeQualityOfItemAt(position pos, short int amount) {
    Field *field;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        if (field->changeQualityOfTopItem(amount)) {
            sendRemoveItemFromMapToAllVisibleCharacters(0, pos.x, pos.y, pos.z, field);
        }
    }
}

bool World::changeItem(ScriptItem item) {

    if (item.type == ScriptItem::it_showcase1 || item.type == ScriptItem::it_showcase2) {
        if (item.owner->character == Character::player) {
            Container *showcase;

            if (item.type == ScriptItem::it_showcase1) {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 0 ].top();
            } else if (item.type == ScriptItem::it_showcase2) {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 1 ].top();
            }

            //Neues Item an der Position erzeugen
            showcase->changeItem(item);
            sendChangesOfContainerContentsIM(showcase);
            return true;
        } else {
            return false;
        }
    } else if (item.type == ScriptItem::it_inventory || item.type == ScriptItem::it_belt) {
        item.owner->characterItems[ item.itempos ] = (Item)item;

        //Wenn character ein Spieler ist ein update schicken
        if (item.owner->character == Character::player) {
            dynamic_cast<Player *>(item.owner)->sendCharacterItemAtPos(item.itempos);
        }

        item.owner->updateAppearanceForAll(true);
        return true;
    } else if (item.type == ScriptItem::it_field) {
        Field *field;
        Item dummy;

        if (GetPToCFieldAt(field, item.pos.x, item.pos.y, item.pos.z)) {
            Item it;

            if (field->TakeTopItem(it)) {
                field->PutTopItem(static_cast<Item>(item));

                if (item.getId() != it.getId() || it.getNumber() != item.getNumber()) {
                    sendSwapItemOnMapToAllVisibleCharacter(it.getId(), item.pos.x, item.pos.y, item.pos.z, item, field);
                }
            }

            return true;
        } else {
            return false;
        }
    } else if (item.type == ScriptItem::it_container) {
        if (item.inside) {
            item.inside->changeItem(item);
            sendChangesOfContainerContentsIM(item.inside);
            return true;
        }
    }

    return false;
}

std::string World::getItemName(TYPE_OF_ITEM_ID itemid, uint8_t language) {
    NamesStruct name;

    if (ItemNames->find(itemid, name)) {
        if (language == 0) {
            return name.German;
        } else {
            return name.English;
        }
    }

    return "notfound";
}


CommonStruct World::getItemStats(ScriptItem item) {
    CommonStruct data;

    if (CommonItems->find(item.getId(),data)) {
        return data;
    } else {
        data.id = 0;
        return data;
    }
}

CommonStruct World::getItemStatsFromId(TYPE_OF_ITEM_ID id) {
    CommonStruct data;

    if (CommonItems->find(id,data)) {
        return data;
    } else {
        data.id = 0;
        return data;
    }
}

bool World::isCharacterOnField(position pos) {
    if (findCharacterOnField(pos.x, pos.y, pos.z)) {
        return true;
    } else {
        return false;
    }
}

fuse_ptr<Character> World::getCharacterOnField(position pos) {
    return fuse_ptr<Character>(findCharacterOnField(pos.x, pos.y, pos.z));
}

bool World::erase(ScriptItem item, int amount) {
    if (amount > item.getNumber()) {
        amount = item.getNumber();
    }

    //Item befindet sich in einen der beiden Showcases
    if (item.type == ScriptItem::it_showcase1 || item.type == ScriptItem::it_showcase2) {
        if (item.owner->character == Character::player) {
            Container *showcase;

            if (item.type == ScriptItem::it_showcase1) {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 0 ].top();
            } else {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 1 ].top();
            }

            showcase->increaseAtPos(item.itempos, -amount);
            sendChangesOfContainerContentsIM(showcase);

            return true;
        } else {
            return false;
        }
    }
    //Item befindet sich am K�rper oder im G�rtel
    else if
    (item.type == ScriptItem::it_inventory || item.type == ScriptItem::it_belt) {
        //Wenn Item rechts belegt und links ein Belegt ist [Zweihanditem] das Belegt mit l�schen
        if (item.itempos == RIGHT_TOOL && (item.owner->GetItemAt(LEFT_TOOL)).getId() == BLOCKEDITEM) {
            item.owner->increaseAtPos(LEFT_TOOL, -255);
        } else if (item.itempos == LEFT_TOOL && (item.owner->GetItemAt(RIGHT_TOOL)).getId() == BLOCKEDITEM) {
            item.owner->increaseAtPos(RIGHT_TOOL, -255);
        }

        item.owner->increaseAtPos(item.itempos, -amount);
        return true;
    }
    //Item befindet sich auf einen Feld am Boden liegend.
    else if (item.type == ScriptItem::it_field) {
        Field *field;
        Item dummy;

        if (GetPToCFieldAt(field, item.pos.x, item.pos.y, item.pos.z)) {
            bool erased=false;
            field->increaseTopItem(-amount, erased);

            if (erased) {
                sendRemoveItemFromMapToAllVisibleCharacters(0, item.pos.x, item.pos.y, item.pos.z, field);
            }

            return true;
        } else {
            logMissingField("erase", item.pos);
            return false;
        }
    } else if (item.type == ScriptItem::it_container) {
        if (item.inside) {
            item.inside->increaseAtPos(item.itempos, -amount);
            sendChangesOfContainerContentsIM(item.inside);
            return true;
        } else {
            return false;
        }
    }

    return false;
}


bool World::increase(ScriptItem item, short int count) {
    //Item befindet sich in einen der beiden Showcases
    if (item.type == ScriptItem::it_showcase1 || item.type == ScriptItem::it_showcase2) {
        if (item.owner->character == Character::player) {
            Container *showcase;

            if (item.type == ScriptItem::it_showcase1) {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 0 ].top();
            } else {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 1 ].top();
            }

            showcase->increaseAtPos(item.itempos, count);
            sendChangesOfContainerContentsIM(showcase);
            return true;
        } else {
            return false;
        }
    }
    //Item befindet sich am K�rper oder im Rucksack
    else if (item.type == ScriptItem::it_inventory || item.type == ScriptItem::it_belt) {
        item.owner->increaseAtPos(item.itempos, count);
        return true;
    }
    //Item befindet sich auf einen Feld am Boden liegend.
    else if (item.type == ScriptItem::it_field) {
        Field *field;

        if (GetPToCFieldAt(field, item.pos.x, item.pos.y, item.pos.z)) {
            bool erased=false;
            field->increaseTopItem(count, erased);

            if (erased) {
                sendRemoveItemFromMapToAllVisibleCharacters(0, item.pos.x, item.pos.y, item.pos.z, field);
            }

            return true;
        } else {
            logMissingField("increase", item.pos);
            return false;
        }
    }

    else if (item.type == ScriptItem::it_container) {
        if (item.inside) {
            item.inside->increaseAtPos(item.itempos, count);
            sendChangesOfContainerContentsIM(item.inside);
            return true;
        } else {
            return false;
        }
    }

    return false;
}

bool World::swap(ScriptItem item, TYPE_OF_ITEM_ID newitem, unsigned short int newQuality) {
    //Item befindet sich in einen der beiden Showcases
    if (item.type == ScriptItem::it_showcase1 || item.type == ScriptItem::it_showcase2) {
        if (item.owner->character == Character::player) {
            Container *showcase;

            if (item.type == ScriptItem::it_showcase1) {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 0 ].top();
            } else {
                showcase = dynamic_cast<Player *>(item.owner)->showcases[ 1 ].top();
            }

            showcase->swapAtPos(item.itempos, newitem, newQuality);
            sendChangesOfContainerContentsIM(showcase);
            return true;
        } else {
            return false;
        }
    }
    //Item befindet sich am K�rper oder im Rucksack
    else if (item.type == ScriptItem::it_inventory || item.type == ScriptItem::it_belt) {
        item.owner->swapAtPos(item.itempos, newitem, newQuality);
        return true;
    }
    //Item befindet sich auf einen Feld am Boden liegend.
    else if (item.type == ScriptItem::it_field) {
        Field *field;

        if (GetPToCFieldAt(field, item.pos.x, item.pos.y, item.pos.z)) {
            bool ok;
            Item it;

            if (field->ViewTopItem(it)) {
                ok = field->swapTopItem(newitem, newQuality);

                if (ok) {
                    Item dummy;
                    dummy.setId(newitem);
                    dummy.setNumber(it.getNumber());

                    if (it.getId() != newitem) {
                        sendSwapItemOnMapToAllVisibleCharacter(it.getId(), item.pos.x, item.pos.y, item.pos.z, dummy, field);
                    }
                } else {
                    std::stringstream ss;
                    ss << "World::swap: Swapping item on Field (" << item.pos.x << ", " << item.pos.y << ", " << item.pos.z << ") failed!\n";
                    Logger::writeError("scripts", ss.str());
                    return false;
                }
            }
        } else {
            logMissingField("swap", item.pos);
            return false;
        }
    }

    else if (item.type == ScriptItem::it_container) {
        if (item.inside) {
            item.inside->swapAtPos(item.itempos, newitem, newQuality);
            sendChangesOfContainerContentsIM(item.inside);
            return true;
        } else {
            return false;
        }
    }


    return false;
}

ScriptItem World::createFromId(TYPE_OF_ITEM_ID id, unsigned short int count, position pos, bool allways, int quali, int data) {
    Field *field;
    ScriptItem sItem;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        CommonStruct com;

        if (CommonItems->find(id, com)) {
            g_item.setId(id);
            g_item.setNumber(count);
            g_item.setWear(com.AgingSpeed);
            g_item.setQuality(quali);
            g_item.setData(data);
            g_cont = NULL;
            sItem = g_item;
            sItem.pos = pos;
            sItem.type = ScriptItem::it_field;
            sItem.itempos = 255;
            sItem.owner = NULL;

            if (allways) {
                putItemAlwaysOnMap(NULL,pos.x,pos.y,pos.z);
            } else {
                putItemOnMap(NULL,pos.x,pos.y,pos.z);
            }

            return sItem;
        } else {
            std::stringstream ss;
            ss << "World::createFromId: Item " << id << " was not found in CommonItems!\n";
            Logger::writeError("scripts", ss.str());
            return sItem;
        }
    } else {
        logMissingField("createFromId", pos);
        return sItem;
    }

    return sItem;

}

bool World::createFromItem(ScriptItem item, position pos, bool allways) {
    Field *field;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        g_item = static_cast<Item>(item);
        g_cont = NULL;

        if (allways) {
            putItemAlwaysOnMap(NULL,pos.x,pos.y,pos.z);
        } else {
            putItemOnMap(NULL,pos.x,pos.y,pos.z);
        }

        return true;
    } else {
        logMissingField("createFromItem", pos);
        return false;
    }

    return false;
}

fuse_ptr<Character> World::createMonster(unsigned short id, position pos, short movepoints) {
    Field *field;
    Monster *newMonster;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        try {
            newMonster = new Monster(id, pos);
#ifdef LUASCRIPT_DEBUG
            std::cout<<"Erschaffe neues Monster: " << newMonster->name << " an Position (x,y,z) " << pos.x << " " << pos.y << " " << pos.z << std::endl;
#endif
            newMonster->actionPoints = movepoints;
            newMonsters.push_back(newMonster);
            field->setChar();
            sendCharacterMoveToAllVisiblePlayers(newMonster, NORMALMOVE, 4);
            return fuse_ptr<Character>(newMonster);

        } catch (Monster::unknownIDException &) {
            std::stringstream ss;
            ss << "World::createMonster: Failed to create monster with unknown id " << id << "!\n";
            Logger::writeError("scripts", ss.str());
            return fuse_ptr<Character>();
        }
    } else {
        logMissingField("createMonster", pos);
        return fuse_ptr<Character>();
    }

    return fuse_ptr<Character>();
}

void World::gfx(unsigned short int gfxid, position pos) {
    makeGFXForAllPlayersInRange(pos.x, pos.y, pos.z, MAXVIEW, gfxid);
}

void World::makeSound(unsigned short int soundid, position pos) {
    makeSoundForAllPlayersInRange(pos.x, pos.y, pos.z, MAXVIEW, soundid);
}

bool World::isItemOnField(position pos) {
    Field *field;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        Item dummy;
        return field->ViewTopItem(dummy);
    } else {
        logMissingField("isItemOnField", pos);
    }

    return false;
}

ScriptItem World::getItemOnField(position pos) {
    Field *field;
    ScriptItem item;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        Item It;

        if (field->ViewTopItem(It)) {
            item = It;
            item.pos = pos;
            item.type = ScriptItem::it_field;
            return item;
        }
    } else {
        logMissingField("getItemOnField", pos);
    }

    return item;
}

void World::changeTile(short int tileid, position pos) {
    Field *field;

    if (GetPToCFieldAt(field, pos.x, pos.y, pos.z)) {
        field->setTileId(tileid);
        field->updateFlags();
    }
}


void World::sendMapUpdate(position pos, uint8_t range) {
    std::vector<Player *> temp;
    std::vector<Player *>::iterator pIterator;
    temp=Players.findAllCharactersInRangeOf(pos.x,pos.y,pos.z, range); //getPlayersInRange(pos.x,pos.y,pos.z,MAXVIEW);

    for (pIterator = temp.begin(); pIterator != temp.end(); ++pIterator) {
        (*pIterator)->sendFullMap();
        sendAllVisibleCharactersToPlayer((*pIterator), true);
    }
}

bool World::createSavedArea(uint16_t tileid, position pos, uint16_t height, uint16_t width) {
    //Schleife durch alle spalten
    Map *dummy;

    for (time_t akt_x = pos.x; akt_x < pos.x+width; ++akt_x) {
        for (time_t akt_y = pos.y; akt_y < pos.y+height; ++akt_y) {
            //Pr�fen ob auf der position ein g�ltiges Feld ist, wenn ja false zur�ck liefern
            if (maps.findMapForPos(akt_x, akt_y, pos.z, dummy)) {
                std::cerr<<"World::createSavedArea: Aborted map insertion, map for field at ("<<akt_x <<", "<<akt_y<<", "<<pos.z<<") found!"<<std::endl;
                return false;
            }
        }
    }

    Map *tempmap = new Map(width,height);
    bool disappear=true;
    tempmap->Init(pos.x, pos.y, pos.z, disappear);

    Field *tempf;

    for (int _x=0; _x<width; ++_x)
        for (int _y=0; _y<height; ++_y) {
            if (tempmap->GetPToCFieldAt(tempf, _x+pos.x, _y+pos.y)) {
                tempf->setTileId(tileid);
                tempf->updateFlags();
            } else {
                std::cerr << "World::createSavedArea: For map inserted at (" << pos.x << ", " << pos.y << ", " << pos.z << ") no Field was found for offset (" << _x << ", " << _y << ")!" << std::endl;
            }

        }

    maps.InsertMap(tempmap);
    std::cout<<" Map Created by createSavedArea command at x: "<<pos.x<<" y: "<<pos.y<<" z: "<<pos.z<<" height: "<<height<<" width: "<<width<<" standard tile: "<<tileid<<"!"<<std::endl;
    return true;
}


bool World::getArmorStruct(TYPE_OF_ITEM_ID id, ArmorStruct &ret) {
    //Has to be an own function cant give a pointer of Armor items to the script
    ArmorStruct as;

    if (id == 0) {
        ret = as;
        return false;
    } else {
        return ArmorItems->find(id, ret);
    }

    //return armor;

}

bool World::getWeaponStruct(TYPE_OF_ITEM_ID id, WeaponStruct &ret) {
    //Has to be an own function cant give a pointer of Armor items to the script
    WeaponStruct ws;

    if (id == 0) {
        ret = ws;
        return false;
    } else {
        return WeaponItems->find(id, ret);
    }
}

bool World::getNaturalArmor(Character::race_type id, MonsterArmor &ret) {
    MonsterArmor ma;

    if (id == 0) {
        ret = ma;
        return false;
    } else {
        return NaturalArmors->find(id, ret);
    }
}

bool World::getMonsterAttack(Character::race_type id, AttackBoni &ret) {
    return MonsterAttacks->find(id, ret);
}

void World::sendMonitoringMessage(std::string msg, unsigned char id) {
    //send this Command to all Monitoring Clients
    boost::shared_ptr<BasicServerCommand>cmd(new BBMessageTC(msg, id));
    monitoringClientList->sendCommand(cmd);
}

void World::logMissingField(const std::string &function, const position &field) {
    std::stringstream ss;
    ss << "World::" << function << ": Field (" << field.x << ", " << field.y << ", " << field.z << ") was not found!\n";
    Logger::writeError("scripts", ss.str());
}
Something went wrong with that request. Please try again.