Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Free area check code

 
Post new topic   Reply to topic Printable version
 View previous topic  module loading process Post :: Post Toggle lvz object  View next topic  
Author Message
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Fri Apr 11, 2003 2:13 pm    Post subject: Free area check code Reply to topic Reply with quote

Code: Show/Hide

//11/4/03

#include "asss.h"

typedef struct {
   int x,y;
   int w,h;
   Player in[32]; //ToDo: Might want to make this bigger
} mapArea;

//Interfaces
local Imodman     *mm;   //Optional
local Ilogman     *lm;   //Log console/file
local Iplayerdata *pd;   //FOR_EACH_PLAYER(p) macro
local Imainloop   *mainloop; //Periodic callbacks

//My globals
//ToDo: Add your own areas here
local mapArea AmmoArea;

//Prototypes
local void makeArea(mapArea *are, int x, int y, int w, int h);
local void insArea(mapArea *area, Player *p);
local void rmArea(mapArea *area, Player *p);
local int  bInArea(mapArea *area, Player *p);
local void lsArea(mapArea *area);

//Must be called to setup the struct
local void makeArea(mapArea *area, int x, int y, int w, int h) {
   int i;
   area->x = x;
   area->y = y;
   area->w = w;
   area->h = h;

   for(i=0; i<32; i++) {
      area->in[i].pid = -1;
   }
}

//Add player. Can call if no more space
local void insArea(mapArea *area, Player *p) {
   int i;
   rmArea(area, p); //No duplicates wanted
   for(i=0; i<32; i++) {
      if (area->in[i].pid == -1) {
         area->in[i] = *p;
         return;
      }
   }
}

//Remove player. Can call if player doesn't exist
local void rmArea(mapArea *area, Player *p) {
   int i;
   for(i=0; i<32; i++) {
      if (area->in[i].pid == p->pid) {
         area->in[i].pid = -1;
         return;
      }
   }
}

//bool/boolean/Boolean is screwed in Dev-C++
local int bInArea(mapArea *area, Player *p) {
   int tx, ty;
   if (p->p_ship == 8) return 0; //In spec
   tx = p->position.x / 16;
   ty = p->position.y / 16;

   if (tx < area->x) return 0;
   if (tx > area->x + area->w) return 0;
   if (ty < area->y) return 0;
   if (ty > area->y + area->h) return 0;
 
   return 1;
}

//Dumps to log
local void lsArea(mapArea *area) {
   int i;
   if (!lm) return; //Safety

   for(i=0; i<32; i++) {
      if (area->in[i].pid == -1) continue;
      //ToDo: Put your own module name here
      lm->Log(L_INFO, "<hioctane> lsArea: %s", area->in[i].name);
   }
}

//You might want a timer for each area and stagger them or you
// could put all your areas into one timer (could get slow?)
local int checkHotspot() {
   local Link *link; //used by macro
   Player *p;
   if (!pd) return 1; //Safety

   pd->Lock();
   FOR_EACH_PLAYER(p){
      if (bInArea(&AmmoArea, p)) {
         insArea(&AmmoArea, p);
         //ToDo: make your own sendPrize
         // or PM for it (if your desperate)
         sendPrize(p, PRIZE_SUPER);
         sendPrize(p, PRIZE_FULLCHARGE);
      } else rmArea(&AmmoArea, p);
   }
   pd->Unlock();

   return 1;
}

//This is what Asss can see and calls
//ToDo: Put your own module name here (MM_ prefix of course)
EXPORT int MM_hioctane(int action, Imodman *mm_, Arena *arena) {
   int result = MM_OK; //FAIL;

   //Act on the action flags
   switch(action) {
   case MM_LOAD:
      //Save Imodman pointer (optional)
      mm = mm_;
   
      //Get interfaces (access to functions in other modules)
      lm        = mm->GetInterface(I_LOGMAN, ALLARENAS);
      pd       = mm->GetInterface(I_PLAYERDATA, HOMEARENA);
      mainloop = mm->GetInterface(I_MAINLOOP,   HOMEARENA);
      //ToDo: Check interface validity

      //ToDo: Add your own areas here
      //533,428 to 544,435
      makeArea(&AmmoArea, 533,428, 11,7);

      mainloop->SetTimer(checkHotspot, 800, 175, NULL, HOMEARENA);

      result = MM_OK;
      break;
   //ToDo: Other cases here (like MM_UNLOAD)
   default:
      result = MM_OK;
      break;
   }

   return result;
}


