diff options
author | jacopo grandi <jak.sk8@hotmail.it> | 2021-02-20 10:26:22 +0100 |
---|---|---|
committer | jacopo grandi <jak.sk8@hotmail.it> | 2021-02-20 10:26:22 +0100 |
commit | d1dc6ddc035193ab6e2570f58b7e3a79fc335bce (patch) | |
tree | 6d2f1ab0c2e2d11fa7faa4060624585e870b567f | |
parent | 2eef87c8970db643c4ef09e0fd9e8110c8193043 (diff) |
sock work
-rw-r--r-- | build/army/army.txt | bin | 21528 -> 21528 bytes | |||
-rw-r--r-- | build/player.txt | 2 | ||||
-rw-r--r-- | build/test.exe | bin | 775710 -> 776738 bytes | |||
-rw-r--r-- | design/notes.txt | 13 | ||||
-rw-r--r-- | hud/hud.c | 79 | ||||
-rw-r--r-- | hud/hud.h | 6 | ||||
-rw-r--r-- | net/net.c | 22 | ||||
-rw-r--r-- | net/net.h | 2 |
8 files changed, 90 insertions, 34 deletions
diff --git a/build/army/army.txt b/build/army/army.txt Binary files differindex 033dd7c..7e40c14 100644 --- a/build/army/army.txt +++ b/build/army/army.txt diff --git a/build/player.txt b/build/player.txt index a0fa615..4d4dd70 100644 --- a/build/player.txt +++ b/build/player.txt @@ -1 +1 @@ -The Nameless One
\ No newline at end of file +The one without a name
\ No newline at end of file diff --git a/build/test.exe b/build/test.exe Binary files differindex a57fe22..44ed130 100644 --- a/build/test.exe +++ b/build/test.exe diff --git a/design/notes.txt b/design/notes.txt index c9903bb..0cd57c3 100644 --- a/design/notes.txt +++ b/design/notes.txt @@ -52,6 +52,13 @@ view from 18:02:21 to the end: details: +implement stats hud view: +? stats have to include individual weapon cooldowns and damage, what to do + either put all as lines in stats (clean, but ugly) + or modify the components directly (messy) + or have the components hold both values, with mouse over showing the chain of effects +! components hold both values, stats screen is for stats only. + implement cost function: oh boy cost of a component = c_c @@ -160,12 +167,6 @@ implement augment calculation: ok add_speed -> done, not tested -implement stats hud view: -? stats have to include individual weapon cooldowns and damage, what to do - either put all as lines in stats (clean, but ugly) - or modify the components directly (messy) -! put them in stats later - implement sound: partially done, need sound design @@ -98,9 +98,11 @@ void hud_init (graphic_settings *gs, hud *h, txtd *t) { info_unit_init(&h->fnu.uinfo); strcpy(h->og.army_listcur, "army"); strcpy(h->og.playername, ""); + strcpy(h->og.ip, "192.168.1.255"); h->nameedit = NULL; h->og.battle_state = 0; - h->og.edit_playername = 0; + h->og.input_playername = 0; + h->og.input_ip = 0; hud_reset(gs, h, t); } @@ -201,7 +203,11 @@ void hud_process_sel (graphic_settings *gs, hud *h, MKb *mkb, void hud_edit_close(hud *h) { h->nameedit = NULL; - h->og.edit_playername = 0; + if (h->og.input_playername != 0) { + info_save_playername(h->og.playername); + } + h->og.input_playername = 0; + h->og.input_ip = 0; } void hud_open_fnu (hud *h, info_unit *u, int i) { @@ -353,6 +359,7 @@ void hud_process_overlay_game (graphic_settings *gs, hud *h, MKb *mkb, gst->cam[0] = -gs->resx/2+gst->map_battle.sx*gst->map_battle.ts/2; gst->cam[1] = -gs->resy/2+gst->map_battle.sy*gst->map_battle.ts/2; h->state = 3; + net_server_close(nets); } } } @@ -369,6 +376,7 @@ void hud_process_overlay_game (graphic_settings *gs, hud *h, MKb *mkb, gst->cam[0] = -gs->resx/2+gst->map_battle.sx*gst->map_battle.ts/2; gst->cam[1] = -gs->resy/2+gst->map_battle.sy*gst->map_battle.ts/2; h->state = 3; + net_client_close(netc); } } @@ -398,22 +406,28 @@ void hud_process_overlay_game (graphic_settings *gs, hud *h, MKb *mkb, } if (mouse_in_button(mousepos, t, &h->og.join_game)) { + h->og.battle_state = 0; + net_server_close(nets); printf("open client\n"); net_client_open(netc); - int conn = net_client_connect(netc, "127.0.0.1", SERVER_PORT); + int conn = net_client_connect(netc, h->og.ip, SERVER_PORT); if (conn == 0) { int armysize = sizeof(unit)*gst->army_bp[0].uslen; char data[armysize]; memcpy(data, gst->army_bp[0].us, armysize); net_client_send(netc, data, armysize); printf("send (%d)\n", armysize); + h->og.battle_state = 2; + } else { + printf("close client"); + net_client_close(netc); } - h->og.battle_state = 2; } if (mouse_in_button(mousepos, t, &h->og.host_game)) { + net_client_close(netc); printf("open server\n"); - net_server_open(nets, SERVER_PORT); + net_server_open(nets, "127.0.0.1", SERVER_PORT); h->og.battle_state = 1; } @@ -474,8 +488,20 @@ void hud_process_overlay_game (graphic_settings *gs, hud *h, MKb *mkb, float sizen[2] = { get_text_width(sn, t), 10 }; if (pt_rect(mousepos, pn, sizen)) { h->nameedit = h->og.playername; - printf("editing this %s\n", h->nameedit); - h->og.edit_playername = 1; + h->og.input_playername = 1; + } + } else { + Mix_PlayChannel( -1, sounds[SOUND_SUCCESS], 0 ); + hud_edit_close(h); + } + + if (h->nameedit == NULL) { + float pn[2] = { h->og.rect_battle.x+5, h->og.rect_battle.y+25 }; + char sn[64]; sprintf(sn, "IP: %s", h->og.playername); + float sizen[2] = { get_text_width(sn, t), 10 }; + if (pt_rect(mousepos, pn, sizen)) { + h->nameedit = h->og.ip; + h->og.input_ip = 1; } } else { Mix_PlayChannel( -1, sounds[SOUND_SUCCESS], 0 ); @@ -498,15 +524,33 @@ void hud_process_overlay_game (graphic_settings *gs, hud *h, MKb *mkb, void hud_edit_name(hud *h, MKb *mkb, Mix_Chunk *sounds[]) { for (int i=0; i<mkb->kbnum; i++) { - if (mkb->kb[i] >= SDL_SCANCODE_A - && mkb->kb[i] <= SDL_SCANCODE_Z) { - if (strlen(h->nameedit) < 31) { + if (strlen(h->nameedit) < 31) { + if (mkb->kb[i] >= SDL_SCANCODE_A + && mkb->kb[i] <= SDL_SCANCODE_Z) { char c = mkb->kb[i]-SDL_SCANCODE_A+'a'; if (SDL_GetModState() & KMOD_SHIFT) { c = mkb->kb[i]-SDL_SCANCODE_A+'A'; } sprintf(h->nameedit, "%s%c", h->nameedit, c); } + if (mkb->kb[i] >= SDL_SCANCODE_1 + && mkb->kb[i] <= SDL_SCANCODE_9) { + char c = mkb->kb[i]-SDL_SCANCODE_1+'1'; + sprintf(h->nameedit, "%s%c", h->nameedit, c); + } + if (mkb->kb[i] >= SDL_SCANCODE_KP_1 + && mkb->kb[i] <= SDL_SCANCODE_KP_9) { + char c = mkb->kb[i]-SDL_SCANCODE_KP_1+'1'; + sprintf(h->nameedit, "%s%c", h->nameedit, c); + } + if (mkb->kb[i] == SDL_SCANCODE_KP_0 + || mkb->kb[i] == SDL_SCANCODE_0) { + sprintf(h->nameedit, "%s0", h->nameedit); + } + if (mkb->kb[i] == SDL_SCANCODE_KP_PERIOD + || mkb->kb[i] == SDL_SCANCODE_PERIOD) { + sprintf(h->nameedit, "%s.", h->nameedit); + } } } if (mkb_search(mkb, SDL_SCANCODE_SPACE)) { @@ -519,7 +563,8 @@ void hud_edit_name(hud *h, MKb *mkb, Mix_Chunk *sounds[]) { if (strlen(h->nameedit) > 0) h->nameedit[strlen(h->nameedit)-1] = '\0'; } - if (mkb_search(mkb, SDL_SCANCODE_ESCAPE)) { + if (mkb_search(mkb, SDL_SCANCODE_ESCAPE) + || mkb_search(mkb, SDL_SCANCODE_RETURN)) { Mix_PlayChannel( -1, sounds[SOUND_SUCCESS], 0 ); hud_edit_close(h); return; } @@ -773,14 +818,22 @@ void hud_render_overlay_game (overlay_game *og, MKb *mkb, float pn[2] = { og->rect_battle.x+5, og->rect_battle.y+5 }; char sn[64]; - if (og->edit_playername == 1) { + if (og->input_playername == 1) { sprintf(sn, "PLAYER NAME: %s_", og->playername); - printf("looool\n"); } else { sprintf(sn, "PLAYER NAME: %s", og->playername); } render_text_scaled(rend, sn, pn, t, 1); + float pip[2] = { og->rect_battle.x+5, og->rect_battle.y+25 }; + char sip[64]; + if (og->input_ip == 1) { + sprintf(sip, "IP: %s_", og->ip); + } else { + sprintf(sip, "IP: %s", og->ip); + } + render_text_scaled(rend, sip, pip, t, 1); + render_button(rend, t, &og->host_game); render_button(rend, t, &og->join_game); render_button(rend, t, &og->start_battle); @@ -36,9 +36,11 @@ typedef struct { char army_list[64][32]; int army_listlen; char army_listcur[32]; - char playername[32]; int battle_state; - int edit_playername; + char playername[32]; + int input_playername; + char ip[32]; + int input_ip; } overlay_game; typedef struct { @@ -21,14 +21,14 @@ void net_init () { p_libsys_init (); } -int net_open_socket(PSocket **socket, int port) { +int net_open_socket(PSocket **socket, char ip[], int port) { *socket = p_socket_new (P_SOCKET_FAMILY_INET, P_SOCKET_TYPE_STREAM, P_SOCKET_PROTOCOL_TCP, NULL); p_socket_set_blocking (*socket, FALSE); p_socket_set_timeout (*socket, 10); - PSocketAddress *sock_addr = p_socket_address_new("127.0.0.1", port); + PSocketAddress *sock_addr = p_socket_address_new(ip, port); int sender_port = 0; if (p_socket_bind (*socket, sock_addr, FALSE, NULL) == FALSE) { p_socket_free (*socket); @@ -58,7 +58,7 @@ int net_recv (PSocket *socket, char buffer[]) { printf("received (%d)\n", len); return len; } - printf("error server recv\n"); + printf("error recv\n"); return -1; } else { len += recv_now; @@ -74,7 +74,7 @@ int net_recv (PSocket *socket, char buffer[]) { void net_client_open(net_client *c) { - int err = net_open_socket(&c->socket, 0); + int err = net_open_socket(&c->socket, "127.0.0.1", 0); if (err > 0) printf("error opening client socket: %d\n", err); } @@ -89,8 +89,8 @@ int net_client_connect (net_client *c, char ip[], int port) { { p_socket_address_free (c->addr_server); p_socket_free (c->socket); - p_uthread_exit (-1); printf("error client socket freed\n"); + return 1; } } @@ -116,15 +116,15 @@ int net_client_recv (net_client *c, char buffer[]) { } void net_client_close (net_client *c) { - p_socket_close (c->socket, NULL); + if (c->socket != NULL) p_socket_close (c->socket, NULL); p_socket_address_free (c->addr_server); p_socket_free (c->socket); } -void net_server_open(net_server *s, int port) { - int err = net_open_socket(&s->socket, port); +void net_server_open(net_server *s, char ip[], int port) { + int err = net_open_socket(&s->socket, ip, port); if (err > 0) printf("error opening server socket: %d\n", err); if (p_socket_listen (s->socket, NULL) == FALSE) { printf("error server socket listen, nobody in.\n"); @@ -162,7 +162,7 @@ void net_server_send (net_server *s, char data[], int sizeofdata) { } void net_server_close (net_server *s) { - p_socket_close (s->socket, NULL); - p_socket_free (s->sock_client); - p_socket_free(s->socket); + if (s->socket != NULL) p_socket_close (s->socket, NULL); + if (s->sock_client != NULL) p_socket_free (s->sock_client); + if (s->socket != NULL) p_socket_free(s->socket); } @@ -26,7 +26,7 @@ typedef struct { PSocket *sock_client; } net_server; -void net_server_open(net_server *s, int port); +void net_server_open(net_server *s, char ip[], int port); void net_server_accept(net_server *s); int net_server_recv(net_server *s, char buffer[]); void net_server_send (net_server *s, char data[], int sizeofdata); |