/* warp.c generated by valac 0.35.2.5-825d5, the Vala compiler
 * generated from warp.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * Gnome Nibbles: Gnome Worm Game
 * Copyright (C) 2015 Iulian-Gabriel Radu <iulian.radu67@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* This is a fairly literal translation of the GPLv2+ original by*/
/* Sean MacIsaac, Ian Peters, Guillaume Béland.*/

#include <glib.h>
#include <glib-object.h>
#include <gee.h>


#define TYPE_WARP (warp_get_type ())
#define WARP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_WARP, Warp))
#define WARP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_WARP, WarpClass))
#define IS_WARP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_WARP))
#define IS_WARP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_WARP))
#define WARP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_WARP, WarpClass))

typedef struct _Warp Warp;
typedef struct _WarpClass WarpClass;
typedef struct _WarpPrivate WarpPrivate;

#define TYPE_WARP_MANAGER (warp_manager_get_type ())
#define WARP_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_WARP_MANAGER, WarpManager))
#define WARP_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_WARP_MANAGER, WarpManagerClass))
#define IS_WARP_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_WARP_MANAGER))
#define IS_WARP_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_WARP_MANAGER))
#define WARP_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_WARP_MANAGER, WarpManagerClass))

typedef struct _WarpManager WarpManager;
typedef struct _WarpManagerClass WarpManagerClass;
typedef struct _WarpManagerPrivate WarpManagerPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	WARP_MANAGER_WARP_ADDED_SIGNAL,
	WARP_MANAGER_LAST_SIGNAL
};
static guint warp_manager_signals[WARP_MANAGER_LAST_SIGNAL] = {0};

struct _Warp {
	GObject parent_instance;
	WarpPrivate * priv;
	gint x;
	gint y;
	gint wx;
	gint wy;
};

struct _WarpClass {
	GObjectClass parent_class;
};

struct _WarpManager {
	GObject parent_instance;
	WarpManagerPrivate * priv;
	GeeLinkedList* warps;
};

struct _WarpManagerClass {
	GObjectClass parent_class;
};


static gpointer warp_parent_class = NULL;
static gpointer warp_manager_parent_class = NULL;

GType warp_get_type (void) G_GNUC_CONST;
enum  {
	WARP_DUMMY_PROPERTY
};
Warp* warp_new (gint x, gint y, gint wx, gint wy);
Warp* warp_construct (GType object_type, gint x, gint y, gint wx, gint wy);
static void warp_finalize (GObject * obj);
GType warp_manager_get_type (void) G_GNUC_CONST;
enum  {
	WARP_MANAGER_DUMMY_PROPERTY
};
#define WARP_MANAGER_MAX_WARPS 200
WarpManager* warp_manager_new (void);
WarpManager* warp_manager_construct (GType object_type);
void warp_manager_add_warp (WarpManager* self, gint* board, int board_length1, int board_length2, gint x, gint y, gint wx, gint wy);
#define NIBBLES_GAME_WARPCHAR 'W'
Warp* warp_manager_get_warp (WarpManager* self, gint x, gint y);
static void warp_manager_finalize (GObject * obj);


Warp* warp_construct (GType object_type, gint x, gint y, gint wx, gint wy) {
	Warp * self = NULL;
	gint _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	self = (Warp*) g_object_new (object_type, NULL);
	_tmp0_ = x;
	self->x = _tmp0_;
	_tmp1_ = y;
	self->y = _tmp1_;
	_tmp2_ = wx;
	self->wx = _tmp2_;
	_tmp3_ = wy;
	self->wy = _tmp3_;
	return self;
}


Warp* warp_new (gint x, gint y, gint wx, gint wy) {
	return warp_construct (TYPE_WARP, x, y, wx, wy);
}


static void warp_class_init (WarpClass * klass) {
	warp_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = warp_finalize;
}


static void warp_instance_init (Warp * self) {
}


static void warp_finalize (GObject * obj) {
	Warp * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_WARP, Warp);
	G_OBJECT_CLASS (warp_parent_class)->finalize (obj);
}


