Browse Source

Replaced the very ineffective clif_foreachclient() with map_foreachpc() since they essentially do the same thing (bugreport:1174).
Rewrote map_foreachpc() so that its callback function signature now uses a more natural 'sd' instead of a DBKey/void* pair.
Rewrote atcommand_users() to use a single function, instead of depending on two helper functions and global objects.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@12683 54d463be-8e91-2dee-dedb-b68131a5f0ec

ultramage 17 năm trước cách đây
mục cha
commit
62cc01866b
12 tập tin đã thay đổi với 68 bổ sung74 xóa
  1. 1 1
      src/common/utils.c
  2. 39 28
      src/map/atcommand.c
  3. 2 25
      src/map/clif.c
  4. 0 1
      src/map/clif.h
  5. 1 1
      src/map/intif.c
  6. 16 8
      src/map/map.c
  7. 1 1
      src/map/map.h
  8. 1 1
      src/map/mob.c
  9. 4 4
      src/map/pc.c
  10. 1 1
      src/map/pet.c
  11. 1 2
      src/map/script.c
  12. 1 1
      src/map/unit.c

+ 1 - 1
src/common/utils.c

@@ -239,7 +239,7 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B)
 
 	if( result > UINT_MAX )
 	{
-		ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)", A, B, result);
+		ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)\n", A, B, result);
 		return UINT_MAX;
 	}
 

+ 39 - 28
src/map/atcommand.c

