//-----------------------STATIC METHODS------------------

        public static InstalledItem CreateInstance(World world, InstalledItem proto, Tile tile, bool spawn = false)
        {
            if (!proto.funcPositionValid(world, tile.world_x, tile.world_y))
            {
                return(null);
            }
            //Debug.Log("InstalledItem.CreateInstance");
            InstalledItem o = new InstalledItem(world, proto, spawn);


            o.tile = tile;

            if (!o.tile.placeInstalledItem(o))
            {
                return(null);
            }


            if (o.linksToNeighbour)
            {
                //this type needs to tell it's neighbours about its creation

                world.informNeighbours(o);
            }

            if (o.luaOnCreate != null)
            {
                World.CallLuaFunction(o.luaOnCreate, o);
            }
            return(o);
        }
Beispiel #2
0
        //-------------------------STATIC FUNCTIONS-----------------
        public static void CalculateRooms(InstalledItem source)
        {
            return;
            //Tile tile = source.tile;
            //World world = tile.world;
            //Room oldRoom = tile.room;

            //closedSet.Clear();
            //tileQu.Clear();

            //ActualFloodFill(world, tile, tile.North, oldRoom);
            //ActualFloodFill(world, tile, tile.East, oldRoom);
            //ActualFloodFill(world, tile, tile.South, oldRoom);
            //ActualFloodFill(world, tile, tile.West, oldRoom);

            //if (oldRoom != world.outside) {
            //  tile.room = world.outside;
            //  oldRoom.tiles.Remove(tile);
            //}



            //if (oldRoom != world.outside) {
            //  if (oldRoom.tiles.Count == 0) {
            //    world.DeleteRoom(tile.room);
            //  }


            //}
        }
        private InstalledItem(World world, InstalledItem proto, bool spawn)
        {
            //this.updateActions = new List<Action>();
            //this.jobs = new List<Job>();
            //this.prototypeId = proto.prototypeId;
            this.type           = proto.type; // nice field name, doofus.
            this.niceName       = proto.niceName;
            this.movementFactor = proto.movementFactor;
            this.width          = proto.width;
            this.height         = proto.height;

            this.linksToNeighbour = proto.linksToNeighbour;
            this.spriteName       = proto.spriteName;
            this.setLinkedSpriteNames(proto);
            this.build          = proto.build;
            this.trash          = proto.trash;
            this.prototype      = proto;
            this.randomRotation = proto.randomRotation;
            this.roomEnclosure  = proto.roomEnclosure;
            this.recipeName     = proto.recipeName;
            if (proto.updateActions != null)
            {
                this.updateActions = proto.updateActions;
            }
            this.itemParameters     = new ItemParameters(proto.itemParameters);//.GetItems();
            this.enterRequestedFunc = proto.enterRequestedFunc;
            this.neighbourTypes     = proto.neighbourTypes;
            this.inventory          = new Inventory(world, proto.inventorySlots, INVENTORY_TYPE.INSTALLED_ITEM, this);
            this.workTileOffset     = new Vector2(proto.workTileOffset.x, proto.workTileOffset.y);
            //set the countdown timer
            this.updateActionCountDownMax   = proto.updateActionCountDownMax;
            this.updateActionCountDownRange = proto.updateActionCountDownRange;
            this.updateActionCountDown      = proto.updateActionCountDownMax;
            this.workRecipeName             = proto.workRecipeName;
            this.isWorkstation          = proto.isWorkstation;
            this.canSpawnOnTileTypeList = proto.canSpawnOnTileTypeList;
            this.growthStages           = proto.growthStages;
            this.growthStage            = proto.growthStage;
            this.deconRecipeName        = proto.deconRecipeName;
            this.xClearance             = proto.xClearance;
            this.yClearance             = proto.yClearance;
            this.luaOnCreate            = proto.luaOnCreate;
            this.workCondition          = proto.workCondition;
            this.editOnClick            = proto.editOnClick;
            this.availableRecipes       = proto.availableRecipes;
            this.nextWorkRecipeName     = this.workRecipeName;
            this.onRemoved       = proto.onRemoved;
            this.canChangeRecipe = proto.canChangeRecipe;
            this.active          = false;
            this.powerGenerated  = proto.powerGenerated;
            this.powerUsed       = proto.powerUsed;
            if (spawn && this.growthStages.Count > 0)
            {
                this.growthStage = UnityEngine.Random.Range(0, itemParameters.GetInt("maxGrowthStage"));
            }
        }
