Example #1
0
 public void EnqueueFront(IdPixel pixel)
 {
     lock (queue)
     {
         queue.AddFirst(pixel);
     }
 }
Example #2
0
 public void Enqueue(IdPixel pixel)
 {
     lock (queue)
     {
         queue.AddLast(pixel);
     }
 }
Example #3
0
        private List <IdPixel> SelectOneColor()
        {
            List <IdPixel> toPlace = new List <IdPixel>(packetSize);

            lock (queue)
            {
                List <LinkedListNode <IdPixel> > toRemove = new List <LinkedListNode <IdPixel> >(packetSize);

                int firstColor = -1;
                for (LinkedListNode <IdPixel> node = queue.First; node != null; node = node.Next)
                {
                    IdPixel pixel = node.Value;
                    if (firstColor == -1)
                    {
                        firstColor = pixel.Color;
                    }
                    else if (firstColor != pixel.Color)
                    {
                        continue;
                    }
                    if (toPlace.Count >= packetSize)
                    {
                        break;
                    }
                    toPlace.Add(pixel);
                    toRemove.Add(node);
                }

                foreach (var node in toRemove)
                {
                    queue.Remove(node);
                }
            }
            return(toPlace);
        }
        private List <IdPixel> GenerateTaskQueue(UsefulTask task)
        {
            List <IdPixel> queue = new List <IdPixel>();

            for (int j = 0; j < task.Image.Height; j++)
            {
                for (int i = 0; i < task.Image.Width; i++)
                {
                    Color canvasPixel;
                    Color bordersPixel;

                    int canvasX = task.X + i;
                    int canvasY = task.Y + j;
                    if (canvasX < 0 || canvasY < 0 || canvasX >= canvas.Width || canvasY >= canvas.Height)
                    {
                        continue;
                    }
                    canvasPixel  = canvas.GetPixel(canvasX, canvasY);
                    bordersPixel = borders.GetPixel(canvasX, canvasY);

                    if (bordersPixel.R == 204 && bordersPixel.G == 204 && bordersPixel.B == 204)
                    {
                        continue;
                    }
                    var taskPixel = task.Image.GetPixel(i, j);
                    if (taskPixel.A == 0)
                    {
                        continue;
                    }
                    var reqPixel = GetAntidotColor(canvasX, canvasY);
                    if (canvasPixel == reqPixel)
                    {
                        continue;
                    }
                    if (!curCanvasInvPalette.ContainsKey(reqPixel))
                    {
                        continue;
                    }
                    IdPixel pixel = new IdPixel(curCanvasInvPalette[reqPixel], task.X + i, task.Y + j);
                    queue.Add(pixel);
                }
            }
            return(queue);
        }
        private void AddManual(List <IdPixel> queue, int userNumber, int totalUsers)
        {
            var pixels = guiData.ManualPixels;

            for (int i = userNumber; i < pixels.Count; i += totalUsers)
            {
                if (queue.Count >= maxQueueSize)
                {
                    break;
                }

                UsefulPixel reqPixel = pixels[i];
                if (iterCount - lastUpdateIterCount[reqPixel.X, reqPixel.Y] < pixelResendDelay) //avoid spamming the same place
                {
                    continue;
                }
                var canvasPixel = canvas.GetPixel(reqPixel.X, reqPixel.Y);
                if (canvasPixel.R == 204 && canvasPixel.G == 204 && canvasPixel.B == 204) //protected pixel
                {
                    continue;
                }
                if (canvasPixel == reqPixel.Color)
                {
                    continue;
                }
                if (reqPixel.Color.R == 255 && reqPixel.Color.G == 255 && reqPixel.Color.B == 255 && canvasPixel.A == 0)
                {
                    continue;
                }

                if (!curCanvasInvPalette.ContainsKey(reqPixel.Color))
                {
                    continue;
                }
                IdPixel pixel = new IdPixel(curCanvasInvPalette[reqPixel.Color], reqPixel.X, reqPixel.Y);
                queue.Add(pixel);
                lastUpdateIterCount[reqPixel.X, reqPixel.Y] = iterCount;
            }
        }
        private void AddTasks(List <IdPixel> queue, List <UsefulTask> completedTasksToFill)
        {
            Dictionary <int, List <IdPixel> > pixels = new Dictionary <int, List <IdPixel> >();

            foreach (UsefulTask task in guiData.Tasks)
            {
                bool completed = true;
                for (int j = 0; j < task.Image.Height; j++)
                {
                    for (int i = 0; i < task.Image.Width; i++)
                    {
                        foreach (KeyValuePair <int, List <IdPixel> > pair in pixels)
                        {
                            if (pair.Value.Count >= maxQueueSize)
                            {
                                goto loopend;
                            }
                        }

                        int canvasX = task.X + i;
                        int canvasY = task.Y + j;
                        if (canvasX < 0 || canvasY < 0 || canvasX >= canvas.Width || canvasY >= canvas.Height)
                        {
                            continue;
                        }
                        Color bordersPixel;
                        bordersPixel = borders.GetPixel(canvasX, canvasY);
                        if (bordersPixel.R == 204 && bordersPixel.G == 204 && bordersPixel.B == 204)
                        {
                            continue;
                        }

                        var reqPixel = task.Image.GetPixel(i, j);
                        if (!curCanvasInvPalette.ContainsKey(reqPixel))
                        {
                            continue;
                        }

                        var canvasPixel = canvas.GetPixel(canvasX, canvasY);
                        if (canvasPixel == reqPixel)
                        {
                            continue;
                        }
                        if (reqPixel.R == 255 && reqPixel.G == 255 && reqPixel.B == 255 && canvasPixel.A == 0)
                        {
                            continue;
                        }

                        completed = false;
                        if (iterCount - lastUpdateIterCount[canvasX, canvasY] < pixelResendDelay) //avoid spamming the same place
                        {
                            continue;
                        }

                        int     pxId  = curCanvasInvPalette[reqPixel];
                        IdPixel pixel = new IdPixel(pxId, canvasX, canvasY);
                        if (pixels.ContainsKey(pxId))
                        {
                            pixels[pxId].Add(pixel);
                        }
                        else
                        {
                            pixels.Add(pxId, new List <IdPixel>()
                            {
                                pixel
                            });
                        }
                    }
                }
                if (completed && !task.KeepRepairing)
                {
                    completedTasksToFill.Add(task);
                }
            }

loopend:
            while (queue.Count < maxQueueSize && pixels.Count > 0)
            {
                KeyValuePair <int, List <IdPixel> > maxPair = new KeyValuePair <int, List <IdPixel> >();
                int maxPx = -1;
                foreach (KeyValuePair <int, List <IdPixel> > pair in pixels)
                {
                    if (pair.Value.Count > maxPx)
                    {
                        maxPx   = pair.Value.Count;
                        maxPair = pair;
                    }
                }
                queue.AddRange(maxPair.Value);
                pixels.Remove(maxPair.Key);
            }
            if (queue.Count > maxQueueSize)
            {
                queue.RemoveRange(maxQueueSize, queue.Count - maxQueueSize);
            }
            foreach (IdPixel px in queue)
            {
                lastUpdateIterCount[px.X, px.Y] = iterCount;
            }
        }