Guest User

Hurtworld Oxide Map Dumping Plugin

a guest
Jan 21st, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Oxide.Core;
  5. using Newtonsoft.Json;
  6. using UnityEngine;
  7.  
  8. namespace Oxide.Plugins
  9. {
  10.     public class SpawnInfo
  11.     {
  12.         public string Name;
  13.         public List<vec2> coords;
  14.  
  15.         public SpawnInfo()
  16.         {
  17.             coords = new List<vec2>();
  18.         }
  19.     }
  20.  
  21.     public class vec2
  22.     {
  23.         public float x;
  24.         public float y;
  25.  
  26.         public vec2(float x_, float y_)
  27.         {
  28.             x = x_;
  29.             y = y_;
  30.         }
  31.     }
  32.  
  33.     [Info("MiniPlugin", "therapy", 0.1)]
  34.     [Description("Tool Plugin for Map Extraction")]
  35.     class MiniPlugin : HurtworldPlugin
  36.     {
  37.         void RenderMap()
  38.         {
  39.             Dictionary<string, List<vec2>> dots = new Dictionary<string,List<vec2>>();
  40.            
  41.             for (float x = -4000.0f; x < 4000.0f; x += 2.0f)
  42.             {
  43.                 for (float y = -4000.0f; y < 4000.0f; y += 2.0f)
  44.                 {
  45.                     RaycastHit hitInfo;
  46.  
  47.                     //Send a ray from (x,y) at height 1000.0 units downwards and see what it hits
  48.                     Vector3 start = new Vector3(x, 1000.0f,y);
  49.                     if (Physics.Raycast(new Ray(start, Vector3.down), out hitInfo,2000.0f))
  50.                     {
  51.                         //Puts("Ray hit " + hitInfo.transform.name + " Collider: " + hitInfo.collider.name);
  52.  
  53.                         string name = hitInfo.collider.name;
  54.                         //Exclude this collider because it appears so often that the JSON renderer will die
  55.                         //with Out Of Memory exception...
  56.                         if (name == "DesertTerrain_split0")
  57.                             continue;
  58.  
  59.                         //Add pixel information for this collider to the list
  60.                         if (!dots.ContainsKey(name))
  61.                             dots[name] = new List<vec2>();
  62.                         dots[name].Add(new vec2(x, y));
  63.                     }
  64.                 }
  65.             }
  66.  
  67.  
  68.             //Write the JSON file, this is going to be huge (depending on resolution of the for-loops above, this will hit 150MB+
  69.             Oxide.Core.Configuration.DynamicConfigFile file = Interface.GetMod().DataFileSystem.GetDatafile("map");
  70.             file["dots"] = dots;
  71.             file.Save();
  72.         }
  73.         void DumpStuff()
  74.         {
  75.             //Get the CreateSpawnDirector, the class responsible for deserializing the map data
  76.             CreatureSpawnDirector spawnDirector = UnityEngine.Object.FindObjectOfType<CreatureSpawnDirector>();
  77.  
  78.             //This will contain the output
  79.             List<SpawnInfo> output = new List<SpawnInfo>();
  80.  
  81.             //Iterate over each spawn configuration
  82.             //Each spawn configuration represents one type of node (i.e. "IronRockResourceNodeServer") and contain a list
  83.             //of each cell this can spawn in as well as its spawn rate (ignored here)
  84.             foreach (SpawnConfiguration spawnConfig in spawnDirector.SpawnConfigs)
  85.             {
  86.                 Puts("Node type: " + spawnConfig.Object.name);
  87.  
  88.                 SpawnInfo info = new SpawnInfo();
  89.                 info.Name = spawnConfig.Object.name;
  90.  
  91.                 //Now iterate over each cell spawn info
  92.                 foreach (KeyValuePair<int, int> spawnRate in spawnConfig.CellSpawnRates)
  93.                 {
  94.                     // Puts("    Spawning in cell " + spawnRate.Key + " with coords: " + Singleton<PlayerActivityGrid>.Instance.CellManager.GetCellCenter(spawnRate.Key));
  95.  
  96.                     //Node that the actual position is randomized inside a given cell, so for mapping purposes I just get the center
  97.                     //coordinates
  98.                     UnityEngine.Vector2 foolol = Singleton<PlayerActivityGrid>.Instance.CellManager.GetCellCenter(spawnRate.Key);
  99.                     info.coords.Add(new vec2(foolol.x, foolol.y));
  100.                 }
  101.  
  102.                 output.Add(info);
  103.             }
  104.  
  105.             Oxide.Core.Configuration.DynamicConfigFile file = Interface.GetMod().DataFileSystem.GetDatafile("resource_dump");
  106.             file["nodes"] = output;
  107.             file.Save();
  108.             Puts("Dumped objects.");
  109.  
  110.         }
  111.         void OnServerInitialized()
  112.         {
  113.             Puts("Server ready.");
  114.             //Uncomment whatever you want here:
  115.  
  116.             //This dumps the resource JSON
  117.             //DumpStuff();
  118.  
  119.             //This dumps the map JSON
  120.             //RenderMap();
  121.         }
  122.     }
  123. }
Add Comment
Please, Sign In to add comment