int write_crt(int fd, creature *crt_ptr, char perm_only ) { int n, cnt, cnt2=0, error=0; otag *op; n = write(fd, crt_ptr, sizeof(creature)); if(n < sizeof(creature)) merror("write_crt", FATAL); cnt = count_inv(crt_ptr, perm_only); n = write(fd, &cnt, sizeof(int)); if(n < sizeof(int)) merror("write_crt", FATAL); if(cnt > 0) { op = crt_ptr->first_obj; while(op && cnt2<cnt) { if(!perm_only || (perm_only && (F_ISSET(op->obj, OPERMT)))) { if(write_obj(fd, op->obj, perm_only) < 0) error = 1; cnt2++; } op = op->next_tag; } } if(cnt != cnt2 || error) return(-1); else return(0); }
int write_creature(FILE *fp, int num, creature *crt) { int cnt; char perm_only; otag *op; perm_only = 0; fprintf(fp, "#begcrt\n"); /* basic creature format (excluding pointers) */ write_creature_basic(fp, num, crt); fprintf(fp, "#endcrt\n"); /* objects carried by creature */ cnt = count_inv(crt, perm_only); write_int(fp, cnt); if(cnt > 0) { op = crt->first_obj; while(op) { if(!perm_only || (perm_only && op->obj->flags[0] & 1)) write_object(fp, -1, op->obj); op = op->next_tag; } } /* end of complete creature */ return(0); }
void partition_and_count(int *A, int l, int r, long long *inv_count) { int m; if (l >= r) return; m = (l + r)/2; partition_and_count(A, l, m, inv_count); partition_and_count(A, m+1, r, inv_count); *inv_count += count_inv(A, l, r); }