Beispiel #4
0
        public void RegisterStockpile(InstalledItem item)
        {
            Inventory inv = item.tile.InventoryGetRef();

            if (!stockpiles.Contains(inv))
            {
                stockpiles.Add(inv);
            }
            stockpilesList.Add(inv);
        }
Beispiel #5
0
        public void UnregisterStockpile(InstalledItem item)
        {
            Inventory inv = item.tile.InventoryGetRef();

            if (stockpiles.Contains(inv))
            {
                stockpiles.Remove(inv);
            }
            stockpilesList.Remove(inv);
        }
        public static Tile.CAN_ENTER CallEnterRequested(string func, InstalledItem item)
        {
            LuaFunction luaFunc = World.current.lua[func] as LuaFunction;

            Tile.CAN_ENTER enter = Tile.CAN_ENTER.YES;
            if (luaFunc != null)
            {
                enter = (Tile.CAN_ENTER)luaFunc.Call(item)[0];
            }

            return(enter);
        }
        public static void LoadFromFile()
        {
            //LoadLua();
            prototypes      = new Dictionary <string, InstalledItem>();
            trashPrototypes = new List <InstalledItem>();
            //prototypesById = new Dictionary<int, string>();
            prototypeRecipes = new Dictionary <string, string>();


            int    unnamedCounter = 0;
            string path           = Application.streamingAssetsPath;

            path = System.IO.Path.Combine(path, "data", "InstalledItems");

            string[] files = Directory.GetFiles(path, "*.json");

            foreach (string file in files)
            {
                string  json = File.ReadAllText(file);
                JObject installedItemJson = JObject.Parse(json);

                //JArray installedItemsArray = (JArray)jo["InstalledItems"];


                //foreach (JObject installedItemJson in installedItemsArray) {

                string name     = Funcs.jsonGetString(installedItemJson["name"], "unnamed_" + unnamedCounter);
                string niceName = Funcs.jsonGetString(installedItemJson["niceName"], name);
                unnamedCounter += 1;
                string sprite   = Funcs.jsonGetString(installedItemJson["sprite"], null);
                float  movement = Funcs.jsonGetFloat(installedItemJson["movementFactor"], 1);
                bool   trash    = Funcs.jsonGetBool(installedItemJson["trash"], false);
                bool   build    = Funcs.jsonGetBool(installedItemJson["build"], false);
                bool   rotate   = Funcs.jsonGetBool(installedItemJson["randomRotation"], false);
                int    w        = Funcs.jsonGetInt(installedItemJson["width"], 1);
                int    h        = Funcs.jsonGetInt(installedItemJson["height"], 1);
                //int id = Funcs.jsonGetInt(installedItemJson["id"], -1);
                bool   enclosesRoom        = Funcs.jsonGetBool(installedItemJson["enclosesRoom"], false);
                string recipeName          = Funcs.jsonGetString(installedItemJson["recipe"], null);
                bool   linked              = Funcs.jsonGetBool(installedItemJson["linked"], false);
                int    inventorySlots      = Funcs.jsonGetInt(installedItemJson["inventorySlots"], 0);
                int    workTileOffsetX     = Funcs.jsonGetInt(installedItemJson["workTileOffsetX"], 0);
                int    workTileOffsetY     = Funcs.jsonGetInt(installedItemJson["workTileOffsetY"], 0);
                float  updateActionCD      = Funcs.jsonGetFloat(installedItemJson["updateActionCountDown"], 0);
                float  updateActionCDRange = Funcs.jsonGetFloat(installedItemJson["updateActionCountDownRange"], 0);
                bool   IsWorkstation       = Funcs.jsonGetBool(installedItemJson["workstation"], false);
                string workRecipeName      = Funcs.jsonGetString(installedItemJson["workRecipe"], null);
                bool   paletteSwap         = Funcs.jsonGetBool(installedItemJson["paletteSwap"], false);
                float  trashSpawnChance    = Funcs.jsonGetFloat(installedItemJson["trashSpawnChance"], 0);
                string deconRecipeName     = Funcs.jsonGetString(installedItemJson["deconstructRecipe"], null);
                int    yClearance          = Funcs.jsonGetInt(installedItemJson["y_clearance"], 0);
                int    xClearance          = Funcs.jsonGetInt(installedItemJson["x_clearance"], 0);
                string workCondition       = Funcs.jsonGetString(installedItemJson["workCondition"], null);
                bool   editOnClick         = Funcs.jsonGetBool(installedItemJson["editOnClick"], false);
                bool   canChangeRecipe     = Funcs.jsonGetBool(installedItemJson["canChangeRecipe"], false);

                //string onPlaced = Funcs.jsonGetString(installedItemJson["onPlaced"], null);
                string onRemoved      = Funcs.jsonGetString(installedItemJson["onRemoved"], null);
                float  powerGenerated = Funcs.jsonGetFloat(installedItemJson["powerGenerated"], 0);
                float  powerUsed      = Funcs.jsonGetFloat(installedItemJson["powerUsed"], 0);



                string luaOnCreate = Funcs.jsonGetString(installedItemJson["onCreate"], null);

                JArray        jsonCanSpawnOn = Funcs.jsonGetArray(installedItemJson, "canSpawnOn");
                List <string> canSpawnOn     = new List <string>();
                if (jsonCanSpawnOn != null)
                {
                    foreach (string tempName in jsonCanSpawnOn)
                    {
                        canSpawnOn.Add(tempName);
                    }
                }


                List <string> sprites          = new List <string>();
                JArray        spritesJsonArray = Funcs.jsonGetArray(installedItemJson, "sprites");
                if (spritesJsonArray != null)
                {
                    foreach (string tempSpriteName in spritesJsonArray)
                    {
                        sprites.Add(tempSpriteName);
                    }
                }

                List <string> availableRecipes = new List <string>();
                JArray        otherRecipes     = Funcs.jsonGetArray(installedItemJson, "availableWorkRecipes");
                if (otherRecipes != null)
                {
                    foreach (string tempRecipeName in otherRecipes)
                    {
                        availableRecipes.Add(tempRecipeName);
                    }
                }
                if (workRecipeName != null && !availableRecipes.Contains(workRecipeName))
                {
                    availableRecipes.Add(workRecipeName);
                }

                List <string> neighbourTypeList = new List <string>();
                if (linked)
                {
                    JArray neighbourTypes = Funcs.jsonGetArray(installedItemJson, "neighbourTypes");//(JArray)installedItemJson["neighbourTypes"];

                    if (neighbourTypes != null)
                    {
                        foreach (string s in neighbourTypes)
                        {
                            neighbourTypeList.Add(s);
                            Debug.Log("added [" + s + "]");
                        }
                    }
                }
                if (!neighbourTypeList.Contains(name))
                {
                    neighbourTypeList.Add(name);
                }



                InstalledItem proto = new InstalledItem();//CreateOneInstalledItemPrototype(name, niceName, sprite, movement, w, h, linked, build, trash, rotate, id, enclosesRoom, recipeName);
                proto.neighbourTypes = neighbourTypeList;
                //proto.roomEnclosure = enclosesRoom;
                proto.niceName = niceName;
                //proto.prototypeId = id;
                proto.type                       = name;
                proto.spriteName                 = sprite;
                proto.movementFactor             = movement;
                proto.width                      = w;
                proto.height                     = h;
                proto.linksToNeighbour           = linked;
                proto.funcPositionValid          = proto.isPositionValid;
                proto.build                      = build;
                proto.trash                      = trash;
                proto.prototype                  = null;
                proto.randomRotation             = rotate;
                proto.roomEnclosure              = enclosesRoom;
                proto.recipeName                 = recipeName;
                proto.inventorySlots             = inventorySlots;
                proto.workTileOffset             = new Vector2(workTileOffsetX, workTileOffsetY);
                proto.updateActionCountDownMax   = updateActionCD;
                proto.updateActionCountDownRange = updateActionCDRange;
                proto.isWorkstation              = IsWorkstation;
                proto.workRecipeName             = workRecipeName;
                proto.paletteSwap                = paletteSwap;
                proto.trashSpawnChance           = trashSpawnChance;
                proto.canSpawnOnTileTypeList     = canSpawnOn;
                proto.deconRecipeName            = deconRecipeName;
                proto.xClearance                 = xClearance;
                proto.yClearance                 = yClearance;
                proto.luaOnCreate                = luaOnCreate;
                proto.workCondition              = workCondition;
                proto.editOnClick                = editOnClick;
                proto.availableRecipes           = availableRecipes;
                proto.canChangeRecipe            = canChangeRecipe;
                proto.powerGenerated             = powerGenerated;
                proto.powerUsed                  = powerUsed;



                proto.onRemoved = onRemoved;
                //proto.inventory = new Inventory(inventorySlots, INVENTORY_TYPE.INSTALLED_ITEM, proto);

                //Debug.Log(proto.ToString() + "\n" + workTileOffsetX + "," + workTileOffsetY);

                if (linked)
                {
                    string n_s    = Funcs.jsonGetString(installedItemJson["neighbour_s"], "");
                    string n_ns   = Funcs.jsonGetString(installedItemJson["neighbour_ns"], "");
                    string n_nsw  = Funcs.jsonGetString(installedItemJson["neighbour_nsw"], "");
                    string n_sw   = Funcs.jsonGetString(installedItemJson["neighbour_sw"], "");
                    string n_nesw = Funcs.jsonGetString(installedItemJson["neighbour_nesw"], "");
                    proto.setLinkedSpriteNames(n_ns, n_nsw, n_s, n_sw, n_nesw);
                }

                if (proto.paletteSwap)
                {
                    List <string> randoSprites = NYDISpriteManager.Instance.GetSpritesStarting(proto.spriteName);
                    //foreach (string s in randoSprites) {
                    //  Debug.Log(s);
                    //}
                    proto.setRandomSprites(randoSprites);
                }
                else if (sprites.Count > 0)
                {
                    proto.setRandomSprites(sprites);
                    //Debug.Log("proto " + proto.type + " has " + proto.randomSprites.Count + " random sprites");
                }

                JArray growth = Funcs.jsonGetArray(installedItemJson, "growth");
                proto.growthStages = new Dictionary <int, Growth>();

                if (growth != null)
                {
                    foreach (JObject growthStage in growth)
                    {
                        int    stage   = Funcs.jsonGetInt(growthStage["stage"], -1);
                        int    length  = Funcs.jsonGetInt(growthStage["length"], -1);
                        string gSprite = Funcs.jsonGetString(growthStage["sprite"], null);
                        string gdecon  = Funcs.jsonGetString(growthStage["deconstructRecipe"], deconRecipeName);

                        if (stage >= 0 && length > 0 && gSprite != null)
                        {
                            proto.growthStages[stage] = new Growth(stage, length, gSprite, gdecon);
                        }
                    }
                }
                if (proto.growthStages.Count > 0)
                {
                    proto.growthStage = 0;
                    proto.itemParameters.SetInt("maxGrowthStage", proto.growthStages.Values.Max(e => e.stage));
                }
                else
                {
                    proto.growthStage = -1;
                }


                JArray parameters = Funcs.jsonGetArray(installedItemJson, "parameters");
                if (parameters != null)
                {
                    foreach (JObject prm in parameters)
                    {
                        string prmName = Funcs.jsonGetString(prm["name"], null);
                        string prmType = Funcs.jsonGetString(prm["type"], null);

                        if (prmName != null && prmType != null)
                        {
                            switch (prmType)
                            {
                            case "float":
                                float prmValueFloat = Funcs.jsonGetFloat(prm["value"], 0);
                                proto.itemParameters.SetFloat(prmName, prmValueFloat);
                                break;

                            case "int":
                                int prmValueInt = Funcs.jsonGetInt(prm["value"], 0);
                                proto.itemParameters.SetInt(prmName, prmValueInt);
                                break;

                            case "string":
                                string prmValueString = Funcs.jsonGetString(prm["value"], "");
                                proto.itemParameters.Set(prmName, prmValueString);
                                break;

                            case "bool":
                                bool prmValueBool = Funcs.jsonGetBool(prm["value"], false);
                                proto.itemParameters.SetBool(prmName, prmValueBool);
                                break;
                            }
                        }
                    }
                }

                string updateActionName = Funcs.jsonGetString(installedItemJson["updateAction"], null);
                if (updateActionName != null)
                {
                    proto.updateActions = new List <string>();
                    proto.updateActions.Add(updateActionName);
                }

                string enterRequestionName = Funcs.jsonGetString(installedItemJson["onEnterRequested"], null);

                if (enterRequestionName != null)
                {
                    proto.enterRequestedFunc = enterRequestionName;
                }

                //if (name == "installed::door") {
                //  proto.enterRequested += InstalledItemActions.Door_EnterRequested;
                //}
                //  proto.updateActions += InstalledItemActions.Door_UpdateActions;


                //} else if (name == "installed::stockpile") {
                //  proto.updateActions += InstalledItemActions.Stockpile_UpdateActions;
                //} else if (IsWorkstation) {
                //  proto.updateActions += InstalledItemActions.Workstation_UpdateActions;
                //}
                prototypes.Add(proto.type, proto);
                //prototypesById.Add(proto.prototypeId, proto.type);
                prototypeRecipes.Add(proto.type, proto.recipeName);
                proto.recipe = GetRecipe(proto.type);
            }
            //InstalledItem proto = InstalledItemProto("installed::wall", "walls_none", 0, 1, 1,true);
            //

            foreach (string ss in prototypes.Keys)
            {
                InstalledItem item = prototypes[ss];
                if (item.trash)
                {
                    trashPrototypes.Add(item);
                }
            }
        }
 private void setLinkedSpriteNames(InstalledItem Item)
 {
     setLinkedSpriteNames(Item.sprite_ns, Item.sprite_nsw, Item.sprite_s, Item.sprite_sw, Item.sprite_nesw);
 }
        //public static void CallFunctions(string[] functions, InstalledItem item, float deltaTime) {

        //  foreach (string func in functions) {
        //    LuaFunction luaFunc = World.current.lua[func] as LuaFunction;
        //    if (luaFunc != null) {
        //      luaFunc.Call(item, deltaTime);

        //    }

        //    //World.current.lua.       CallFunction( (func, new[] { item, deltaTime });
        //  }

        //}

        //public static void Stockpile_UpdateActions(InstalledItem item, float deltaTime) {
        //  //Debug.Log("hello");


        //  if (item.tile.IsInventoryEmpty() && !item.tile.HasPendingJob) {

        //    string itemName = InventoryItem.GetRandomPrototype().type;

        //    Tile nearest = World.current.inventoryManager.GetNearest(item.tile, itemName, false);
        //    if (nearest == null) return;

        //    Job j = Job.MakeTileToTileJob(
        //      nearest,
        //        item.tile,
        //        //World.current.jobQueue.HaulToTileComplete,
        //        //World.current.jobQueue.HaulJobCancelled,
        //        itemName,
        //        InventoryItem.GetStackSize(itemName)
        //        );
        //    j.cbLuaRegisterJobComplete("JobQueue_HaulToTileComplete");
        //    j.cbLuaRegisterJobCancelled("JobQueue_HaulJobCancelled");
        //    j.cancelIfReturned = true;

        //    World.current.jobQueue.Push(j);

        //  } else if (!item.tile.IsInventoryEmpty() && !item.tile.HasPendingJob) {
        //    string itemName = item.tile.GetFirstInventoryItem();
        //    int qtyRequired = InventoryItem.GetStackSize(itemName) - item.tile.InventoryTotal(itemName);
        //    Tile nearest = World.current.inventoryManager.GetNearest(item.tile, itemName, false);
        //    if (nearest != null) {
        //      qtyRequired = Mathf.Min(qtyRequired, nearest.InventoryTotal(itemName));

        //      if (qtyRequired > 0) {
        //        Job j = Job.MakeTileToTileJob(
        //          nearest,
        //          item.tile,
        //          //World.current.jobQueue.HaulToTileComplete,
        //          //World.current.jobQueue.HaulJobCancelled,
        //          itemName,
        //          qtyRequired

        //            );
        //        j.cancelIfReturned = true;
        //        j.cbLuaRegisterJobComplete("JobQueue_HaulToTileComplete");
        //        j.cbLuaRegisterJobCancelled("JobQueue_HaulJobCancelled");

        //        World.current.jobQueue.Push(j);
        //      }
        //    }
        //  }
        //}

        //public static void MiningController_UpdateActions(InstalledItem item, float deltaTime) {
        //  Tile tile = item.GetWorkSpot();


        //  if (!tile.HasPendingJob) {
        //    Job j = new Job(
        //      tile,
        //      MiningController_JobComplete,
        //      MiningController_JobCancelled,
        //      JOB_TYPE.WORK_AT_STATION,
        //      1,
        //      "recipe::installed::mining_controller::work"

        //      );

        //    WorldController.Instance.world.jobQueue.Push(j);
        //  }

        //}



        public static void Workstation_OnDemand(InstalledItem item)
        {
            //Workstation_UpdateActions(item, 1);
            World.CallLuaFunction("OnUpdate_Workstation", item, 1);
        }