// data structure
#include <acknex.h>
#include <default.c>
STRING* test = "test.wmb";
BMAP* arrow_cursor_bmp = "arrow.tga";
var total_panels = 0; // total number of registered panels
PANEL* panel_list[64]; // panel list array
// sets resolution, depth, and window mode and alters aspect
function set_resolution(_x,_y,_depth,_window)
{
var new_ratio;
var view_aspect;
video_set(_x,_y,_depth,_window);
wait(1);
//if fullscreen, alter aspect
if(video_screen == 1)
{
new_ratio = _x / _y;
view_aspect = new_ratio / 1.6;
camera.aspect = view_aspect;
}
// frame rate
fps_max = 60;
fps_lock = 1;
time_factor = 1;
}
// main mouse function
function init_mouse()
{
// local vectors
VECTOR temp_vec;
// mouse properties
mouse_mode = 1;
mouse_pointer = 0;
// mouse start position
wait(1);
mouse_pos.x = (screen_size.x / 2);
mouse_pos.y = (screen_size.y / 2);
// change to default cursor
mouse_map = arrow_cursor_bmp;
// loop forever
while(1)
{
// if mouse valid
if(mouse_valid == 1)
{
// update mouse position
vec_set(mouse_pos.x,mouse_cursor.x);
}
wait(1);
}
}
// main startup function
function main()
{
// set the resolution
set_resolution(1440,900,32,1);
// buffer before level load
wait(3);
// load level
level_load(test);
init_mouse();
}
// displays on screen text at a position for a limited time
function display_text(var x_pos, var y_pos, STRING* text_str, var seconds)
{
proc_mode = PROC_LATE;
var panel_size_x = 0;
var font_height = 15;
var display_count = 0;
// set seconds to ticks
seconds *= 16;
// set draw text mode
draw_textmode("Arial",0,font_height,100);
// loop while mouse is over this entity
while(display_count < seconds)
{
// position and show description
panel_size_x = str_width(text_str,NULL);
font_height = 15;
draw_quad(NULL,vector(x_pos + 2,y_pos,0),NULL,vector(panel_size_x + 4,font_height,0),NULL,vector(1,1,1),100,0);
draw_text(text_str,x_pos + 2,y_pos,vector(255,255,255));
display_count = minv(display_count + (1 * time_step),seconds);
wait(1);
}
}
function sort_panels(PANEL* panel)
{
if(panel==NULL) return; //invalid panel parameter
//
var list_no; for(list_no=0; list_no<total_panels; list_no++)
{ if(panel_list[list_no]!=NULL)
if(panel_list[list_no].layer > panel.layer)
layer_sort(panel_list[list_no], panel_list[list_no].layer-1);
}
layer_sort(panel,total_panels);
}
//moves a panel with mouse
function move_panel(PANEL* panel)
{
//local variables
VECTOR panel_vec;
VECTOR pointer_vec;
VECTOR panel_offset;
vec_set(panel_vec.x,vector(panel.pos_x,panel.pos_y,0));
//get initial mouse position
pointer_vec.x = mouse_pos.x;
pointer_vec.y = mouse_pos.y;
sort_panels(panel);
// while mouse held, loop
while(mouse_left == 1)
{
panel_offset.x = mouse_pos.x - pointer_vec.x;
panel_offset.y = mouse_pos.y - pointer_vec.y;
panel.pos_x = panel_vec.x + panel_offset.x;
panel.pos_y = panel_vec.y + panel_offset.y;
// restrict panel positions to on-screen
panel.pos_x = clamp(panel.pos_x,0,screen_size.x - panel.size_x);
panel.pos_y = clamp(panel.pos_y,0,screen_size.y - panel.size_y);
wait(1);
}
}
// register the panel
function register_panel(PANEL* panel)
{
panel_list[total_panels] = panel;
total_panels ++;
}
// inventory art
BMAP* backpack_bmp = "backpack_panel.tga";
BMAP* items_bmp = "item_icons.tga";
BMAP* item_x48_bmp = "item_button_x48.tga";
SOUND* select_snd = "select.wav";
var vol_effects = 30; // misc. sound effect volume
// container variable arrays
var slot_owner[500]; // if > 0, the slot is being used by something
var slot_origin[500]; // original slot number
var item_id[500]; // id number of the slot item; if 0, no item in slot
var bmp_icon_x[500]; // x location of the upper left pixel of the bmap icon
var bmp_icon_y[500]; // y location of the upper left pixel of the bmap icon
var name_index[500]; // name lookup index
var file_index[500]; // mdl file lookup index
var synergy_id[500]; // item has synergy with item number (item_id)
var synergy_usecode[500]; // item synergy use code, looks up the action to be taken
// pointers used for the 6 inventory pack slots
PANEL* pack_slot1;
PANEL* pack_slot2;
PANEL* pack_slot3;
PANEL* pack_slot4;
PANEL* pack_slot5;
PANEL* pack_slot6;
// helps determine the clicked slot number
var panel_slot_start = 0;
var item_hover_time;
// test item data
var ITM_TOME[9] = {-99, -99, 1, 48, 0, 1, 1, 0, 0};
var ITM_MILLBOWL[9] = {-99, -99, 40, 96, 0, 40, 40, 0, 0};
STRING* welcome_str = "Welcome";
TEXT* welcome_txt =
{
layer = -10;
pos_x = 10;
pos_y = 10;
string (welcome_str,"this is","a","TEXT");
flags = CENTER_X | TRANSLUCENT | SHOW;
}
// looks up and returns the icon of the indicated slot
function lookup_icon(var slot_no)
{
BMAP* icon_bmp;
STRING* icon_str = "item_icons.tga";
str_cat(icon_str,"#");
str_cat(icon_str,str_for_num(NULL,bmp_icon_x[slot_no]));
str_cat(icon_str,"#");
str_cat(icon_str,str_for_num(NULL,bmp_icon_y[slot_no]));
str_cat(icon_str,"#48#48");
icon_bmp = bmap_create(icon_str);
return(icon_bmp);
}
// looks up and returns the name string of the indicated slot
function lookup_name_str(var slot_no)
{
STRING* name_str = "Unknown";
if(name_index[slot_no] == 1){str_cpy(name_str,"Tome");}
if(name_index[slot_no] == 40){str_cpy(name_str,"Milling Bowl");}
return(name_str);
}
// looks up and returns the mdl file string of the indicated slot
function lookup_file_str(var slot_no)
{
STRING* file_str = "Unknown";
if(file_index[slot_no] == 1){str_cpy(file_str,"tome1.mdl");}
if(file_index[slot_no] == 40){str_cpy(file_str,"bowl1.mdl");}
return(file_str);
}
// adds an item to the specified slot
function slot_add_item(var* item_array, var slot_no)
{
// slot_owner is not modified, it is a property of the slot itself
slot_origin[slot_no] = slot_no;
item_id[slot_no] = item_array[2];
bmp_icon_x[slot_no] = item_array[3];
bmp_icon_y[slot_no] = item_array[4];
name_index[slot_no] = item_array[5];
file_index[slot_no] = item_array[6];
synergy_id[slot_no] = item_array[7];
synergy_usecode[slot_no] = item_array[8];
}
// clears the indicated slot
function slot_clear(var slot_no)
{
// slot_owner is not modified, it is a property of the slot itself
slot_origin[slot_no] = 0;
item_id[slot_no] = 0;
bmp_icon_x[slot_no] = 0;
bmp_icon_y[slot_no] = 0;
name_index[slot_no] = 0;
file_index[slot_no] = 0;
synergy_id[slot_no] = 0;
synergy_usecode[slot_no] = 0;
}
// copies item data from the source slot to the target slot
function slot_copy(var source_slot, var target_slot)
{
// slot_owner is not modified, it is a property of the slot itself
slot_origin[target_slot] = slot_origin[source_slot];
item_id[target_slot] = item_id[source_slot];
bmp_icon_x[target_slot] = bmp_icon_x[source_slot];
bmp_icon_y[target_slot] = bmp_icon_y[source_slot];
name_index[target_slot] = name_index[source_slot];
file_index[target_slot] = file_index[source_slot];
synergy_id[target_slot] = synergy_id[source_slot];
synergy_usecode[target_slot] = synergy_usecode[source_slot];
}
// inventory panel click event
function click_inv_panel(PANEL* inv_panel)
{
// var format;
// var pixel;
//
// // lock and read panel background to determine if it was actually clicked
// format = bmap_lock(inv_panel.bmap,0);
// pixel = pixel_for_bmap(inv_panel.bmap,mouse_pos.x - inv_panel.pos_x,mouse_pos.y - inv_panel.pos_y);
// bmap_unlock(inv_panel.bmap);
//
// // if panel was clicked
// if(pixel != 0)
// {
move_panel(inv_panel);
// }
}
// inventory panel slot click event
function click_inv_slot(var button_number, PANEL* inv_panel)
{
// determine which slot was clicked
var slot = panel_slot_start + (button_number - 1);
// cursor icon pointer
BMAP* mouse_item_bmp;
// end info display loop from the last slot
item_hover_time = -99;
// if clicked slot has item data
if(item_id[slot] > 0)
{
// move item data from the slot to the cursor
slot_copy(slot,0);
slot_clear(slot);
// play select sound
snd_play(select_snd,vol_effects,0);
// while mouse is held
while(mouse_left == 1)
{
// draw the icon under the cursor
mouse_item_bmp = lookup_icon(0);
draw_quad(mouse_item_bmp,vector(mouse_pos.x - 24,mouse_pos.y - 24,0),NULL,vector(48,48,0),NULL,NULL,100,0);
wait(1);
}
wait(2);
// if cursor still has data (item released over game view)
if(item_id[0] > 0)
{
// move item data from the cursor back to the original slot
slot_copy(0,slot_origin[0]);
slot_clear(0);
// play select sound
snd_play(select_snd,vol_effects,0);
}
}
}
// inventory panel slot mouse over event
function touch_inv_slot(var button_number, PANEL* inv_panel)
{
// determine which slot was touched
var slot = panel_slot_start + (button_number - 1);
// name string
STRING* name_str = "Unknown";
item_hover_time = 0;
// if the slot has item data
if(item_id[slot] > 0)
{
while(item_hover_time < 5 && item_hover_time != -99)
{
if(mouse_moving == 0){item_hover_time = minv(item_hover_time + (1 * time_step),5);}
wait(1);
}
if(item_hover_time == 5)
{
name_str = lookup_name_str(slot);
while(item_hover_time != -99)
{
display_text(mouse_pos.x,mouse_pos.y - 18,name_str,0.001);
wait(1);
}
}
}
// if slot is empty
else
{
item_hover_time = -99;
}
}
// inventory panel slot mouse off event
function release_inv_slot(var button_number, PANEL* inv_panel)
{
// determine over which slot the mouse was released
var slot = panel_slot_start + (button_number - 1);
// if cursor moved away from the slot
if(event_type == EVENT_RELEASE)
{
item_hover_time = -99;
}
// if mouse was truely released over a slot
if(event_type == EVENT_BUTTONUP)
{
// cursor has item data
if(item_id[0] > 0)
{
// if drop slot is empty
if(item_id[slot] == 0)
{
// move item data from the cursor to the slot
slot_copy(0,slot);
slot_clear(0);
// slot_origin must be updated to new slot location
slot_origin[slot] = slot;
}
// if drop slot is not empty
else
{
// move item data from the cursor back to the original slot
slot_copy(0,slot_origin[0]);
slot_clear(0);
}
// play select sound
snd_play(select_snd,vol_effects,0);
}
}
}
// fills the inv panel with an amount of inventory slots
function create_slots(PANEL* inv_panel, var rows, var cols, var start_x, var start_y, var start_slot)
{
var rows_created = 0;
var cols_created = 0;
var x_pos;
var y_pos;
var slot_step;
x_pos = start_x;
y_pos = start_y;
slot_step = start_slot;
while(rows_created < rows)
{
while(cols_created < cols)
{
pan_setwindow(inv_panel,0,x_pos,y_pos,48,48,items_bmp,bmp_icon_x[slot_step],bmp_icon_y[slot_step]);
pan_setbutton(inv_panel,0,0,x_pos,y_pos,item_x48_bmp,item_x48_bmp,item_x48_bmp,item_x48_bmp,click_inv_slot,release_inv_slot,touch_inv_slot);
x_pos += 48;
slot_step ++;
cols_created ++;
wait(1);
}
y_pos += 48;
x_pos = start_x;
cols_created = 0;
rows_created ++;
wait(1);
}
}
// creates the pack panel
function create_inv_panel(var start_slot, var x_pos, var y_pos)
{
PANEL* inv_panel = pan_create("bmap = backpack_bmp; event = click_inv_panel;",NULL);
inv_panel.pos_x = x_pos;
inv_panel.pos_y = y_pos;
create_slots(inv_panel,4,8,64,168,start_slot);
register_panel(inv_panel);
// bring this panel to front
layer_sort(inv_panel,total_panels);
return(inv_panel);
}
// starts the inventory system
function inv_startup()
{
slot_add_item(ITM_TOME,16);
slot_add_item(ITM_MILLBOWL,17);
pack_slot1 = create_inv_panel(16,32,32);
pack_slot2 = create_inv_panel(49,160,48);
pack_slot3 = create_inv_panel(81,288,64);
pack_slot4 = create_inv_panel(113,416,80);
pack_slot5 = create_inv_panel(145,544,96);
pack_slot6 = create_inv_panel(177,672,112);
set(pack_slot1,SHOW);
set(pack_slot2,SHOW);
set(pack_slot3,SHOW);
set(pack_slot4,SHOW);
set(pack_slot5,SHOW);
set(pack_slot6,SHOW);
while(1)
{
if(key_1 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot1,SHOW);
sort_panels(pack_slot1);
while(key_1 == 1){wait(1);}
}
if(key_2 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot2,SHOW);
sort_panels(pack_slot2);
while(key_2 == 1){wait(1);}
}
if(key_3 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot3,SHOW);
sort_panels(pack_slot3);
while(key_3 == 1){wait(1);}
}
if(key_4 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot4,SHOW);
sort_panels(pack_slot4);
while(key_4 == 1){wait(1);}
}
if(key_5 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot5,SHOW);
sort_panels(pack_slot5);
while(key_5 == 1){wait(1);}
}
if(key_6 == 1)
{
snd_play(select_snd,vol_effects,0);
toggle(pack_slot6,SHOW);
sort_panels(pack_slot6);
while(key_6 == 1){wait(1);}
}
// determine starting slot number for inventory mouse functions
if(mouse_panel == pack_slot1){panel_slot_start = 16;}
if(mouse_panel == pack_slot2){panel_slot_start = 49;}
if(mouse_panel == pack_slot3){panel_slot_start = 81;}
if(mouse_panel == pack_slot4){panel_slot_start = 113;}
if(mouse_panel == pack_slot5){panel_slot_start = 145;}
if(mouse_panel == pack_slot6){panel_slot_start = 177;}
wait(1);
}
}