The original idea was to add the player to a list on entering the area and prize them. Then remove the player when they left the area. But what actually happens is they get prized and added..oops just realised in[] gets full of duplicates icon_confused.gif anyway, they get prized and added just for being in the area, not for entering it only.

Also I notcied that the PRIZE_blah enum is bust after doing GivePrize(ROCKET) and getting a brick icon_exclaim.gif

Edit: Formatted it more nicely (bleh, now that I've painstakingly cut and paste the code I might as well figure out how to make a module of it...)
Thanks to Helicon (or m/b h?) for the thingy posted earlier.
Fixed duplicate thing (I think)
Added more stuff so it's now less example functions and more of a module.
Edit: Fixed spec bug
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
Grelminar
Creator of Asss


Joined: Feb 26 2003
Posts: 378
Offline

PostPosted: Fri Apr 11, 2003 8:57 pm    Post subject: Reply to topic Reply with quote

Ok, there are a bunch of things wrong with this.

1. the arbitrary limit of 32. Use lists for this sort of thing.

2. putting Player structs in your thingy. Always use pointers to Players. Never create your own Players.

3. there's no way for mapmakers or anyone else to set up a bunch of areas without writing code.

4. only rectangular areas are supported.

5. use symbolic constant for spec ship number.

6. "local Link *link" is very bad: local is #defined to be static (I picked up that bit from zlib). It should be used only for functions, indicating that they're local to a particular .c file. Blame C for giving 4 different meanings to the keyword "static", but still use it correctly.

7. there's no way for other modules to add events on players entering or leaving areas.

8. you don't clean up after yourself in MM_UNLOAD.

9. this duplicates a lot of work that I did a while ago and already exists in asss: look at the region stuff in mapdata.c.
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Sat Apr 12, 2003 7:06 am    Post subject: Reply to topic Reply with quote

Grelminar wrote:
6. "local Link *link" is very bad: local is #defined to be static (I picked up that bit from zlib). It should be used only for functions, indicating that they're local to a particular .c file. Blame C for giving 4 different meanings to the keyword "static", but still use it correctly.

That bit was copied from Helicon.c (trying to pass on the blame).

Grelminar wrote:
8. you don't clean up after yourself in MM_UNLOAD.

The code I originally posted wasn't supposed to be a module in its self, but in hindsight I can see that others just really need the bInArea() function and none of the other stuff.

Grelminar wrote:
9. this duplicates a lot of work that I did a while ago and already exists in asss: look at the region stuff in mapdata.c.

I'll have another look at this. I glanced at it and figured that a region was something like H7 and not between some coords.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
Helicon
Server Help Squatter


Joined: Dec 03 2002
Posts: 771
Location: GNU Doldrums
Offline

PostPosted: Sat Apr 12, 2003 6:38 pm    Post subject: Reply to topic Reply with quote

asshole. Any recommendations for dealing around the Link* ? How can i make use of FOR_EACH_PLAYER while needing a Link?
_________________
Signatures just seem so quaint.
Back to top
View users profile Send private message Add User to Ignore List
Grelminar
Creator of Asss


Joined: Feb 26 2003
Posts: 378
Offline

PostPosted: Sat Apr 12, 2003 9:41 pm    Post subject: Reply to topic Reply with quote

Just declare one as an automatic variable:
Link *link;

Look at the code; I do this hundreds of times.
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> ASSS Custom Projects All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 892 page(s) served in previous 5 minutes.

phpBB Created this page in 0.480767 seconds : 30 queries executed (93.4%): GZIP compression disabled