| 529 | } |
| 530 | |
| 531 | wzapi::scripting_instance* scripting_engine::loadPlayerScript(const WzString& path, int player, AIDifficulty difficulty) |
| 532 | { |
| 533 | ASSERT_OR_RETURN(nullptr, player >= 0 && (player < MAX_PLAYERS || player == selectedPlayer), "Player index %d out of bounds", player); |
| 534 | |
| 535 | debug(LOG_SCRIPT, "loadPlayerScript[%d]: %s", player, path.toUtf8().c_str()); |
| 536 | |
| 537 | int realDifficulty; |
| 538 | if (game.type == LEVEL_TYPE::SKIRMISH) |
| 539 | { |
| 540 | realDifficulty = static_cast<int8_t>(difficulty); |
| 541 | } |
| 542 | else // campaign |
| 543 | { |
| 544 | realDifficulty = (int)getDifficultyLevel(); |
| 545 | } |
| 546 | |
| 547 | wzapi::scripting_instance* pNewInstance = loadPlayerScriptByBackend(path, player, realDifficulty); |
| 548 | if (!pNewInstance) |
| 549 | { |
| 550 | // failed to create new scripting instance |
| 551 | debug(LOG_ERROR, "Failed to create new scripting instance: path:\"%s\", player: %d, difficulty: %d", path.toUtf8().c_str(), player, static_cast<int>(difficulty)); |
| 552 | return nullptr; |
| 553 | } |
| 554 | |
| 555 | // Create group map |
| 556 | GROUPMAP *psMap = new GROUPMAP; |
| 557 | auto insert_result = groups.insert(ENGINEMAP::value_type(pNewInstance, psMap)); |
| 558 | ASSERT(insert_result.second, "Entry for this engine %p already exists in ENGINEMAP!", static_cast<void *>(pNewInstance)); |
| 559 | |
| 560 | json globalVars = json::object(); |
| 561 | // Special global variables |
| 562 | //== * ```version``` Current version of the game, set in *major.minor* format. |
| 563 | globalVars["version"] = version_getVersionString(); |
| 564 | //== * ```selectedPlayer``` The player controlled by the client on which the script runs. |
| 565 | globalVars["selectedPlayer"] = selectedPlayer; |
| 566 | //== * ```gameTime``` The current game time. Updated before every invokation of a script. |
| 567 | pNewInstance->setSpecifiedGlobalVariable("gameTime", gameTime, wzapi::GlobalVariableFlags::ReadOnlyUpdatedFromApp | wzapi::GlobalVariableFlags::DoNotSave); |
| 568 | //== * ```modList``` An array of the current loaded mods (mod names). |
| 569 | globalVars["modList"] = getModNamesList(); |
| 570 | |
| 571 | |
| 572 | //== * ```difficulty``` The currently set campaign difficulty, or the current AI's difficulty setting. It will be one of |
| 573 | //== ```SUPEREASY``` (campaign only), ```EASY```, ```MEDIUM```, ```HARD``` or ```INSANE```. |
| 574 | if (game.type == LEVEL_TYPE::SKIRMISH) |
| 575 | { |
| 576 | globalVars["difficulty"] = static_cast<int8_t>(difficulty); |
| 577 | } |
| 578 | else // campaign |
| 579 | { |
| 580 | globalVars["difficulty"] = (int)getDifficultyLevel(); |
| 581 | } |
| 582 | //== * ```mapName``` The name of the current map. |
| 583 | globalVars["mapName"] = game.map; |
| 584 | //== * ```tilesetType``` The area name of the map. |
| 585 | std::string tilesetType("CUSTOM"); |
| 586 | switch (currentMapTileset) |
| 587 | { |
| 588 | case MAP_TILESET::ARIZONA: |
no test coverage detected