GType warp_get_type (void) {
	static volatile gsize warp_type_id__volatile = 0;
	if (g_once_init_enter (&warp_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (WarpClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) warp_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Warp), 0, (GInstanceInitFunc) warp_instance_init, NULL };
		GType warp_type_id;
		warp_type_id = g_type_register_static (G_TYPE_OBJECT, "Warp", &g_define_type_info, 0);
		g_once_init_leave (&warp_type_id__volatile, warp_type_id);
	}
	return warp_type_id__volatile;
}


WarpManager* warp_manager_construct (GType object_type) {
	WarpManager * self = NULL;
	GeeLinkedList* _tmp0_;
	self = (WarpManager*) g_object_new (object_type, NULL);
	_tmp0_ = gee_linked_list_new (TYPE_WARP, (GBoxedCopyFunc) g_object_ref, g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->warps);
	self->warps = _tmp0_;
	return self;
}


WarpManager* warp_manager_new (void) {
	return warp_manager_construct (TYPE_WARP_MANAGER);
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


void warp_manager_add_warp (WarpManager* self, gint* board, int board_length1, int board_length2, gint x, gint y, gint wx, gint wy) {
	gboolean add;
	gint _tmp0_;
	g_return_if_fail (self != NULL);
	add = TRUE;
	_tmp0_ = x;
	if (_tmp0_ < 0) {
		GeeLinkedList* _tmp19_;
		gint _tmp20_;
		gint _tmp21_;
		GeeLinkedList* _tmp22_;
		gint _tmp23_;
		gint _tmp24_;
		gint _tmp25_;
		gint _tmp26_;
		Warp* _tmp27_;
		Warp* _tmp28_;
		{
			GeeLinkedList* _warp_list;
			GeeLinkedList* _tmp1_;
			GeeLinkedList* _tmp2_;
			gint _warp_size;
			GeeLinkedList* _tmp3_;
			gint _tmp4_;
			gint _tmp5_;
			gint _warp_index;
			_tmp1_ = self->warps;
			_tmp2_ = _g_object_ref0 (_tmp1_);
			_warp_list = _tmp2_;
			_tmp3_ = _warp_list;
			_tmp4_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp3_);
			_tmp5_ = _tmp4_;
			_warp_size = _tmp5_;
			_warp_index = -1;
			while (TRUE) {
				gint _tmp6_;
				gint _tmp7_;
				gint _tmp8_;
				Warp* warp;
				GeeLinkedList* _tmp9_;
				gint _tmp10_;
				gpointer _tmp11_;
				Warp* _tmp12_;
				gint _tmp13_;
				gint _tmp14_;
				_tmp6_ = _warp_index;
				_warp_index = _tmp6_ + 1;
				_tmp7_ = _warp_index;
				_tmp8_ = _warp_size;
				if (!(_tmp7_ < _tmp8_)) {
					break;
				}
				_tmp9_ = _warp_list;
				_tmp10_ = _warp_index;
				_tmp11_ = gee_abstract_list_get ((GeeAbstractList*) _tmp9_, _tmp10_);
				warp = (Warp*) _tmp11_;
				_tmp12_ = warp;
				_tmp13_ = _tmp12_->wx;
				_tmp14_ = x;
				if (_tmp13_ == _tmp14_) {
					Warp* _tmp15_;
					gint _tmp16_;
					Warp* _tmp17_;
					gint _tmp18_;
					_tmp15_ = warp;
					_tmp16_ = wx;
					_tmp15_->wx = _tmp16_;
					_tmp17_ = warp;
					_tmp18_ = wy;
					_tmp17_->wy = _tmp18_;
					_g_object_unref0 (warp);
					_g_object_unref0 (_warp_list);
					return;
				}
				_g_object_unref0 (warp);
			}
			_g_object_unref0 (_warp_list);
		}
		_tmp19_ = self->warps;
		_tmp20_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp19_);
		_tmp21_ = _tmp20_;
		if (_tmp21_ == WARP_MANAGER_MAX_WARPS) {
			return;
		}
		_tmp22_ = self->warps;
		_tmp23_ = x;
		_tmp24_ = y;
		_tmp25_ = wx;
		_tmp26_ = wy;
		_tmp27_ = warp_new (_tmp23_, _tmp24_, _tmp25_, _tmp26_);
		_tmp28_ = _tmp27_;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp22_, _tmp28_);
		_g_object_unref0 (_tmp28_);
	} else {
		gboolean _tmp48_;
		gint* _tmp60_;
		gint _tmp60__length1;
		gint _tmp60__length2;
		gint _tmp61_;
		gint _tmp62_;
		gint _tmp63_;
		gint* _tmp64_;
		gint _tmp64__length1;
		gint _tmp64__length2;
		gint _tmp65_;
		gint _tmp66_;
		gint _tmp67_;
		gint* _tmp68_;
		gint _tmp68__length1;
		gint _tmp68__length2;
		gint _tmp69_;
		gint _tmp70_;
		gint _tmp71_;
		gint* _tmp72_;
		gint _tmp72__length1;
		gint _tmp72__length2;
		gint _tmp73_;
		gint _tmp74_;
		gint _tmp75_;
		{
			GeeLinkedList* _warp_list;
			GeeLinkedList* _tmp29_;
			GeeLinkedList* _tmp30_;
			gint _warp_size;
			GeeLinkedList* _tmp31_;
			gint _tmp32_;
			gint _tmp33_;
			gint _warp_index;
			_tmp29_ = self->warps;
			_tmp30_ = _g_object_ref0 (_tmp29_);
			_warp_list = _tmp30_;
			_tmp31_ = _warp_list;
			_tmp32_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp31_);
			_tmp33_ = _tmp32_;
			_warp_size = _tmp33_;
			_warp_index = -1;
			while (TRUE) {
				gint _tmp34_;
				gint _tmp35_;
				gint _tmp36_;
				Warp* warp;
				GeeLinkedList* _tmp37_;
				gint _tmp38_;
				gpointer _tmp39_;
				Warp* _tmp40_;
				gint _tmp41_;
				gint _tmp42_;
				_tmp34_ = _warp_index;
				_warp_index = _tmp34_ + 1;
				_tmp35_ = _warp_index;
				_tmp36_ = _warp_size;
				if (!(_tmp35_ < _tmp36_)) {
					break;
				}
				_tmp37_ = _warp_list;
				_tmp38_ = _warp_index;
				_tmp39_ = gee_abstract_list_get ((GeeAbstractList*) _tmp37_, _tmp38_);
				warp = (Warp*) _tmp39_;
				_tmp40_ = warp;
				_tmp41_ = _tmp40_->x;
				_tmp42_ = wx;
				if (_tmp41_ == _tmp42_) {
					Warp* _tmp43_;
					gint _tmp44_;
					Warp* _tmp45_;
					gint _tmp46_;
					Warp* _tmp47_;
					_tmp43_ = warp;
					_tmp44_ = x;
					_tmp43_->x = _tmp44_;
					_tmp45_ = warp;
					_tmp46_ = y;
					_tmp45_->y = _tmp46_;
					add = FALSE;
					_tmp47_ = warp;
					g_signal_emit (self, warp_manager_signals[WARP_MANAGER_WARP_ADDED_SIGNAL], 0, _tmp47_);
				}
				_g_object_unref0 (warp);
			}
			_g_object_unref0 (_warp_list);
		}
		_tmp48_ = add;
		if (_tmp48_) {
			GeeLinkedList* _tmp49_;
			gint _tmp50_;
			gint _tmp51_;
			Warp* warp;
			gint _tmp52_;
			gint _tmp53_;
			gint _tmp54_;
			gint _tmp55_;
			Warp* _tmp56_;
			GeeLinkedList* _tmp57_;
			Warp* _tmp58_;
			Warp* _tmp59_;
			_tmp49_ = self->warps;
			_tmp50_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp49_);
			_tmp51_ = _tmp50_;
			if (_tmp51_ == WARP_MANAGER_MAX_WARPS) {
				return;
			}
			_tmp52_ = x;
			_tmp53_ = y;
			_tmp54_ = wx;
			_tmp55_ = wy;
			_tmp56_ = warp_new (_tmp52_, _tmp53_, _tmp54_, _tmp55_);
			warp = _tmp56_;
			_tmp57_ = self->warps;
			_tmp58_ = warp;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp57_, _tmp58_);
			_tmp59_ = warp;
			g_signal_emit (self, warp_manager_signals[WARP_MANAGER_WARP_ADDED_SIGNAL], 0, _tmp59_);
			_g_object_unref0 (warp);
		}
		_tmp60_ = board;
		_tmp60__length1 = board_length1;
		_tmp60__length2 = board_length2;
		_tmp61_ = x;
		_tmp62_ = y;
		_tmp60_[(_tmp61_ * _tmp60__length2) + _tmp62_] = (gint) NIBBLES_GAME_WARPCHAR;
		_tmp63_ = _tmp60_[(_tmp61_ * _tmp60__length2) + _tmp62_];
		_tmp64_ = board;
		_tmp64__length1 = board_length1;
		_tmp64__length2 = board_length2;
		_tmp65_ = x;
		_tmp66_ = y;
		_tmp64_[((_tmp65_ + 1) * _tmp64__length2) + _tmp66_] = (gint) NIBBLES_GAME_WARPCHAR;
		_tmp67_ = _tmp64_[((_tmp65_ + 1) * _tmp64__length2) + _tmp66_];
		_tmp68_ = board;
		_tmp68__length1 = board_length1;
		_tmp68__length2 = board_length2;
		_tmp69_ = x;
		_tmp70_ = y;
		_tmp68_[(_tmp69_ * _tmp68__length2) + (_tmp70_ + 1)] = (gint) NIBBLES_GAME_WARPCHAR;
		_tmp71_ = _tmp68_[(_tmp69_ * _tmp68__length2) + (_tmp70_ + 1)];
		_tmp72_ = board;
		_tmp72__length1 = board_length1;
		_tmp72__length2 = board_length2;
		_tmp73_ = x;
		_tmp74_ = y;
		_tmp72_[((_tmp73_ + 1) * _tmp72__length2) + (_tmp74_ + 1)] = (gint) NIBBLES_GAME_WARPCHAR;
		_tmp75_ = _tmp72_[((_tmp73_ + 1) * _tmp72__length2) + (_tmp74_ + 1)];
	}
}


