// File: lotuskip[P]yuxtapa/client/base.cpp
// Function: Base::init_ncurses()
bool Base::init_ncurses()
{
	short c;
	// First init the animation:
	string full_anim = Config::get_anim();
	for(c = 0; c < num_syms(full_anim); ++c)
		anim_syms.push_back(sym_at(full_anim, c));

	// Then the actual NCurses stuff
	if(!initscr())
	{
		cout << "Failed to init NCurses screen!" << endl;
		return false;
	}

	getmaxyx(stdscr, scr_y, scr_x);
	if(scr_x < MIN_SCREEN_X || scr_y < MIN_SCREEN_Y)
	{
		endwin();
		cout << "Terminal window is too small! Required minimum is "
			<< int(MIN_SCREEN_X) << 'x' << int(MIN_SCREEN_Y) << '.' << endl;
		return false;
	}
	signal(SIGWINCH, update_screen_size);

	start_color();
	raw();
	keypad(stdscr, TRUE);
	noecho();
	timeout(GETCH_TIMEOUT);
	set_escdelay(OUR_ESC_DELAY);
	nonl();

	// init base colours first: (with black background)
	char d;
	char ct[BASE_COLOURS] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN,
		COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE,
		COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE,
		COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE };
	for(c = 0; c < BASE_COLOURS; ++c)
		init_pair(c, ct[c], COLOR_BLACK);

	// figure out colour mode:
	if(can_change_color() && COLORS >= MAX_PREDEF_COLOUR)
	{
		colmode = CM_FULL;
		term_colours = new short[3*(MAX_PREDEF_COLOUR-BASE_COLOURS)];
		for(c = 0; c < MAX_PREDEF_COLOUR-BASE_COLOURS; ++c)
		{
			// store old colours:
			color_content(c+BASE_COLOURS, &term_colours[3*c],
				&term_colours[3*c+1], &term_colours[3*c+2]);
			// and replace with ours:
			init_color(c+BASE_COLOURS, colour[c][0], colour[c][1],
				colour[c][2]); 
		}
		// (see common/col_codes.h)
		for(c = BASE_COLOURS; c < C_BG_HEAL; ++c)
			init_pair(c, c, COLOR_BLACK);
		for(d = 0; d < 6; ++d)
		{
			for(c = 0; c < 7; ++c)
				init_pair(C_GREEN_ON_HEAL+d*7+c, C_BASE_GREEN+c, C_BG_HEAL+d);
		}
		init_pair(C_UNKNOWN, C_WALL_DIM, C_WALL_DIM); 
	}
	else if(COLORS >= MAX_PREDEF_COLOUR) // immutable multi-colour
	{
		colmode = CM_MANY;
		for(c = BASE_COLOURS; c < C_BG_HEAL; ++c)
			init_pair(c, fixed_remap[c-BASE_COLOURS], COLOR_BLACK);
		for(d = 0; d < 6; ++d)
		{
			for(c = 0; c < 7; ++c)
				init_pair(C_GREEN_ON_HEAL+d*7+c, fixed_fgc[c], fixed_bgc[d]);
		}
		init_pair(C_UNKNOWN, 8, 8);
	}
	else // only 8+8 colours
	{
		colmode = CM_FEW;
		// Init the pairs with non-black background
		for(d = 0; d < 6; ++d)
		{
			for(c = 0; c < 7; ++c)
				init_pair(BASE_COLOURS+d*7+c, fgc[c], bgc[d]);
		}
		init_pair(BASE_COLOURS+6*7, BASE_LIGHT_GRAY, BASE_LIGHT_GRAY);
	}
	
	// (ysize, xsize, ypos, xpos)
	windows[STAT_WIN] = newwin(VIEWSIZE - MSG_WIN_Y, MIN_SCREEN_X - VIEWSIZE, 0, 0);
	windows[VIEW_WIN] = newwin(VIEWSIZE, VIEWSIZE, 0, MIN_SCREEN_X - VIEWSIZE);
	windows[MSG_WIN] = newwin(MSG_WIN_Y, MSG_WIN_X, VIEWSIZE - MSG_WIN_Y, 0);
	windows[CHAT_WIN] = newwin(scr_y - VIEWSIZE, scr_x, VIEWSIZE, 0);

	refresh();
	flushinp();
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
callq 0x16170
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x5220
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
movl %eax, -0x78(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1a800
movw %ax, -0x72(%rbp)
jmp 0x8ca8
movl -0x78(%rbp), %eax
movw -0x72(%rbp), %cx
movswl %cx, %ecx
cmpl %ecx, %eax
jge 0x8d1d
movswl -0x4(%rbp), %edx
leaq -0x58(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1a890
jmp 0x8cc9
leaq 0x1fdd0(%rip), %rdi     # 0x28aa0
leaq -0x58(%rbp), %rsi
callq 0xa160
jmp 0x8cdb
leaq -0x58(%rbp), %rdi
callq 0x5968
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x8c92
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x934d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5968
jmp 0x934d
callq 0x53a0
movq %rax, -0x80(%rbp)
jmp 0x8d28
movq -0x80(%rbp), %rax
cmpq $0x0, %rax
jne 0x8d73
movq 0x1f287(%rip), %rdi     # 0x27fc0
leaq 0x132d4(%rip), %rsi     # 0x1c014
callq 0x54b0
movq %rax, -0x88(%rbp)
jmp 0x8d4e
movq -0x88(%rbp), %rdi
movq 0x1f244(%rip), %rsi     # 0x27fa0
callq 0x5510
jmp 0x8d63
movb $0x0, -0x1(%rbp)
movl $0x1, -0x5c(%rbp)
jmp 0x9336
xorl %eax, %eax
movq 0x1f23c(%rip), %rcx     # 0x27fb8
cmpq (%rcx), %rax
je 0x8d9a
movq 0x1f230(%rip), %rax     # 0x27fb8
movq (%rax), %rax
movswl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x8da7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x8c(%rbp)
jmp 0x8da7
movl -0x8c(%rbp), %eax
movw %ax, 0x1fd04(%rip)      # 0x28ab8
xorl %eax, %eax
movq 0x1f1fb(%rip), %rcx     # 0x27fb8
cmpq (%rcx), %rax
je 0x8ddb
movq 0x1f1ef(%rip), %rax     # 0x27fb8
movq (%rax), %rax
movswl 0x6(%rax), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x8de8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x90(%rbp)
jmp 0x8de8
movl -0x90(%rbp), %eax
movw %ax, 0x1fcc5(%rip)      # 0x28aba
movzwl 0x1fcbe(%rip), %eax     # 0x28aba
cmpl $0x50, %eax
jl 0x8e11
movzwl 0x1fcb0(%rip), %eax     # 0x28ab8
cmpl $0x18, %eax
jge 0x8ec1
callq 0x5890
jmp 0x8e18
movq 0x1f1a1(%rip), %rdi     # 0x27fc0
leaq 0x1320d(%rip), %rsi     # 0x1c033
callq 0x54b0
movq %rax, -0x98(%rbp)
jmp 0x8e34
movq -0x98(%rbp), %rdi
movl $0x50, %esi
callq 0x5810
movq %rax, -0xa0(%rbp)
jmp 0x8e4e
movq -0xa0(%rbp), %rdi
movl $0x78, %esi
callq 0x55c0
movq %rax, -0xa8(%rbp)
jmp 0x8e68
movq -0xa8(%rbp), %rdi
movl $0x18, %esi
callq 0x5810
movq %rax, -0xb0(%rbp)
jmp 0x8e82
movq -0xb0(%rbp), %rdi
movl $0x2e, %esi
callq 0x55c0
movq %rax, -0xb8(%rbp)
jmp 0x8e9c
movq -0xb8(%rbp), %rdi
movq 0x1f0f6(%rip), %rsi     # 0x27fa0
callq 0x5510
jmp 0x8eb1
movb $0x0, -0x1(%rbp)
movl $0x1, -0x5c(%rbp)
jmp 0x9336
leaq 0x498(%rip), %rsi       # 0x9360
movl $0x1c, %edi
callq 0x55d0
callq 0x52e0
jmp 0x8ed9
callq 0x5180
jmp 0x8ee0
movq 0x1f0d1(%rip), %rax     # 0x27fb8
movq (%rax), %rdi
movl $0x1, %esi
callq 0x51c0
jmp 0x8ef6
callq 0x58e0
jmp 0x8efd
movq 0x1f0b4(%rip), %rax     # 0x27fb8
movq (%rax), %rdi
movl $0x28, %esi
callq 0x55b0
jmp 0x8f13
movl $0x14, %edi
callq 0x5460
jmp 0x8f1f
callq 0x5760
jmp 0x8f26
movq 0x131c3(%rip), %rax     # 0x1c0f0
movq %rax, -0x70(%rbp)
movq 0x131c0(%rip), %rax     # 0x1c0f8
movq %rax, -0x68(%rbp)
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x10, %eax
jge 0x8f70
movswl -0x4(%rbp), %edi
movslq %edi, %rax
movsbl -0x70(%rbp,%rax), %esi
xorl %edx, %edx
callq 0x5310
jmp 0x8f60
jmp 0x8f62
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x8f42
callq 0x5950
movb %al, -0xb9(%rbp)
jmp 0x8f7d
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x8f8c
jmp 0x90ee
movq 0x1f035(%rip), %rax     # 0x27fc8
cmpl $0x50, (%rax)
jl 0x90ee
movl $0x0, 0x1fb16(%rip)     # 0x28abc
movl $0x180, %edi            # imm = 0x180
callq 0x5030
movq %rax, -0xc8(%rbp)
jmp 0x8fb9
movq -0xc8(%rbp), %rax
movq %rax, 0x1faf9(%rip)     # 0x28ac0
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x40, %eax
jge 0x9046
movswl -0x4(%rbp), %esi
movl %esi, %eax
addl $0x10, %eax
movq 0x1fada(%rip), %rcx     # 0x28ac0
movl %esi, %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdi
leaq (%rcx,%rdi,2), %rsi
leaq 0x2(%rcx,%rdi,2), %rdx
leaq 0x4(%rcx,%rdi,2), %rcx
movswl %ax, %edi
callq 0x51e0
jmp 0x9006
movzwl -0x4(%rbp), %eax
movl %eax, %ecx
addl $0x10, %eax
movswq %cx, %rcx
leaq (%rcx,%rcx,2), %rdi
leaq 0x130e2(%rip), %rcx     # 0x1c100
movswl (%rcx,%rdi,2), %esi
movswl 0x2(%rcx,%rdi,2), %edx
movswl 0x4(%rcx,%rdi,2), %ecx
movswl %ax, %edi
callq 0x5740
jmp 0x9036
jmp 0x9038
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x8fcd
movw $0x10, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x43, %eax
jge 0x9074
movswl -0x4(%rbp), %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x5310
jmp 0x9064
jmp 0x9066
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x904c
movb $0x0, -0x5d(%rbp)
movsbl -0x5d(%rbp), %eax
cmpl $0x6, %eax
jge 0x90d6
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x7, %eax
jge 0x90ca
movsbl -0x5d(%rbp), %edx
movl %edx, %edi
shll $0x3, %edi
subl %edx, %edi
movswl -0x4(%rbp), %eax
movl %eax, %esi
movl %edi, %ecx
leal 0x43(%rcx,%rsi), %ecx
addl $0x49, %eax
addl $0x43, %edx
movswl %cx, %edi
movswl %ax, %esi
callq 0x5310
jmp 0x90ba
jmp 0x90bc
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x9087
jmp 0x90cc
movb -0x5d(%rbp), %al
addb $0x1, %al
movb %al, -0x5d(%rbp)
jmp 0x9078
movl $0x6d, %edi
movl $0x13, %edx
movl %edx, %esi
callq 0x5310
jmp 0x90e9
jmp 0x9268
movq 0x1eed3(%rip), %rax     # 0x27fc8
cmpl $0x50, (%rax)
jl 0x91d1
movl $0x1, 0x1f9b4(%rip)     # 0x28abc
movw $0x10, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x43, %eax
jge 0x9143
movswq -0x4(%rbp), %rax
movl %eax, %edi
leaq 0x1315b(%rip), %rcx     # 0x1c280
movzbl -0x10(%rax,%rcx), %esi
xorl %edx, %edx
callq 0x5310
jmp 0x9133
jmp 0x9135
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x910e
movb $0x0, -0x5d(%rbp)
movsbl -0x5d(%rbp), %eax
cmpl $0x6, %eax
jge 0x91b9
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x7, %eax
jge 0x91ad
movsbq -0x5d(%rbp), %rcx
movl %ecx, %eax
movl %eax, %esi
shll $0x3, %esi
subl %eax, %esi
movzwl -0x4(%rbp), %eax
movl %eax, %edx
movl %esi, %eax
leal 0x43(%rax,%rdx), %eax
movswq %dx, %rdx
leaq 0x1312f(%rip), %rsi     # 0x1c2b3
movzbl (%rdx,%rsi), %esi
leaq 0x1312b(%rip), %rdx     # 0x1c2ba
movzbl (%rcx,%rdx), %edx
movswl %ax, %edi
callq 0x5310
jmp 0x919d
jmp 0x919f
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x9156
jmp 0x91af
movb -0x5d(%rbp), %al
addb $0x1, %al
movb %al, -0x5d(%rbp)
jmp 0x9147
movl $0x6d, %edi
movl $0x8, %edx
movl %edx, %esi
callq 0x5310
jmp 0x91cc
jmp 0x9266
movl $0x2, 0x1f8e1(%rip)     # 0x28abc
movb $0x0, -0x5d(%rbp)
movsbl -0x5d(%rbp), %eax
cmpl $0x6, %eax
jge 0x9251
movw $0x0, -0x4(%rbp)
movswl -0x4(%rbp), %eax
cmpl $0x7, %eax
jge 0x9245
movsbq -0x5d(%rbp), %rcx
movl %ecx, %eax
movl %eax, %esi
shll $0x3, %esi
subl %eax, %esi
movzwl -0x4(%rbp), %eax
movl %eax, %edx
movl %esi, %eax
leal 0x10(%rax,%rdx), %eax
movswq %dx, %rdx
leaq 0x130a4(%rip), %rsi     # 0x1c2c0
movsbl (%rdx,%rsi), %esi
leaq 0x130a0(%rip), %rdx     # 0x1c2c7
movsbl (%rcx,%rdx), %edx
movswl %ax, %edi
callq 0x5310
jmp 0x9235
jmp 0x9237
movw -0x4(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x4(%rbp)
jmp 0x91ee
jmp 0x9247
movb -0x5d(%rbp), %al
addb $0x1, %al
movb %al, -0x5d(%rbp)
jmp 0x91df
movl $0x3a, %edi
movl $0x7, %edx
movl %edx, %esi
callq 0x5310
jmp 0x9264
jmp 0x9266
jmp 0x9268
movl $0x8, %edi
movl $0x39, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x5490
movq %rax, -0xd0(%rbp)
jmp 0x9284
movq -0xd0(%rbp), %rax
movq %rax, 0x1f83e(%rip)     # 0x28ad0
movl $0x17, %esi
xorl %edx, %edx
movl $0x39, %ecx
movl %esi, %edi
callq 0x5490
movq %rax, -0xd8(%rbp)
jmp 0x92ae
movq -0xd8(%rbp), %rax
movq %rax, 0x1f82c(%rip)     # 0x28ae8
movl $0xf, %edi
movl $0x39, %esi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x5490
movq %rax, -0xe0(%rbp)
jmp 0x92db
movq -0xe0(%rbp), %rax
movq %rax, 0x1f7f7(%rip)     # 0x28ae0
movzwl 0x1f7c8(%rip), %edi     # 0x28ab8
addl $-0x17, %edi
movzwl 0x1f7c0(%rip), %esi     # 0x28aba
movl $0x17, %edx
xorl %ecx, %ecx
callq 0x5490
movq %rax, -0xe8(%rbp)
jmp 0x930f
movq -0xe8(%rbp), %rax
movq %rax, 0x1f7bb(%rip)     # 0x28ad8
callq 0x5100
jmp 0x9324
callq 0x5210
jmp 0x932b
movb $0x1, -0x1(%rbp)
movl $0x1, -0x5c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5968
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x5968
movq -0x30(%rbp), %rdi
callq 0x5830
nop