Resource:IT/Map manager

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
« Torna alla Pagina principale italiana .

Il map manager è una risorsa inclusa nel pacchetto server di MTA:SA DM e comprende comandi, funzioni ed eventi per le gamemodes per gestire dinamicamente le loro mappe. Ad esempio, quando un server ha bisogno di caricare diverse mappe per una race mode, invece di essere messe tutte nella risorsa della gamemode, possono essere inserite in una risorsa separata ed essere successivamente richiamate con la funzione "changeGamemodeMap" quando una nuova gara inizia.

Più specificamente, il map manager memorizza la lista di tutte le gamemodes/mappe e ne gestisce il caricamento; applica alcune impostazioni della mappa che hanno effetto sul mondo del gioco e setta le regole della mappa e il tipo di gioco su ASE. Comprende inoltre una funzione che visualizza ed aggiorna automaticamente nel browser la mode/mappa.

Un semplice tutorial

In questa sezione continueremo a lavorare sulla gamemode che abbiamo creato nella pagina introduzione allo Scripting. Aggiungeremo una semplice risorsa mappa che conterrà gli spawn dei giocatori e caricherà i dati nello script principale quando un giocatore dovrà spawnare.

Innanzitutto crea una cartella sotto

<SERVER>/mods/deathmatch/resources/

, che chiamerai mymap. Dentro

/mymap/

crea un file di testo chiamato meta.xml, che è necessario a tutte le risorse.

Inserisci questo codice all'interno di meta.xml:

<meta>
   <info type="map" gamemodes="myserver"/>
   <map src="mymap.map"/>
</meta>

Notare che questa risorsa è collegata allo script principale tramite l'attributo

gamemodes=""

, che contiene il nome della risorsa principale. Nel tag

map

è indicato il nome del file .map che contiene le informazioni sulla mappa. Ora crea un altro file di testo sotto

/mymap/

e chiamalo mymap.map, inserendovi questo codice:

<map>
   <spawnpoint id="spawnpoint1" posX="1959.5487060547" posY="-1714.4613037109" posZ="18" rot="63.350006103516" model="0"/>
</map>

Notare che

spawnpoint

è il tipo di elemento usato nella funzione getElementsByType; così come

id

è usato nella funzione getElementByID. Per caricare i dati della mappa lo script principale ha bisogno di un collegamento con la risorsa della mappa. Modifichiamo il file script.lua nella risorsa

myserver

. Inserisci il seguente codice:

function loadMap(startedMap)
	mapRoot = getResourceRootElement(startedMap)
end

addEventHandler("onGamemodeMapStart", g_root, loadMap)

In pratica, l'evento

onGamemodeMapStart

ci da l'handle della mappa ("startedMap"), che abbiamo usato per estrarre l'handle della risorsa contenente la mappa ("mapRoot"). Con l'handle della risorsa possiamo estrarne le informazioni sugli spawnpoint. Creando la funzione

joinHandler()

dentro script.lua, invece di specificare le coordinate x, y e z, possiamo usare i dati della mappa come segue:

function joinHandler()
	local spawn = getElementsByType("spawnpoint", mapRoot) --Creiamo un array locale contenente gli elementi <spawnpoint>
	local x,y,z,r --Creaiamo delle variabili locali per tutte le coordinate
	for key, value in pairs(spawn) do --Inseriamo nelle variabili delle coordinate le coordinate prese dall'elemento <spawnpoint>
		x = getElementData(value, "posX")
		y = getElementData(value, "posY")
		z = getElementData(value, "posZ")
		r = getElementData(value, "rot")
	end
	spawnPlayer(source, x, y, z) --Spawniamo il giocatore alle coordinate appena prese
	fadeCamera(source, true) --E impostiamo la sua telecamera su di lui
end

Ora puoi far partire la gamemode sul tuo server avviando in console il seguente comando:

gamemode myserver mymap

Un tutorial più avanzato

In questo tutorial, invece di avviare la mappa con il comando sopra descritto quando avviamo il server, avvieremo la risorsa con qualche linea di script. Questo può tornare utile se hai molte mappe e vuoi che il server le avvii automaticamente.

Prima di tutto dobbiamo creare un handler per l'evento onResourceStart, che si attiva quando la risorsa principale viene avviata con il comando

gamemode

. Poi, con la funzione assegnata all'evento, carichiamo la risorsa mappa e inizializziamo la mappa:

function Initialize(startedResource)
	mapRes = getResourceFromName("mymap") --Otteniamo l'handle della risorsa "mymap"
	manager = getResourceFromName("mapmanager") --E quello di "mapmanager"
	startResource(mapRes) --Inizializziamo la risorsa "mymap"
	
	call(manager, "changeGamemodeMap", mapRes, getThisResource()) --E chiamiamo la funzione "changeGamemodeMap" dentro mapmanager
end

addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), Initialize) --Impostiamo Inizialize() come handler per onResourceStart

La funzione call permette, come visto, di chiamare una funzione presente in un'altra risorsa, in questo caso la funzione

