Advertisement
xxZeus

Map Loader.pwn

Jul 5th, 2015
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.04 KB | None | 0 0
  1. /*================================================================================
  2.  *                 Dynamic Web based Map loader/unloader
  3.  *
  4.  * author: BroZeus ( http://forum.sa-mp.com/member.php?u=224655 )
  5.  * web demo: http://plwip.tk/mydemo/mapadder/
  6.  * version: 1.0
  7.  ================================================================================
  8.  
  9.  INTRODUCTION :
  10.  
  11.     This is a map loader and unloader which works with the web scripts that comes with this script.
  12.     Maps can be Added or deleted from web using web scripts provided with this script.
  13.     Maps can be loaded or unloaded into server using this script.
  14.     This package of pawn script and web based scripts use MySQL database for its operation.
  15.  
  16.   Avaliable Commands and thier info :
  17.  
  18.     -> /mapload     - Shows a list of 'UNLoaded' maps from database. After clicking on a map from
  19.                       list its information is shown in a dialog provided with two buttons "Load"
  20.                       and "Cancel". As the name suggest "Load" button loads the map into server.
  21.  
  22.     -> /ml          - Short form of command '/mapload'
  23.    
  24.     -> /mapunload   - This command is the opposite of '/mapload' command. This command shows a
  25.                       list of 'Loaded' maps. After clicking on a map from list its information is
  26.                       shown in a dialog with two buttons named "UnLoad" and "Cancel". Again as
  27.                       the name suggests 'UnLoad' button unloads the map.
  28.  
  29.     -> /mul         - Short form of command '/mapunload'
  30.    
  31.     CREDITS :
  32.  
  33.         SA-MP Team          : For developing and imrpoving SA-MP
  34.         Zeex                : For zcmd include
  35.         BlueG               : For MySQL plugin
  36.         Bjoern Klinggaard   : For bPopup, a javascript based plugin used in webscripts
  37.         Alan Williamson     : For Line numberer plugin, a javascript based plugin used in webscripts
  38.         And I guess me too?
  39.  */
  40.  
  41.  
  42.  
  43. #include <a_samp>
  44. #include <zcmd>
  45. #include <a_mysql>
  46.  
  47. //#include <streamer> //optional, works both with streamer and without streamer
  48.  
  49. //---------------------------------- Settings ---------------------------------------------
  50.  
  51. #define mysql_host       "127.0.0.1"
  52. #define mysql_user       "root"
  53. #define mysql_password   ""
  54. #define mysql_database   "chat"
  55.  
  56. #define AdminCheck(%0) !IsPlayerAdmin(%0)// Method to check if player is admin or not, see below
  57.  
  58. /*
  59.     Do NOT change the part '#define AdminCheck(%0)', you need to change the part '!IsPlayerAdmin(%0)'
  60.     Example :
  61.  
  62.         Lets say I have a variable named pInfo[playerid][pAdmin] to check for player admin and I
  63.         want that only admins with admin level 5 or more can use map loading/unloading command.
  64.         So I will do something like this :
  65.        
  66.             #define AdminCheck(%0) pInfo[%0][pAdmin] < 5
  67.            
  68.         PS. '%0' is the playerid, use '%0' not playerid in #define
  69. */
  70.  
  71.  
  72. #define MAX_MAPS 100 //max maps that can be loaded at a time in server
  73.  
  74. //--------------------------------------------------------------------------------------
  75.  
  76. #define SCM SendClientMessage
  77.  
  78. new db;
  79. new cur[MAX_PLAYERS][25];//current selection of map in dialog
  80.  
  81. enum dialog_id
  82. {
  83.     M_LOAD,
  84.     M_UNLOAD,
  85.     M_LINFO,
  86.     M_UINFO
  87. }
  88.  
  89. enum minfo
  90. {
  91.     mName[25],
  92.     mID,          //ID in MySQL table
  93.     Float:mStream,      //stream distance of map
  94.     mObj,         //total objects in map
  95.     bool:mLoaded  //used to check whether map info is loaded in a paticular slot
  96. }
  97.  
  98. new m_info[MAX_MAPS][minfo];//holds map information for LOADED maps
  99. new obj[MAX_MAPS][MAX_OBJECTS];//holds objects' id
  100.  
  101. #if defined FILTERSCRIPT
  102.  
  103. public OnFilterScriptInit()
  104. {
  105.     mysql_log(LOG_ALL);
  106.     db = mysql_connect(mysql_host, mysql_user, mysql_database, mysql_password);
  107.     if(mysql_errno())
  108.     {
  109.         print("\n-----------------------------------------------------------------\n");
  110.         print("Connection to MySQL database failed. GameMode will now be closed.");
  111.         print("\n-----------------------------------------------------------------\n");
  112.         SendRconCommand("exit");
  113.     }
  114.     else
  115.     {
  116.         print(">>> Connection to MySQL database has been made!");
  117.         mysql_tquery(db, "CREATE TABLE IF NOT EXISTS `maps` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(25) NOT NULL, `stream` FLOAT(15,7) NOT NULL, PRIMARY KEY (`id`))", "QueryFinish", "");
  118.     }
  119.     return 1;
  120. }
  121.  
  122. public OnFilterScriptExit()
  123. {
  124.     mysql_close(db);
  125.     return 1;
  126. }
  127.  
  128. #else
  129.  
  130. public OnGameModeInit()
  131. {
  132.     mysql_log(LOG_ALL);
  133.     db = mysql_connect(mysql_host, mysql_user, mysql_database, mysql_password);
  134.     if(mysql_errno())
  135.     {
  136.         print("\n---------------------------------------------------------------\n");
  137.         print("Connection to MySQL database failed. GameMode will now be closed. ");
  138.         print("\n---------------------------------------------------------------\n");
  139.         SendRconCommand("exit");
  140.     }
  141.     else
  142.     {
  143.         print(">>> Connection to MySQL database has been made!");
  144.         mysql_tquery(db, "CREATE TABLE IF NOT EXISTS `maps` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(25) NOT NULL, `stream` FLOAT(15,7) NOT NULL, PRIMARY KEY (`id`))", "QueryFinish", "");
  145.     }
  146.     return 1;
  147. }
  148.  
  149. public OnGameModeExit()
  150. {
  151.     mysql_close(db);
  152.     return 1;
  153. }
  154.  
  155. #endif
  156.  
  157. main()
  158. {
  159.     print("\n--------------------------------------------\n");
  160.     print(" Web based Dynamic Map Loader/UnLoader loaded");
  161.     print(" Author : BroZeus ");
  162.     print("\n--------------------------------------------\n");
  163. }
  164.  
  165. //==================== STOCKS ====================
  166.  
  167. stock GetEmptySlot()
  168. {
  169.     for(new i = 0; i < MAX_MAPS; i++)
  170.         if(!m_info[i][mLoaded])return i;
  171.     return -1;
  172. }
  173.  
  174. stock GetSlot(map_name[])
  175. {
  176.     for(new i = 0; i < MAX_MAPS; i++)
  177.     {
  178.         if((!strcmp(map_name, m_info[i][mName])) && strlen(map_name) == strlen(m_info[i][mName]))return i;
  179.     }
  180.     return -1;
  181. }
  182.  
  183. //==================== COMMANDS ====================
  184.  
  185. CMD:mul(playerid, params[])return cmd_mapunload(playerid, params);
  186.  
  187. CMD:mapunload(playerid, params[])
  188. {
  189.     #pragma unused params
  190.     if(AdminCheck(playerid))return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}You are not authorized to use this command.");
  191.     SCM(playerid, -1, "{f0f000}[SERVER]: {ffffff}Loading maps' information, please wait..");
  192.     new dstr[600], g = -1;
  193.     for(new i = 0;i < MAX_MAPS; i++)
  194.     {
  195.         if(!m_info[i][mLoaded])continue;
  196.         if(g == 1)strcat(dstr, "\n");
  197.         if((i+1) % 2)strcat(dstr, "{00f0f0}");
  198.         else strcat(dstr, "{f0f000}");//alternating color to rows in dialog
  199.         strcat(dstr, m_info[i][mName]);
  200.         g = 1;
  201.     }
  202.     if(g == -1)return SCM(playerid, -1, "{ff0000}[SERVER]:{ffffff} No maps loaded! You can load map(s) via /mapload or /ml");
  203.     ShowPlayerDialog(playerid, M_UNLOAD, DIALOG_STYLE_LIST, "{ff0000}U{ffffff}nload Map", dstr, "Select", "Cancel");
  204.     return 1;
  205. }
  206.  
  207. CMD:ml(playerid, params[])return cmd_mapload(playerid, params);
  208.  
  209. CMD:mapload(playerid, params[])
  210. {
  211.     #pragma unused params
  212.     if(AdminCheck(playerid))return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}You are not authorized to use this command.");
  213.     SCM(playerid, -1, "{f0f000}[SERVER]: {ffffff}Loading maps' information, please wait..");
  214.     mysql_tquery(db, "SELECT `name` FROM `maps`", "mListLoad", "d", playerid);
  215.     return 1;
  216. }
  217.  
  218. //==================== Forwards & Publics ====================
  219.  
  220. forward QueryFinish();
  221. public QueryFinish()return 1;
  222.  
  223. forward mListLoad(playerid);
  224. public mListLoad(playerid)
  225. {
  226.     if(!IsPlayerConnected(playerid))return 1;
  227.     new rows = cache_get_row_count(db);
  228.     if(!rows) return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}No maps in database!");
  229.     new temp[30], dstr[600], g = -1;
  230.     for(new i = 0; i < rows; i++)
  231.     {
  232.         cache_get_row(i, 0, temp, db);
  233.         if(GetSlot(temp) != -1)continue;
  234.         if((i+1) % 2)strcat(dstr, "{00f0f0}");
  235.         else strcat(dstr, "{f0f000}");//alternating color to rows in dialog
  236.         strcat(dstr, temp);
  237.         if(i < rows-1)strcat(dstr, "\n");
  238.         g = 1;
  239.     }
  240.     if(g == -1)return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}All maps are loaded into server. By this command only maps which are not loaded are viewed.");
  241.     ShowPlayerDialog(playerid, M_LOAD, DIALOG_STYLE_LIST, "{00cc00}L{ffffff}oad Map", dstr, "Select", "Cancel");
  242.     return 1;
  243. }
  244.  
  245. forward ShowMap(playerid);
  246. public ShowMap(playerid)
  247. {
  248.     if(!IsPlayerConnected(playerid))return 1;
  249.     new rows = cache_get_row_count(db);
  250.     if(!rows) return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}Looks like someone deleted that map while you were viewing list.");
  251.     new temp[30], dstr[200];
  252.     format(dstr, sizeof(dstr), "{ffffff}Map ID:\t\t{00f0f0}");
  253.     cache_get_field_content(0, "id", temp), strcat(dstr, temp);
  254.     strcat(dstr, "\n{ffffff}Map Name:\t\t{00f0f0}");
  255.     cache_get_field_content(0, "name", temp), strcat(dstr, temp);
  256.     strcat(dstr, "\n{ffffff}Draw Distance:\t\t{00f0f0}");
  257.     cache_get_field_content(0, "stream", temp), strcat(dstr, temp);
  258.     ShowPlayerDialog(playerid, M_LINFO, DIALOG_STYLE_MSGBOX, "Map Information", dstr, "Load", "Cancel");
  259.     return 1;
  260. }
  261.  
  262. forward LoadmInfo(playerid);
  263. public LoadmInfo(playerid)
  264. {
  265.     new rows = cache_get_row_count(db);
  266.     if(!rows) return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}Looks like someone deleted that map while you were viewing list.");
  267.     new slot = GetEmptySlot();
  268.     if(slot == -1)return SCM(playerid, -1, "{ff0000}[SERVER]: {ffffff}Max map loading limit has been reached.");
  269.     new temp[30], query[150];
  270.     m_info[slot][mLoaded] = true;
  271.     cache_get_field_content(0, "id", temp),m_info[slot][mID] = strval(temp);
  272.     cache_get_field_content(0, "name", temp),format(m_info[slot][mName], 25, temp);
  273.     cache_get_field_content(0, "stream", temp),m_info[slot][mStream] = floatstr(temp);
  274.     format(query, sizeof(query), "SELECT * FROM `%s`", m_info[slot][mName]);
  275.     mysql_tquery(db, query, "ObjLoad", "d", slot);
  276.     return 1;
  277. }
  278.  
  279. forward ObjLoad(slot);
  280. public ObjLoad(slot)
  281. {
  282.     m_info[slot][mObj] = cache_get_row_count(db);
  283.     new temp[30], mModel, Float:mx, Float:my, Float:mz, Float:mrx, Float:mry, Float:mrz;
  284.     for(new i = 0; i < m_info[slot][mObj]; i++)
  285.     {
  286.         cache_get_field_content(i, "model", temp),mModel = strval(temp);
  287.         cache_get_field_content(i, "x", temp),mx = floatstr(temp);
  288.         cache_get_field_content(i, "y", temp),my = floatstr(temp);
  289.         cache_get_field_content(i, "z", temp),mz = floatstr(temp);
  290.         cache_get_field_content(i, "rx", temp),mrx = floatstr(temp);
  291.         cache_get_field_content(i, "ry", temp),mry = floatstr(temp);
  292.         cache_get_field_content(i, "rz", temp),mrz = floatstr(temp);
  293.         #if defined Streamer_IncludeFileVersion
  294.             obj[slot][i] = CreateDynamicObject(mModel, mx, my, mz, mrx, mry, mrz, -1, -1, -1, 200.0, m_info[slot][mStream]);
  295.         #else
  296.             obj[slot][i] = CreateObject(mModel, mx, my, mz, mrx, mry, mrz, m_info[slot][mStream]);
  297.         #endif
  298.     }
  299.     new msg[100];
  300.     format(msg, sizeof(msg), "{00cc00}[SERVER]: {ffffff} Map named {00f0f0}%s {ffffff}has been loaded into server", m_info[slot][mName]);
  301.     SendClientMessageToAll(-1, msg);
  302.     return 1;
  303. }
  304.  
  305.  
  306. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  307. {
  308.     switch(dialogid)
  309.     {
  310.         case M_LOAD:
  311.         {
  312.             if(!response)return 1;
  313.             format(cur[playerid], 25, inputtext);
  314.             new query[80];
  315.             format(query, sizeof(query), "SELECT * FROM `maps` WHERE `name` = '%s' LIMIT 1", inputtext);
  316.             mysql_tquery(db, query, "ShowMap", "d", playerid);
  317.             return 1;
  318.         }
  319.         case M_LINFO:
  320.         {
  321.             if(!response)return 1;
  322.             new query[200];
  323.             format(query, sizeof(query), "{f0f000}[SERVER]: {ffffff}Loading map..");
  324.             SCM(playerid, -1, query);
  325.             if(GetSlot(cur[playerid]) != -1)return SCM(playerid, -1, "{f0f000}[SERVER]: {ffffff}Looks like someone already loaded that map when you were viewing list");
  326.             format(query, sizeof(query), "SELECT * FROM `maps` WHERE `name` = '%s' LIMIT 1", cur[playerid]);
  327.             mysql_tquery(db, query, "LoadmInfo", "d", playerid);
  328.             return 1;
  329.         }
  330.         case M_UNLOAD:
  331.         {
  332.             if(!response)return 1;
  333.             format(cur[playerid], 25, inputtext);
  334.             new dstr[200], slot = GetSlot(inputtext);
  335.             format(dstr, sizeof(dstr), "{ffffff}Map Name:\t\t{00f0f0}%s\n{ffffff}Map ID:\t\t{00f0f0}%i\n{ffffff}Total Objects:\t\t{00f0f0}%i\n{ffffff}Draw Distance:\t\t{00f0f0}%f", m_info[slot][mName], m_info[slot][mID], m_info[slot][mObj], m_info[slot][mStream]);
  336.             ShowPlayerDialog(playerid, M_UINFO, DIALOG_STYLE_MSGBOX, "Map Information", dstr, "UnLoad", "Cancel");
  337.             return 1;
  338.         }
  339.         case M_UINFO:
  340.         {
  341.             if(!response)return 1;
  342.             SCM(playerid, -1, "{f0f000}[SERVER]: {ffffff}Map Unloading in progress..");
  343.             new slot = GetSlot(cur[playerid]);
  344.             m_info[slot][mLoaded] = false;
  345.             new temp[30];
  346.             format(temp, sizeof(temp), m_info[slot][mName]);
  347.             format(m_info[slot][mName], 25, "\0");
  348.             for(new i = 0; i < m_info[slot][mObj]; i++)
  349.             {
  350.                  #if defined Streamer_IncludeFileVersion
  351.                      DestroyDynamicObject(obj[slot][i]);
  352.                 #else
  353.                     DestroyObject(obj[slot][i]);
  354.                 #endif
  355.                 obj[slot][i] = -1;
  356.             }
  357.             m_info[slot][mStream] = -1.0;
  358.             m_info[slot][mObj] = -1;
  359.             m_info[slot][mID] = -1;
  360.             new msg[100];
  361.             format(msg, sizeof(msg), "{FFAF02}[SERVER]: {ffffff}Map named {00f0f0}%s {ffffff}has been unloaded", temp);
  362.             SendClientMessageToAll(-1, msg);
  363.             return 1;
  364.         }
  365.     }
  366.     return 0;
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement