From a8c9badce4cdc4c96f2d8ed368b2a5a035344f96 Mon Sep 17 00:00:00 2001 From: xtreme8000 Date: Sat, 23 Dec 2017 17:55:29 +0100 Subject: [PATCH] More bug fixes * sort players by score on player list * draw minimap with a texture instead of glDrawPixels * apply weapon spread to all guns (makes shotgun work and is prob correct on others too) * improve texture scaling for older hardware * cleaned up chunk.c a bit * leg animation improved, reversed on foot sound --- resources/VERSION | 1 + src/cameracontroller.c | 2 + src/chunk.c | 49 +++++-------- src/chunk.h | 10 +-- src/common.h | 5 +- src/main.c | 155 ++++++++++++++++++++++++----------------- src/network.c | 23 ++++-- src/player.c | 57 ++++++++++----- src/player.h | 3 +- src/texture.c | 25 ++++++- src/texture.h | 1 + src/tracer.c | 17 +---- src/tracer.h | 2 +- src/weapon.c | 155 +++++++++++++++++++++-------------------- src/weapon.h | 2 +- 15 files changed, 289 insertions(+), 218 deletions(-) create mode 100644 resources/VERSION diff --git a/resources/VERSION b/resources/VERSION new file mode 100644 index 0000000..a1c2c6a --- /dev/null +++ b/resources/VERSION @@ -0,0 +1 @@ +v0.1.1 \ No newline at end of file diff --git a/src/cameracontroller.c b/src/cameracontroller.c index 279bb81..3306187 100644 --- a/src/cameracontroller.c +++ b/src/cameracontroller.c @@ -51,6 +51,8 @@ void cameracontroller_fps(float dt) { players[local_player_id].input.buttons.packed = 0; } + //float speedx = (camera_rot_x-last_rot_x)/dt*0.02F; + //float speedy = (camera_rot_y-last_rot_y)/dt*0.02F; players[local_player_id].orientation.x = sin(last_rot_x)*sin(last_rot_y); players[local_player_id].orientation.y = cos(last_rot_y); players[local_player_id].orientation.z = cos(last_rot_x)*sin(last_rot_y); diff --git a/src/chunk.c b/src/chunk.c index 3990817..78ae175 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -2,10 +2,7 @@ #include #include -int chunk_display_lists[CHUNKS_PER_DIM*CHUNKS_PER_DIM]; -int chunk_max_height[CHUNKS_PER_DIM*CHUNKS_PER_DIM]; -float chunk_last_update[CHUNKS_PER_DIM*CHUNKS_PER_DIM]; -int chunk_created[CHUNKS_PER_DIM*CHUNKS_PER_DIM] = {0}; +struct chunk chunks[CHUNKS_PER_DIM*CHUNKS_PER_DIM] = {0}; int chunk_geometry_changed[CHUNKS_PER_DIM*CHUNKS_PER_DIM*2]; int chunk_geometry_changed_lenght = 0; @@ -38,7 +35,7 @@ float chunk_draw_visible() { if(tmp_y>=CHUNKS_PER_DIM) { tmp_y -= CHUNKS_PER_DIM; } - if(camera_CubeInFrustum(x*CHUNK_SIZE+CHUNK_SIZE/2,0.0F,y*CHUNK_SIZE+CHUNK_SIZE/2,CHUNK_SIZE/2,chunk_max_height[tmp_y*CHUNKS_PER_DIM+tmp_x])) { + if(camera_CubeInFrustum(x*CHUNK_SIZE+CHUNK_SIZE/2,0.0F,y*CHUNK_SIZE+CHUNK_SIZE/2,CHUNK_SIZE/2,chunks[tmp_y*CHUNKS_PER_DIM+tmp_x].max_height)) { chunks_to_draw_x[index] = x*CHUNK_SIZE; chunks_to_draw_y[index++] = y*CHUNK_SIZE; } @@ -101,13 +98,13 @@ void chunk_render(int x, int y) { y -= CHUNKS_PER_DIM; } - if(chunk_created[((y*CHUNKS_PER_DIM)|x)]) { + if(chunks[((y*CHUNKS_PER_DIM)|x)].created) { if(chunk_render_mode) { glLineWidth(4.0F); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); } matrix_upload(); - glCallList(chunk_display_lists[((y*CHUNKS_PER_DIM)|x)]); + glCallList(chunks[((y*CHUNKS_PER_DIM)|x)].display_list); if(chunk_render_mode) { glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); } @@ -219,15 +216,21 @@ int chunk_generate(int displaylist, int chunk_x, int chunk_y) { if((x%64)>0 && (z%64)>0) { for(int y=map_size_y-1;y>=0;y--) { if(map_colors[x+(y*map_size_z+z)*map_size_x]!=0xFFFFFFFF) { - map_minimap[(x+z*(map_size_x+1))*3+0] = red(map_colors[x+(y*map_size_z+z)*map_size_x]); - map_minimap[(x+z*(map_size_x+1))*3+1] = green(map_colors[x+(y*map_size_z+z)*map_size_x]); - map_minimap[(x+z*(map_size_x+1))*3+2] = blue(map_colors[x+(y*map_size_z+z)*map_size_x]); + map_minimap[(x+z*map_size_x)*4+0] = red(map_colors[x+(y*map_size_z+z)*map_size_x]); + map_minimap[(x+z*map_size_x)*4+1] = green(map_colors[x+(y*map_size_z+z)*map_size_x]); + map_minimap[(x+z*map_size_x)*4+2] = blue(map_colors[x+(y*map_size_z+z)*map_size_x]); break; } } } } } + glPixelStorei(GL_UNPACK_ROW_LENGTH,map_size_x); + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + glBindTexture(GL_TEXTURE_2D,texture_minimap.texture_id); + glTexSubImage2D(GL_TEXTURE_2D,0,chunk_x,chunk_y,CHUNK_SIZE,CHUNK_SIZE,GL_RGBA,GL_UNSIGNED_BYTE,map_minimap+(chunk_x+chunk_y*map_size_x)*4); + glBindTexture(GL_TEXTURE_2D,0); + return settings.greedy_meshing?chunk_generate_greedy(displaylist,chunk_x,chunk_y):chunk_generate_naive(displaylist,chunk_x,chunk_y); } @@ -1857,35 +1860,19 @@ void chunk_update_all() { //printf("UPDATE GEOMETRY: %i %i\n",chunk_geometry_changed[closest_index]%CHUNKS_PER_DIM,chunk_geometry_changed[closest_index]/CHUNKS_PER_DIM); int chunk_x = (chunk_geometry_changed[closest_index]%CHUNKS_PER_DIM)*CHUNK_SIZE; int chunk_y = (chunk_geometry_changed[closest_index]/CHUNKS_PER_DIM)*CHUNK_SIZE; - if(!glIsList(chunk_display_lists[chunk_geometry_changed[closest_index]])) { - chunk_display_lists[chunk_geometry_changed[closest_index]] = glGenLists(1); + if(!glIsList(chunks[chunk_geometry_changed[closest_index]].display_list)) { + chunks[chunk_geometry_changed[closest_index]].display_list = glGenLists(1); } + chunks[chunk_geometry_changed[closest_index]].max_height = chunk_generate(chunks[chunk_geometry_changed[closest_index]].display_list,chunk_x,chunk_y); glxcheckErrors(__FILE__,__LINE__); - chunk_max_height[chunk_geometry_changed[closest_index]] = chunk_generate(chunk_display_lists[chunk_geometry_changed[closest_index]], chunk_x, chunk_y); - glxcheckErrors(__FILE__,__LINE__); - chunk_last_update[chunk_geometry_changed[closest_index]] = glfwGetTime(); - chunk_created[chunk_geometry_changed[closest_index]] = 1; + chunks[chunk_geometry_changed[closest_index]].last_update = glfwGetTime(); + chunks[chunk_geometry_changed[closest_index]].created = 1; for(int i=closest_index;i0) { - for(int k=0;k #include #include +#include #if __APPLE__ #include @@ -63,7 +64,7 @@ typedef unsigned char boolean; #define red(col) ((col)&0xFF) #define green(col) (((col)>>8)&0xFF) #define blue(col) (((col)>>16)&0xFF) - +#define alpha(col) (((col)>>24)&0xFF) #define PI 3.1415F #define DOUBLEPI (PI*2.0F) @@ -75,6 +76,7 @@ typedef unsigned char boolean; #include "glx.h" #include "sound.h" +#include "texture.h" #include "chunk.h" #include "map.h" #include "aabb.h" @@ -87,7 +89,6 @@ typedef unsigned char boolean; #include "grenade.h" #include "cameracontroller.h" #include "font.h" -#include "texture.h" #include "weapon.h" #include "matrix.h" #include "tracer.h" diff --git a/src/main.c b/src/main.c index 1e22543..ed900be 100644 --- a/src/main.c +++ b/src/main.c @@ -94,12 +94,31 @@ float drawScene(float dt) { } } + /*matrix_upload(); + glBegin(GL_QUADS); + glColor3f(fog_color[0],fog_color[1],fog_color[2]); + int step = 10; + float f = settings.render_distance; + for(int k=0;k<360;k+=step) { + glVertex3f(cos(k/180.0F*PI)*f+camera_x,0.0F,sin(k/180.0F*PI)*f+camera_z); + glVertex3f(cos((k+step)/180.0F*PI)*f+camera_x,0.0F,sin((k+step)/180.0F*PI)*f+camera_z); + + glVertex3f(cos((k+step)/180.0F*PI)*f+camera_x,96.0F,sin((k+step)/180.0F*PI)*f+camera_z); + glVertex3f(cos(k/180.0F*PI)*f+camera_x,96.0F,sin(k/180.0F*PI)*f+camera_z); + } + glEnd();*/ + return r; } +struct player_table { + unsigned char id; + unsigned int score; +}; + int cmp(const void* a, const void* b) { - struct Player* aa = (struct Player*)a; - struct Player* bb = (struct Player*)b; + struct player_table* aa = (struct player_table*)a; + struct player_table* bb = (struct player_table*)b; return bb->score-aa->score; } @@ -422,7 +441,7 @@ void display(float dt) { blk.y = pos[2]; blk.z = 63-pos[1]; network_send(PACKET_BLOCKACTION_ID,&blk,sizeof(blk)); - read_PacketBlockAction(&blk,sizeof(blk)); + //read_PacketBlockAction(&blk,sizeof(blk)); } } if(players[local_player_id].input.buttons.lmb @@ -514,7 +533,7 @@ void display(float dt) { } players[local_player_id].input.buttons.rmb = 0; } else { - if(players[local_player_id].team!=TEAM_SPECTATOR) { + if(players[local_player_id].team!=TEAM_SPECTATOR && (screen_current==SCREEN_NONE || camera_mode!=CAMERAMODE_FPS)) { float tmp2 = players[local_player_id].physics.eye.y; players[local_player_id].physics.eye.y = last_cy; if(camera_mode==CAMERAMODE_FPS) { @@ -624,11 +643,6 @@ void display(float dt) { font_select(FONT_FIXEDSYS); } - //struct Player* p = malloc(sizeof(players)); - //memcpy(p,players,sizeof(players)); - //qsort(p,PLAYERS_MAX,sizeof(players)/PLAYERS_MAX,cmp); - struct Player* p = players; - char score_str[8]; glColor3ub(gamestate.team_1.red,gamestate.team_1.green,gamestate.team_1.blue); switch(gamestate.gamemode_type) { @@ -664,30 +678,42 @@ void display(float dt) { font_centered(settings.window_width/4.0F*3.0F,487*scalef,53.0F*scalef,score_str); glColor3f(1.0F,1.0F,1.0F); - unsigned char cntt[3] = {0}; + + + struct player_table pt[PLAYERS_MAX]; + int connected = 0; for(int k=0;kwinning?&sound_horn:&sound_pickup,0.0F,0.0F,0.0F); + players[p->player_id].score += 10; } } @@ -696,20 +697,17 @@ void network_disconnect() { enet_peer_reset(peer); } -int network_connect(char* ip, int port) { - if(network_connected) { - network_disconnect(); - } +int network_connect_sub(char* ip, int port, int version) { ENetAddress address; ENetEvent event; enet_address_set_host(&address,ip); address.port = port; - peer = enet_host_connect(client,&address,1,VERSION_075); + peer = enet_host_connect(client,&address,1,version); network_logged_in = 0; if(peer==NULL) { return 0; } - if(enet_host_service(client,&event,5000)>0 && event.type == ENET_EVENT_TYPE_CONNECT) { + if(enet_host_service(client,&event,5000)>0 && event.type==ENET_EVENT_TYPE_CONNECT) { network_connected = 1; return 1; } @@ -717,6 +715,19 @@ int network_connect(char* ip, int port) { return 0; } +int network_connect(char* ip, int port) { + if(network_connected) { + network_disconnect(); + } + if(network_connect_sub(ip,port,VERSION_075)) { + return 1; + } + if(network_connect_sub(ip,port,VERSION_076)) { + return 1; + } + return 0; +} + void network_update() { if(network_connected) { ENetEvent event; diff --git a/src/player.c b/src/player.c index 8d13409..1923e77 100644 --- a/src/player.c +++ b/src/player.c @@ -164,7 +164,7 @@ float* player_tool_translate_func(struct Player* p) { } float player_height(struct Player* p) { - return p->input.keys.crouch?0.95F:1.0F; + return p->input.keys.crouch?1.05F:1.1F; } int player_damage(int damage_sections) { @@ -314,12 +314,23 @@ void player_update(float dt) { sound_create(NULL,SOUND_WORLD,weapon_sound(players[k].weapon), players[k].pos.x,players[k].pos.y,players[k].pos.z )->stick_to_player = k; - tracer_add(players[k].weapon,players[k].input.buttons.rmb, + + float o[3] = {players[k].orientation.x, + players[k].orientation.y, + players[k].orientation.z}; + + weapon_spread(&players[k],o); + + struct Camera_HitType hit; + camera_hit(&hit,k, + players[k].physics.eye.x, + players[k].physics.eye.y+player_height(&players[k]), + players[k].physics.eye.z, + o[0],o[1],o[2],128.0F); + tracer_add(players[k].weapon, players[k].physics.eye.x,players[k].physics.eye.y+player_height(&players[k]),players[k].physics.eye.z, - players[k].orientation.x,players[k].orientation.y,players[k].orientation.z); + o[0],o[1],o[2]); particle_create_casing(&players[k]); - struct Camera_HitType hit; - camera_hit_fromplayer(&hit,k,128.0F); switch(hit.type) { case CAMERA_HITTYPE_PLAYER: { @@ -339,6 +350,12 @@ void player_update(float dt) { } } +static float foot_function(struct Player* p) { + float f = (glfwGetTime()-p->sound.feet_started_cycle)/(p->input.keys.sprint?(0.5F/1.3F):0.5F); + f = f*2.0F-1.0F; + return p->sound.feet_cylce?f:-f; +} + int player_render(struct Player* p, int id, Ray* ray, char render) { if(!p->alive) { matrix_push(); @@ -423,8 +440,8 @@ int player_render(struct Player* p, int id, Ray* ray, char render) { matrix_pointAt(p->orientation.x,0.0F,p->orientation.z); matrix_rotate(90.0F,0.0F,1.0F,0.0F); matrix_translate(torso->xsiz*0.1F*0.5F-leg->xsiz*0.1F*0.5F,-torso->zsiz*0.1F*(p->input.keys.crouch?0.6F:1.0F),p->input.keys.crouch?(-torso->zsiz*0.1F*0.75F):0.0F); - matrix_rotate(45.0F*sin(time*DOUBLEPI/1000.0F)*a,1.0F,0.0F,0.0F); - matrix_rotate(45.0F*sin(time*DOUBLEPI/1000.0F)*b,0.0F,0.0F,1.0F); + matrix_rotate(45.0F*foot_function(p)*a,1.0F,0.0F,0.0F); + matrix_rotate(45.0F*foot_function(p)*b,0.0F,0.0F,1.0F); matrix_upload(); if(render) kv6_render(leg,p->team); @@ -438,8 +455,8 @@ int player_render(struct Player* p, int id, Ray* ray, char render) { matrix_pointAt(p->orientation.x,0.0F,p->orientation.z); matrix_rotate(90.0F,0.0F,1.0F,0.0F); matrix_translate(-torso->xsiz*0.1F*0.5F+leg->xsiz*0.1F*0.5F,-torso->zsiz*0.1F*(p->input.keys.crouch?0.6F:1.0F),p->input.keys.crouch?(-torso->zsiz*0.1F*0.75F):0.0F); - matrix_rotate(-45.0F*sin(time*DOUBLEPI/1000.0F)*a,1.0F,0.0F,0.0F); - matrix_rotate(-45.0F*sin(time*DOUBLEPI/1000.0F)*b,0.0F,0.0F,1.0F); + matrix_rotate(-45.0F*foot_function(p)*a,1.0F,0.0F,0.0F); + matrix_rotate(-45.0F*foot_function(p)*b,0.0F,0.0F,1.0F); matrix_upload(); if(render) kv6_render(leg,p->team); @@ -815,15 +832,21 @@ int player_move(struct Player* p, float fsynctics, int id) { player_coordsystem_adjust2(p); - if(sqrt(pow(p->physics.velocity.x,2.0F)+pow(p->physics.velocity.z,2.0F))>0.125F - && !p->physics.airborne - && (p->input.keys.up || p->input.keys.down || p->input.keys.left || p->input.keys.right) - && glfwGetTime()-p->sound.feet_started>(p->input.keys.sprint?(0.5F/1.3F):0.5F)) { + if(p->input.keys.up || p->input.keys.down || p->input.keys.left || p->input.keys.right) { + - struct Sound_wav* footstep[8] = {&sound_footstep1,&sound_footstep2,&sound_footstep3,&sound_footstep4, - &sound_wade1,&sound_wade2,&sound_wade3,&sound_wade4}; - sound_create(NULL,local?SOUND_LOCAL:SOUND_WORLD,footstep[(rand()%4)+(p->physics.wade?4:0)],p->pos.x,p->pos.y,p->pos.z)->stick_to_player = id; - p->sound.feet_started = glfwGetTime(); + if(glfwGetTime()-p->sound.feet_started>(p->input.keys.sprint?(0.5F/1.3F):0.5F) + && sqrt(pow(p->physics.velocity.x,2.0F)+pow(p->physics.velocity.z,2.0F))>0.125F + && !p->physics.airborne) { + struct Sound_wav* footstep[8] = {&sound_footstep1,&sound_footstep2,&sound_footstep3,&sound_footstep4, + &sound_wade1,&sound_wade2,&sound_wade3,&sound_wade4}; + sound_create(NULL,local?SOUND_LOCAL:SOUND_WORLD,footstep[(rand()%4)+(p->physics.wade?4:0)],p->pos.x,p->pos.y,p->pos.z)->stick_to_player = id; + p->sound.feet_started = glfwGetTime(); + } + if(glfwGetTime()-p->sound.feet_started_cycle>(p->input.keys.sprint?(0.5F/1.3F):0.5F)) { + p->sound.feet_started_cycle = glfwGetTime(); + p->sound.feet_cylce = !p->sound.feet_cylce; + } } //sound_position(&p->sound.feet,p->pos.x,p->pos.y,p->pos.z); diff --git a/src/player.h b/src/player.h index 531467b..6e40e0d 100644 --- a/src/player.h +++ b/src/player.h @@ -103,7 +103,8 @@ extern struct Player { struct { struct Sound_source feet; - float feet_started; + float feet_started, feet_started_cycle; + char feet_cylce; struct Sound_source tool; float tool_started; } sound; diff --git a/src/texture.c b/src/texture.c index 67d6576..70ea569 100644 --- a/src/texture.c +++ b/src/texture.c @@ -3,6 +3,7 @@ #include "lodepng/lodepng.c" struct texture texture_splash; +struct texture texture_minimap; struct texture texture_health; struct texture texture_block; @@ -36,6 +37,8 @@ int texture_create(struct texture* t, char* filename) { texture_resize_pow2(t); + glPixelStorei(GL_UNPACK_ROW_LENGTH,t->width); + glPixelStorei(GL_UNPACK_ALIGNMENT,1); glEnable(GL_TEXTURE_2D); glGenTextures(1,&t->texture_id); glxcheckErrors(__FILE__,__LINE__); @@ -56,6 +59,8 @@ int texture_create_buffer(struct texture* t, int width, int height, unsigned cha t->pixels = buff; texture_resize_pow2(t); + glPixelStorei(GL_UNPACK_ROW_LENGTH,t->width); + glPixelStorei(GL_UNPACK_ALIGNMENT,1); glEnable(GL_TEXTURE_2D); glGenTextures(1,&t->texture_id); glBindTexture(GL_TEXTURE_2D,t->texture_id); @@ -131,12 +136,28 @@ void texture_resize_pow2(struct texture* t) { h += h; } + if(t->width==w && t->height==h) { + return; + } + printf("original: %i:%i now: %i:%i\n",t->width,t->height,w,h); unsigned int* pixels_new = malloc(w*h*sizeof(unsigned int)); for(int y=0;ypixels)[x*t->width/w+y*t->height/h*t->width]; + float px = (float)x/(float)w*(float)t->width; + float py = (float)y/(float)h*(float)t->height; + float u = px-(int)px; + float v = py-(int)py; + unsigned int aa = ((unsigned int*)t->pixels)[(int)px+(int)py*t->width]; + unsigned int ba = ((unsigned int*)t->pixels)[min((int)px+(int)py*t->width+1,t->width*t->height)]; + unsigned int ab = ((unsigned int*)t->pixels)[min((int)px+(int)py*t->width+t->width,t->width*t->height)]; + unsigned int bb = ((unsigned int*)t->pixels)[min((int)px+(int)py*t->width+1+t->width,t->width*t->height)]; + pixels_new[x+y*w] = 0; + pixels_new[x+y*w] |= (int)((1.0F-v)*u*red(ba)+(1.0F-v)*(1.0F-u)*red(aa)+v*u*red(bb)+v*(1.0F-u)*red(ab)); + pixels_new[x+y*w] |= (int)((1.0F-v)*u*green(ba)+(1.0F-v)*(1.0F-u)*green(aa)+v*u*green(bb)+v*(1.0F-u)*green(ab))<<8; + pixels_new[x+y*w] |= (int)((1.0F-v)*u*blue(ba)+(1.0F-v)*(1.0F-u)*blue(aa)+v*u*blue(bb)+v*(1.0F-u)*blue(ab))<<16; + pixels_new[x+y*w] |= (int)((1.0F-v)*u*alpha(ba)+(1.0F-v)*(1.0F-u)*alpha(aa)+v*u*alpha(bb)+v*(1.0F-u)*alpha(ab))<<24; } } @@ -199,4 +220,6 @@ void texture_init() { } } texture_create_buffer(&texture_color_selection,64,64,(unsigned char*)pixels); + + texture_create_buffer(&texture_minimap,map_size_x,map_size_z,map_minimap); } diff --git a/src/texture.h b/src/texture.h index 8853a56..e20b1e6 100644 --- a/src/texture.h +++ b/src/texture.h @@ -5,6 +5,7 @@ struct texture { }; extern struct texture texture_splash; +extern struct texture texture_minimap; extern struct texture texture_health; extern struct texture texture_block; diff --git a/src/tracer.c b/src/tracer.c index 9d0f727..e131ed1 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -2,31 +2,21 @@ struct Tracer* tracers; -void tracer_add(unsigned char type, char scoped, float x, float y, float z, float dx, float dy, float dz) { - int a = (type==WEAPON_SHOTGUN)?5:1; +void tracer_add(unsigned char type, float x, float y, float z, float dx, float dy, float dz) { for(int k=0;k>0x10) & 0x7FFF; +} + +void weapon_spread(struct Player* p, float* d) { float spread = 0.0F; - switch(gun) { + switch(p->weapon) { case WEAPON_RIFLE: - spread = 0.0060000001F; + spread = 0.006F; break; case WEAPON_SMG: spread = 0.012F; @@ -114,12 +120,9 @@ void weapon_spread(int gun, char scoped, float* out) { spread = 0.024F; break; } - float basex = (rand()-rand())/16383.0F*spread*(scoped?0.5F:1.0F); - float basey = (rand()-rand())/16383.0F*spread*(scoped?0.5F:1.0F); - float basez = (rand()-rand())/16383.0F*spread*(scoped?0.5F:1.0F); - out[0] += basex; - out[1] += basey; - out[2] += basez; + d[0] += (ms_rand()-ms_rand())/16383.0F*spread*(p->input.buttons.rmb?0.5F:1.0F)*((p->input.keys.crouch && p->weapon!=WEAPON_SHOTGUN)?0.5F:1.0F); + d[1] += (ms_rand()-ms_rand())/16383.0F*spread*(p->input.buttons.rmb?0.5F:1.0F)*((p->input.keys.crouch && p->weapon!=WEAPON_SHOTGUN)?0.5F:1.0F); + d[2] += (ms_rand()-ms_rand())/16383.0F*spread*(p->input.buttons.rmb?0.5F:1.0F)*((p->input.keys.crouch && p->weapon!=WEAPON_SHOTGUN)?0.5F:1.0F); } int weapon_ammo(int gun) { @@ -134,7 +137,7 @@ int weapon_ammo(int gun) { } void weapon_set() { - //players[local_player_id].weapon = WEAPON_SMG; + //players[local_player_id].weapon = WEAPON_SHOTGUN; switch(players[local_player_id].weapon) { case WEAPON_RIFLE: local_player_ammo = 10; @@ -199,64 +202,71 @@ void weapon_shoot() { //https://pastebin.com/raw/TMjKSTXG //http://paste.quacknet.org/view/a3ea2743 - /*float o[3] = {players[local_player_id].orientation.x, - players[local_player_id].orientation.y, - players[local_player_id].orientation.z}; - - weapon_spread(players[local_player_id].weapon,players[local_player_id].input.buttons.rmb,o);*/ - - struct Camera_HitType hit; - camera_hit_fromplayer(&hit,local_player_id,128.0F); - /*camera_hit(&hit,local_player_id, - players[local_player_id].physics.eye.x, - players[local_player_id].physics.eye.y+player_height(&players[local_player_id]), - players[local_player_id].physics.eye.z, - o[0],o[1],o[2],128.0F);*/ - - switch(hit.type) { - case CAMERA_HITTYPE_PLAYER: - { - int type = player_damage(hit.player_section); - sound_create(NULL,SOUND_WORLD,(type==HITTYPE_HEAD)?&sound_spade_whack:&sound_hitplayer,players[hit.player_id].pos.x,players[hit.player_id].pos.y,players[hit.player_id].pos.z)->stick_to_player = hit.player_id; - particle_create(0x0000FF,players[hit.player_id].physics.eye.x,players[hit.player_id].physics.eye.y+player_section_height(type),players[hit.player_id].physics.eye.z,2.0F,1.0F,8,0.1F,0.4F); - - if(players[local_player_id].input.buttons.packed!=network_buttons_last) { - struct PacketWeaponInput in; - in.player_id = local_player_id; - in.primary = players[local_player_id].input.buttons.lmb; - in.secondary = players[local_player_id].input.buttons.rmb; - network_send(PACKET_WEAPONINPUT_ID,&in,sizeof(in)); - - network_buttons_last = players[local_player_id].input.buttons.packed; - } - - struct PacketPositionData orient; - orient.x = players[local_player_id].orientation.x; - orient.y = players[local_player_id].orientation.z; - orient.z = -players[local_player_id].orientation.y; - network_send(PACKET_ORIENTATIONDATA_ID,&orient,sizeof(orient)); - - struct PacketHit h; - h.player_id = hit.player_id; - h.hit_type = type; - network_send(PACKET_HIT_ID,&h,sizeof(h)); - //printf("hit on %s (%i)\n",players[hit.player_id].name,h.hit_type); - break; - } - case CAMERA_HITTYPE_BLOCK: - if(map_damage(hit.x,hit.y,hit.z,weapon_block_damage(players[local_player_id].weapon))==100) { - struct PacketBlockAction blk; - blk.action_type = ACTION_DESTROY; - blk.player_id = local_player_id; - blk.x = hit.x; - blk.y = hit.z; - blk.z = 63-hit.y; - network_send(PACKET_BLOCKACTION_ID,&blk,sizeof(blk)); - //read_PacketBlockAction(&blk,sizeof(blk)); - } else { - particle_create(map_get(hit.x,hit.y,hit.z),hit.xb+0.5F,hit.yb+0.5F,hit.zb+0.5F,2.5F,1.0F,4,0.1F,0.25F); + for(int i=0;i<((players[local_player_id].weapon==WEAPON_SHOTGUN)?6:1);i++) { + float o[3] = {players[local_player_id].orientation.x, + players[local_player_id].orientation.y, + players[local_player_id].orientation.z}; + + weapon_spread(&players[local_player_id],o); + + struct Camera_HitType hit; + //camera_hit_fromplayer(&hit,local_player_id,128.0F); + camera_hit(&hit,local_player_id, + players[local_player_id].physics.eye.x, + players[local_player_id].physics.eye.y+player_height(&players[local_player_id]), + players[local_player_id].physics.eye.z, + o[0],o[1],o[2],128.0F); + + switch(hit.type) { + case CAMERA_HITTYPE_PLAYER: + { + int type = player_damage(hit.player_section); + sound_create(NULL,SOUND_WORLD,(type==HITTYPE_HEAD)?&sound_spade_whack:&sound_hitplayer,players[hit.player_id].pos.x,players[hit.player_id].pos.y,players[hit.player_id].pos.z)->stick_to_player = hit.player_id; + particle_create(0x0000FF,players[hit.player_id].physics.eye.x,players[hit.player_id].physics.eye.y+player_section_height(type),players[hit.player_id].physics.eye.z,2.0F,1.0F,8,0.1F,0.4F); + + if(players[local_player_id].input.buttons.packed!=network_buttons_last) { + struct PacketWeaponInput in; + in.player_id = local_player_id; + in.primary = players[local_player_id].input.buttons.lmb; + in.secondary = players[local_player_id].input.buttons.rmb; + network_send(PACKET_WEAPONINPUT_ID,&in,sizeof(in)); + + network_buttons_last = players[local_player_id].input.buttons.packed; + } + + struct PacketPositionData orient; + orient.x = players[local_player_id].orientation.x; + orient.y = players[local_player_id].orientation.z; + orient.z = -players[local_player_id].orientation.y; + network_send(PACKET_ORIENTATIONDATA_ID,&orient,sizeof(orient)); + + struct PacketHit h; + h.player_id = hit.player_id; + h.hit_type = type; + network_send(PACKET_HIT_ID,&h,sizeof(h)); + //printf("hit on %s (%i)\n",players[hit.player_id].name,h.hit_type); + break; } - break; + case CAMERA_HITTYPE_BLOCK: + if(map_damage(hit.x,hit.y,hit.z,weapon_block_damage(players[local_player_id].weapon))==100) { + struct PacketBlockAction blk; + blk.action_type = ACTION_DESTROY; + blk.player_id = local_player_id; + blk.x = hit.x; + blk.y = hit.z; + blk.z = 63-hit.y; + network_send(PACKET_BLOCKACTION_ID,&blk,sizeof(blk)); + //read_PacketBlockAction(&blk,sizeof(blk)); + } else { + particle_create(map_get(hit.x,hit.y,hit.z),hit.xb+0.5F,hit.yb+0.5F,hit.zb+0.5F,2.5F,1.0F,4,0.1F,0.25F); + } + break; + } + + tracer_add(players[local_player_id].weapon, + players[local_player_id].physics.eye.x,players[local_player_id].physics.eye.y+player_height(&players[local_player_id]),players[local_player_id].physics.eye.z, + o[0],o[1],o[2] + ); } double horiz_recoil, vert_recoil; @@ -309,6 +319,8 @@ void weapon_shoot() { camera_rot_x += horiz_recoil; camera_rot_y -= vert_recoil; + camera_overflow_adjust(); + struct Sound_wav* shoot; switch(players[local_player_id].weapon) { case WEAPON_RIFLE: @@ -322,13 +334,6 @@ void weapon_shoot() { break; } - camera_overflow_adjust(); - sound_create(NULL,SOUND_LOCAL,shoot,players[local_player_id].pos.x,players[local_player_id].pos.y,players[local_player_id].pos.z); - tracer_add(players[local_player_id].weapon,players[local_player_id].input.buttons.rmb, - players[local_player_id].physics.eye.x,players[local_player_id].physics.eye.y+player_height(&players[local_player_id]),players[local_player_id].physics.eye.z, - players[local_player_id].orientation.x,players[local_player_id].orientation.y,players[local_player_id].orientation.z - ); - particle_create_casing(&players[local_player_id]); } diff --git a/src/weapon.h b/src/weapon.h index 51de5a7..fed8d8a 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -10,7 +10,7 @@ float weapon_delay(int gun); int weapon_ammo(int gun); struct Sound_wav* weapon_sound(int gun); struct Sound_wav* weapon_sound_reload(int gun); -void weapon_spread(int gun, char scoped, float* out); +void weapon_spread(struct Player* p, float* d); extern float weapon_reload_start, weapon_last_shot; extern unsigned char weapon_reload_inprogress;