@@ -6447,37 +6447,51 @@ int atcommand_pettalk(const int fd, struct map_session_data* sd, const char* com
 /*==========================================
  * @users - displays the number of players present on each map (percentage)
  *------------------------------------------*/
-
-static DBMap* users_db = NULL; // unsigned int mapindex -> int users
-static int users_all;
-
-static int atcommand_users_sub1(struct map_session_data* sd,va_list va)
-{
-	int users = (int)(uidb_get(users_db,(unsigned int)sd->mapindex)) + 1;
-	users_all++;
-	uidb_put(users_db,(unsigned int)sd->mapindex,(void *)users);
-	return 0;
-}
-
-static int atcommand_users_sub2(DBKey key,void* val,va_list va)
-{
-	char buf[256];
-	struct map_session_data* sd = va_arg(va,struct map_session_data*);
-	sprintf(buf,"%s: %d (%d%%)",mapindex_id2name(key.i),(int)val,(int)val * 100 / users_all);
-	clif_displaymessage(sd->fd,buf);
-	return 0;
-}
-
 int atcommand_users(const int fd, struct map_session_data* sd, const char* command, const char* message)
 {
 	char buf[256];
+	DBMap* users_db; // unsigned int mapindex -> int users
+	int users_all;
+
+	users_db = uidb_alloc(DB_OPT_BASE);
 	users_all = 0;
 
-	users_db->clear(users_db, NULL);
-	clif_foreachclient(atcommand_users_sub1);
-	users_db->foreach(users_db,atcommand_users_sub2,sd);
+	// count users on each map
+	{
+		struct s_mapiterator* iter;
+		struct map_session_data* sd;
+
+		iter = mapit_getallusers();
+		for( sd = (struct map_session_data*)mapit_first(iter); mapit_exists(iter); sd = (struct map_session_data*)mapit_next(iter) )
+		{
+			int users = (int)uidb_get(users_db,sd->mapindex) + 1;
+			uidb_put(users_db,(unsigned int)sd->mapindex,(void *)users);
+			users_all++;
+		}
+		mapit_free(iter);
+	}
+
+	// display results for each map
+	{
+		DBIterator* iter;
+		DBKey index;
+		int users;
+
+		iter = users_db->iterator(users_db);
+		for( users = (int)iter->first(iter,&index); iter->exists(iter); users = (int)iter->next(iter,&index) )
+		{
+			sprintf(buf,"%s: %d (%d%%)",mapindex_id2name(index.i),users,users * 100 / users_all);
+			clif_displaymessage(sd->fd,buf);
+		}
+		iter->destroy(iter);
+	}
+
+	// display overall count
 	sprintf(buf,"all: %d",users_all);
 	clif_displaymessage(fd,buf);
+
+	users_db->destroy(users_db,NULL);
+
 	return 0;
 }
 
@@ -7436,8 +7450,7 @@ int atcommand_whodrops(const int fd, struct map_session_data* sd, const char* co
 	}
 	for (i = 0; i < count; i++) {
 		item_data = item_array[i];
-		sprintf(atcmd_output, "Item: '%s'[%d]",
-			item_data->jname,item_data->slot);
+		sprintf(atcmd_output, "Item: '%s'[%d]", item_data->jname,item_data->slot);
 		clif_displaymessage(fd, atcmd_output);
 
 		if (item_data->mob[0].chance == 0) {
@@ -8701,14 +8714,12 @@ int atcommand_config_read(const char* cfgName)
 
 void do_init_atcommand()
 {
-	users_db = uidb_alloc(DB_OPT_BASE);
 	add_timer_func_list(atshowmobs_timer, "atshowmobs_timer");
 	return;
 }
 
 void do_final_atcommand()
 {
-	users_db->destroy(users_db,NULL);
 }
 
 

+ 2 - 25
src/map/clif.c

@@ -160,29 +160,6 @@ uint16 clif_getport(void)
 	return map_port;
 }
 
-/*==========================================
- * 全てのclientに対してfunc()実行
- *------------------------------------------*/
-int clif_foreachclient(int (*func)(struct map_session_data*, va_list),...) //recoded by sasuke, bug when player count gets higher [Kevin]
-{
-	int i;
-	va_list ap;
-	struct map_session_data *sd;
-
-	va_start(ap,func);
-
-	for(i = 0; i < fd_max; i++) {
-		if ( session[i] && session[i]->func_parse == clif_parse) {
-			sd = (struct map_session_data*)session[i]->session_data;
-			if ( sd && sd->state.active )
-				func(sd, ap);
-		}
-	}
-
-	va_end(ap);
-	return 0;
-}
-
 /*==========================================
  * clif_sendでAREA*指定時用
  *------------------------------------------*/
@@ -6339,7 +6316,7 @@ int clif_guild_emblem(struct map_session_data *sd,struct guild *g)
 /// Sends update of the guild id/emblem id to everyone in the area.
 void clif_guild_emblem_area(struct block_list* bl)
 {
-	char buf[12];
+	uint8 buf[12];
 
 	nullpo_retv(bl);
 
@@ -7916,7 +7893,7 @@ void clif_parse_LoadEndAck(int fd,struct map_session_data *sd)
 #endif
 
 		// Notify everyone that this char logged in [Skotlex].
-		clif_foreachclient(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
+		map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 1);
 
 		//Login Event
 		npc_script_event(sd, NPCE_LOGIN);

+ 0 - 1
src/map/clif.h

@@ -402,7 +402,6 @@ void clif_send_quest_delete(struct map_session_data * sd, int quest_id);
 void clif_send_quest_status(struct map_session_data * sd, int quest_id, bool active);
 
 
-int clif_foreachclient(int (*)(struct map_session_data*,va_list),...);
 int clif_send(const uint8* buf, int len, struct block_list* bl, enum send_target type);
 int do_final_clif(void);
 int do_init_clif(void);

+ 1 - 1
src/map/intif.c

@@ -906,7 +906,7 @@ int mapif_parse_WisToGM(int fd)
 	safestrncpy(Wisp_name, (char*)RFIFOP(fd,4), NAME_LENGTH);
 	safestrncpy(message, (char*)RFIFOP(fd,30), mes_len);
 	// information is sended to all online GM
-	clif_foreachclient(mapif_parse_WisToGM_sub, min_gm_level, Wisp_name, message, mes_len);
+	map_foreachpc(mapif_parse_WisToGM_sub, min_gm_level, Wisp_name, message, mes_len);
 
 	if (message != mbuf)
 		aFree(message);

+ 16 - 8
src/map/map.c

@@ -1739,12 +1739,22 @@ struct block_list * map_id2bl(int id)
 	return bl;
 }
 
-void map_foreachpc(int (*func)(DBKey,void*,va_list),...)
+void map_foreachpc(int (*func)(struct map_session_data* sd, va_list args), ...)
 {
-	va_list ap;
-	va_start(ap,func);
-	pc_db->vforeach(pc_db,func,ap);
-	va_end(ap);
+	DBIterator* iter;
+	struct map_session_data* sd;
+	va_list args, argscopy;
+
+	va_start(args,func);
+	iter = pc_db->iterator(pc_db);
+	for( sd = (struct map_session_data*)iter->first(iter,NULL); iter->exists(iter); sd = (struct map_session_data*)iter->next(iter,NULL) )
+	{
+		va_copy(argscopy,args);
+		func(sd, argscopy);
+		va_end(argscopy);
+	}
+	iter->destroy(iter);
+	va_end(args);
 }
 
 void map_foreachmob(int (*func)(DBKey,void*,va_list),...)
@@ -3120,10 +3130,8 @@ void do_final(void)
 	ShowStatus("Successfully terminated.\n");
 }
 
-static int map_abort_sub(DBKey key,void * data,va_list ap)
+static int map_abort_sub(struct map_session_data* sd, va_list ap)
 {
-	struct map_session_data *sd = (TBL_PC*)data;
-
 	chrif_save(sd,1);
 	return 1;
 }

+ 1 - 1
src/map/map.h

@@ -562,7 +562,7 @@ int map_eraseipport(unsigned short map, uint32 ip, uint16 port);
 int map_eraseallipport(void);
 void map_addiddb(struct block_list *);
 void map_deliddb(struct block_list *bl);
-void map_foreachpc(int (*func)(DBKey,void*,va_list),...);
+void map_foreachpc(int (*func)(struct map_session_data* sd, va_list args), ...);
 void map_foreachmob(int (*func)(DBKey,void*,va_list),...);
 int map_foreachiddb(int (*)(DBKey,void*,va_list),...);
 struct map_session_data * map_nick2sd(const char*);

+ 1 - 1
src/map/mob.c

@@ -1766,7 +1766,7 @@ static int mob_ai_hard(int tid, unsigned int tick, int id, intptr data)
 	if (battle_config.mob_ai&0x20)
 		map_foreachmob(mob_ai_sub_lazy,tick);
 	else
-		clif_foreachclient(mob_ai_sub_foreachclient,tick);
+		map_foreachpc(mob_ai_sub_foreachclient,tick);
 
 	return 0;
 }

