private PuzzleView GetPuzzleView(Puzzle puzzle, GopUser user) { var view = new PuzzleView { Id = puzzle.Id, Altar = puzzle.Altar, NumberOfOrbs = puzzle.NumberOfOrbs, NumberOfPlayers = puzzle.NumberOfPlayers, Orbs = puzzle.Orbs, StartLocations = puzzle.StartLocations }; var submissions = puzzle.PuzzleSubmissions; if (submissions.Count == 0) { return(view); } view.Par = submissions.Min(s => s.Score); view.NumberOfSolvers = submissions.Where(s => s.Score == view.Par).GroupBy(s => new GopUser(s.UserId, s.IpAddress)).Count(); var userScores = submissions .Where(s => user.Equals(new GopUser(s.UserId, s.IpAddress))) .Select(s => s.Score) .ToList(); if (userScores.Count > 0) { view.Score = userScores.Min(); view.PuzzlePoints = GetPuzzlePoints(view.Par, view.Score); } return(view); }
private void EaseToNextLevel() { ILevel memory = level; PuzzleView reality = puzzleView; Sequence sequence = DOTween.Sequence(); if (memory != null) { // 淡出 Tween out1 = DOTween.To(() => memory.puzzle.localPosition, x => memory.puzzle.localPosition = x, memory.puzzle.localPosition + new Vector3(0, -20, 0), 1) .SetEase(Ease.InOutCubic); sequence.Join(out1); } // Next NextLevel(); // 淡入 Tween in1 = DOTween.To(() => level.puzzle.localPosition, x => level.puzzle.localPosition = x, level.puzzle.localPosition + new Vector3(0, 20, 0), 1) .SetEase(Ease.InOutCubic).From(); sequence.Join(in1); sequence.OnComplete(() => { if (reality != null) { GameObject.Destroy(reality.gameObject); } level.puzzle.touchEnable = true; }); }
public static PuzzleDvec puzzle_fill_dvec_from_file(PuzzleContext context, Bitmap bitmap) { PuzzleView view = puzzle_getview_from_gdimage(context, bitmap); if (context.puzzle_enable_autocrop) { puzzle_autocrop_view(context, ref view); } return(puzzle_fill_dvec(puzzle_fill_avglgls(context, view, context.puzzle_lambdas))); }
public static PuzzleView puzzle_getview_from_gdimage(PuzzleContext context, Bitmap bitmap) { PuzzleView view = new PuzzleView(); view.map = null; view.width = bitmap.Width; view.height = bitmap.Height; if (view.width > context.puzzle_max_width || view.height > context.puzzle_max_height) { throw new Exception(); } if (view.width == 0 || view.height == 0) { throw new Exception(); } int x1 = view.width - 1; int y1 = view.height - 1; if (view.width <= 0 || view.height <= 0) { throw new Exception(); } if (x1 > int.MaxValue || y1 > int.MaxValue) { throw new Exception(); } view.map = new byte[view.width * view.height]; Color pixel; int maptr = 0;// view.map; int x = (int)x1; int y; do { y = (int)y1; do { pixel = bitmap.GetPixel(x, y); view.map[maptr++] = (byte)((pixel.R * 77 + pixel.G * 151 + pixel.B * 28 + 128) / 256); } while (y-- != 0); } while (x-- != 0); return(view); }
public static PuzzleAvgLvls puzzle_fill_avglgls(PuzzleContext context, PuzzleView view, int lambdas) { double width = (double)view.width; double height = (double)view.height; PuzzleAvgLvls avglvls = new PuzzleAvgLvls(); avglvls.lambdas = lambdas; avglvls.lvls = new double[lambdas * lambdas]; double xshift = (width - width * lambdas / (lambdas + 1)) / 2; double yshift = (height - height * lambdas / (lambdas + 1)) / 2; int p = (int)Math.Round(Math.Min(width, height) / ((lambdas + 1) * context.puzzle_p_ratio)); if (p < PuzzleNative.PUZZLE_MIN_P) { p = PuzzleNative.PUZZLE_MIN_P; } double x, y; int xd, yd; int px, py; int lwidth, lheight; int lx = 0; int ly; do { ly = 0; do { x = xshift + (double)lx * (width - 1) / (lambdas + 1); y = yshift + (double)ly * (height - 1) / (lambdas + 1); lwidth = (int)Math.Round(xshift + (lx + 1) * (width - 1) / (lambdas + 1) - x); lheight = (int)Math.Round(yshift + (ly + 1) * (height - 1) / (lambdas + 1) - y); xd = p < lwidth ? (int)Math.Round(x + (lwidth - p) / 2.0) : (int)Math.Round(x); yd = p < lheight ? (int)Math.Round(y + (lheight - p) / 2.0) : (int)Math.Round(y); px = view.width - xd < p ? 1 : p; py = view.height - yd < p ? 1 : p; avglvls.lvls[avglvls.lambdas * ly + lx] = (px > 0 && py > 0) ? puzzle_get_avglvl(ref view, xd, yd, px, py) : 0; } while (++ly < lambdas); } while (++lx < lambdas); return(avglvls); }
public static double puzzle_softedgedlvl(ref PuzzleView view, int x, int y) { int xlimit = x + PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE; int ylimit = y + PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE; if (x >= view.width || y >= view.height || xlimit <= x || ylimit <= y) { throw new Exception(); } uint lvl = 0; int count = 0; int ax = x > PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE ? x - PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE : 0; int ay; do { if (ax >= view.width) { break; } ay = y > PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE ? y - PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE : 0; do { if (ay >= view.height) { break; } count++; lvl += (uint)view.map[view.width * ay + ax]; } while (ay++ < ylimit); } while (ax++ < xlimit); if (count <= 0) { return(0.0); } return(lvl / (double)count); }
private void NextLevel() { level = new Level(); level.MakePuzzle(PuzzleParams.GetPuzzleParamsByRank(rank++)); //level.MakePuzzle(new QuadValue[,] //{ // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Right | QuadValue.Down, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Down, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Right, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, //}); level.puzzle.localPosition = new Vector3(0, 0, Style.PuzzleDepth); level.puzzle.AddEventListener(PuzzleEvent.SolveChange, SolveChangeHandler); GameObject puzzlego = new GameObject("Puzzle"); puzzleView = puzzlego.AddComponent<PuzzleView>(); puzzleView.data = level.puzzle; }
public static double puzzle_get_avglvl(ref PuzzleView view, int x, int y, int width, int height) { if (width <= 0 || height <= 0) { throw new Exception(); } int xlimit = x + width - 1; int ylimit = y + height - 1; if (xlimit < x || ylimit < y) { throw new Exception(); } double lvl = 0.0; int ax = x; int ay; do { if (ax >= view.width) { throw new Exception(); } ay = y; do { if (ay >= view.height) { throw new Exception(); } lvl += puzzle_softedgedlvl(ref view, ax, ay); } while (ay++ < ylimit); } while (ax++ < xlimit); return(lvl / (double)(width * height)); }
private void NextLevel() { level = new Level(); level.MakePuzzle(PuzzleParams.GetPuzzleParamsByRank(rank++)); //level.MakePuzzle(new QuadValue[,] //{ // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Right | QuadValue.Down, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Down, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Right, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, // { QuadValue.Back, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front, QuadValue.Front }, //}); level.puzzle.localPosition = new Vector3(0, 0, Style.PuzzleDepth); level.puzzle.AddEventListener(PuzzleEvent.SolveChange, SolveChangeHandler); GameObject puzzlego = new GameObject("Puzzle"); puzzleView = puzzlego.AddComponent <PuzzleView>(); puzzleView.data = level.puzzle; }
public static void puzzle_autocrop_view(PuzzleContext context, ref PuzzleView view) { int cropx0, cropx1; int cropy0, cropy1; puzzle_autocrop_axis(context, view, out cropx0, out cropx1, view.width, view.height, (int)view.width, 1 - (int)(view.width * view.height)); puzzle_autocrop_axis(context, view, out cropy0, out cropy1, view.height, view.width, 1, 0); if (cropx0 > cropx1 || cropy0 > cropy1) { throw new Exception(); } int maptr = 0; int x; int y = cropy0; do { x = cropx0; do { view.map[maptr++] = view.map[view.width * y + x]; } while (x++ != cropx1); } while (y++ != cropy1); view.width = cropx1 - cropx0 + 1; view.height = cropy1 - cropy0 + 1; byte[] newArr = new byte[view.width * view.height]; Array.Copy(view.map, newArr, newArr.Length); view.map = newArr; if (view.width <= 0 || view.height <= 0) { throw new Exception(); } }
public static void puzzle_autocrop_axis(PuzzleContext context, PuzzleView view, out int crop0, out int crop1, int axisn, int axiso, int omaptrinc, int nmaptrinc) { int chunk_n1 = axisn - 1; int chunk_o1 = axiso - 1; crop0 = 0; crop1 = chunk_n1; if (axisn < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING || axiso < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING) { return; } double[] chunk_contrasts = new double[chunk_n1 + 1]; if (axisn >= int.MaxValue || axiso >= int.MaxValue) { throw new Exception(); } double chunk_contrast = 0.0, total_contrast = 0.0, barrier_contrast; int chunk_n = chunk_n1; byte level = 0; byte previous_level = 0; int chunk_o; int maptr = 0; do { chunk_contrast = 0.0; chunk_o = chunk_o1; do { level = view.map[maptr]; if (previous_level > level) { chunk_contrast += previous_level - level; } else { chunk_contrast += level - previous_level; } maptr += omaptrinc; } while (chunk_o-- != 0); chunk_contrasts[chunk_n] = chunk_contrast; total_contrast += chunk_contrast; maptr += nmaptrinc; } while (chunk_n-- != 0); barrier_contrast = total_contrast * context.puzzle_contrast_barrier_for_cropping; total_contrast = 0.0; crop0 = 0; do { total_contrast += chunk_contrasts[crop0]; if (total_contrast >= barrier_contrast) { break; } } while (crop0++ < chunk_n1); total_contrast = 0.0; crop1 = chunk_n1; do { total_contrast += chunk_contrasts[crop1]; if (total_contrast >= barrier_contrast) { break; } } while (crop1-- > 0); chunk_contrasts = null; if (crop0 > chunk_n1 || crop1 > chunk_n1) { throw new Exception(); } int max_crop = (int)Math.Round(chunk_n1 * context.puzzle_max_cropping_ratio); if (max_crop > chunk_n1) { throw new Exception(); } crop0 = Math.Min(crop0, max_crop); crop1 = Math.Max(crop1, chunk_n1 - max_crop); }
public static void puzzle_autocrop_axis(PuzzleContext context, PuzzleView view, out int crop0, out int crop1, int axisn, int axiso, int omaptrinc, int nmaptrinc) { int chunk_n1 = axisn - 1; int chunk_o1 = axiso - 1; crop0 = 0; crop1 = chunk_n1; if (axisn < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING || axiso < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING) return; double[] chunk_contrasts = new double[chunk_n1 + 1]; if (axisn >= int.MaxValue || axiso >= int.MaxValue) throw new Exception(); double chunk_contrast = 0.0, total_contrast = 0.0, barrier_contrast; int chunk_n = chunk_n1; byte level = 0; byte previous_level = 0; int chunk_o; int maptr = 0; do { chunk_contrast = 0.0; chunk_o = chunk_o1; do { level = view.map[maptr]; if (previous_level > level) chunk_contrast += previous_level - level; else chunk_contrast += level - previous_level; maptr += omaptrinc; } while (chunk_o-- != 0); chunk_contrasts[chunk_n] = chunk_contrast; total_contrast += chunk_contrast; maptr += nmaptrinc; } while (chunk_n-- != 0); barrier_contrast = total_contrast * context.puzzle_contrast_barrier_for_cropping; total_contrast = 0.0; crop0 = 0; do { total_contrast += chunk_contrasts[crop0]; if (total_contrast >= barrier_contrast) break; } while (crop0++ < chunk_n1); total_contrast = 0.0; crop1 = chunk_n1; do { total_contrast += chunk_contrasts[crop1]; if (total_contrast >= barrier_contrast) break; } while (crop1-- > 0); chunk_contrasts = null; if (crop0 > chunk_n1 || crop1 > chunk_n1) throw new Exception(); int max_crop = (int)Math.Round(chunk_n1 * context.puzzle_max_cropping_ratio); if (max_crop > chunk_n1) throw new Exception(); crop0 = Math.Min(crop0, max_crop); crop1 = Math.Max(crop1, chunk_n1 - max_crop); }
public static double puzzle_softedgedlvl(ref PuzzleView view, int x, int y) { int xlimit = x + PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE; int ylimit = y + PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE; if (x >= view.width || y >= view.height || xlimit <= x || ylimit <= y) throw new Exception(); uint lvl = 0; int count = 0; int ax = x > PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE ? x - PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE : 0; int ay; do { if (ax >= view.width) break; ay = y > PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE ? y - PuzzleNative.PUZZLE_PIXEL_FUZZ_SIZE : 0; do { if (ay >= view.height) break; count++; lvl += (uint)view.map[view.width * ay + ax]; } while (ay++ < ylimit); } while (ax++ < xlimit); if (count <= 0) return 0.0; return lvl / (double) count; }
public static double puzzle_get_avglvl(ref PuzzleView view, int x, int y, int width, int height) { if (width <= 0 || height <= 0) throw new Exception(); int xlimit = x + width - 1; int ylimit = y + height - 1; if (xlimit < x || ylimit < y) throw new Exception(); double lvl = 0.0; int ax = x; int ay; do { if (ax >= view.width) throw new Exception(); ay = y; do { if (ay >= view.height) throw new Exception(); lvl += puzzle_softedgedlvl(ref view, ax, ay); } while (ay++ < ylimit); } while (ax++ < xlimit); return lvl / (double)(width * height); }
public static PuzzleView puzzle_getview_from_gdimage(PuzzleContext context, Bitmap bitmap) { PuzzleView view = new PuzzleView(); view.map = null; view.width = bitmap.Width; view.height = bitmap.Height; if (view.width > context.puzzle_max_width || view.height > context.puzzle_max_height) throw new Exception(); if (view.width == 0 || view.height == 0) throw new Exception(); int x1 = view.width - 1; int y1 = view.height - 1; if (view.width <= 0 || view.height <= 0) throw new Exception(); if (x1 > int.MaxValue || y1 > int.MaxValue) throw new Exception(); view.map = new byte[view.width * view.height]; Color pixel; int maptr = 0;// view.map; int x = (int)x1; int y; do { y = (int)y1; do { pixel = bitmap.GetPixel(x, y); view.map[maptr++] = (byte)((pixel.R * 77 + pixel.G * 151 + pixel.B * 28 + 128) / 256); } while (y-- != 0); } while (x-- != 0); return view; }
public static PuzzleAvgLvls puzzle_fill_avglgls(PuzzleContext context, PuzzleView view, int lambdas) { double width = (double)view.width; double height = (double)view.height; PuzzleAvgLvls avglvls = new PuzzleAvgLvls(); avglvls.lambdas = lambdas; avglvls.lvls = new double[lambdas * lambdas]; double xshift = (width - width * lambdas / (lambdas + 1)) / 2; double yshift = (height - height * lambdas / (lambdas + 1)) / 2; int p = (int)Math.Round(Math.Min(width, height) / ((lambdas + 1) * context.puzzle_p_ratio)); if (p < PuzzleNative.PUZZLE_MIN_P) p = PuzzleNative.PUZZLE_MIN_P; double x, y; int xd, yd; int px, py; int lwidth, lheight; int lx = 0; int ly; do { ly = 0; do { x = xshift + (double) lx * (width - 1) / (lambdas + 1); y = yshift + (double) ly * (height - 1) / (lambdas + 1); lwidth = (int)Math.Round(xshift + (lx + 1) * (width - 1) / (lambdas + 1) - x); lheight = (int)Math.Round(yshift + (ly + 1) * (height - 1) / (lambdas + 1) - y); xd = p < lwidth ? (int)Math.Round(x + (lwidth - p) / 2.0) : (int)Math.Round(x); yd = p < lheight ? (int)Math.Round(y + (lheight - p) / 2.0) : (int)Math.Round(y); px = view.width - xd < p ? 1 : p; py = view.height - yd < p ? 1 : p; avglvls.lvls[avglvls.lambdas * ly + lx] = (px > 0 && py > 0) ? puzzle_get_avglvl(ref view, xd, yd, px, py) : 0; } while (++ly < lambdas); } while (++lx < lambdas); return avglvls; }
public static void puzzle_autocrop_view(PuzzleContext context, ref PuzzleView view) { int cropx0, cropx1; int cropy0, cropy1; puzzle_autocrop_axis(context, view, out cropx0, out cropx1, view.width, view.height, (int)view.width, 1 - (int)(view.width * view.height)); puzzle_autocrop_axis(context, view, out cropy0, out cropy1, view.height, view.width, 1, 0); if (cropx0 > cropx1 || cropy0 > cropy1) throw new Exception(); int maptr = 0; int x; int y = cropy0; do { x = cropx0; do { view.map[maptr++] = view.map[view.width * y + x]; } while (x++ != cropx1); } while (y++ != cropy1); view.width = cropx1 - cropx0 + 1; view.height = cropy1 - cropy0 + 1; byte[] newArr = new byte[view.width * view.height]; Array.Copy(view.map, newArr, newArr.Length); view.map = newArr; if (view.width <= 0 || view.height <= 0) throw new Exception(); }