/// <summary> /// This callback is called for every point that changes materials in a digging operation. /// </summary> public static void PointCallback(float x, float y, float z, int matPrev, int matNow, int lod) { // If material was changed from non-air to air: add a particle animation. if (matPrev != 0 && matNow == 0) { // Create particle systems on demand. if (!instance.stoneParticles.ContainsKey(matPrev)) { SolidTerrainResource res = TerrainResource.FromIndex(matPrev) as SolidTerrainResource; if (res != null) { instance.stoneParticles.Add(matPrev, new StoneParticles(res, LocalScript.world)); } else { instance.stoneParticles.Add(matPrev, null); } } // Add particle. StoneParticles particles = instance.stoneParticles[matPrev]; if (particles != null) { /*instance.stoneParticles[matPrev].particlesStones.AddParticle3D( * new vec3(x, y, z) + RandomDir() * (float)random.NextDouble() * .3f, * RandomDir() * (float)random.NextDouble() * .4f, * new vec4(1), * .4f + (float)random.NextDouble() * .3f, * .2f + (float)random.NextDouble() * .3f, * RandomDir(), * RandomDir(), * new vec3(0));*/ } } }
public void Hit(object message) { if (!(message is HitMessage)) { return; } // Tree has been hit (by an axe), so we create some wood cylinders the player can pick up ContainingWorld.RemoveEntity(thisEntity); // Gather wood from "real" trees only, not from cacti etc. if (treeType != TreeType.Birch) { return; } int numberOfWoodCylinders = 3 * (int)(amountOfWood + 1.0f); for (int i = 0; i < numberOfWoodCylinders; ++i) { ItemEntity itemEntity = new ItemEntity(new MaterialItem(TerrainResource.FromName("BirchWood"), MaterialShape.Cylinder, new vec3(0.2f, 0.7f, 0.2f))); ContainingWorld.AddEntity(itemEntity, mat4.Translate(Position + new vec3(0, 1 + i, 0))); } }
public MaterialItem(TerrainResource material, MaterialShape shape, vec3 size, int stackSize = 1) : base(material.Name + " " + shape, null, 1.0f, ItemCategory.Material, stackSize) { Material = material; Shape = shape; Size = size; Description = "A " + shape + " made of " + material.Name + " with " + DimensionString; Icon = material.Name + "," + shape; }
/// <summary> /// Initializes all crafting rules /// </summary> public void InitCraftingRules() { Debug.Assert(craftingRules.Count == 0, "Expected empty ruleset"); TerrainResource dirt = TerrainResource.FromName("Dirt"); TerrainResource wood = TerrainResource.FromName("Wood"); TerrainResource iron = TerrainResource.FromName("Iron"); TerrainResource coal = TerrainResource.FromName("Coal"); TerrainResource gold = TerrainResource.FromName("Gold"); TerrainResource copper = TerrainResource.FromName("Copper"); TerrainResource aoicrystal = TerrainResource.FromName("AoiCrystal"); /*craftingRules.Add(new ExplicitCraftingRule(new MaterialItem(dirt, MaterialShape.Cube, new vec3(1)), * new [] { new ResourceItem(dirt, 1f) } , * new [] { new ResourceItem(dirt, .5f) } ));*/ // Tools craftingRules.Add(new ExplicitCraftingRule(new ToolItem(ToolType.Pickaxe), (new [] { new ResourceItem(iron, 1f), new ResourceItem(wood, 1f) }), (new [] { new ResourceItem(iron, .1f), new ResourceItem(wood, .2f) }))); craftingRules.Add(new ExplicitCraftingRule(new ToolItem(ToolType.Axe), (new [] { new ResourceItem(iron, 1f), new ResourceItem(wood, 1f) }), (new [] { new ResourceItem(iron, .1f), new ResourceItem(wood, .2f) }))); craftingRules.Add(new ExplicitCraftingRule(new ToolItem(ToolType.Shovel), (new [] { new ResourceItem(iron, 1f), new ResourceItem(wood, 1f) }), (new [] { new ResourceItem(iron, .1f), new ResourceItem(wood, .2f) }))); craftingRules.Add(new ExplicitCraftingRule(new ToolItem(ToolType.Hammer), (new [] { new ResourceItem(iron, 4f), new ResourceItem(wood, 2f) }), (new [] { new ResourceItem(iron, .2f), new ResourceItem(wood, .3f) }))); // MaterialItems List <TerrainResource> craftMaterials = new List <TerrainResource>(); craftMaterials.Add(dirt); craftMaterials.Add(wood); craftMaterials.Add(coal); craftMaterials.Add(iron); craftMaterials.Add(gold); craftMaterials.Add(aoicrystal); craftMaterials.Add(copper); for (int i = 1; i <= 14; ++i) { craftMaterials.Add(TerrainResource.FromName("Stone." + i.ToString("00"))); } foreach (var mat in craftMaterials) { craftingRules.Add(new MaterialCraftingRule(mat, MaterialShape.Cube, new vec3(1))); craftingRules.Add(new MaterialCraftingRule(mat, MaterialShape.Cylinder, new vec3(1))); craftingRules.Add(new MaterialCraftingRule(mat, MaterialShape.Sphere, new vec3(1))); } }
/// <summary> /// Adds a resource of a given terrain material type. Amount can be negative. /// </summary> public void AddResource(TerrainResource mat, float amount) { Debug.Assert(mat != null); if (amount > 0) { AddItem(new ResourceItem(mat, amount)); } else { RemoveItem(new ResourceItem(mat, -amount)); } }
/// <summary> /// This callback is called once per changed material in a chunk and reports the amount of volume changed (in m^3). /// </summary> public static void StatCallback(int mat, float volume, int lod) { if (mat != 0) { // Resolve terrain material. TerrainResource material = TerrainResource.FromIndex(mat); Debug.Assert(material != null, "Invalid terrain material"); // Add proper amount of material to player inventory. // If the material changed by a negative volume we want to collect a positive amount. instance.player.Inventory.AddResource(material, -volume); } }
/// <summary> /// Initializes the terrain materials and settings. /// </summary> public static void init(World world) { Instance = new UpvoidMinerWorldGenerator(); Instance.world = world; // Register all terrain resources (and thus terrain materials). TerrainResource.RegisterResources(world.Terrain); // Get handle to dirt for generation. Instance.terrainDirt = TerrainResource.FromName("Dirt"); Instance.terrainRock = TerrainResource.FromName("Stone.09"); Instance.terrainDesert = TerrainResource.FromName("Desert"); // load entities LoadEntities(); // init terrain world.SetTerrainGenerator(new TerrainGenerator()); world.TerrainGenerator.SetCsgNode(Instance.createTerrain()); }
/// <summary> /// Starts /// </summary> public static void Startup(IntPtr _unmanagedModule) { // Get and save the resource domain of the mod, needed for loading resources. UpvoidMiner.Mod = Module.FromHandle(_unmanagedModule); UpvoidMiner.ModDomain = UpvoidMiner.Mod.ResourceDomain; // Create the world. Multiple worlds could be created here, but we only want one. // Use the UpvoidMinerWorldGenerator, which will create a simple terrain with some vegetation. World world = Universe.CreateWorld("UpvoidMinerWorld"); UpvoidMinerWorldGenerator.init(world); world.Start(); for (int i = 0; i < 3; ++i) { TerrainResource mat = TerrainResource.FromName("Stone." + (i + 1).ToString("00")); Debug.Assert(mat != null, "Invalid material"); MaterialItem testItem = new MaterialItem(mat, MaterialShape.Cube, new vec3(1), 1); world.AddEntity(new ItemEntity(testItem), mat4.Translate(new vec3(5f, i * 2f, ((i % 3) * 2f)))); } }
/// <summary> /// Places the current digging shape shape of a given material /// </summary> public void PlaceMaterial(TerrainResource material, vec3 worldNormal, vec3 position, float radius) { position = AlignPlacementPosition(position); switch (CurrentDiggingShape) { case DiggingShape.Sphere: digging.DigSphere(worldNormal, position, radius, new[] { 0 }, material.Index, DiggingController.DigMode.Add); break; case DiggingShape.Box: digging.DigBox(worldNormal, position, radius, new[] { 0 }, material.Index, DiggingController.DigMode.Add); break; case DiggingShape.Cylinder: digging.DigCylinder(worldNormal, position, radius, new[] { 0 }, material.Index, DiggingController.DigMode.Add); break; default: throw new Exception("Unsupported digging shape used"); } }
public override void OnUse(Player player, vec3 _worldPos, vec3 _worldNormal, Entity _hitEntity) { switch (ToolType) { case ToolType.Pickaxe: // Pickaxe has small radius but can dig everywhere player.DigMaterial(_worldNormal, _worldPos, digRadiusPickaxe, null); return; case ToolType.Shovel: // Shovel has big radius but can only dig dirt player.DigMaterial(_worldNormal, _worldPos, digRadiusShovel, new[] { TerrainResource.FromName("Dirt").Index, TerrainResource.FromName("Desert").Index }); return; case ToolType.Axe: if (_hitEntity != null) { _hitEntity[TriggerId.getIdByName("Hit")] |= new HitMessage(player.thisEntity); } return; case ToolType.DroneChain: // Add a drone to the use-position. player.AddDrone(_worldPos); // Remove that drone from inventory. player.Inventory.RemoveItem(new ToolItem(ToolType)); return; case ToolType.Hammer: // TODO return; default: throw new InvalidOperationException("Unknown tool"); } }
public ResourceItem(TerrainResource material, float volume = 0f) : base(material.Name, "The terrain resource " + material.Name, 1.0f, ItemCategory.Resources, volume) { Material = material; Icon = material.Name; }
/// <summary> /// This is called by the engine at mod startup and initializes the local part of the UpvoidMiner mod. /// </summary> public static void Startup(IntPtr _unmanagedModule) { // Get and save the resource domain of the mod, needed for loading resources. UpvoidMiner.Mod = Module.FromHandle(_unmanagedModule); UpvoidMiner.ModDomain = UpvoidMiner.Mod.ResourceDomain; // Get the world (created by the host script). world = Universe.GetWorldByName("UpvoidMinerWorld"); // No loading screen for clients (since the server generates the world) if (Scripting.IsHost) { // Register a callback for the terrain generation so the GUI can be notified when the world is ready. world.Terrain.AddVolumeUpdateCallback(VolumeCallback, false, 0, 4); // Show a splash screen in the GUI client. WebGui.DefaultUI.LoadURL("http://localhost:" + Webserver.DefaultWebserver.Port + "/Mods/Upvoid/UpvoidMiner/0.0.1/SplashScreen.html"); // Register a socket for sending progress updates to the loading screen generationProgressSocket = new WebSocketHandler(); Webserver.DefaultWebserver.RegisterWebSocketHandler(UpvoidMiner.ModDomain, "GenerationProgressSocket", generationProgressSocket); Webserver.DefaultWebserver.RegisterDynamicContent(UpvoidMiner.ModDomain, "ActivatePlayer", (WebRequest request, WebResponse response) => ActivatePlayer()); Webserver.DefaultWebserver.RegisterDynamicContent(UpvoidMiner.ModDomain, "IsPlayerActivated", (WebRequest request, WebResponse response) => response.AppendBody((player != null).ToString())); Webserver.DefaultWebserver.RegisterDynamicContent(UpvoidMiner.ModDomain, "GenerationProgressQuery", webGenerationProgress); Webserver.DefaultWebserver.RegisterDynamicContent(UpvoidMiner.ModDomain, "OpenSiteInBrowser", (WebRequest request, WebResponse response) => Process.Start(request.GetQuery("url"))); } // Create a simple camera that allows free movement. camera = new GenericCamera(); camera.Position = new vec3(0, 10, 0); camera.FarClippingPlane = 1750.0; // Client-only: register terrain materials if (!Scripting.IsHost) { TerrainResource.RegisterResources(world.Terrain); } // Place the camera in the world. world.AttachCamera(camera); Rendering.SetupDefaultPipeline(camera); // Create an active region around the player spawn // Active regions help the engine to decide which parts of a world are important (to generate, render, etc.) // In near future it will be updated when the player moves out of it //world.AddActiveRegion(new ivec3(), 100f, 400f, 40f, 40f); Settings.InitSettingsHandlers(); Webserver.DefaultWebserver.RegisterDynamicContent(UpvoidMiner.ModDomain, "QuitGame", (WebRequest request, WebResponse response) => Scripting.ShutdownEngine()); // Register for input press events. Input.OnPressInput += HandlePressInput; // Register sockets for resource downloading progress bar resourceDownloadProgressSocket = new WebSocketHandler(); Webserver.DefaultWebserver.RegisterWebSocketHandler(UpvoidMiner.ModDomain, "ResourceDownloadProgress", resourceDownloadProgressSocket); if (!Scripting.IsHost) { ActivatePlayer(); } }
/// <summary> /// Populates the inventory with a list of items that we start with. /// </summary> void generateInitialItems() { if (!File.Exists(UpvoidMiner.SavePathInventory)) { // Tools Inventory.AddItem(new ToolItem(ToolType.Shovel)); Inventory.AddItem(new ToolItem(ToolType.Pickaxe)); Inventory.AddItem(new ToolItem(ToolType.Axe)); //Inventory.AddItem(new ToolItem(ToolType.Hammer)); Inventory.AddItem(new ToolItem(ToolType.DroneChain, 5)); // Testing resource/material items. /*TerrainResource dirt = ContainingWorld.Terrain.QueryMaterialFromName("Dirt"); * TerrainResource stone06 = ContainingWorld.Terrain.QueryMaterialFromName("Stone.06"); * Inventory.AddResource(dirt, 10); * Inventory.AddItem(new ResourceItem(dirt, 3f)); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Sphere, new vec3(1))); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Sphere, new vec3(1), 2)); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Cylinder, new vec3(1,2,2))); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Sphere, new vec3(2))); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Cube, new vec3(2))); * Inventory.AddItem(new MaterialItem(stone06, MaterialShape.Cylinder, new vec3(1,2,2))); * Inventory.AddItem(new MaterialItem(dirt, MaterialShape.Sphere, new vec3(1)));*/ Inventory.AddItem(new MaterialItem(TerrainResource.FromName("AoiCrystal"), MaterialShape.Sphere, new vec3(1))); Inventory.AddItem(new MaterialItem(TerrainResource.FromName("FireRock"), MaterialShape.Cube, new vec3(1))); Inventory.AddItem(new MaterialItem(TerrainResource.FromName("AlienRock"), MaterialShape.Cylinder, new vec3(1))); } else // Load inventory { InventorySave save = JsonConvert.DeserializeObject <InventorySave>(File.ReadAllText(UpvoidMiner.SavePathInventory)); Dictionary <long, Item> id2item = new Dictionary <long, Item>(); foreach (var item in save.toolItems) { id2item.Add(item.Id, new ToolItem(item.Type, item.StackSize)); Inventory.AddItem(id2item[item.Id]); } foreach (var item in save.materialItems) { id2item.Add(item.Id, new MaterialItem(TerrainResource.FromName(item.Resource), item.Shape, new vec3(item.SizeX, item.SizeY, item.SizeZ), item.StackSize)); Inventory.AddItem(id2item[item.Id]); } foreach (var item in save.resourceItems) { id2item.Add(item.Id, new ResourceItem(TerrainResource.FromName(item.Resource), item.Volume)); Inventory.AddItem(id2item[item.Id]); } Inventory.ClearQuickAccess(); for (int i = 0; i < Inventory.QuickaccessSlots; ++i) { if (id2item.ContainsKey(save.quickAccess[i])) { Inventory.SetQuickAccess(id2item[save.quickAccess[i]], i); } } Inventory.Select(save.currentQuickAccess); } Gui.OnUpdate(); }
/// <summary> /// Adds a resource to the global dictionary. /// </summary> private static void addResource(TerrainResource res) { indexToResource.Add(res.Index, res); nameToResource.Add(res.Name, res); }
public MaterialCraftingRule(TerrainResource material, MaterialShape shape, vec3 size) { Material = material; Shape = shape; Size = size; }