+ 4 - 4
src/map/pc.c

@@ -6996,7 +6996,7 @@ int map_day_timer(int tid, unsigned int tick, int id, intptr data)
 		return 0; //Already day.
 	
 	night_flag = 0; // 0=day, 1=night [Yor]
-	clif_foreachclient(pc_daynight_timer_sub);
+	map_foreachpc(pc_daynight_timer_sub);
 	strcpy(tmp_soutput, (data == 0) ? msg_txt(502) : msg_txt(60)); // The day has arrived!
 	intif_GMmessage(tmp_soutput, strlen(tmp_soutput) + 1, 0);
 	return 0;
@@ -7017,7 +7017,7 @@ int map_night_timer(int tid, unsigned int tick, int id, intptr data)
 		return 0; //Already nigth.
 
 	night_flag = 1; // 0=day, 1=night [Yor]
-	clif_foreachclient(pc_daynight_timer_sub);
+	map_foreachpc(pc_daynight_timer_sub);
 	strcpy(tmp_soutput, (data == 0) ? msg_txt(503) : msg_txt(59)); // The night has fallen...
 	intif_GMmessage(tmp_soutput, strlen(tmp_soutput) + 1, 0);
 	return 0;
@@ -7093,7 +7093,7 @@ int duel_showinfo(const unsigned int did, struct map_session_data* sd)
 			duel_list[did].members_count + duel_list[did].invites_count);
 
 	clif_disp_onlyself(sd, output, strlen(output));
-	clif_foreachclient(duel_showinfo_sub, sd, &p);
+	map_foreachpc(duel_showinfo_sub, sd, &p);
 	return 0;
 }
 
@@ -7155,7 +7155,7 @@ int duel_leave(const unsigned int did, struct map_session_data* sd)
 	duel_list[did].members_count--;
 	
 	if(duel_list[did].members_count == 0) {
-		clif_foreachclient(duel_leave_sub, did); 
+		map_foreachpc(duel_leave_sub, did); 
 		duel_count--;
 	}
 	

+ 1 - 1
src/map/pet.c

@@ -971,7 +971,7 @@ static int pet_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
 
 static int pet_ai_hard(int tid, unsigned int tick, int id, intptr data)
 {
-	clif_foreachclient(pet_ai_sub_foreachclient,tick);
+	map_foreachpc(pet_ai_sub_foreachclient,tick);
 
 	return 0;
 }

+ 1 - 2
src/map/script.c

@@ -9151,12 +9151,11 @@ BUILDIN_FUNC(emotion)
 	return 0;
 }
 
-static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
+static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
 {
 	int m=va_arg(ap,int);
 	int g_id=va_arg(ap,int);
 	int flag=va_arg(ap,int);
-	TBL_PC *sd = (TBL_PC*)data;
 
 	if(!sd || sd->bl.m != m)
 		return 0;

+ 1 - 1
src/map/unit.c

@@ -1860,7 +1860,7 @@ int unit_free(struct block_list *bl, int clrtype)
 			duel_reject(sd->duel_invite, sd);
 	
 		// Notify friends that this char logged out. [Skotlex]
-		clif_foreachclient(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
+		map_foreachpc(clif_friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
 		party_send_logout(sd);
 		guild_send_memberinfoshort(sd,0);
 		pc_cleareventtimer(sd);