changeGamemodeMap

dentro la riosrsa mapmanager.

Se provassi ad avviare il server adesso, riceveresti molti errori "access denied" (accesso negato). QUesto perché la risorsa principale, myserver, non ha ancora accesso di default alla funzione startResource. Dobbiamo aggiungere la funzione nell'Access Control List con i diritti necessari.

Vai nella cartella

<SERVER>/mods/deathmatch/

. Vedrai un file chiamato acl.xml, aprilo con un qualsiasi editor di testo ed inserisci il seguente codice:

<group name="myserver">
   <acl name="acl_myserver"/>
   <object name="resource.myserver"/>
</group>
<acl name="acl_myserver">
   <right name="function.startResource" access="true"/>
   <right name="function.stopResource" access="true"/>
   <right name="function.restartResource" access="true"/>
</acl>

Assicurati che acl.xml sia chiuso, quindi riavvia il server; esso ricaricherà il file ACL, dando i diritti di accesso della risorsa alla funzione. Ora puoi avviare la funzione con:

gamemode myserver

E lo script avvierà la mappa per voi.

Usage

To use the map manager, your resources must first be marked as either gamemodes or maps.

You have to tag the gamemode resource with the correct type in its info tag:

<info description="A gamemode" type="gamemode" />

Map resources also need the type="map" tag, plus a gamemodes tag listing the gamemode resources they're compatible with in a comma-separated list without spaces.

<info description="A gamemode map" type="map" gamemodes="ctv,koth" />

There can be only one gamemode and one gamemode map loaded at once.

Optional resource attributes

These attributes all go in the corresponding resource's info tag.

name: A friendly name for your gamemode or map, to be displayed in the start messages or map listings instead of the filename.

Commands

changemap newmap [newgamemode] (changes the gamemode map to a new one, optionally changing the gamemode as well)

changemode newgamemode [newmap] (changes to a new gamemode, optionally starting a map with it)

gamemode newgamemode [newmap] (same as previous one)

stopmode (stops the current mode and mode map)

stopmap (stops the current mode map)

maps [gamemode] (lists all maps in the server, optionally all maps compatible with a gamemode)

gamemodes (lists all gamemodes)

Settings

*mapmanager.color [hex color string] (changes the mapmanager's output messages' color) (default: #E1AA5A)

*mapmanager.messages [boolean] (whether map/gm changes are enabled) (default: true)

*mapmanager.ASE [boolean] (whether the manager will set ASE gametype / mapname) (default: true)

Exported functions

bool changeGamemode ( resource newGamemode, [ resource mapToLoadWith ] )

Changes the gamemode to a new one, optionally specifying an initial map for it (will load without a map by default).

bool changeGamemodeMap ( resource newMap, [ resource gamemodeToChangeTo ] )

Changes the GM map to a new one, optionally specifying a gamemode to change to before loading it (will load with the current gamemode by default).

table getGamemodes ( )

Returns a table of all gamemode resource pointers.

table getGamemodesCompatibleWithMap ( resource theMap )

Returns a table of compatible gamemode resource pointers.

table getMaps ( )

Returns a table of all map resource pointers.

table getMapsCompatibleWithGamemode ( [ resource theGamemode ] )

Returns a table of compatible map resource pointers. If the gamemode is left blank, it returns all maps which aren't compatible with any gamemode.

resource getRunningGamemode ( )

Returns the currently running gamemode's resource pointer.

resource getRunningGamemodeMap ( )

Returns the currently running GM map's resource pointer.

bool isGamemode ( resource theGamemode )

Determines if a resource is a gamemode or not.

bool isGamemodeCompatibleWithMap ( resource theGamemode, resource theMap )

Determines if a gamemode is compatible with a map or not.

bool isMap ( resource theMap )

Determines if a resource is a map or not.

bool isMapCompatibleWithGamemode ( resource theMap, resource theGamemode )

Determines if a map is compatible with a gamemode or not.

bool stopGamemode ( )

Stops the current gamemode and its map.

bool stopGamemodeMap ( )

Stop the current GM map. Determines if a map is compatible with a gamemode or not.

Fired events

(For all these events, "source" is the resource's root element.)

onGamemodeStart ( resource startedGamemode )

Fired before a gamemode starts.

onGamemodeStop ( resource stoppedGamemode )

Fired before a gamemode is stopped.

onGamemodeMapStart ( resource startedMap )

Fired before a GM map starts.

onGamemodeMapStop ( resource stoppedMap )

Fired before a GM map is stopped.

Supported map settings

The following settings from the registry are applied by the map manager when a map is started:
gamespeed [number]: The map's game speed.
gravity [number]: The map's gravity.
time [string of the form hh:mm]: The map's time.
weather [number]: The map's weather ID.
waveheight [number]: The map's wave height.
locked_time [boolean]: Whether the set time will be frozen by the manager or not.
minplayers [number]: The required minimum number of players to start the map.
maxplayers [number]: The allowed maximum number of players to start the map.