Warp* warp_manager_get_warp (WarpManager* self, gint x, gint y) {
	Warp* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	{
		GeeLinkedList* _warp_list;
		GeeLinkedList* _tmp0_;
		GeeLinkedList* _tmp1_;
		gint _warp_size;
		GeeLinkedList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _warp_index;
		_tmp0_ = self->warps;
		_tmp1_ = _g_object_ref0 (_tmp0_);
		_warp_list = _tmp1_;
		_tmp2_ = _warp_list;
		_tmp3_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_warp_size = _tmp4_;
		_warp_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			gint _tmp7_;
			Warp* warp;
			GeeLinkedList* _tmp8_;
			gint _tmp9_;
			gpointer _tmp10_;
			gboolean _tmp11_ = FALSE;
			gboolean _tmp12_ = FALSE;
			gboolean _tmp13_ = FALSE;
			gboolean _tmp14_ = FALSE;
			gint _tmp15_;
			Warp* _tmp16_;
			gint _tmp17_;
			_tmp5_ = _warp_index;
			_warp_index = _tmp5_ + 1;
			_tmp6_ = _warp_index;
			_tmp7_ = _warp_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _warp_list;
			_tmp9_ = _warp_index;
			_tmp10_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _tmp9_);
			warp = (Warp*) _tmp10_;
			_tmp15_ = x;
			_tmp16_ = warp;
			_tmp17_ = _tmp16_->x;
			if (_tmp15_ == _tmp17_) {
				gint _tmp18_;
				Warp* _tmp19_;
				gint _tmp20_;
				_tmp18_ = y;
				_tmp19_ = warp;
				_tmp20_ = _tmp19_->y;
				_tmp14_ = _tmp18_ == _tmp20_;
			} else {
				_tmp14_ = FALSE;
			}
			if (_tmp14_) {
				_tmp13_ = TRUE;
			} else {
				gboolean _tmp21_ = FALSE;
				gint _tmp22_;
				Warp* _tmp23_;
				gint _tmp24_;
				_tmp22_ = x;
				_tmp23_ = warp;
				_tmp24_ = _tmp23_->x;
				if (_tmp22_ == (_tmp24_ + 1)) {
					gint _tmp25_;
					Warp* _tmp26_;
					gint _tmp27_;
					_tmp25_ = y;
					_tmp26_ = warp;
					_tmp27_ = _tmp26_->y;
					_tmp21_ = _tmp25_ == _tmp27_;
				} else {
					_tmp21_ = FALSE;
				}
				_tmp13_ = _tmp21_;
			}
			if (_tmp13_) {
				_tmp12_ = TRUE;
			} else {
				gboolean _tmp28_ = FALSE;
				gint _tmp29_;
				Warp* _tmp30_;
				gint _tmp31_;
				_tmp29_ = x;
				_tmp30_ = warp;
				_tmp31_ = _tmp30_->x;
				if (_tmp29_ == _tmp31_) {
					gint _tmp32_;
					Warp* _tmp33_;
					gint _tmp34_;
					_tmp32_ = y;
					_tmp33_ = warp;
					_tmp34_ = _tmp33_->y;
					_tmp28_ = _tmp32_ == (_tmp34_ + 1);
				} else {
					_tmp28_ = FALSE;
				}
				_tmp12_ = _tmp28_;
			}
			if (_tmp12_) {
				_tmp11_ = TRUE;
			} else {
				gboolean _tmp35_ = FALSE;
				gint _tmp36_;
				Warp* _tmp37_;
				gint _tmp38_;
				_tmp36_ = x;
				_tmp37_ = warp;
				_tmp38_ = _tmp37_->x;
				if (_tmp36_ == (_tmp38_ + 1)) {
					gint _tmp39_;
					Warp* _tmp40_;
					gint _tmp41_;
					_tmp39_ = y;
					_tmp40_ = warp;
					_tmp41_ = _tmp40_->y;
					_tmp35_ = _tmp39_ == (_tmp41_ + 1);
				} else {
					_tmp35_ = FALSE;
				}
				_tmp11_ = _tmp35_;
			}
			if (_tmp11_) {
				result = warp;
				_g_object_unref0 (_warp_list);
				return result;
			}
			_g_object_unref0 (warp);
		}
		_g_object_unref0 (_warp_list);
	}
	result = NULL;
	return result;
}


static void warp_manager_class_init (WarpManagerClass * klass) {
	warp_manager_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = warp_manager_finalize;
	warp_manager_signals[WARP_MANAGER_WARP_ADDED_SIGNAL] = g_signal_new ("warp_added", TYPE_WARP_MANAGER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, TYPE_WARP);
}


static void warp_manager_instance_init (WarpManager * self) {
}


static void warp_manager_finalize (GObject * obj) {
	WarpManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_WARP_MANAGER, WarpManager);
	_g_object_unref0 (self->warps);
	G_OBJECT_CLASS (warp_manager_parent_class)->finalize (obj);
}


GType warp_manager_get_type (void) {
	static volatile gsize warp_manager_type_id__volatile = 0;
	if (g_once_init_enter (&warp_manager_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (WarpManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) warp_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WarpManager), 0, (GInstanceInitFunc) warp_manager_instance_init, NULL };
		GType warp_manager_type_id;
		warp_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "WarpManager", &g_define_type_info, 0);
		g_once_init_leave (&warp_manager_type_id__volatile, warp_manager_type_id);
	}
	return warp_manager_type_id__volatile;
}



