public void PutObject(ServerAction response) { bool success = false; if (inventory.ContainsKey(response.objectId)) { foreach (SimObj rso in VisibleSimObjs(response.forceVisible)) { if (rso.IsReceptacle && (rso.UniqueID == response.receptacleObjectId || rso.Type == response.ReceptableSimObjType())) { SimObj so = removeObjectInventory(response.objectId); if ((!IsOpenable(rso) || IsOpen(rso)) && ((response.forceVisible && SimUtil.AddItemToReceptacle(so, rso.Receptacle)) || SimUtil.AddItemToVisibleReceptacle(so, rso.Receptacle, m_Camera))) { success = true; } else { addObjectInventory(so); } break; } } } actionFinished(success); }
private void PutObject_ray(string item) { int x = Screen.width / 2; int y = Screen.height / 2; Ray ray = m_Camera.GetComponent <Camera>().ScreenPointToRay(new Vector3(x, y)); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit)) { //check if the receptacle is in visible range bool inrange = false; //check if the object we are trying to put something in is in visible range foreach (SimObj o in currentVisibleObjects) { //check if the ID of the object we are looking at is in array of visible objects if (hit.transform.GetComponent <SimObj>().UniqueID == o.UniqueID) { inrange = true; } } if (inrange) { //if (SimUtil.AddItemToVisibleReceptacle(inventory[current_Object_In_Inventory], hit.transform.GetComponent<Receptacle>(), gameObject.GetComponentInChildren<Camera>()) == false) if (SimUtil.AddItemToReceptacle(inventory[current_Object_In_Inventory], hit.transform.GetComponent <Receptacle>()) == false) { Debug.Log("There's no space for that!"); } else { removeObjectInventory(current_Object_In_Inventory); } } if (!inrange) { Debug.Log("It's too far away to put stuff in"); } } }
void TryToAddItem(UnityEngine.Object item, Transform pivot, Receptacle r) { if (pivot.childCount > 0) { Debug.Log("Pivot is already filled, not adding item"); return; } if (EditorUtility.IsPersistent(r)) { Debug.Log("Can't add items to a recepticle when not in scene, not adding"); return; } GameObject itemGo = null; try { itemGo = (GameObject)item; } catch (Exception e) { Debug.Log(e); return; } if (item == null) { Debug.Log("Item was null, not adding"); return; } if (itemGo == r.gameObject || itemGo.transform.IsChildOf(r.transform)) { Debug.Log("Can't add item to itself, not adding"); return; } if (EditorUtility.IsPersistent(itemGo)) { // instantiate the object Debug.Log("Instantiating " + itemGo.name + " and placing in the scene"); itemGo = GameObject.Instantiate(itemGo) as GameObject; return; } SimObj o = itemGo.GetComponent <SimObj>(); if (o == null) { Debug.Log("Item was not a SimObj, not adding"); return; } for (int i = 0; i < r.Pivots.Length; i++) { if (r.Pivots[i].childCount > 0) { foreach (Transform c in r.Pivots[i]) { if (c == itemGo.transform) { Debug.Log("Item is already in a pivot, not adding"); return; } } } } // if we've made it this far the item is OK // parent it under the receptacle SimUtil.AddItemToReceptacle(o, r); // don't scale the item }
public void RandomInitialize(ServerAction response) { this.excludeObjectIds = response.excludeObjectIds; System.Random rnd = new System.Random(response.randomSeed); SimObj[] simObjects = GameObject.FindObjectsOfType(typeof(SimObj)) as SimObj[]; int pickupableCount = 0; for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; if (IsPickupable(so)) { pickupableCount++; SimUtil.TakeItem(so); } if (IsOpenable(so) && response.randomizeOpen) { if (rnd.NextDouble() < 0.5) { openSimObj(so); } else { closeSimObj(so); } } } //shuffle objects for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; int randomIndex = rnd.Next(i, simObjects.Length); simObjects [i] = simObjects [randomIndex]; simObjects [randomIndex] = so; } Dictionary <SimObjType, HashSet <SimObjType> > receptacleObjects = new Dictionary <SimObjType, HashSet <SimObjType> > (); foreach (ReceptacleObjectList rol in response.receptacleObjects) { HashSet <SimObjType> objectTypes = new HashSet <SimObjType> (); SimObjType receptacleType = (SimObjType)Enum.Parse(typeof(SimObjType), rol.receptacleObjectType); foreach (string itemObjectType in rol.itemObjectTypes) { objectTypes.Add((SimObjType)Enum.Parse(typeof(SimObjType), itemObjectType)); } receptacleObjects.Add(receptacleType, objectTypes); } bool[] consumedObjects = new bool[simObjects.Length]; int randomTries = 0; HashSet <SimObjType> seenObjTypes = new HashSet <SimObjType> (); while (pickupableCount > 0) { if (randomTries > 5) { Debug.Log("Pickupable count still at, but couldn't place all objects: " + pickupableCount); break; } randomTries++; foreach (SimObj so in simObjects) { if (so.IsReceptacle && !excludeObject(so)) { int totalRandomObjects = rnd.Next(1, so.Receptacle.Pivots.Length + 1); for (int i = 0; i < totalRandomObjects; i++) { for (int j = 0; j < simObjects.Length; j++) { if (Array.Exists(response.excludeReceptacleObjectPairs, e => e.objectId == simObjects [j].UniqueID && e.receptacleObjectId == so.UniqueID)) { Debug.Log("skipping object id receptacle id pair, " + simObjects [j].UniqueID + " " + so.UniqueID); continue; } if (!consumedObjects[j] && IsPickupable(simObjects[j]) && receptacleObjects[so.Type].Contains(simObjects[j].Type) && (!response.uniquePickupableObjectTypes || !seenObjTypes.Contains(simObjects[j].Type)) && SimUtil.AddItemToReceptacle(simObjects [j], so.Receptacle)) { consumedObjects [j] = true; seenObjTypes.Add(simObjects [j].Type); pickupableCount--; break; } } } } } } }
public void RandomInitialize(ServerAction response) { bool success = true; this.excludeObjectIds = response.excludeObjectIds; Dictionary <SimObjType, HashSet <SimObjType> > receptacleObjects = new Dictionary <SimObjType, HashSet <SimObjType> > (); HashSet <SimObjType> pickupable = new HashSet <SimObjType> (); foreach (ReceptacleObjectList rol in response.receptacleObjects) { HashSet <SimObjType> objectTypes = new HashSet <SimObjType> (); SimObjType receptacleType = (SimObjType)Enum.Parse(typeof(SimObjType), rol.receptacleObjectType); foreach (string itemObjectType in rol.itemObjectTypes) { objectTypes.Add((SimObjType)Enum.Parse(typeof(SimObjType), itemObjectType)); pickupable.Add((SimObjType)Enum.Parse(typeof(SimObjType), itemObjectType)); } receptacleObjects.Add(receptacleType, objectTypes); } Debug.Log("random seed:Z " + response.randomSeed); System.Random rnd = new System.Random(response.randomSeed); SimObj[] simObjects = GameObject.FindObjectsOfType(typeof(SimObj)) as SimObj[]; // Sorting to ensure that our randomization is deterministic when using a seed // without sorting, there is no guarantee how the objects will get returned from from FindObjectsOfType // so the shuffle becomes non-deterministic Array.Sort(simObjects, delegate(SimObj a, SimObj b) { return(a.UniqueID.CompareTo(b.UniqueID)); }); int pickupableCount = 0; for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; if (IsPickupable(so)) { pickupableCount++; SimUtil.TakeItem(so); } if (IsOpenable(so) && response.randomizeOpen) { if (rnd.NextDouble() < 0.5) { openSimObj(so); } else { closeSimObj(so); } } } //shuffle objects rnd = new System.Random(response.randomSeed); for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; int randomIndex = rnd.Next(i, simObjects.Length); simObjects [i] = simObjects [randomIndex]; simObjects [randomIndex] = so; } rnd = new System.Random(response.randomSeed); List <SimObj> simObjectsFiltered = new List <SimObj> (); for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; if (IsPickupable(so) && pickupable.Contains(so.Type)) { double val = rnd.NextDouble(); if (val > response.removeProb) { // Keep the item int numRepeats = 1; if (response.maxNumRepeats > 1) { numRepeats = rnd.Next(1, response.maxNumRepeats); } for (int j = 0; j < numRepeats; j++) { // Add a copy of the item. SimObj copy = Instantiate(so); copy.name += "" + j; copy.UniqueID = so.UniqueID + "_copy_" + j; simObjectsFiltered.Add(copy); } } else { pickupableCount--; } } else { simObjectsFiltered.Add(simObjects [i]); } } simObjects = simObjectsFiltered.ToArray(); int randomTries = 0; HashSet <SimObjType> seenObjTypes = new HashSet <SimObjType> (); rnd = new System.Random(response.randomSeed); while (pickupableCount > 0) { if (randomTries > 5) { Debug.Log("Pickupable count still at, but couldn't place all objects: " + pickupableCount); success = false; break; } randomTries++; int[] randomOrder = new int[simObjects.Length]; for (int rr = simObjects.Length - 1; rr >= 0; rr--) { int randomLoc = simObjects.Length - 1 - rnd.Next(0, (simObjects.Length - rr)); randomOrder [rr] = randomOrder [randomLoc]; randomOrder [randomLoc] = rr; } for (int ss = 0; ss < simObjects.Length; ss++) { int j = randomOrder [ss]; foreach (SimObj so in simObjects) { if (so.IsReceptacle && !excludeObject(so)) { if (response.excludeReceptacleObjectPairs != null && Array.Exists(response.excludeReceptacleObjectPairs, e => e.objectId == simObjects [j].UniqueID && e.receptacleObjectId == so.UniqueID)) { //Debug.Log ("skipping object id receptacle id pair, " + simObjects [j].UniqueID + " " + so.UniqueID); continue; } if (IsPickupable(simObjects [j]) && receptacleObjects.ContainsKey(so.Type) && receptacleObjects [so.Type].Contains(simObjects [j].Type) && (!response.uniquePickupableObjectTypes || !seenObjTypes.Contains(simObjects [j].Type)) && SimUtil.AddItemToReceptacle(simObjects [j], so.Receptacle)) { //Debug.Log ("Put " + simObjects [j].Type + " " + simObjects[j].name + " in " + so.Type); seenObjTypes.Add(simObjects [j].Type); pickupableCount--; break; } } } } } if (response.randomizeObjectAppearance) { // Use a random texture for each object individually. rnd = new System.Random(response.randomSeed); for (int i = 0; i < simObjects.Length; i++) { SimObj so = simObjects [i]; if (so.gameObject.activeSelf) { Randomizer randomizer = (so.gameObject.GetComponentInChildren <Randomizer> () as Randomizer); if (randomizer != null) { randomizer.Randomize(rnd.Next(0, 2147483647)); } } } } if (imageSynthesis != null) { imageSynthesis.OnSceneChange(); } actionFinished(success); }